summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOpenGnSys Support Team <soporte-og@soleta.eu>2024-01-25 12:28:02 +0100
committerOpenGnSys Support Team <soporte-og@soleta.eu>2024-01-25 12:28:02 +0100
commit8260cd0ac22641800f23e58ae05dabd2d207e27a (patch)
tree67630521876f82fadd15acff47839b1daaa2a5cf
parent34a7cd4c4f4ec4700343ed682cca5f7628a96f48 (diff)
views: consolidate server error reporting
use ogserver_down() and ogserver_error()
-rw-r--r--ogcp/views.py181
1 files changed, 110 insertions, 71 deletions
diff --git a/ogcp/views.py b/ogcp/views.py
index 5f9de2d..bf30e0f 100644
--- a/ogcp/views.py
+++ b/ogcp/views.py
@@ -535,9 +535,11 @@ def action_setup_select():
client_choices = []
for ip in ips:
r = server.get('/client/info', payload={'client': [ip]})
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
+
client_name = r.json()['name']
client_choices.append((ip, f"{client_name} ({ip})"))
form.selected_client.choices = client_choices
@@ -711,9 +713,11 @@ def action_image_restore():
image_id = form.image.data
server = get_server_from_clients(ips)
r = server.get('/images')
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
+
images_list = r.json()['images']
image = search_image(images_list, int(image_id))
if not image:
@@ -774,6 +778,11 @@ def action_image_restore():
part_choices = []
for ip in ips:
r = server.get('/client/setup', payload={'client': [ip]})
+ if not r:
+ return ogserver_down('commands')
+ if r.status_code != requests.codes.ok:
+ return ogserver_error('commands')
+
if r.status_code == requests.codes.ok:
partitions = r.json()['partitions']
parts = []
@@ -794,10 +803,6 @@ def action_image_restore():
flash(_(f'Computers have different partition setup'), category='error')
return redirect(url_for('commands'))
- else:
- flash(_('ogServer was unable to obtain setup of selected computer {}').format(ip), category='error')
- return redirect(url_for('commands'))
-
form.partition.choices = [
(f"{disk_id} {part_id}",
f"Disk {disk_id} | Partition {part_id} "
@@ -833,9 +838,11 @@ def action_hardware():
form.ips.data = ' '.join(ips)
server = get_server_from_clients(ips)
r = server.get('/hardware', payload={'client': list(ips)})
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
+
hardware = r.json()['hardware']
return render_template('actions/hardware.html', form=form,
hardware=hardware, scopes=scopes)
@@ -852,11 +859,15 @@ def action_software():
r = server.get('/software', payload={'client': ips,
'disk': int(disk),
'partition': int(partition)})
- if r.status_code == requests.codes.ok:
- software = r.json()['software']
- scopes, clients = get_scopes(set(ips))
- return render_template('actions/software_list.html',
- software=software, form=form, scopes=scopes)
+ if not r:
+ return ogserver_down('commands')
+ if r.status_code != requests.codes.ok:
+ return ogserver_error('commands')
+
+ software = r.json()['software']
+ scopes, clients = get_scopes(set(ips))
+ return render_template('actions/software_list.html',
+ software=software, form=form, scopes=scopes)
elif form.update.data:
r = server.post('/software', payload={'clients': ips,
'disk': disk,
@@ -877,9 +888,10 @@ def action_software():
form.ips.data = ' '.join(ips)
server = get_server_from_clients(ips)
r = server.get('/client/setup', payload={'client': list(ips)})
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
if not r.json()['partitions']:
flash(_('Software inventory is not available. Boot client in ogLive mode to obtain it'), category='error')
@@ -917,9 +929,10 @@ def action_session():
server = get_server_from_clients(list(ips))
form.ips.data = ' '.join(ips)
r = server.get('/session', payload={'client': list(ips)})
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
sessions = r.json()['sessions']
if not sessions:
@@ -949,9 +962,10 @@ def action_client_info():
payload = {'client': list(ips)}
r = server.get('/client/info', payload)
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
db_client = r.json()
@@ -984,27 +998,30 @@ def action_client_info():
form.boot.render_kw = {'readonly': True}
r = server.get('/oglive/list')
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('ogServer: error retrieving oglive list'),
- category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
+
available_oglives = r.json()['oglive']
for oglive in available_oglives:
choice = (oglive.get('directory'), oglive.get('directory'))
form.livedir.choices.append(choice)
r = server.get('/mode')
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
available_modes = [(mode, mode) for mode in r.json()['modes']]
form.boot.choices = list(available_modes)
r = server.get('/scopes')
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
rooms = parse_scopes_from_tree(r.json(), 'room')
rooms = [(room['id'], room['name']) for room in rooms
@@ -1014,9 +1031,10 @@ def action_client_info():
form.submit.render_kw = {"style": "visibility:hidden;"}
r = server.get('/images')
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
images = r.json()['images']
@@ -1082,9 +1100,10 @@ def action_client_update():
payload = {'client': list(ips)}
r = server.get('/client/info', payload)
+ if not r:
+ return ogserver_down('scopes')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('scopes'))
+ return ogserver_error('scopes')
db_client = r.json()
@@ -1101,9 +1120,11 @@ def action_client_update():
current_mode = db_client['boot']
r = server.get('/mode')
+ if not r:
+ return ogserver_down('scopes')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('scopes'))
+ return ogserver_error('scopes')
+
available_modes = [(current_mode, current_mode)]
available_modes.extend([(mode, mode) for mode in r.json()['modes']
if mode != current_mode])
@@ -1111,9 +1132,10 @@ def action_client_update():
form.boot.render_kw = {'readonly': True}
r = server.get('/scopes')
+ if not r:
+ return ogserver_down('scopes')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('scopes'))
+ return ogserver_error('scopes')
room_id = db_client['room']
rooms = parse_scopes_from_tree(r.json(), 'room')
@@ -1144,10 +1166,11 @@ def action_client_update():
setup[0]['code'] = 'MBR'
r = server.get('/images')
+ if not r:
+ return ogserver_down('scopes')
if r.status_code != requests.codes.ok:
- flash(_('ogServer: error retrieving images list'),
- category='error')
- return redirect(url_for('scopes'))
+ return ogserver_error('scopes')
+
images = r.json()['images']
for entry in setup:
if images and entry['image'] != 0:
@@ -1289,18 +1312,20 @@ def action_client_add():
form.server.data = params['scope-server']
server = get_server_from_ip_port(params['scope-server'])
r = server.get('/mode')
+ if not r:
+ return ogserver_down('scopes')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('scopes'))
+ return ogserver_error('scopes')
available_modes = [(mode, mode) for mode in r.json()['modes']]
form.boot.choices = list(available_modes)
form.mac.render_kw = {'placeholder': 'aabbccddeeaa'}
r = server.get('/scopes')
+ if not r:
+ return ogserver_down('scopes')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('scopes'))
+ return ogserver_error('scopes')
room_id = params['scope-room']
rooms = parse_scopes_from_tree(r.json(), 'room')
@@ -1346,9 +1371,10 @@ def action_clients_import_get():
form.server.data = params['scope-server']
server = get_server_from_ip_port(params['scope-server'])
r = server.get('/scopes')
+ if not r:
+ return ogserver_down('scopes')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('scopes'))
+ return ogserver_error('scopes')
rooms = parse_scopes_from_tree(r.json(), 'room')
selected_room_id = params['scope-room']
@@ -1511,10 +1537,11 @@ def action_mode():
except ServerErrorCode:
return ogserver_error('commands')
r = server.get('/mode')
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Ogserver replied with status code not ok'),
- category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
+
most_used_mode = max(modes_set, key=lambda m: len(modes_set[m]))
available_modes = []
if most_used_mode in r.json()['modes']:
@@ -1559,10 +1586,11 @@ def action_oglive():
server = get_server_from_clients(list(ips))
r = server.get('/oglive/list')
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Ogserver replied with status code not ok'),
- category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
+
available_oglives = [(oglive.get('directory'), oglive.get('directory'))
for oglive in r.json()['oglive']]
available_oglives.insert(0, ('default', 'default'))
@@ -1583,9 +1611,10 @@ def action_image_create():
ip = form.ip.data
server = get_server_from_clients([ip])
r = server.get('/client/info', payload={"client": [ip]})
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
disk, partition, code = form.os.data.split(' ')
payload = {"clients": [ip],
@@ -1610,9 +1639,10 @@ def action_image_create():
server = get_server_from_clients(ips)
r = server.get('/client/setup', payload={'client': list(ips)})
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
for part in r.json()['partitions'][1:]:
form.os.choices.append(
@@ -1622,9 +1652,10 @@ def action_image_create():
f"{FS_CODES[part.get('filesystem')]}")
)
r = server.get('/client/info', payload={'client': list(ips)})
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
client_repo_id = r.json()['repo_id']
try:
@@ -1652,9 +1683,10 @@ def action_image_update():
image_id = form.image.data
server = get_server_from_clients([ip])
r = server.get('/images')
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
images_list = r.json()['images']
image = search_image(images_list, int(image_id))
@@ -1693,9 +1725,10 @@ def action_image_update():
server = get_server_from_clients(ips)
r = server.get('/client/info', payload={'client': list(ips)})
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
repo_id = r.json()['repo_id']
try:
@@ -1713,9 +1746,10 @@ def action_image_update():
form.image.choices.append((image['id'], image['name']))
r = server.get('/client/setup', payload={'client': list(ips)})
+ if not r:
+ return ogserver_down('commands')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('commands'))
+ return ogserver_error('commands')
for part in r.json()['partitions'][1:]:
form.os.choices.append(
@@ -1825,9 +1859,10 @@ def action_center_delete():
return redirect(url_for('scopes'))
server = get_server_from_ip_port(params['scope-server'])
r = server.get('/scopes')
+ if not r:
+ return ogserver_down('scopes')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('scopes'))
+ return ogserver_error('scopes')
selected_center_id = params['scope-center']
centers = parse_scopes_from_tree(r.json(), 'center')
@@ -1864,9 +1899,10 @@ def action_room_add():
return redirect(url_for('scopes'))
server = get_server_from_ip_port(params['scope-server'])
r = server.get('/scopes')
+ if not r:
+ return ogserver_down('scopes')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('scopes'))
+ return ogserver_error('scopes')
selected_center_id = params['scope-center']
centers = parse_scopes_from_tree(r.json(), 'center')
@@ -1906,9 +1942,10 @@ def action_room_delete():
return redirect(url_for('scopes'))
server = get_server_from_ip_port(params['scope-server'])
r = server.get('/scopes')
+ if not r:
+ return ogserver_down('scopes')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('scopes'))
+ return ogserver_error('scopes')
rooms = parse_scopes_from_tree(r.json(), 'room')
selected_room_id = params['scope-room']
@@ -2451,9 +2488,11 @@ def action_image_info():
id = ids.pop()
server = get_server_from_ip_port(params['image-server'])
r = server.get('/images')
+ if not r:
+ return ogserver_down('images')
if r.status_code != requests.codes.ok:
- flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error')
- return redirect(url_for('images'))
+ return ogserver_error('images')
+
images = r.json()['images']
image = next(img for img in images if img['id'] == int(id))