summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2022-05-20 10:22:42 +0200
committerJose M. Guisado <jguisado@soleta.eu>2022-05-20 10:22:42 +0200
commitd90ab82cec328004e743b2bfbb7c25b32eb299ea (patch)
treefcfbfd01f3517631119b7f41ae8363e1259177bd
parentf88bcf78ac9ffefcedfb4e108b703dac40ca5839 (diff)
format: use autopep8
Use autopep8 for coding format, and only for whitespace changes. This change drops use of tabs in favor of spaces. Doesn't use autopep8 --aggresive option. Format command: $ autopep8 --inline --recursive . When using git-blame, use --ignore-rev in order to ignore this reformatting commit.
-rw-r--r--cli/__init__.py1
-rw-r--r--cli/cli.py204
-rw-r--r--cli/objects/client.py89
-rw-r--r--cli/objects/disks.py266
-rw-r--r--cli/objects/images.py274
-rw-r--r--cli/objects/modes.py93
-rw-r--r--cli/objects/poweroff.py86
-rw-r--r--cli/objects/scopes.py9
-rw-r--r--cli/objects/wol.py110
-rw-r--r--cli/utils.py37
-rwxr-xr-xogcli66
11 files changed, 628 insertions, 607 deletions
diff --git a/cli/__init__.py b/cli/__init__.py
index 8b13789..e69de29 100644
--- a/cli/__init__.py
+++ b/cli/__init__.py
@@ -1 +0,0 @@
-
diff --git a/cli/cli.py b/cli/cli.py
index de05f55..1a71e9a 100644
--- a/cli/cli.py
+++ b/cli/cli.py
@@ -16,107 +16,109 @@ import argparse
import requests
import sys
+
class OgREST():
- def __init__(self, ip, port, api_token):
- self.URL = f'http://{ip}:{port}'
- self.HEADERS = {'Authorization' : api_token}
-
- def get(self, path, payload=None):
- try:
- r = requests.get(f'{self.URL}{path}',
- headers=self.HEADERS,
- json=payload)
- if r.status_code != 200:
- sys.exit(f"Cannot connect to ogServer: "
- f"{r.status_code} HTTP status code")
- except IOError as e:
- sys.exit(f"Cannot connect to ogServer: {e}")
- return r
-
- def post(self, path, payload):
- try:
- r = requests.post(f'{self.URL}{path}',
- headers=self.HEADERS,
- json=payload)
- print(r.text)
- if r.status_code not in {200, 202}:
- sys.exit(f"Cannot connect to ogServer: "
- f"{r.status_code} HTTP status code")
- except IOError as e:
- sys.exit(f"Cannot connect to ogServer: {e}")
- return r
+ def __init__(self, ip, port, api_token):
+ self.URL = f'http://{ip}:{port}'
+ self.HEADERS = {'Authorization': api_token}
+
+ def get(self, path, payload=None):
+ try:
+ r = requests.get(f'{self.URL}{path}',
+ headers=self.HEADERS,
+ json=payload)
+ if r.status_code != 200:
+ sys.exit(f"Cannot connect to ogServer: "
+ f"{r.status_code} HTTP status code")
+ except IOError as e:
+ sys.exit(f"Cannot connect to ogServer: {e}")
+ return r
+
+ def post(self, path, payload):
+ try:
+ r = requests.post(f'{self.URL}{path}',
+ headers=self.HEADERS,
+ json=payload)
+ print(r.text)
+ if r.status_code not in {200, 202}:
+ sys.exit(f"Cannot connect to ogServer: "
+ f"{r.status_code} HTTP status code")
+ except IOError as e:
+ sys.exit(f"Cannot connect to ogServer: {e}")
+ return r
+
class OgCLI():
- def __init__(self, cfg):
- self.rest = OgREST(cfg['ip'], cfg['port'], cfg['api_token'])
-
- def list(self, args):
- choices = ['clients', 'scopes', 'modes', 'hardware',
- 'client', 'images', 'disks']
- parser = argparse.ArgumentParser(prog='ogcli list')
- parser.add_argument('item', choices=choices)
- parsed_args = parser.parse_args([args[0]])
-
- if parsed_args.item == 'clients':
- OgClient.list_clients(self.rest)
- elif parsed_args.item == 'client':
- OgClient.get_client_properties(self.rest, args[1:])
- elif parsed_args.item == 'hardware':
- OgClient.list_client_hardware(self.rest, args[1:])
- elif parsed_args.item == 'modes':
- OgModes.list_available_modes(self.rest)
- elif parsed_args.item == 'scopes':
- OgScope.list_scopes(self.rest)
- elif parsed_args.item == 'images':
- OgImage.list_images(self.rest)
- elif parsed_args.item == 'disks':
- OgDisk.list_disks(self.rest, args[1:])
-
- def set(self, args):
- choices = ['modes', 'mode']
- parser = argparse.ArgumentParser(prog='ogcli set')
- parser.add_argument('item', choices=choices)
- parsed_args = parser.parse_args([args[0]])
-
- if parsed_args.item in choices:
- OgModes.set_modes(self.rest, args[1:])
-
- def send(self, args):
- choices = ['wol', 'poweroff', 'refresh']
- parser = argparse.ArgumentParser(prog='ogcli send')
- parser.add_argument('send_obj', choices=choices)
- parsed_args = parser.parse_args([args[0]])
-
- if parsed_args.send_obj == 'wol':
- OgWol.send_wol(self.rest, args[1:])
- elif parsed_args.send_obj == 'poweroff':
- OgPoweroff.send_poweroff(self.rest, args[1:])
- elif parsed_args.send_obj == 'refresh':
- OgClient.send_refresh(self.rest, args[1:])
-
- def restore(self, args):
- choices = ['image']
- parser = argparse.ArgumentParser(prog='ogcli restore')
- parser.add_argument('send_obj', choices=choices)
- parsed_args = parser.parse_args([args[0]])
-
- if parsed_args.send_obj == 'image':
- OgImage.restore_image(self.rest, args[1:])
-
- def create(self, args):
- choices = ['image']
- parser = argparse.ArgumentParser(prog='ogcli create')
- parser.add_argument('create_obj', choices=choices)
- parsed_args = parser.parse_args([args[0]])
-
- if parsed_args.create_obj == 'image':
- OgImage.create_image(self.rest, args[1:])
-
- def setup(self, args):
- choices = ['disk']
- parser = argparse.ArgumentParser(prog='ogcli setup')
- parser.add_argument('setup_obj', choices=choices)
- parsed_args = parser.parse_args([args[0]])
-
- if parsed_args.setup_obj == 'disk':
- OgDisk.setup_disk(self.rest, args[1:])
+ def __init__(self, cfg):
+ self.rest = OgREST(cfg['ip'], cfg['port'], cfg['api_token'])
+
+ def list(self, args):
+ choices = ['clients', 'scopes', 'modes', 'hardware',
+ 'client', 'images', 'disks']
+ parser = argparse.ArgumentParser(prog='ogcli list')
+ parser.add_argument('item', choices=choices)
+ parsed_args = parser.parse_args([args[0]])
+
+ if parsed_args.item == 'clients':
+ OgClient.list_clients(self.rest)
+ elif parsed_args.item == 'client':
+ OgClient.get_client_properties(self.rest, args[1:])
+ elif parsed_args.item == 'hardware':
+ OgClient.list_client_hardware(self.rest, args[1:])
+ elif parsed_args.item == 'modes':
+ OgModes.list_available_modes(self.rest)
+ elif parsed_args.item == 'scopes':
+ OgScope.list_scopes(self.rest)
+ elif parsed_args.item == 'images':
+ OgImage.list_images(self.rest)
+ elif parsed_args.item == 'disks':
+ OgDisk.list_disks(self.rest, args[1:])
+
+ def set(self, args):
+ choices = ['modes', 'mode']
+ parser = argparse.ArgumentParser(prog='ogcli set')
+ parser.add_argument('item', choices=choices)
+ parsed_args = parser.parse_args([args[0]])
+
+ if parsed_args.item in choices:
+ OgModes.set_modes(self.rest, args[1:])
+
+ def send(self, args):
+ choices = ['wol', 'poweroff', 'refresh']
+ parser = argparse.ArgumentParser(prog='ogcli send')
+ parser.add_argument('send_obj', choices=choices)
+ parsed_args = parser.parse_args([args[0]])
+
+ if parsed_args.send_obj == 'wol':
+ OgWol.send_wol(self.rest, args[1:])
+ elif parsed_args.send_obj == 'poweroff':
+ OgPoweroff.send_poweroff(self.rest, args[1:])
+ elif parsed_args.send_obj == 'refresh':
+ OgClient.send_refresh(self.rest, args[1:])
+
+ def restore(self, args):
+ choices = ['image']
+ parser = argparse.ArgumentParser(prog='ogcli restore')
+ parser.add_argument('send_obj', choices=choices)
+ parsed_args = parser.parse_args([args[0]])
+
+ if parsed_args.send_obj == 'image':
+ OgImage.restore_image(self.rest, args[1:])
+
+ def create(self, args):
+ choices = ['image']
+ parser = argparse.ArgumentParser(prog='ogcli create')
+ parser.add_argument('create_obj', choices=choices)
+ parsed_args = parser.parse_args([args[0]])
+
+ if parsed_args.create_obj == 'image':
+ OgImage.create_image(self.rest, args[1:])
+
+ def setup(self, args):
+ choices = ['disk']
+ parser = argparse.ArgumentParser(prog='ogcli setup')
+ parser.add_argument('setup_obj', choices=choices)
+ parsed_args = parser.parse_args([args[0]])
+
+ if parsed_args.setup_obj == 'disk':
+ OgDisk.setup_disk(self.rest, args[1:])
diff --git a/cli/objects/client.py b/cli/objects/client.py
index 4dc64fd..dbb7f15 100644
--- a/cli/objects/client.py
+++ b/cli/objects/client.py
@@ -9,49 +9,50 @@ import argparse
from cli.utils import print_json
+
class OgClient():
- @staticmethod
- def list_clients(rest):
- r = rest.get('/clients')
- print_json(r.text)
-
- @staticmethod
- def list_client_hardware(rest, args):
- parser = argparse.ArgumentParser()
- parser.add_argument('--client-ip',
- nargs=1,
- type=str,
- required=True,
- help='client IP')
- parsed_args = parser.parse_args(args)
-
- payload = {'client': parsed_args.client_ip}
- r = rest.get('/hardware', payload=payload)
- print_json(r.text)
-
- @staticmethod
- def get_client_properties(rest, args):
- parser = argparse.ArgumentParser()
- parser.add_argument('--client-ip',
- nargs=1,
- required=True,
- help='client IP')
- parsed_args = parser.parse_args(args)
-
- payload = {'client': parsed_args.client_ip}
- r = rest.get('/client/info', payload=payload)
- print_json(r.text)
-
- @staticmethod
- def send_refresh(rest, args):
- parser = argparse.ArgumentParser()
- parser.add_argument('--client-ip',
- action='append',
- default=[],
- required=True,
- help='Client IP')
- parsed_args = parser.parse_args(args)
-
- payload = {'clients': parsed_args.client_ip}
- rest.post('/refresh', payload=payload)
+ @staticmethod
+ def list_clients(rest):
+ r = rest.get('/clients')
+ print_json(r.text)
+
+ @staticmethod
+ def list_client_hardware(rest, args):
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--client-ip',
+ nargs=1,
+ type=str,
+ required=True,
+ help='client IP')
+ parsed_args = parser.parse_args(args)
+
+ payload = {'client': parsed_args.client_ip}
+ r = rest.get('/hardware', payload=payload)
+ print_json(r.text)
+
+ @staticmethod
+ def get_client_properties(rest, args):
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--client-ip',
+ nargs=1,
+ required=True,
+ help='client IP')
+ parsed_args = parser.parse_args(args)
+
+ payload = {'client': parsed_args.client_ip}
+ r = rest.get('/client/info', payload=payload)
+ print_json(r.text)
+
+ @staticmethod
+ def send_refresh(rest, args):
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--client-ip',
+ action='append',
+ default=[],
+ required=True,
+ help='Client IP')
+ parsed_args = parser.parse_args(args)
+
+ payload = {'clients': parsed_args.client_ip}
+ rest.post('/refresh', payload=payload)
diff --git a/cli/objects/disks.py b/cli/objects/disks.py
index deaf5b3..4df9994 100644
--- a/cli/objects/disks.py
+++ b/cli/objects/disks.py
@@ -10,136 +10,140 @@ import re
from cli.utils import print_json
+
class OgDisk():
- @staticmethod
- def list_disks(rest, args):
- parser = argparse.ArgumentParser()
- parser.add_argument('--client-ip',
- nargs='?',
- required=True,
- help='Client IP to query')
- parsed_args = parser.parse_args(args)
- payload = {'client': [parsed_args.client_ip]}
-
- r = rest.get('/client/setup', payload=payload)
- print_json(r.text)
-
- @staticmethod
- def setup_disk(rest, args):
- def parse_size(size):
- size = size.upper()
- units = {"M": 10**3, "G": 10**6, "T": 10**9} # Mapped to K
- # size = re.sub(r'(\d+)([MGT])', r'\1 \2', size)
- match = re.match(r'(\d+)([MGT])', size)
- if match:
- if len(match.groups()) == 2:
- number, unit = match.groups()
- return str(int(float(number)*units[unit]))
- print(f'Error parsing size {size}. Aborting.')
- return None
-
- disk_type_map = {'dos': 'MSDOS', 'gpt': 'GPT'}
- part_types = ['LINUX', 'EFI', 'WINDOWS', 'CACHE']
- fs_types = ['EXT4', 'FAT32', 'NTFS', 'CACHE']
-
- parser = argparse.ArgumentParser()
- parser.add_argument('--type',
- nargs='?',
- required=True,
- choices=['dos','gpt'],
- help='Disk partition scheme')
- parser.add_argument('--num',
- nargs='?',
- default=1,
- help='Disk number (defaults to 1)')
- parser.add_argument('--format',
- nargs='?',
- const=True,
- type=lambda x: x.split(','),
- help='Indicates which partitions to reformat if they are already present. '\
- 'Use --part alone to mean all partitions.')
- parser.add_argument('--part',
- nargs='+',
- action='append',
- type=lambda x: x.split(','),
- required=True,
- help='Partition definition (syntax: "num,part_scheme,fs,size")')
- group = parser.add_argument_group('clients', 'Client selection args')
- group.add_argument('--center-id',
- type=int,
- action='append',
- default=[],
- required=False,
- help='Clients from given center id')
- group.add_argument('--room-id',
- type=int,
- action='append',
- default=[],
- required=False,
- help='Clients from given room id')
- group.add_argument('--client-ip',
- action='append',
- default=[],
- required=False,
- help='Specific client IP')
-
- parsed_args = parser.parse_args(args)
- r = rest.get('/scopes')
- scopes = r.json()
- ips = set()
-
- for center in parsed_args.center_id:
- center_scope = scope_lookup(center, 'center', scopes)
- ips.update(ips_in_scope(center_scope))
- for room in parsed_args.room_id:
- room_scope = scope_lookup(room, 'room', scopes)
- ips.update(ips_in_scope(room_scope))
- for l in parsed_args.client_ip:
- ips.add(l)
- if not ips:
- print("No clients found")
- return None
-
- payload = {'clients': parsed_args.client_ip, 'type': disk_type_map[parsed_args.type], 'disk': str(parsed_args.num),
- 'cache': '0', 'cache_size': '0', 'partition_setup': []}
- for i, p in enumerate(parsed_args.part, start=1):
- p = p[0]
- part_num, code, fs, size = p[0], p[1].upper(), p[2].upper(), p[3]
-
- if code not in part_types:
- print(f'Specified partition type {code} is not supported. Aborting...')
- return
- if fs not in fs_types:
- print(f'Specified filesystem {code} is not supported. Aborting...')
- return
- size = parse_size(size)
-
- for j in range(i, int(part_num)):
- part = {'partition': str(j), 'code':'EMPTY',
- 'filesystem': 'EMPTY', 'size': '0',
- 'format': '0'}
- payload['partition_setup'].append(part)
-
- if parsed_args.format is True or (type(parsed_args.format) == list and part_num in parsed_args.format):
- do_format = '1'
- else:
- do_format = '0'
-
- if fs == 'CACHE':
- payload['cache'] = '1' # Assuming flag specifying if there's cache in the setup
- payload['cache_size'] = size
- part = {'partition': str(p[0]), 'code':code.upper(),
- 'filesystem': fs.upper(), 'size': size,
- 'format': do_format}
- payload['partition_setup'].append(part)
-
- last_partnum = int(parsed_args.part[-1][0][0])
- # Pad with empty partitions if no 4th part was defined
- for i in range(last_partnum + 1, 5):
- part = {'partition': str(i), 'code':'EMPTY',
- 'filesystem': 'EMPTY', 'size': '0',
- 'format': '0'}
- payload['partition_setup'].append(part)
-
- rest.post('/setup', payload=payload)
+ @staticmethod
+ def list_disks(rest, args):
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--client-ip',
+ nargs='?',
+ required=True,
+ help='Client IP to query')
+ parsed_args = parser.parse_args(args)
+ payload = {'client': [parsed_args.client_ip]}
+
+ r = rest.get('/client/setup', payload=payload)
+ print_json(r.text)
+
+ @staticmethod
+ def setup_disk(rest, args):
+ def parse_size(size):
+ size = size.upper()
+ units = {"M": 10**3, "G": 10**6, "T": 10**9} # Mapped to K
+ # size = re.sub(r'(\d+)([MGT])', r'\1 \2', size)
+ match = re.match(r'(\d+)([MGT])', size)
+ if match:
+ if len(match.groups()) == 2:
+ number, unit = match.groups()
+ return str(int(float(number)*units[unit]))
+ print(f'Error parsing size {size}. Aborting.')
+ return None
+
+ disk_type_map = {'dos': 'MSDOS', 'gpt': 'GPT'}
+ part_types = ['LINUX', 'EFI', 'WINDOWS', 'CACHE']
+ fs_types = ['EXT4', 'FAT32', 'NTFS', 'CACHE']
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--type',
+ nargs='?',
+ required=True,
+ choices=['dos', 'gpt'],
+ help='Disk partition scheme')
+ parser.add_argument('--num',
+ nargs='?',
+ default=1,
+ help='Disk number (defaults to 1)')
+ parser.add_argument('--format',
+ nargs='?',
+ const=True,
+ type=lambda x: x.split(','),
+ help='Indicates which partitions to reformat if they are already present. '
+ 'Use --part alone to mean all partitions.')
+ parser.add_argument('--part',
+ nargs='+',
+ action='append',
+ type=lambda x: x.split(','),
+ required=True,
+ help='Partition definition (syntax: "num,part_scheme,fs,size")')
+ group = parser.add_argument_group('clients', 'Client selection args')
+ group.add_argument('--center-id',
+ type=int,
+ action='append',
+ default=[],
+ required=False,
+ help='Clients from given center id')
+ group.add_argument('--room-id',
+ type=int,
+ action='append',
+ default=[],
+ required=False,
+ help='Clients from given room id')
+ group.add_argument('--client-ip',
+ action='append',
+ default=[],
+ required=False,
+ help='Specific client IP')
+
+ parsed_args = parser.parse_args(args)
+ r = rest.get('/scopes')
+ scopes = r.json()
+ ips = set()
+
+ for center in parsed_args.center_id:
+ center_scope = scope_lookup(center, 'center', scopes)
+ ips.update(ips_in_scope(center_scope))
+ for room in parsed_args.room_id:
+ room_scope = scope_lookup(room, 'room', scopes)
+ ips.update(ips_in_scope(room_scope))
+ for l in parsed_args.client_ip:
+ ips.add(l)
+ if not ips:
+ print("No clients found")
+ return None
+
+ payload = {'clients': parsed_args.client_ip, 'type': disk_type_map[parsed_args.type], 'disk': str(parsed_args.num),
+ 'cache': '0', 'cache_size': '0', 'partition_setup': []}
+ for i, p in enumerate(parsed_args.part, start=1):
+ p = p[0]
+ part_num, code, fs, size = p[0], p[1].upper(), p[2].upper(), p[3]
+
+ if code not in part_types:
+ print(
+ f'Specified partition type {code} is not supported. Aborting...')
+ return
+ if fs not in fs_types:
+ print(
+ f'Specified filesystem {code} is not supported. Aborting...')
+ return
+ size = parse_size(size)
+
+ for j in range(i, int(part_num)):
+ part = {'partition': str(j), 'code': 'EMPTY',
+ 'filesystem': 'EMPTY', 'size': '0',
+ 'format': '0'}
+ payload['partition_setup'].append(part)
+
+ if parsed_args.format is True or (type(parsed_args.format) == list and part_num in parsed_args.format):
+ do_format = '1'
+ else:
+ do_format = '0'
+
+ if fs == 'CACHE':
+ # Assuming flag specifying if there's cache in the setup
+ payload['cache'] = '1'
+ payload['cache_size'] = size
+ part = {'partition': str(p[0]), 'code': code.upper(),
+ 'filesystem': fs.upper(), 'size': size,
+ 'format': do_format}
+ payload['partition_setup'].append(part)
+
+ last_partnum = int(parsed_args.part[-1][0][0])
+ # Pad with empty partitions if no 4th part was defined
+ for i in range(last_partnum + 1, 5):
+ part = {'partition': str(i), 'code': 'EMPTY',
+ 'filesystem': 'EMPTY', 'size': '0',
+ 'format': '0'}
+ payload['partition_setup'].append(part)
+
+ rest.post('/setup', payload=payload)
diff --git a/cli/objects/images.py b/cli/objects/images.py
index 8299923..89d57c2 100644
--- a/cli/objects/images.py
+++ b/cli/objects/images.py
@@ -10,140 +10,144 @@ from cli.utils import *
import argparse
+
class OgImage():
- @staticmethod
- def list_images(rest):
- r = rest.get('/images')
- print_json(r.text)
-
- @staticmethod
- def restore_image(rest, args):
- parser = argparse.ArgumentParser()
- parser.add_argument('--disk',
- nargs='?',
- required=True,
- help='Disk')
- parser.add_argument('--part',
- nargs='?',
- required=True,
- help='Partition')
- parser.add_argument('--id',
- nargs='?',
- type=int,
- required=True,
- help='Image id to be restored')
- parser.add_argument('--type',
- nargs='?',
- required=False,
- choices=['unicast', 'unicast-direct', 'tiptorrent'],
- default='tiptorrent',
- help='Transfer method. (Default: tiptorrent)')
- parser.add_argument('--repo',
- nargs='?',
- default=urlparse(rest.URL).netloc.split(':')[0],
- help='Images repository ip')
- group = parser.add_argument_group('clients', 'Client selection args')
- group.add_argument('--center-id',
- type=int,
- action='append',
- default=[],
- required=False,
- help='Clients from given center id')
- group.add_argument('--room-id',
- type=int,
- action='append',
- default=[],
- required=False,
- help='Clients from given room id')
- group.add_argument('--client-ip',
- action='append',
- default=[],
- required=False,
- help='Specific client IP')
- parsed_args = parser.parse_args(args)
-
- r = rest.get('/scopes')
- scopes = r.json()
- ips = set()
-
- for center in parsed_args.center_id:
- center_scope = scope_lookup(center, 'center', scopes)
- ips.update(ips_in_scope(center_scope))
- for room in parsed_args.room_id:
- room_scope = scope_lookup(room, 'room', scopes)
- ips.update(ips_in_scope(room_scope))
- for l in parsed_args.client_ip:
- ips.add(l)
-
- if not ips:
- print('No clients specified.')
- return
-
- r = rest.get('/images')
- images = r.json()
- found_image = [img for img in images['images'] if img['id'] == parsed_args.id]
- if not found_image:
- print(f'Image with id {parsed_args.id} not found.')
- return
- else:
- found_image = found_image[0]
-
- payload = {'disk': parsed_args.disk, 'partition': parsed_args.part,
- 'id': str(parsed_args.id), 'name': found_image['name'],
- 'profile': str(found_image['software_id']),
- 'repository': parsed_args.repo,
- 'type': parsed_args.type.upper(), 'clients': list(ips)}
- r = rest.post('/image/restore', payload=payload)
-
- @staticmethod
- def create_image(rest, args):
- parser = argparse.ArgumentParser()
- parser.add_argument('--disk',
- nargs='?',
- required=True,
- help='Disk')
- parser.add_argument('--part',
- nargs='?',
- required=True,
- help='Partition')
- parser.add_argument('--name',
- nargs='?',
- required=True,
- help='Image name')
- parser.add_argument('--desc',
- nargs='?',
- required=False,
- help='Image description (for new images)')
- parser.add_argument('--repo',
- nargs='?',
- default=urlparse(rest.URL).netloc.split(':')[0],
- help='Images repository ip')
- group = parser.add_argument_group('clients', 'Client selection args')
- group.add_argument('--client-ip',
- action='append',
- default=[],
- required=False,
- help='Specific client IP')
- parsed_args = parser.parse_args(args)
-
- r = rest.get('/client/info', payload={'client':parsed_args.client_ip})
- center_id = r.json()['center']
-
- r = rest.get('/client/setup', payload={'client':parsed_args.client_ip})
- if r.status_code == 200:
- part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and
- x['partition'] == int(parsed_args.part),
- r.json()['partitions']))
- if not part_info:
- print('Partition not found.')
- return
- fs_code = list(part_info)[0]['code']
-
- payload = {'clients': parsed_args.client_ip, 'disk': parsed_args.disk, 'center_id': center_id,
- 'partition': parsed_args.part, 'code': str(fs_code), 'name': parsed_args.name,
- 'repository': parsed_args.repo, 'id': '0'}
- if parsed_args.desc:
- payload['description'] = parsed_args.desc
-
- rest.post('/image/create', payload=payload)
+ @staticmethod
+ def list_images(rest):
+ r = rest.get('/images')
+ print_json(r.text)
+
+ @staticmethod
+ def restore_image(rest, args):
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--disk',
+ nargs='?',
+ required=True,
+ help='Disk')
+ parser.add_argument('--part',
+ nargs='?',
+ required=True,
+ help='Partition')
+ parser.add_argument('--id',
+ nargs='?',
+ type=int,
+ required=True,
+ help='Image id to be restored')
+ parser.add_argument('--type',
+ nargs='?',
+ required=False,
+ choices=['unicast',
+ 'unicast-direct', 'tiptorrent'],
+ default='tiptorrent',
+ help='Transfer method. (Default: tiptorrent)')
+ parser.add_argument('--repo',
+ nargs='?',
+ default=urlparse(rest.URL).netloc.split(':')[0],
+ help='Images repository ip')
+ group = parser.add_argument_group('clients', 'Client selection args')
+ group.add_argument('--center-id',
+ type=int,
+ action='append',
+ default=[],
+ required=False,
+ help='Clients from given center id')
+ group.add_argument('--room-id',
+ type=int,
+ action='append',
+ default=[],
+ required=False,
+ help='Clients from given room id')
+ group.add_argument('--client-ip',
+ action='append',
+ default=[],
+ required=False,
+ help='Specific client IP')
+ parsed_args = parser.parse_args(args)
+
+ r = rest.get('/scopes')
+ scopes = r.json()
+ ips = set()
+
+ for center in parsed_args.center_id:
+ center_scope = scope_lookup(center, 'center', scopes)
+ ips.update(ips_in_scope(center_scope))
+ for room in parsed_args.room_id:
+ room_scope = scope_lookup(room, 'room', scopes)
+ ips.update(ips_in_scope(room_scope))
+ for l in parsed_args.client_ip:
+ ips.add(l)
+
+ if not ips:
+ print('No clients specified.')
+ return
+
+ r = rest.get('/images')
+ images = r.json()
+ found_image = [img for img in images['images']
+ if img['id'] == parsed_args.id]
+ if not found_image:
+ print(f'Image with id {parsed_args.id} not found.')
+ return
+ else:
+ found_image = found_image[0]
+
+ payload = {'disk': parsed_args.disk, 'partition': parsed_args.part,
+ 'id': str(parsed_args.id), 'name': found_image['name'],
+ 'profile': str(found_image['software_id']),
+ 'repository': parsed_args.repo,
+ 'type': parsed_args.type.upper(), 'clients': list(ips)}
+ r = rest.post('/image/restore', payload=payload)
+
+ @staticmethod
+ def create_image(rest, args):
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--disk',
+ nargs='?',
+ required=True,
+ help='Disk')
+ parser.add_argument('--part',
+ nargs='?',
+ required=True,
+ help='Partition')
+ parser.add_argument('--name',
+ nargs='?',
+ required=True,
+ help='Image name')
+ parser.add_argument('--desc',
+ nargs='?',
+ required=False,
+ help='Image description (for new images)')
+ parser.add_argument('--repo',
+ nargs='?',
+ default=urlparse(rest.URL).netloc.split(':')[0],
+ help='Images repository ip')
+ group = parser.add_argument_group('clients', 'Client selection args')
+ group.add_argument('--client-ip',
+ action='append',
+ default=[],
+ required=False,
+ help='Specific client IP')
+ parsed_args = parser.parse_args(args)
+
+ r = rest.get('/client/info', payload={'client': parsed_args.client_ip})
+ center_id = r.json()['center']
+
+ r = rest.get('/client/setup',
+ payload={'client': parsed_args.client_ip})
+ if r.status_code == 200:
+ part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and
+ x['partition'] == int(parsed_args.part),
+ r.json()['partitions']))
+ if not part_info:
+ print('Partition not found.')
+ return
+ fs_code = list(part_info)[0]['code']
+
+ payload = {'clients': parsed_args.client_ip, 'disk': parsed_args.disk, 'center_id': center_id,
+ 'partition': parsed_args.part, 'code': str(fs_code), 'name': parsed_args.name,
+ 'repository': parsed_args.repo, 'id': '0'}
+ if parsed_args.desc:
+ payload['description'] = parsed_args.desc
+
+ rest.post('/image/create', payload=payload)
diff --git a/cli/objects/modes.py b/cli/objects/modes.py
index 8a9754e..2abb091 100644
--- a/cli/objects/modes.py
+++ b/cli/objects/modes.py
@@ -9,56 +9,57 @@ from cli.utils import *
import argparse
+
class OgModes():
- @staticmethod
- def list_available_modes(rest):
- r = rest.get('/mode')
- print(r.text)
+ @staticmethod
+ def list_available_modes(rest):
+ r = rest.get('/mode')
+ print(r.text)
- @staticmethod
- def set_modes(rest, args):
- parser = argparse.ArgumentParser(prog='ogcli set mode')
- group = parser.add_argument_group('clients', 'Client selection args')
- group.add_argument('--center-id',
- type=int,
- action='append',
- default=[],
- required=False,
- help='Clients from given center id')
- group.add_argument('--room-id',
- type=int,
- action='append',
- default=[],
- required=False,
- help='Clients from given room id')
- group.add_argument('--client-ip',
- action='append',
- default=[],
- required=False,
- help='Specific client IP')
- parser.add_argument('--mode',
- nargs=1,
- required=True,
- help='Boot mode to be set')
- parsed_args = parser.parse_args(args)
+ @staticmethod
+ def set_modes(rest, args):
+ parser = argparse.ArgumentParser(prog='ogcli set mode')
+ group = parser.add_argument_group('clients', 'Client selection args')
+ group.add_argument('--center-id',
+ type=int,
+ action='append',
+ default=[],
+ required=False,
+ help='Clients from given center id')
+ group.add_argument('--room-id',
+ type=int,
+ action='append',
+ default=[],
+ required=False,
+ help='Clients from given room id')
+ group.add_argument('--client-ip',
+ action='append',
+ default=[],
+ required=False,
+ help='Specific client IP')
+ parser.add_argument('--mode',
+ nargs=1,
+ required=True,
+ help='Boot mode to be set')
+ parsed_args = parser.parse_args(args)
- r = rest.get('/scopes')
- scopes = r.json()
- ips = set()
+ r = rest.get('/scopes')
+ scopes = r.json()
+ ips = set()
- for center in parsed_args.center_id:
- center_scope = scope_lookup(center, 'center', scopes)
- ips.update(ips_in_scope(center_scope))
- for room in parsed_args.room_id:
- room_scope = scope_lookup(room, 'room', scopes)
- ips.update(ips_in_scope(room_scope))
- for l in parsed_args.client_ip:
- ips.add(l)
+ for center in parsed_args.center_id:
+ center_scope = scope_lookup(center, 'center', scopes)
+ ips.update(ips_in_scope(center_scope))
+ for room in parsed_args.room_id:
+ room_scope = scope_lookup(room, 'room', scopes)
+ ips.update(ips_in_scope(room_scope))
+ for l in parsed_args.client_ip:
+ ips.add(l)
- if not ips:
- print("No clients found")
- return None
+ if not ips:
+ print("No clients found")
+ return None
- payload = {'clients': list(ips), 'mode': parsed_args.mode[0]}
- r = rest.post('/mode', payload=payload)
+ payload = {'clients': list(ips), 'mode': parsed_args.mode[0]}
+ r = rest.post('/mode', payload=payload)
diff --git a/cli/objects/poweroff.py b/cli/objects/poweroff.py
index f5eb1dd..da3666c 100644
--- a/cli/objects/poweroff.py
+++ b/cli/objects/poweroff.py
@@ -9,47 +9,49 @@ from cli.utils import *
import argparse
+
class OgPoweroff():
- @staticmethod
- def send_poweroff(rest, args):
- parser = argparse.ArgumentParser()
- group = parser.add_argument_group('clients', 'Client selection options')
- group.add_argument('--center-id',
- type=int,
- action='append',
- default=[],
- required=False,
- help='Clients from given center id')
- group.add_argument('--room-id',
- type=int,
- action='append',
- default=[],
- required=False,
- help='Clients from given room id')
- group.add_argument('--client-ip',
- action='append',
- default=[],
- required=False,
- help='Specific client IP')
- parsed_args = parser.parse_args(args)
-
- r = rest.get('/scopes')
- scopes = r.json()
- ips = set()
-
- for center in parsed_args.center_id:
- center_scope = scope_lookup(center, 'center', scopes)
- ips.update(ips_in_scope(center_scope))
- for room in parsed_args.room_id:
- room_scope = scope_lookup(room, 'room', scopes)
- ips.update(ips_in_scope(room_scope))
- for l in parsed_args.client_ip:
- ips.add(l)
-
- if not ips:
- print("No clients found")
- return None
-
- payload = {'clients': list(ips)}
- r = rest.post('/poweroff', payload=payload)
+ @staticmethod
+ def send_poweroff(rest, args):
+ parser = argparse.ArgumentParser()
+ group = parser.add_argument_group(
+ 'clients', 'Client selection options')
+ group.add_argument('--center-id',
+ type=int,
+ action='append',
+ default=[],
+ required=False,
+ help='Clients from given center id')
+ group.add_argument('--room-id',
+ type=int,
+ action='append',
+ default=[],
+ required=False,
+ help='Clients from given room id')
+ group.add_argument('--client-ip',
+ action='append',
+ default=[],
+ required=False,
+ help='Specific client IP')
+ parsed_args = parser.parse_args(args)
+
+ r = rest.get('/scopes')
+ scopes = r.json()
+ ips = set()
+
+ for center in parsed_args.center_id:
+ center_scope = scope_lookup(center, 'center', scopes)
+ ips.update(ips_in_scope(center_scope))
+ for room in parsed_args.room_id:
+ room_scope = scope_lookup(room, 'room', scopes)
+ ips.update(ips_in_scope(room_scope))
+ for l in parsed_args.client_ip:
+ ips.add(l)
+
+ if not ips:
+ print("No clients found")
+ return None
+
+ payload = {'clients': list(ips)}
+ r = rest.post('/poweroff', payload=payload)
diff --git a/cli/objects/scopes.py b/cli/objects/scopes.py
index 5383932..9416f4f 100644
--- a/cli/objects/scopes.py
+++ b/cli/objects/scopes.py
@@ -9,9 +9,10 @@ import argparse
from cli.utils import print_json
+
class OgScope():
- @staticmethod
- def list_scopes(rest):
- r = rest.get('/scopes')
- print_json(r.text)
+ @staticmethod
+ def list_scopes(rest):
+ r = rest.get('/scopes')
+ print_json(r.text)
diff --git a/cli/objects/wol.py b/cli/objects/wol.py
index 5b8e490..e788ce1 100644
--- a/cli/objects/wol.py
+++ b/cli/objects/wol.py
@@ -9,64 +9,66 @@ from cli.utils import *
import argparse
+
class OgWol():
- @staticmethod
- def send_wol(rest, args):
- def scope_lookup(scope_id, scope_type, d):
- if scope_id == d.get('id') and \
- scope_type == d.get('type'):
- return d
- for scope in d['scope']:
- lookup = scope_lookup(scope_id,
- scope_type,
- scope)
- if lookup is not None:
- return lookup
- return None
+ @staticmethod
+ def send_wol(rest, args):
+ def scope_lookup(scope_id, scope_type, d):
+ if scope_id == d.get('id') and \
+ scope_type == d.get('type'):
+ return d
+ for scope in d['scope']:
+ lookup = scope_lookup(scope_id,
+ scope_type,
+ scope)
+ if lookup is not None:
+ return lookup
+ return None
- parser = argparse.ArgumentParser()
- parser.add_argument('--type',
- nargs='?',
- choices=['broadcast','unicast'],
- default='broadcast',
- help='')
- group = parser.add_argument_group('clients', 'Client selection options')
- group.add_argument('--center-id',
- type=int,
- action='append',
- default=[],
- required=False,
- help='Clients from given center id')
- group.add_argument('--room-id',
- type=int,
- action='append',
- default=[],
- required=False,
- help='Clients from given room id')
- group.add_argument('--client-ip',
- action='append',
- default=[],
- required=False,
- help='Specific client IP')
- parsed_args = parser.parse_args(args)
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--type',
+ nargs='?',
+ choices=['broadcast', 'unicast'],
+ default='broadcast',
+ help='')
+ group = parser.add_argument_group(
+ 'clients', 'Client selection options')
+ group.add_argument('--center-id',
+ type=int,
+ action='append',
+ default=[],
+ required=False,
+ help='Clients from given center id')
+ group.add_argument('--room-id',
+ type=int,
+ action='append',
+ default=[],
+ required=False,
+ help='Clients from given room id')
+ group.add_argument('--client-ip',
+ action='append',
+ default=[],
+ required=False,
+ help='Specific client IP')
+ parsed_args = parser.parse_args(args)
- r = rest.get('/scopes')
- scopes = r.json()
- ips = set()
+ r = rest.get('/scopes')
+ scopes = r.json()
+ ips = set()
- for center in parsed_args.center_id:
- center_scope = scope_lookup(center, 'center', scopes)
- ips.update(ips_in_scope(center_scope))
- for room in parsed_args.room_id:
- room_scope = scope_lookup(room, 'room', scopes)
- ips.update(ips_in_scope(room_scope))
- for l in parsed_args.client_ip:
- ips.add(l)
+ for center in parsed_args.center_id:
+ center_scope = scope_lookup(center, 'center', scopes)
+ ips.update(ips_in_scope(center_scope))
+ for room in parsed_args.room_id:
+ room_scope = scope_lookup(room, 'room', scopes)
+ ips.update(ips_in_scope(room_scope))
+ for l in parsed_args.client_ip:
+ ips.add(l)
- if not ips:
- print("No clients found")
- return None
+ if not ips:
+ print("No clients found")
+ return None
- payload = {'type': parsed_args.type, 'clients': list(ips)}
- r = rest.post('/wol', payload=payload)
+ payload = {'type': parsed_args.type, 'clients': list(ips)}
+ r = rest.post('/wol', payload=payload)
diff --git a/cli/utils.py b/cli/utils.py
index 208be68..b267207 100644
--- a/cli/utils.py
+++ b/cli/utils.py
@@ -7,25 +7,28 @@
import json
+
def scope_lookup(scope_id, scope_type, d):
- if scope_id == d.get('id') and scope_type == d.get('type'):
- return d
- for scope in d['scope']:
- lookup = scope_lookup(scope_id, scope_type, scope)
- if lookup is not None:
- return lookup
- return None
+ if scope_id == d.get('id') and scope_type == d.get('type'):
+ return d
+ for scope in d['scope']:
+ lookup = scope_lookup(scope_id, scope_type, scope)
+ if lookup is not None:
+ return lookup
+ return None
+
def ips_in_scope(scope):
- if scope is None:
- return []
- if 'ip' in scope:
- return [scope['ip']]
- ips = []
- for child in scope['scope']:
- ips += ips_in_scope(child)
- return ips
+ if scope is None:
+ return []
+ if 'ip' in scope:
+ return [scope['ip']]
+ ips = []
+ for child in scope['scope']:
+ ips += ips_in_scope(child)
+ return ips
+
def print_json(text):
- payload = json.loads(text)
- print(json.dumps(payload, sort_keys=True, indent=2))
+ payload = json.loads(text)
+ print(json.dumps(payload, sort_keys=True, indent=2))
diff --git a/ogcli b/ogcli
index 4cfe339..078a07e 100755
--- a/ogcli
+++ b/ogcli
@@ -14,39 +14,41 @@ import argparse
import json
import sys
-OG_CLI_CFG_PATH="/opt/opengnsys/etc/ogcli.json"
+OG_CLI_CFG_PATH = "/opt/opengnsys/etc/ogcli.json"
+
class CLI():
- def __init__(self):
- try:
- with open(OG_CLI_CFG_PATH, 'r') as json_file:
- self.cfg = json.load(json_file)
- except json.JSONDecodeError:
- sys.exit(f'ERROR: Failed parse malformed JSON file '
- f'{OG_CLI_CFG_PATH}')
- except:
- sys.exit(f'ERROR: cannot open {OG_CLI_CFG_PATH}')
-
- required_cfg_params = {'api_token', 'ip', 'port'}
- difference_cfg_params = required_cfg_params - self.cfg.keys()
- if len(difference_cfg_params) > 0:
- sys.exit(f'Missing {difference_cfg_params} key in '
- f'json config file')
-
- self.ogcli = OgCLI(self.cfg)
-
- parser = argparse.ArgumentParser(prog='ogcli')
- parser.add_argument('command', help='Subcommand to run', nargs='?',
- choices=[attr for attr, _ in getmembers(self.ogcli, lambda x: ismethod(x))
- if not attr.startswith('_')])
- args = parser.parse_args(sys.argv[1:2])
-
- if not hasattr(self.ogcli, args.command):
- parser.print_help()
- sys.exit('Unknown command')
-
- # Call the command with the same name.
- getattr(self.ogcli, args.command)(sys.argv[2:])
+ def __init__(self):
+ try:
+ with open(OG_CLI_CFG_PATH, 'r') as json_file:
+ self.cfg = json.load(json_file)
+ except json.JSONDecodeError:
+ sys.exit(f'ERROR: Failed parse malformed JSON file '
+ f'{OG_CLI_CFG_PATH}')
+ except:
+ sys.exit(f'ERROR: cannot open {OG_CLI_CFG_PATH}')
+
+ required_cfg_params = {'api_token', 'ip', 'port'}
+ difference_cfg_params = required_cfg_params - self.cfg.keys()
+ if len(difference_cfg_params) > 0:
+ sys.exit(f'Missing {difference_cfg_params} key in '
+ f'json config file')
+
+ self.ogcli = OgCLI(self.cfg)
+
+ parser = argparse.ArgumentParser(prog='ogcli')
+ parser.add_argument('command', help='Subcommand to run', nargs='?',
+ choices=[attr for attr, _ in getmembers(self.ogcli, lambda x: ismethod(x))
+ if not attr.startswith('_')])
+ args = parser.parse_args(sys.argv[1:2])
+
+ if not hasattr(self.ogcli, args.command):
+ parser.print_help()
+ sys.exit('Unknown command')
+
+ # Call the command with the same name.
+ getattr(self.ogcli, args.command)(sys.argv[2:])
+
if __name__ == "__main__":
- CLI()
+ CLI()