From 34a7cd4c4f4ec4700343ed682cca5f7628a96f48 Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Thu, 25 Jan 2024 12:12:51 +0100 Subject: views: Check HTTP GET response from ogserver (part 2) Check that connection to ogserver works and GET response is ok. Otherwise, show error message to the user and redirect. If there is an error while talking to server, a serverError or a serverErrorCode is raised so that it is possible to differentiate from different errors and give an appropiate error to the user --- ogcp/views.py | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 165 insertions(+), 21 deletions(-) diff --git a/ogcp/views.py b/ogcp/views.py index 41576af..5f9de2d 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -84,6 +84,20 @@ login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = 'login' +class ServerError(Exception): + pass + +class ServerErrorCode(Exception): + pass + +def ogserver_down(view): + flash(_('Cannot talk to ogserver. Is ogserver down?'), category='error') + return redirect(url_for(view)) + +def ogserver_error(view): + flash(_('ogserver replied with a bad HTTP status code'), category='error') + return redirect(url_for(view)) + def validate_elements(elements, min_len=1, max_len=float('inf')): valid = True if len(elements) < min_len: @@ -109,6 +123,10 @@ def get_client_setup(ip): payload = {'client': [ip]} server = get_server_from_clients([ip]) r = server.get('/client/setup', payload) + if not r: + raise ServerError + if r.status_code != requests.codes.ok: + raise ServerErrorCode db_partitions = r.json()['partitions'] for partition in db_partitions: if partition['partition'] == 0: @@ -137,7 +155,12 @@ def get_clients(state_filter=None): def get_repository(repository_id, server): - repositories = get_repositories(server) + try: + repositories = get_repositories(server) + except ServerError: + raise ServerError + except ServerErrorCode: + raise ServerErrorCode [repository] = [repository for repository in repositories if repository['id'] == repository_id] return repository @@ -145,6 +168,10 @@ def get_repository(repository_id, server): def get_repositories(server): r = server.get('/repositories') + if not r: + raise ServerError + if r.status_code != requests.codes.ok: + raise ServerErrorCode repositories = r.json()['repositories'] return repositories @@ -539,7 +566,12 @@ def action_setup_show(): ips = set(args['ips'].split(' ')) base_client = args['selected_client'] - db_partitions = get_client_setup(base_client) + try: + db_partitions = get_client_setup(base_client) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') if not db_partitions: flash(_('Partition information is not available. Boot client in ogLive mode to obtain it'), category='error') return redirect(url_for('commands')) @@ -639,6 +671,10 @@ def search_image(images_list, image_id): def get_images_grouped_by_repos_from_server(server): r = server.get('/images') + if not r: + raise ServerError + if r.status_code != requests.codes.ok: + raise ServerErrorCode images = r.json()['images'] repos={} @@ -654,6 +690,10 @@ def get_clients_repo(server, ips): repo_id=None for ip in ips: r = server.get('/client/info', payload={'client': [ip]}) + if not r: + raise ServerError + if r.status_code != requests.codes.ok: + raise ServerErrorCode repo_id_aux = r.json()['repo_id'] if repo_id is None: repo_id = repo_id_aux @@ -679,7 +719,12 @@ def action_image_restore(): if not image: flash(_(f'Image to restore was not found'), category='error') return redirect(url_for('commands')) - repository = get_repository(image['repo_id'], server) + try: + repository = get_repository(image['repo_id'], server) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') payload = {'disk': disk, 'partition': partition, @@ -703,11 +748,23 @@ def action_image_restore(): server = get_server_from_clients(ips) - repo_id = get_clients_repo(server, ips) + try: + repo_id = get_clients_repo(server, ips) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') + if repo_id is None: flash(_(f'Computers have different repos assigned'), category='error') return redirect(url_for('commands')) - images = get_images_grouped_by_repos_from_server(server) + try: + images = get_images_grouped_by_repos_from_server(server) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') + if repo_id not in images: flash(_(f'Computer(s) assigned to a repo with no images'), category='error') return redirect(url_for('commands')) @@ -912,7 +969,12 @@ def action_client_info(): form.remote.render_kw = {'readonly': True} form.maintenance.data = db_client['maintenance'] form.maintenance.render_kw = {'readonly': True} - repositories = get_repositories(server) + try: + repositories = get_repositories(server) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') form.repo.choices = [(repo["id"], repo["name"]) for repo in repositories if db_client['repo_id'] == repo["id"]] form.repo.render_kw = {'readonly': True} @@ -959,7 +1021,13 @@ def action_client_info(): images = r.json()['images'] ip = list(ips)[0] - setup = get_client_setup(ip) + try: + setup = get_client_setup(ip) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') + if setup and setup[0].get('code') == 'MSDOS': setup[0]['code'] = 'MBR' @@ -1054,13 +1122,24 @@ def action_client_update(): form.room.choices = list(rooms) form.room.render_kw = {'readonly': True} - repositories = get_repositories(server) + try: + repositories = get_repositories(server) + except ServerError: + return ogserver_down('scopes') + except ServerErrorCode: + return ogserver_error('scopes') form.repo.choices = [(repo["id"], repo["name"]) for repo in repositories if db_client['repo_id'] == repo["id"]] form.repo.choices.extend([(repo["id"], repo["name"]) for repo in repositories if db_client['repo_id'] != repo["id"]]) ip = list(ips)[0] - setup = get_client_setup(ip) + try: + setup = get_client_setup(ip) + except ServerError: + return ogserver_down('scopes') + except ServerErrorCode: + return ogserver_error('scopes') + if setup and setup[0].get('code') == 'MSDOS': setup[0]['code'] = 'MBR' @@ -1230,7 +1309,12 @@ def action_client_add(): form.room.choices = list(rooms) form.room.render_kw = {'readonly': True} - repositories = get_repositories(server) + try: + repositories = get_repositories(server) + except ServerError: + return ogserver_down('scopes') + except ServerErrorCode: + return ogserver_error('scopes') form.repo.choices = [(repo["id"], repo["name"]) for repo in repositories] if params.get('folder'): @@ -1272,7 +1356,12 @@ def action_clients_import_get(): for room in rooms if room['id'] == int(selected_room_id)] form.room.choices = selected_room form.room.render_kw = {'readonly': True} - repositories = get_repositories(server) + try: + repositories = get_repositories(server) + except ServerError: + return ogserver_down('scopes') + except ServerErrorCode: + return ogserver_error('scopes') form.repo.choices = [(repo["id"], repo["name"]) for repo in repositories] form.dhcpd_conf.render_kw = {'placeholder': PLACEHOLDER_TEXT} @@ -1379,6 +1468,10 @@ def get_clients_modes(ips, server): modes = {} for ip in ips: r = server.get('/client/info', payload={"client": [ip]}) + if not r: + raise ServerError + if r.status_code != requests.codes.ok: + raise ServerErrorCode resp = r.json() current_boot = resp['boot'] client_name = resp['name'] @@ -1411,7 +1504,12 @@ def action_mode(): return redirect(url_for('commands')) server = get_server_from_clients(ips) - modes_set = get_clients_modes(ips, server) + try: + modes_set = get_clients_modes(ips, server) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') r = server.get('/mode') if r.status_code != requests.codes.ok: flash(_('Ogserver replied with status code not ok'), @@ -1529,7 +1627,12 @@ def action_image_create(): return redirect(url_for('commands')) client_repo_id = r.json()['repo_id'] - repositories = get_repositories(server) + try: + repositories = get_repositories(server) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') form.repository.choices = [ (repo['id'], repo['name']) for repo in repositories if client_repo_id == repo['id']] form.repository.render_kw = {'readonly': True} @@ -1558,7 +1661,12 @@ def action_image_update(): if not image: flash(_('Image to restore was not found'), category='error') return redirect(url_for('commands')) - repository = get_repository(image['repo_id'], server) + try: + repository = get_repository(image['repo_id'], server) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') payload = {'clients': [ip], 'disk': disk, 'partition': partition, @@ -1590,7 +1698,13 @@ def action_image_update(): return redirect(url_for('commands')) repo_id = r.json()['repo_id'] - images = get_images_grouped_by_repos_from_server(server) + try: + images = get_images_grouped_by_repos_from_server(server) + except ServerError: + return ogserver_down('commands') + except ServerErrorCode: + return ogserver_error('commands') + if repo_id not in images: flash(_('Computer is assigned to a repo with no images'), category='error') @@ -1820,7 +1934,12 @@ def get_images_grouped_by_repos(): server={} server['server'] = resp['server'] images=resp['json']['images'] - all_repos=get_repositories(resp['server']) + try: + all_repos=get_repositories(resp['server']) + except ServerError: + continue + except ServerErrorCode: + continue repos={} for image in images: repo_id=image['repo_id'] @@ -1910,7 +2029,12 @@ def action_repo_update(): repo_id = repos.pop() server_ip_port = params.get('repos-server') server = get_server_from_ip_port(server_ip_port) - repository = get_repository(int(repo_id), server) + try: + repository = get_repository(int(repo_id), server) + except ServerError: + return ogserver_down('manage_repos') + except ServerErrorCode: + return ogserver_error('manage_repos') form.server.data = server_ip_port form.repo_id.data = repo_id @@ -1949,7 +2073,12 @@ def action_repo_delete(): repo_id = int(repo_id) server_ip_port = params.get('repos-server') server = get_server_from_ip_port(server_ip_port) - repository = get_repository(repo_id, server) + try: + repository = get_repository(repo_id, server) + except ServerError: + return ogserver_down('manage_repos') + except ServerErrorCode: + return ogserver_error('manage_repos') form.server.data = server_ip_port form.repo_id.data = repo_id form.name.data = repository['name'] @@ -1976,7 +2105,12 @@ def action_repo_info(): repo_id = int(repo_id) server_ip_port = params.get('repos-server') server = get_server_from_ip_port(server_ip_port) - repository = get_repository(repo_id, server) + try: + repository = get_repository(repo_id, server) + except ServerError: + return ogserver_down('manage_repos') + except ServerErrorCode: + return ogserver_error('manage_repos') form.name.data = repository['name'] form.name.render_kw = {'readonly': True} form.ip.data = repository['ip'] @@ -2332,7 +2466,12 @@ def action_image_info(): form.software_id.data = image['software_id'] form.description.data = image['description'] - responses = get_images_grouped_by_repos() + try: + responses = get_images_grouped_by_repos() + except ServerError: + return ogserver_down('images') + except ServerErrorCode: + return ogserver_error('images') return render_template('actions/image_details.html', form=form, responses=responses) @@ -2362,7 +2501,12 @@ def action_image_delete(): return redirect(url_for('images')) image_name, image_id = images[0] server = get_server_from_ip_port(params['image-server']) - responses = get_images_grouped_by_repos() + try: + responses = get_images_grouped_by_repos() + except ServerError: + return ogserver_down('images') + except ServerErrorCode: + return ogserver_error('images') form.ids.data = image_id form.server.data = params['image-server'] if not validate_elements(images, max_len=1): -- cgit v1.2.3-18-g5258