mirror of https://git.48k.eu/ogcli/
cli: ensure the program returns 0 on success and 1 on error
propagate a returncode in each operation and make it the returncode of the program. Prevent sys.exit calls in post(), get() and delete() request handlers to enable cleanup code and error handling. Keep a basic error log inside the request functions if the connection can't be established or if the response contains an error code.master
parent
ea8210a805
commit
10a3897f92
160
cli/cli.py
160
cli/cli.py
|
@ -32,43 +32,48 @@ class OgREST():
|
||||||
|
|
||||||
def get(self, path, payload=None):
|
def get(self, path, payload=None):
|
||||||
try:
|
try:
|
||||||
r = requests.get(f'{self.URL}{path}',
|
res = requests.get(f'{self.URL}{path}',
|
||||||
headers=self.HEADERS,
|
headers=self.HEADERS,
|
||||||
json=payload)
|
json=payload)
|
||||||
if r.status_code != 200:
|
|
||||||
sys.exit(f"Unsuccessful request to ogServer: "
|
if res.status_code != 200:
|
||||||
f"Response with {r.status_code} HTTP status code")
|
print(f"Error: Request to ogServer failed with status code {res.status_code}")
|
||||||
except IOError as e:
|
return None
|
||||||
sys.exit(f"Cannot connect to ogServer: {e}")
|
|
||||||
return r
|
except requests.exceptions.RequestException as e:
|
||||||
|
print(f"Error: Cannot connect to ogServer: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
def post(self, path, payload):
|
def post(self, path, payload):
|
||||||
try:
|
try:
|
||||||
r = requests.post(f'{self.URL}{path}',
|
res = requests.post(f'{self.URL}{path}',
|
||||||
headers=self.HEADERS,
|
headers=self.HEADERS,
|
||||||
json=payload)
|
json=payload)
|
||||||
if r.text:
|
|
||||||
print(r.text)
|
|
||||||
|
|
||||||
if r.status_code not in {200, 202}:
|
if res.status_code not in {200, 202}:
|
||||||
sys.exit(f"Unsuccessful request to ogServer: "
|
print(f"Error: Request to ogServer failed with status code {res.status_code}")
|
||||||
f"Response with {r.status_code} HTTP status code")
|
return None
|
||||||
except IOError as e:
|
|
||||||
sys.exit(f"Cannot connect to ogServer: {e}")
|
except requests.exceptions.RequestException as e:
|
||||||
return r
|
print(f"Error: Cannot connect to ogServer: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
def delete(self, path, payload):
|
def delete(self, path, payload):
|
||||||
try:
|
try:
|
||||||
r = requests.delete(f'{self.URL}{path}',
|
res = requests.delete(f'{self.URL}{path}',
|
||||||
headers=self.HEADERS,
|
headers=self.HEADERS,
|
||||||
json=payload)
|
json=payload)
|
||||||
print(r.text)
|
if res.status_code != 200:
|
||||||
if r.status_code != 200:
|
print(f"Error: Request to ogServer failed with status code {res.status_code}")
|
||||||
sys.exit(f"Unsuccessful request to ogServer: "
|
return None
|
||||||
f"Response with {r.status_code} HTTP status code")
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
sys.exit(f"Cannot connect to ogServer: {e}")
|
print(f"Error: Cannot connect to ogServer: {e}")
|
||||||
return r
|
return None
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
class OgCLI():
|
class OgCLI():
|
||||||
|
@ -84,29 +89,31 @@ class OgCLI():
|
||||||
if not args:
|
if not args:
|
||||||
print('Missing list subcommand', file=sys.stderr)
|
print('Missing list subcommand', file=sys.stderr)
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
parsed_args = parser.parse_args([args[0]])
|
parsed_args = parser.parse_args([args[0]])
|
||||||
|
ret = 0
|
||||||
if parsed_args.item == 'clients':
|
if parsed_args.item == 'clients':
|
||||||
OgClient.list_clients(self.rest)
|
ret = OgClient.list_clients(self.rest)
|
||||||
elif parsed_args.item == 'client':
|
elif parsed_args.item == 'client':
|
||||||
OgClient.get_client_properties(self.rest, args[1:])
|
ret = OgClient.get_client_properties(self.rest, args[1:])
|
||||||
elif parsed_args.item == 'hardware':
|
elif parsed_args.item == 'hardware':
|
||||||
OgClient.list_client_hardware(self.rest, args[1:])
|
ret = OgClient.list_client_hardware(self.rest, args[1:])
|
||||||
elif parsed_args.item == 'modes':
|
elif parsed_args.item == 'modes':
|
||||||
OgModes.list_available_modes(self.rest)
|
ret = OgModes.list_available_modes(self.rest)
|
||||||
elif parsed_args.item == 'scopes':
|
elif parsed_args.item == 'scopes':
|
||||||
OgScope.list_scopes(self.rest, args[1:])
|
ret = OgScope.list_scopes(self.rest, args[1:])
|
||||||
elif parsed_args.item == 'images':
|
elif parsed_args.item == 'images':
|
||||||
OgImage.list_images(self.rest)
|
ret = OgImage.list_images(self.rest)
|
||||||
elif parsed_args.item == 'disks':
|
elif parsed_args.item == 'disks':
|
||||||
OgDisk.list_disks(self.rest, args[1:])
|
ret = OgDisk.list_disks(self.rest, args[1:])
|
||||||
elif parsed_args.item == 'repos':
|
elif parsed_args.item == 'repos':
|
||||||
OgRepo.list_repos(self.rest)
|
ret = OgRepo.list_repos(self.rest)
|
||||||
elif parsed_args.item == 'servers':
|
elif parsed_args.item == 'servers':
|
||||||
OgServer.list_servers(self.rest)
|
ret = OgServer.list_servers(self.rest)
|
||||||
elif parsed_args.item == 'live':
|
elif parsed_args.item == 'live':
|
||||||
OgLive.list_live(self.rest)
|
ret = OgLive.list_live(self.rest)
|
||||||
|
return ret
|
||||||
|
|
||||||
def set(self, args):
|
def set(self, args):
|
||||||
choices = ['modes', 'mode', 'repo']
|
choices = ['modes', 'mode', 'repo']
|
||||||
|
@ -116,13 +123,15 @@ class OgCLI():
|
||||||
if not args:
|
if not args:
|
||||||
print('Missing set subcommand', file=sys.stderr)
|
print('Missing set subcommand', file=sys.stderr)
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
parsed_args = parser.parse_args([args[0]])
|
parsed_args = parser.parse_args([args[0]])
|
||||||
|
ret = 0
|
||||||
if parsed_args.item in ['modes', 'mode']:
|
if parsed_args.item in ['modes', 'mode']:
|
||||||
OgModes.set_modes(self.rest, args[1:])
|
ret = OgModes.set_modes(self.rest, args[1:])
|
||||||
elif parsed_args.item == 'repo':
|
elif parsed_args.item == 'repo':
|
||||||
OgRepo.set_repo(self.rest, args[1:])
|
ret = OgRepo.set_repo(self.rest, args[1:])
|
||||||
|
return ret
|
||||||
|
|
||||||
def request(self, args):
|
def request(self, args):
|
||||||
choices = ['reboot', 'refresh', 'poweroff', 'wol', 'session']
|
choices = ['reboot', 'refresh', 'poweroff', 'wol', 'session']
|
||||||
|
@ -132,19 +141,21 @@ class OgCLI():
|
||||||
if not args:
|
if not args:
|
||||||
print('Missing request subcommand', file=sys.stderr)
|
print('Missing request subcommand', file=sys.stderr)
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
parsed_args = parser.parse_args([args[0]])
|
parsed_args = parser.parse_args([args[0]])
|
||||||
|
ret = 0
|
||||||
if parsed_args.request_obj == 'wol':
|
if parsed_args.request_obj == 'wol':
|
||||||
OgWol.request_wol(self.rest, args[1:])
|
ret = OgWol.request_wol(self.rest, args[1:])
|
||||||
elif parsed_args.request_obj == 'poweroff':
|
elif parsed_args.request_obj == 'poweroff':
|
||||||
OgPoweroff.request_poweroff(self.rest, args[1:])
|
ret = OgPoweroff.request_poweroff(self.rest, args[1:])
|
||||||
elif parsed_args.request_obj == 'refresh':
|
elif parsed_args.request_obj == 'refresh':
|
||||||
OgClient.request_refresh(self.rest, args[1:])
|
ret = OgClient.request_refresh(self.rest, args[1:])
|
||||||
elif parsed_args.request_obj == 'reboot':
|
elif parsed_args.request_obj == 'reboot':
|
||||||
OgReboot.request_reboot(self.rest, args[1:])
|
ret = OgReboot.request_reboot(self.rest, args[1:])
|
||||||
elif parsed_args.request_obj == 'session':
|
elif parsed_args.request_obj == 'session':
|
||||||
OgSession.request_session(self.rest, args[1:])
|
ret = OgSession.request_session(self.rest, args[1:])
|
||||||
|
return ret
|
||||||
|
|
||||||
def restore(self, args):
|
def restore(self, args):
|
||||||
choices = ['image']
|
choices = ['image']
|
||||||
|
@ -154,11 +165,13 @@ class OgCLI():
|
||||||
if not args:
|
if not args:
|
||||||
print('Missing restore subcommand', file=sys.stderr)
|
print('Missing restore subcommand', file=sys.stderr)
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
parsed_args = parser.parse_args([args[0]])
|
parsed_args = parser.parse_args([args[0]])
|
||||||
|
ret = 0
|
||||||
if parsed_args.restore_obj == 'image':
|
if parsed_args.restore_obj == 'image':
|
||||||
OgImage.restore_image(self.rest, args[1:])
|
ret = OgImage.restore_image(self.rest, args[1:])
|
||||||
|
return ret
|
||||||
|
|
||||||
def create(self, args):
|
def create(self, args):
|
||||||
choices = ['image']
|
choices = ['image']
|
||||||
|
@ -168,11 +181,13 @@ class OgCLI():
|
||||||
if not args:
|
if not args:
|
||||||
print('Missing create subcommand', file=sys.stderr)
|
print('Missing create subcommand', file=sys.stderr)
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
parsed_args = parser.parse_args([args[0]])
|
parsed_args = parser.parse_args([args[0]])
|
||||||
|
ret = 0
|
||||||
if parsed_args.create_obj == 'image':
|
if parsed_args.create_obj == 'image':
|
||||||
OgImage.create_image(self.rest, args[1:])
|
ret = OgImage.create_image(self.rest, args[1:])
|
||||||
|
return ret
|
||||||
|
|
||||||
def setup(self, args):
|
def setup(self, args):
|
||||||
choices = ['disk']
|
choices = ['disk']
|
||||||
|
@ -182,11 +197,13 @@ class OgCLI():
|
||||||
if not args:
|
if not args:
|
||||||
print('Missing setup subcommand', file=sys.stderr)
|
print('Missing setup subcommand', file=sys.stderr)
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
parsed_args = parser.parse_args([args[0]])
|
parsed_args = parser.parse_args([args[0]])
|
||||||
|
ret = 0
|
||||||
if parsed_args.setup_obj == 'disk':
|
if parsed_args.setup_obj == 'disk':
|
||||||
OgDisk.setup_disk(self.rest, args[1:])
|
ret = OgDisk.setup_disk(self.rest, args[1:])
|
||||||
|
return ret
|
||||||
|
|
||||||
def update(self, args):
|
def update(self, args):
|
||||||
choices = ['image', 'repo', 'center', 'room', 'folder']
|
choices = ['image', 'repo', 'center', 'room', 'folder']
|
||||||
|
@ -196,19 +213,21 @@ class OgCLI():
|
||||||
if not args:
|
if not args:
|
||||||
print('Missing update subcommand', file=sys.stderr)
|
print('Missing update subcommand', file=sys.stderr)
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
parsed_args = parser.parse_args([args[0]])
|
parsed_args = parser.parse_args([args[0]])
|
||||||
|
ret = 0
|
||||||
if parsed_args.update_obj == 'image':
|
if parsed_args.update_obj == 'image':
|
||||||
OgImage.update_image(self.rest, args[1:])
|
ret = OgImage.update_image(self.rest, args[1:])
|
||||||
elif parsed_args.update_obj == 'center':
|
elif parsed_args.update_obj == 'center':
|
||||||
OgCenter.update_center(self.rest, args[1:])
|
ret = OgCenter.update_center(self.rest, args[1:])
|
||||||
elif parsed_args.update_obj == 'room':
|
elif parsed_args.update_obj == 'room':
|
||||||
OgRoom.update_room(self.rest, args[1:])
|
ret = OgRoom.update_room(self.rest, args[1:])
|
||||||
elif parsed_args.update_obj == 'folder':
|
elif parsed_args.update_obj == 'folder':
|
||||||
OgFolder.update_folder(self.rest, args[1:])
|
ret = OgFolder.update_folder(self.rest, args[1:])
|
||||||
elif parsed_args.update_obj == 'repo':
|
elif parsed_args.update_obj == 'repo':
|
||||||
OgRepo.update_repo(self.rest, args[1:])
|
ret = OgRepo.update_repo(self.rest, args[1:])
|
||||||
|
return ret
|
||||||
|
|
||||||
def delete(self, args):
|
def delete(self, args):
|
||||||
choices = ['server', 'repo', 'center', 'room', 'client', 'folder']
|
choices = ['server', 'repo', 'center', 'room', 'client', 'folder']
|
||||||
|
@ -218,21 +237,22 @@ class OgCLI():
|
||||||
if not args:
|
if not args:
|
||||||
print('Missing delete subcommand', file=sys.stderr)
|
print('Missing delete subcommand', file=sys.stderr)
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
parsed_args = parser.parse_args([args[0]])
|
parsed_args = parser.parse_args([args[0]])
|
||||||
if parsed_args.delete_obj == 'server':
|
if parsed_args.delete_obj == 'server':
|
||||||
OgServer.delete_server(self.rest, args[1:])
|
ret = OgServer.delete_server(self.rest, args[1:])
|
||||||
elif parsed_args.delete_obj == 'repo':
|
elif parsed_args.delete_obj == 'repo':
|
||||||
OgRepo.delete_repo(self.rest, args[1:])
|
ret = OgRepo.delete_repo(self.rest, args[1:])
|
||||||
elif parsed_args.delete_obj == 'center':
|
elif parsed_args.delete_obj == 'center':
|
||||||
OgCenter.delete_center(self.rest, args[1:])
|
ret = OgCenter.delete_center(self.rest, args[1:])
|
||||||
elif parsed_args.delete_obj == 'room':
|
elif parsed_args.delete_obj == 'room':
|
||||||
OgRoom.delete_room(self.rest, args[1:])
|
ret = OgRoom.delete_room(self.rest, args[1:])
|
||||||
elif parsed_args.delete_obj == 'client':
|
elif parsed_args.delete_obj == 'client':
|
||||||
OgClient.delete_client(self.rest, args[1:])
|
ret = OgClient.delete_client(self.rest, args[1:])
|
||||||
elif parsed_args.delete_obj == 'folder':
|
elif parsed_args.delete_obj == 'folder':
|
||||||
OgFolder.delete_folder(self.rest, args[1:])
|
ret = OgFolder.delete_folder(self.rest, args[1:])
|
||||||
|
return ret
|
||||||
|
|
||||||
def add(self, args):
|
def add(self, args):
|
||||||
choices = ['server', 'repo', 'center', 'room', 'client', 'folder']
|
choices = ['server', 'repo', 'center', 'room', 'client', 'folder']
|
||||||
|
@ -242,18 +262,20 @@ class OgCLI():
|
||||||
if not args:
|
if not args:
|
||||||
print('Missing add subcommand', file=sys.stderr)
|
print('Missing add subcommand', file=sys.stderr)
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
parsed_args = parser.parse_args([args[0]])
|
parsed_args = parser.parse_args([args[0]])
|
||||||
|
ret = 0
|
||||||
if parsed_args.add_obj == 'server':
|
if parsed_args.add_obj == 'server':
|
||||||
OgServer.add_server(self.rest, args[1:])
|
ret = OgServer.add_server(self.rest, args[1:])
|
||||||
elif parsed_args.add_obj == 'repo':
|
elif parsed_args.add_obj == 'repo':
|
||||||
OgRepo.add_repo(self.rest, args[1:])
|
ret = OgRepo.add_repo(self.rest, args[1:])
|
||||||
elif parsed_args.add_obj == 'center':
|
elif parsed_args.add_obj == 'center':
|
||||||
OgCenter.add_center(self.rest, args[1:])
|
ret = OgCenter.add_center(self.rest, args[1:])
|
||||||
elif parsed_args.add_obj == 'room':
|
elif parsed_args.add_obj == 'room':
|
||||||
OgRoom.add_room(self.rest, args[1:])
|
ret = OgRoom.add_room(self.rest, args[1:])
|
||||||
elif parsed_args.add_obj == 'client':
|
elif parsed_args.add_obj == 'client':
|
||||||
OgClient.add_client(self.rest, args[1:])
|
ret = OgClient.add_client(self.rest, args[1:])
|
||||||
elif parsed_args.add_obj == 'folder':
|
elif parsed_args.add_obj == 'folder':
|
||||||
OgFolder.add_folder(self.rest, args[1:])
|
ret = OgFolder.add_folder(self.rest, args[1:])
|
||||||
|
return ret
|
||||||
|
|
|
@ -23,7 +23,10 @@ class OgCenter():
|
||||||
payload = {'name': parsed_args.name}
|
payload = {'name': parsed_args.name}
|
||||||
if parsed_args.desc:
|
if parsed_args.desc:
|
||||||
payload['comment'] = parsed_args.desc
|
payload['comment'] = parsed_args.desc
|
||||||
rest.post('/center/add', payload=payload)
|
res = rest.post('/center/add', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_center(rest, args):
|
def update_center(rest, args):
|
||||||
|
@ -48,7 +51,10 @@ class OgCenter():
|
||||||
}
|
}
|
||||||
if parsed_args.comment:
|
if parsed_args.comment:
|
||||||
payload['comment'] = parsed_args.comment
|
payload['comment'] = parsed_args.comment
|
||||||
rest.post('/center/update', payload=payload)
|
res = rest.post('/center/update', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_center(rest, args):
|
def delete_center(rest, args):
|
||||||
|
@ -60,4 +66,7 @@ class OgCenter():
|
||||||
help='center id in database')
|
help='center id in database')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
payload = {'id': parsed_args.id}
|
payload = {'id': parsed_args.id}
|
||||||
rest.post('/center/delete', payload=payload)
|
res = rest.post('/center/delete', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -14,8 +14,11 @@ class OgClient():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_clients(rest):
|
def list_clients(rest):
|
||||||
r = rest.get('/clients')
|
res = rest.get('/clients')
|
||||||
print_json(r.text)
|
if not res:
|
||||||
|
return 1
|
||||||
|
print_json(res.text)
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_client_hardware(rest, args):
|
def list_client_hardware(rest, args):
|
||||||
|
@ -28,8 +31,11 @@ class OgClient():
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
payload = {'client': parsed_args.client_ip}
|
payload = {'client': parsed_args.client_ip}
|
||||||
r = rest.get('/hardware', payload=payload)
|
res = rest.get('/hardware', payload=payload)
|
||||||
print_json(r.text)
|
if not res:
|
||||||
|
return 1
|
||||||
|
print_json(res.text)
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_client_properties(rest, args):
|
def get_client_properties(rest, args):
|
||||||
|
@ -41,8 +47,11 @@ class OgClient():
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
payload = {'client': parsed_args.client_ip}
|
payload = {'client': parsed_args.client_ip}
|
||||||
r = rest.get('/client/info', payload=payload)
|
res = rest.get('/client/info', payload=payload)
|
||||||
print_json(r.text)
|
if not res:
|
||||||
|
return 1
|
||||||
|
print_json(res.text)
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def request_refresh(rest, args):
|
def request_refresh(rest, args):
|
||||||
|
@ -55,7 +64,10 @@ class OgClient():
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
payload = {'clients': parsed_args.client_ip}
|
payload = {'clients': parsed_args.client_ip}
|
||||||
rest.post('/refresh', payload=payload)
|
res = rest.post('/refresh', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add_client(rest, args):
|
def add_client(rest, args):
|
||||||
|
@ -82,8 +94,8 @@ class OgClient():
|
||||||
nargs='?',
|
nargs='?',
|
||||||
required=True,
|
required=True,
|
||||||
help='specify the ip address of the client')
|
help='specify the ip address of the client')
|
||||||
r = rest.get('/mode')
|
res = rest.get('/mode')
|
||||||
boot_choices = r.json()['modes']
|
boot_choices = res.json()['modes']
|
||||||
parser.add_argument('--boot-mode',
|
parser.add_argument('--boot-mode',
|
||||||
nargs='?',
|
nargs='?',
|
||||||
required=True,
|
required=True,
|
||||||
|
@ -100,7 +112,7 @@ class OgClient():
|
||||||
err = True
|
err = True
|
||||||
if err:
|
if err:
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
payload = {
|
payload = {
|
||||||
'name': parsed_args.hostname,
|
'name': parsed_args.hostname,
|
||||||
'netmask': '0',
|
'netmask': '0',
|
||||||
|
@ -113,7 +125,10 @@ class OgClient():
|
||||||
'netdriver': 'generic',
|
'netdriver': 'generic',
|
||||||
'livedir': 'ogLive'
|
'livedir': 'ogLive'
|
||||||
}
|
}
|
||||||
rest.post('/client/add', payload=payload)
|
res = rest.post('/client/add', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_client(rest, args):
|
def delete_client(rest, args):
|
||||||
|
@ -127,4 +142,7 @@ class OgClient():
|
||||||
payload = {
|
payload = {
|
||||||
'clients': parsed_args.ip,
|
'clients': parsed_args.ip,
|
||||||
}
|
}
|
||||||
rest.post('/client/delete', payload=payload)
|
res = rest.post('/client/delete', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -23,8 +23,11 @@ class OgDisk():
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
payload = {'client': [parsed_args.client_ip]}
|
payload = {'client': [parsed_args.client_ip]}
|
||||||
|
|
||||||
r = rest.get('/client/setup', payload=payload)
|
res = rest.get('/client/setup', payload=payload)
|
||||||
print_json(r.text)
|
if not res:
|
||||||
|
return 1
|
||||||
|
print_json(res.text)
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def setup_disk(rest, args):
|
def setup_disk(rest, args):
|
||||||
|
@ -79,8 +82,8 @@ class OgDisk():
|
||||||
help='Specific client IP')
|
help='Specific client IP')
|
||||||
|
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
r = rest.get('/scopes')
|
res = rest.get('/scopes')
|
||||||
scopes = r.json()
|
scopes = res.json()
|
||||||
ips = set()
|
ips = set()
|
||||||
|
|
||||||
for center in parsed_args.center_id:
|
for center in parsed_args.center_id:
|
||||||
|
@ -93,11 +96,11 @@ class OgDisk():
|
||||||
ips.add(l)
|
ips.add(l)
|
||||||
if not ips:
|
if not ips:
|
||||||
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
||||||
return None
|
return 1
|
||||||
|
|
||||||
if not parsed_args.num.isdigit():
|
if not parsed_args.num.isdigit():
|
||||||
print(f'Invalid disk number: must be an integer value')
|
print(f'Invalid disk number: must be an integer value')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
payload = {'clients': parsed_args.client_ip, 'type': disk_type_map[parsed_args.type], 'disk': str(parsed_args.num),
|
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': []}
|
'cache': '0', 'cache_size': '0', 'partition_setup': []}
|
||||||
|
@ -109,31 +112,31 @@ class OgDisk():
|
||||||
|
|
||||||
if len(p) != 4:
|
if len(p) != 4:
|
||||||
print(f'Invalid partition: requires "num,part_scheme,fs,size", "{",".join(p)}" provided')
|
print(f'Invalid partition: requires "num,part_scheme,fs,size", "{",".join(p)}" provided')
|
||||||
return
|
return 1
|
||||||
part_num, code, fs, size = p[0], p[1].upper(), p[2].upper(), p[3]
|
part_num, code, fs, size = p[0], p[1].upper(), p[2].upper(), p[3]
|
||||||
|
|
||||||
if not part_num.isdigit():
|
if not part_num.isdigit():
|
||||||
print(f'Invalid partition: the first parameter must be a number, "{part_num}" provided')
|
print(f'Invalid partition: the first parameter must be a number, "{part_num}" provided')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
if part_num in defined_part_indices:
|
if part_num in defined_part_indices:
|
||||||
print(f'Invalid partition: partition number "{part_num}" has multiple definitions')
|
print(f'Invalid partition: partition number "{part_num}" has multiple definitions')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
defined_part_indices.add(part_num)
|
defined_part_indices.add(part_num)
|
||||||
|
|
||||||
if code not in part_types:
|
if code not in part_types:
|
||||||
print(f'Invalid partition {i}: specified partition type {code} is not supported. The supported formats are {part_types}')
|
print(f'Invalid partition {i}: specified partition type {code} is not supported. The supported formats are {part_types}')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
if fs not in fs_types:
|
if fs not in fs_types:
|
||||||
print(f'Invalid partition {i}: specified filesystem {fs} is not supported. The supported formats are {fs_types}')
|
print(f'Invalid partition {i}: specified filesystem {fs} is not supported. The supported formats are {fs_types}')
|
||||||
return
|
return 1
|
||||||
try:
|
try:
|
||||||
size = parse_size(size)
|
size = parse_size(size)
|
||||||
except ValueError as error:
|
except ValueError as error:
|
||||||
print(f'Invalid partition {i}: {str(error)}')
|
print(f'Invalid partition {i}: {str(error)}')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
for j in range(i, int(part_num)):
|
for j in range(i, int(part_num)):
|
||||||
part = {'partition': str(j), 'code': 'EMPTY',
|
part = {'partition': str(j), 'code': 'EMPTY',
|
||||||
|
@ -158,4 +161,7 @@ class OgDisk():
|
||||||
'format': '0'}
|
'format': '0'}
|
||||||
payload['partition_setup'].append(part)
|
payload['partition_setup'].append(part)
|
||||||
|
|
||||||
rest.post('/setup', payload=payload)
|
res = rest.post('/setup', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -39,7 +39,10 @@ class OgFolder():
|
||||||
payload['room'] = parsed_args.room_id
|
payload['room'] = parsed_args.room_id
|
||||||
if parsed_args.center_id:
|
if parsed_args.center_id:
|
||||||
payload['center'] = parsed_args.center_id
|
payload['center'] = parsed_args.center_id
|
||||||
rest.post('/folder/add', payload=payload)
|
res = restest.post('/folder/add', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_folder(rest, args):
|
def update_folder(rest, args):
|
||||||
|
@ -58,7 +61,10 @@ class OgFolder():
|
||||||
'id': parsed_args.id,
|
'id': parsed_args.id,
|
||||||
'name': parsed_args.name,
|
'name': parsed_args.name,
|
||||||
}
|
}
|
||||||
rest.post('/folder/update', payload=payload)
|
res = rest.post('/folder/update', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_folder(rest, args):
|
def delete_folder(rest, args):
|
||||||
|
@ -70,4 +76,7 @@ class OgFolder():
|
||||||
help='folder id in database')
|
help='folder id in database')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
payload = {'id': parsed_args.id}
|
payload = {'id': parsed_args.id}
|
||||||
rest.post('/folder/delete', payload=payload)
|
res = rest.post('/folder/delete', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -21,10 +21,10 @@ def get_repository(repository_id, rest):
|
||||||
|
|
||||||
|
|
||||||
def get_repositories(rest):
|
def get_repositories(rest):
|
||||||
r = rest.get('/repositories')
|
res = rest.get('/repositories')
|
||||||
if not r or r.status_code != requests.codes.ok:
|
if not res:
|
||||||
return None
|
return None
|
||||||
repositories = r.json()['repositories']
|
repositories = res.json()['repositories']
|
||||||
return repositories
|
return repositories
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,8 +32,11 @@ class OgImage():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_images(rest):
|
def list_images(rest):
|
||||||
r = rest.get('/images')
|
res = rest.get('/images')
|
||||||
print_json(r.text)
|
if not res:
|
||||||
|
return 1
|
||||||
|
print_json(res.text)
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def restore_image(rest, args):
|
def restore_image(rest, args):
|
||||||
|
@ -78,8 +81,8 @@ class OgImage():
|
||||||
help='Specific client IP')
|
help='Specific client IP')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
r = rest.get('/scopes')
|
res = rest.get('/scopes')
|
||||||
scopes = r.json()
|
scopes = res.json()
|
||||||
ips = set()
|
ips = set()
|
||||||
|
|
||||||
for center in parsed_args.center_id:
|
for center in parsed_args.center_id:
|
||||||
|
@ -93,34 +96,37 @@ class OgImage():
|
||||||
|
|
||||||
if not ips:
|
if not ips:
|
||||||
print('Missing --client-ip, or --room-id/--center-id. No clients provided.')
|
print('Missing --client-ip, or --room-id/--center-id. No clients provided.')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
r = rest.get('/images')
|
res = rest.get('/images')
|
||||||
images = r.json()
|
images = res.json()
|
||||||
found_image = [img for img in images['images']
|
found_image = [img for img in images['images']
|
||||||
if img['id'] == parsed_args.id]
|
if img['id'] == parsed_args.id]
|
||||||
if not found_image:
|
if not found_image:
|
||||||
print(f'Image with id {parsed_args.id} not found.')
|
print(f'Image with id {parsed_args.id} not found.')
|
||||||
return
|
return 1
|
||||||
else:
|
else:
|
||||||
found_image = found_image[0]
|
found_image = found_image[0]
|
||||||
|
|
||||||
selected_repo_id = 0
|
selected_repo_id = 0
|
||||||
for ip in parsed_args.client_ip:
|
for ip in parsed_args.client_ip:
|
||||||
r = rest.get('/client/info', payload={'client': ip})
|
res = rest.get('/client/info', payload={'client': ip})
|
||||||
repo_id = r.json()['repo_id']
|
repo_id = res.json()['repo_id']
|
||||||
if selected_repo_id == 0:
|
if selected_repo_id == 0:
|
||||||
selected_repo_id = repo_id
|
selected_repo_id = repo_id
|
||||||
elif selected_repo_id != repo_id:
|
elif selected_repo_id != repo_id:
|
||||||
print(f'cannot restore clients assigned to different repositories')
|
print(f'cannot restore clients assigned to different repositories')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
payload = {'disk': parsed_args.disk, 'partition': parsed_args.part,
|
payload = {'disk': parsed_args.disk, 'partition': parsed_args.part,
|
||||||
'id': str(parsed_args.id), 'name': found_image['name'],
|
'id': str(parsed_args.id), 'name': found_image['name'],
|
||||||
'profile': str(found_image['software_id']),
|
'profile': str(found_image['software_id']),
|
||||||
'repository_id': repo_id,
|
'repository_id': repo_id,
|
||||||
'type': parsed_args.type.upper(), 'clients': list(ips)}
|
'type': parsed_args.type.upper(), 'clients': list(ips)}
|
||||||
r = rest.post('/image/restore', payload=payload)
|
res = rest.post('/image/restore', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_image(rest, args):
|
def create_image(rest, args):
|
||||||
|
@ -149,19 +155,19 @@ class OgImage():
|
||||||
help='Specific client IP')
|
help='Specific client IP')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
r = rest.get('/client/info', payload={'client': parsed_args.client_ip})
|
res = rest.get('/client/info', payload={'client': parsed_args.client_ip})
|
||||||
center_id = r.json()['center']
|
center_id = res.json()['center']
|
||||||
repo_id = r.json()['repo_id']
|
repo_id = res.json()['repo_id']
|
||||||
|
|
||||||
r = rest.get('/client/setup',
|
res = rest.get('/client/setup',
|
||||||
payload={'client': parsed_args.client_ip})
|
payload={'client': parsed_args.client_ip})
|
||||||
if r.status_code == 200:
|
if res.status_code == 200:
|
||||||
part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and
|
part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and
|
||||||
x['partition'] == int(parsed_args.part),
|
x['partition'] == int(parsed_args.part),
|
||||||
r.json()['partitions']))
|
res.json()['partitions']))
|
||||||
if not part_info:
|
if not part_info:
|
||||||
print('Partition not found.')
|
print('Partition not found.')
|
||||||
return
|
return 1
|
||||||
fs_code = list(part_info)[0]['code']
|
fs_code = list(part_info)[0]['code']
|
||||||
|
|
||||||
image_name = remove_accents(parsed_args.name)
|
image_name = remove_accents(parsed_args.name)
|
||||||
|
@ -171,7 +177,10 @@ class OgImage():
|
||||||
if parsed_args.desc:
|
if parsed_args.desc:
|
||||||
payload['description'] = parsed_args.desc
|
payload['description'] = parsed_args.desc
|
||||||
|
|
||||||
rest.post('/image/create', payload=payload)
|
res = rest.post('/image/create', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_image(rest, args):
|
def update_image(rest, args):
|
||||||
|
@ -202,32 +211,32 @@ class OgImage():
|
||||||
help='Specific client IP')
|
help='Specific client IP')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
r = rest.get('/client/info', payload={'client': parsed_args.client_ip})
|
res = rest.get('/client/info', payload={'client': parsed_args.client_ip})
|
||||||
center_id = r.json()['center']
|
center_id = res.json()['center']
|
||||||
repo_id = int(r.json()['repo_id'])
|
repo_id = int(res.json()['repo_id'])
|
||||||
|
|
||||||
r = rest.get('/client/setup',
|
res = rest.get('/client/setup',
|
||||||
payload={'client': parsed_args.client_ip})
|
payload={'client': parsed_args.client_ip})
|
||||||
if r.status_code == 200:
|
if res.status_code == 200:
|
||||||
part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and
|
part_info = list(filter(lambda x: x['disk'] == int(parsed_args.disk) and
|
||||||
x['partition'] == int(parsed_args.part),
|
x['partition'] == int(parsed_args.part),
|
||||||
r.json()['partitions']))
|
res.json()['partitions']))
|
||||||
if not part_info:
|
if not part_info:
|
||||||
print('Partition not found.')
|
print('Partition not found.')
|
||||||
return
|
return 1
|
||||||
fs_code = list(part_info)[0]['code']
|
fs_code = list(part_info)[0]['code']
|
||||||
|
|
||||||
r = rest.get('/images')
|
res = rest.get('/images')
|
||||||
image_name = None
|
image_name = None
|
||||||
if r.status_code == 200:
|
if res.status_code == 200:
|
||||||
for image in r.json()['images']:
|
for image in res.json()['images']:
|
||||||
if image['id'] == int(parsed_args.id):
|
if image['id'] == int(parsed_args.id):
|
||||||
image_name = image['name']
|
image_name = image['name']
|
||||||
break
|
break
|
||||||
|
|
||||||
if not image_name:
|
if not image_name:
|
||||||
print('Image not found')
|
print('Image not found')
|
||||||
return
|
return 1
|
||||||
else:
|
else:
|
||||||
print(f'Updating {image_name} image')
|
print(f'Updating {image_name} image')
|
||||||
|
|
||||||
|
@ -241,4 +250,7 @@ class OgImage():
|
||||||
'code': str(fs_code),
|
'code': str(fs_code),
|
||||||
'name': image_name}
|
'name': image_name}
|
||||||
|
|
||||||
rest.post('/image/update', payload=payload)
|
res = rest.post('/image/update', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -13,5 +13,8 @@ class OgLive():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_live(rest):
|
def list_live(rest):
|
||||||
r = rest.get('/oglive/list')
|
res = rest.get('/oglive/list')
|
||||||
print_json(r.text)
|
if not res:
|
||||||
|
return 1
|
||||||
|
print_json(res.text)
|
||||||
|
return 0
|
||||||
|
|
|
@ -14,8 +14,11 @@ class OgModes():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_available_modes(rest):
|
def list_available_modes(rest):
|
||||||
r = rest.get('/mode')
|
res = rest.get('/mode')
|
||||||
print_json(r.text)
|
if not res:
|
||||||
|
return 1
|
||||||
|
print_json(res.text)
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def set_modes(rest, args):
|
def set_modes(rest, args):
|
||||||
|
@ -44,8 +47,8 @@ class OgModes():
|
||||||
help='Boot mode to be set')
|
help='Boot mode to be set')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
r = rest.get('/scopes')
|
res = rest.get('/scopes')
|
||||||
scopes = r.json()
|
scopes = res.json()
|
||||||
ips = set()
|
ips = set()
|
||||||
|
|
||||||
for center in parsed_args.center_id:
|
for center in parsed_args.center_id:
|
||||||
|
@ -59,7 +62,10 @@ class OgModes():
|
||||||
|
|
||||||
if not ips:
|
if not ips:
|
||||||
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
||||||
return None
|
return 1
|
||||||
|
|
||||||
payload = {'clients': list(ips), 'mode': parsed_args.mode[0]}
|
payload = {'clients': list(ips), 'mode': parsed_args.mode[0]}
|
||||||
r = rest.post('/mode', payload=payload)
|
res = rest.post('/mode', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -36,8 +36,8 @@ class OgPoweroff():
|
||||||
help='Specific client IP')
|
help='Specific client IP')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
r = rest.get('/scopes')
|
res = rest.get('/scopes')
|
||||||
scopes = r.json()
|
scopes = res.json()
|
||||||
ips = set()
|
ips = set()
|
||||||
|
|
||||||
for center in parsed_args.center_id:
|
for center in parsed_args.center_id:
|
||||||
|
@ -51,7 +51,10 @@ class OgPoweroff():
|
||||||
|
|
||||||
if not ips:
|
if not ips:
|
||||||
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
||||||
return None
|
return 1
|
||||||
|
|
||||||
payload = {'clients': list(ips)}
|
payload = {'clients': list(ips)}
|
||||||
r = rest.post('/poweroff', payload=payload)
|
res = rest.post('/poweroff', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -36,8 +36,8 @@ class OgReboot():
|
||||||
help='Specific client IP')
|
help='Specific client IP')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
r = rest.get('/scopes')
|
res = rest.get('/scopes')
|
||||||
scopes = r.json()
|
scopes = res.json()
|
||||||
ips = set()
|
ips = set()
|
||||||
|
|
||||||
for center in parsed_args.center_id:
|
for center in parsed_args.center_id:
|
||||||
|
@ -51,7 +51,10 @@ class OgReboot():
|
||||||
|
|
||||||
if not ips:
|
if not ips:
|
||||||
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
||||||
return None
|
return 1
|
||||||
|
|
||||||
payload = {'clients': list(ips)}
|
payload = {'clients': list(ips)}
|
||||||
r = rest.post('/reboot', payload=payload)
|
res = rest.post('/reboot', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -13,8 +13,11 @@ class OgRepo():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_repos(rest):
|
def list_repos(rest):
|
||||||
r = rest.get('/repositories')
|
res = rest.get('/repositories')
|
||||||
print_json(r.text)
|
if not res:
|
||||||
|
return 1
|
||||||
|
print_json(res.text)
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _add_repo(rest, parsed_args):
|
def _add_repo(rest, parsed_args):
|
||||||
|
@ -22,37 +25,43 @@ class OgRepo():
|
||||||
'addr': parsed_args.ip,
|
'addr': parsed_args.ip,
|
||||||
'name': parsed_args.name,
|
'name': parsed_args.name,
|
||||||
}
|
}
|
||||||
rest.post('/repository/add', payload=payload)
|
res = rest.post('/repository/add', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _add_repo_ip(rest, parsed_args):
|
def _add_repo_ip(rest, parsed_args):
|
||||||
for ip in parsed_args.ip:
|
for ip in parsed_args.ip:
|
||||||
if not check_address(ip):
|
if not check_address(ip):
|
||||||
print(f'Invalid IP address: {ip}')
|
print(f'Invalid IP address: {ip}')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
r = rest.get('/repositories')
|
res = rest.get('/repositories')
|
||||||
target_repo = None
|
target_repo = None
|
||||||
for repo in r.json()['repositories']:
|
for repo in res.json()['repositories']:
|
||||||
if repo['id'] == parsed_args.id:
|
if repo['id'] == parsed_args.id:
|
||||||
target_repo = repo
|
target_repo = repo
|
||||||
break
|
break
|
||||||
|
|
||||||
if not target_repo:
|
if not target_repo:
|
||||||
print('Invalid repository id specified')
|
print('Invalid repository id specified')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
for ip in parsed_args.ip:
|
for ip in parsed_args.ip:
|
||||||
if ip in target_repo['addr']:
|
if ip in target_repo['addr']:
|
||||||
print(f'The repository already contains the address {ip}')
|
print(f'The repository already contains the address {ip}')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
'id': parsed_args.id,
|
'id': parsed_args.id,
|
||||||
'addr': target_repo['addr'] + parsed_args.ip,
|
'addr': target_repo['addr'] + parsed_args.ip,
|
||||||
'name': target_repo['name'],
|
'name': target_repo['name'],
|
||||||
}
|
}
|
||||||
rest.post('/repository/update', payload=payload)
|
res = rest.post('/repository/update', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add_repo(rest, args):
|
def add_repo(rest, args):
|
||||||
|
@ -101,30 +110,33 @@ class OgRepo():
|
||||||
'name': parsed_args.name,
|
'name': parsed_args.name,
|
||||||
}
|
}
|
||||||
|
|
||||||
rest.post('/repository/update', payload=payload)
|
res = rest.post('/repository/update', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _delete_repo_ip(rest, parsed_args):
|
def _delete_repo_ip(rest, parsed_args):
|
||||||
for ip in parsed_args.ip:
|
for ip in parsed_args.ip:
|
||||||
if not check_address(ip):
|
if not check_address(ip):
|
||||||
print(f'Invalid IP address: {ip}')
|
print(f'Invalid IP address: {ip}')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
r = rest.get('/repositories')
|
res = rest.get('/repositories')
|
||||||
target_repo = None
|
target_repo = None
|
||||||
for repo in r.json()['repositories']:
|
for repo in res.json()['repositories']:
|
||||||
if repo['id'] == parsed_args.id:
|
if repo['id'] == parsed_args.id:
|
||||||
target_repo = repo
|
target_repo = repo
|
||||||
break
|
break
|
||||||
|
|
||||||
if not target_repo:
|
if not target_repo:
|
||||||
print('Invalid repository id specified')
|
print('Invalid repository id specified')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
for ip in parsed_args.ip:
|
for ip in parsed_args.ip:
|
||||||
if ip not in target_repo['addr']:
|
if ip not in target_repo['addr']:
|
||||||
print(f'Invalid address {ip}: The repository has the following IPs: {target_repo["addr"]}')
|
print(f'Invalid address {ip}: The repository has the following IPs: {target_repo["addr"]}')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
target_repo['addr'].remove(ip)
|
target_repo['addr'].remove(ip)
|
||||||
|
|
||||||
|
@ -133,12 +145,18 @@ class OgRepo():
|
||||||
'addr': target_repo['addr'],
|
'addr': target_repo['addr'],
|
||||||
'name': target_repo['name'],
|
'name': target_repo['name'],
|
||||||
}
|
}
|
||||||
rest.post('/repository/update', payload=payload)
|
res = rest.post('/repository/update', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _delete_repo(rest, parsed_args):
|
def _delete_repo(rest, parsed_args):
|
||||||
payload = {'id': parsed_args.id}
|
payload = {'id': parsed_args.id}
|
||||||
rest.post('/repository/delete', payload=payload)
|
res = rest.post('/repository/delete', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_repo(rest, args):
|
def delete_repo(rest, args):
|
||||||
|
@ -184,8 +202,8 @@ class OgRepo():
|
||||||
help='Any valid client ip address')
|
help='Any valid client ip address')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
r = rest.get('/scopes')
|
res = rest.get('/scopes')
|
||||||
scopes = r.json()
|
scopes = res.json()
|
||||||
ips = set()
|
ips = set()
|
||||||
|
|
||||||
for room in parsed_args.room_id:
|
for room in parsed_args.room_id:
|
||||||
|
@ -195,7 +213,10 @@ class OgRepo():
|
||||||
ips.add(l)
|
ips.add(l)
|
||||||
if not ips:
|
if not ips:
|
||||||
print('No valid clients specified.')
|
print('No valid clients specified.')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
payload = {'id': parsed_args.id, 'clients': list(ips)}
|
payload = {'id': parsed_args.id, 'clients': list(ips)}
|
||||||
rest.post('/client/repo', payload=payload)
|
res = rest.post('/client/repo', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -65,7 +65,7 @@ class OgRoom():
|
||||||
err = True
|
err = True
|
||||||
if err:
|
if err:
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
'name': parsed_args.name,
|
'name': parsed_args.name,
|
||||||
|
@ -82,7 +82,10 @@ class OgRoom():
|
||||||
payload['folder_id'] = parsed_args.folder
|
payload['folder_id'] = parsed_args.folder
|
||||||
if parsed_args.desc:
|
if parsed_args.desc:
|
||||||
payload['location'] = parsed_args.desc
|
payload['location'] = parsed_args.desc
|
||||||
rest.post('/room/add', payload=payload)
|
res = rest.post('/room/add', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_room(rest, args):
|
def update_room(rest, args):
|
||||||
|
@ -115,7 +118,7 @@ class OgRoom():
|
||||||
err = True
|
err = True
|
||||||
if err:
|
if err:
|
||||||
parser.print_help(file=sys.stderr)
|
parser.print_help(file=sys.stderr)
|
||||||
sys.exit(1)
|
return 1
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
'id': parsed_args.id,
|
'id': parsed_args.id,
|
||||||
|
@ -124,7 +127,10 @@ class OgRoom():
|
||||||
'gateway': parsed_args.gateway,
|
'gateway': parsed_args.gateway,
|
||||||
}
|
}
|
||||||
|
|
||||||
rest.post('/room/update', payload=payload)
|
res = rest.post('/room/update', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_room(rest, args):
|
def delete_room(rest, args):
|
||||||
|
@ -136,5 +142,8 @@ class OgRoom():
|
||||||
help='room id in database')
|
help='room id in database')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
payload = {'id': parsed_args.id}
|
payload = {'id': parsed_args.id}
|
||||||
rest.post('/room/delete', payload=payload)
|
res = rest.post('/room/delete', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
|
@ -57,8 +57,8 @@ class OgScope():
|
||||||
for ip in parsed_args.client_ip:
|
for ip in parsed_args.client_ip:
|
||||||
ips.add(ip)
|
ips.add(ip)
|
||||||
|
|
||||||
r = rest.get('/scopes')
|
res = rest.get('/scopes')
|
||||||
json_data = json.loads(r.text)
|
json_data = json.loads(res.text)
|
||||||
|
|
||||||
if parsed_args.name:
|
if parsed_args.name:
|
||||||
path = _get_client_path(json_data, None, parsed_args.name)
|
path = _get_client_path(json_data, None, parsed_args.name)
|
||||||
|
@ -71,8 +71,7 @@ class OgScope():
|
||||||
path = _get_client_path(json_data, client_ip, None)
|
path = _get_client_path(json_data, client_ip, None)
|
||||||
for i, item in enumerate(path):
|
for i, item in enumerate(path):
|
||||||
print(' ' * i + item)
|
print(' ' * i + item)
|
||||||
return None
|
|
||||||
else:
|
else:
|
||||||
print_json(r.text)
|
print_json(res.text)
|
||||||
return None
|
return 0
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,11 @@ class OgServer():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_servers(rest):
|
def list_servers(rest):
|
||||||
r = rest.get('/server')
|
res = rest.get('/server')
|
||||||
print_json(r.text)
|
if not res:
|
||||||
|
return 1
|
||||||
|
print_json(res.text)
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add_server(rest, args):
|
def add_server(rest, args):
|
||||||
|
@ -27,10 +30,13 @@ class OgServer():
|
||||||
|
|
||||||
if not check_address(parsed_args.ip):
|
if not check_address(parsed_args.ip):
|
||||||
print(f'Invalid IP address: {parsed_args.ip}')
|
print(f'Invalid IP address: {parsed_args.ip}')
|
||||||
return
|
return 1
|
||||||
|
|
||||||
payload = {'address': parsed_args.ip}
|
payload = {'address': parsed_args.ip}
|
||||||
rest.post('/server', payload=payload)
|
res = rest.post('/server', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_server(rest, args):
|
def delete_server(rest, args):
|
||||||
|
@ -42,4 +48,7 @@ class OgServer():
|
||||||
help='server id in the database')
|
help='server id in the database')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
payload = {'id': parsed_args.id}
|
payload = {'id': parsed_args.id}
|
||||||
rest.delete('/server', payload=payload)
|
res = rest.delete('/server', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -44,8 +44,8 @@ class OgSession():
|
||||||
help='Specific client IP')
|
help='Specific client IP')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
r = rest.get('/scopes')
|
res = rest.get('/scopes')
|
||||||
scopes = r.json()
|
scopes = res.json()
|
||||||
ips = set()
|
ips = set()
|
||||||
|
|
||||||
for center in parsed_args.center_id:
|
for center in parsed_args.center_id:
|
||||||
|
@ -59,11 +59,14 @@ class OgSession():
|
||||||
|
|
||||||
if not ips:
|
if not ips:
|
||||||
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
||||||
return None
|
return 1
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
'clients': list(ips),
|
'clients': list(ips),
|
||||||
'disk': parsed_args.disk,
|
'disk': parsed_args.disk,
|
||||||
'partition': parsed_args.part,
|
'partition': parsed_args.part,
|
||||||
}
|
}
|
||||||
r = rest.post('/session', payload=payload)
|
res = rest.post('/session', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
|
@ -53,8 +53,8 @@ class OgWol():
|
||||||
help='Specific client IP')
|
help='Specific client IP')
|
||||||
parsed_args = parser.parse_args(args)
|
parsed_args = parser.parse_args(args)
|
||||||
|
|
||||||
r = rest.get('/scopes')
|
res = rest.get('/scopes')
|
||||||
scopes = r.json()
|
scopes = res.json()
|
||||||
ips = set()
|
ips = set()
|
||||||
|
|
||||||
for center in parsed_args.center_id:
|
for center in parsed_args.center_id:
|
||||||
|
@ -68,7 +68,10 @@ class OgWol():
|
||||||
|
|
||||||
if not ips:
|
if not ips:
|
||||||
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
print("Missing --client-ip, or --room-id/--center-id. No clients provided.")
|
||||||
return None
|
return 1
|
||||||
|
|
||||||
payload = {'type': parsed_args.type, 'clients': list(ips)}
|
payload = {'type': parsed_args.type, 'clients': list(ips)}
|
||||||
r = rest.post('/wol', payload=payload)
|
res = rest.post('/wol', payload=payload)
|
||||||
|
if not res:
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
3
ogcli
3
ogcli
|
@ -59,7 +59,8 @@ class CLI():
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Call the command with the same name.
|
# Call the command with the same name.
|
||||||
getattr(self.ogcli, args.command)(sys.argv[2:])
|
res = getattr(self.ogcli, args.command)(sys.argv[2:])
|
||||||
|
sys.exit(res)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue