From 233bbdfcab3635c47edddc3276ea25224d37a731 Mon Sep 17 00:00:00 2001 From: Omar Soriano Date: Mon, 3 Jun 2024 18:11:33 +0000 Subject: [PATCH] Address deploy-board lint errors `ruff` was recently introduced to the precommit checks. This PR addresses the existing checks for deploy-board: * Run `ruff check --fix` * Run `ruff check --fix --unsafe-fixes` * Verified the fixes were safe * Use explicit exceptions * Remove unused variables and methods * Remove trailing ws --- deploy-board/deploy_board/urls.py | 7 +-- deploy-board/deploy_board/webapp/accounts.py | 2 +- .../deploy_board/webapp/accounts_views.py | 2 +- .../deploy_board/webapp/agent_report.py | 2 - deploy-board/deploy_board/webapp/auth.py | 16 +++--- .../deploy_board/webapp/cluster_view.py | 20 ++++---- deploy-board/deploy_board/webapp/common.py | 1 - .../deploy_board/webapp/deploy_views.py | 6 ++- deploy-board/deploy_board/webapp/env_views.py | 21 ++++---- .../deploy_board/webapp/error_views.py | 2 +- .../deploy_board/webapp/group_view.py | 51 +++++++++---------- .../helpers/autoscaling_metrics_helper.py | 10 ++-- .../webapp/helpers/base_client.py | 2 +- .../webapp/helpers/baseimages_helper.py | 4 +- .../webapp/helpers/cmdbapiclient.py | 3 +- .../webapp/helpers/environs_helper.py | 4 +- .../webapp/helpers/hosts_helper.py | 5 +- .../webapp/helpers/nimbusclient.py | 4 +- .../deploy_board/webapp/host_views.py | 10 ++-- .../deploy_board/webapp/promote_views.py | 2 +- .../deploy_board/webapp/schedule_views.py | 7 ++- deploy-board/deploy_board/webapp/security.py | 2 +- .../deploy_board/webapp/service_add_ons.py | 8 +-- .../deploy_board/webapp/templatetags/utils.py | 4 +- deploy-board/deploy_board/webapp/urls.py | 3 +- .../deploy_board/webapp/util_views.py | 19 ++++--- deploy-board/deploy_board/wsgi.py | 8 +-- deploy-board/integ_test/env_config_tests.py | 5 +- 28 files changed, 112 insertions(+), 118 deletions(-) diff --git a/deploy-board/deploy_board/urls.py b/deploy-board/deploy_board/urls.py index 70aaaab0b0..d1a757f7d6 100644 --- a/deploy-board/deploy_board/urls.py +++ b/deploy-board/deploy_board/urls.py @@ -3,9 +3,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,9 +18,10 @@ from django.conf.urls.static import static from django.conf import settings +from .settings import IS_PINTEREST + admin.autodiscover() -from .settings import IS_PINTEREST if IS_PINTEREST: urlpatterns = [ diff --git a/deploy-board/deploy_board/webapp/accounts.py b/deploy-board/deploy_board/webapp/accounts.py index f3c6bb0b0c..bf7b8838f2 100644 --- a/deploy-board/deploy_board/webapp/accounts.py +++ b/deploy-board/deploy_board/webapp/accounts.py @@ -36,7 +36,7 @@ def get_accounts(report): def add_account_from_cluster(request, cluster, accounts): account_id = cluster.get("accountId") - account = None; + account = None if account_id is not None: account = accounts_helper.get_by_cell_and_id( request, cluster["cellName"], account_id) diff --git a/deploy-board/deploy_board/webapp/accounts_views.py b/deploy-board/deploy_board/webapp/accounts_views.py index 344d969e4f..512ae9b110 100644 --- a/deploy-board/deploy_board/webapp/accounts_views.py +++ b/deploy-board/deploy_board/webapp/accounts_views.py @@ -1,7 +1,7 @@ from django.shortcuts import render, redirect from django.views.generic import View -from .helpers import accounts_helper; +from .helpers import accounts_helper class AccountsView(View): def get(self, request): diff --git a/deploy-board/deploy_board/webapp/agent_report.py b/deploy-board/deploy_board/webapp/agent_report.py index 4126f680e2..94796c9947 100644 --- a/deploy-board/deploy_board/webapp/agent_report.py +++ b/deploy-board/deploy_board/webapp/agent_report.py @@ -15,10 +15,8 @@ # -*- coding: utf-8 -*- """Helper functions to help generate agents views """ -from deploy_board.webapp.helpers import agents_helper from .common import is_agent_failed from .helpers import builds_helper, deploys_helper, environs_helper, environ_hosts_helper -from deploy_board.settings import IS_PINTEREST import time from collections import OrderedDict from functools import cmp_to_key diff --git a/deploy-board/deploy_board/webapp/auth.py b/deploy-board/deploy_board/webapp/auth.py index ab71538494..7bc91cd15a 100644 --- a/deploy-board/deploy_board/webapp/auth.py +++ b/deploy-board/deploy_board/webapp/auth.py @@ -82,25 +82,25 @@ class OAuthExpiredTokenException(Exception): class OAuthHandler(object): def token_getter(self, **kwargs): - raise NotImplemented + raise NotImplementedError def token_setter(self, token, expires, **kwargs): - raise NotImplemented + raise NotImplementedError def state_generator(self, **kwargs): - raise NotImplemented + raise NotImplementedError def state_getter(self, **kwargs): - raise NotImplemented + raise NotImplementedError def state_setter(self, value, **kwargs): - raise NotImplemented + raise NotImplementedError def state_remove(self, **kwargs): - raise NotImplemented + raise NotImplementedError def token_remove(self, **kwargs): - raise NotImplemented + raise NotImplementedError class SessionOauthHandler(OAuthHandler): @@ -210,7 +210,7 @@ def handle_oauth2_response(self, code, state_with_data, **kwargs): data=str(body) if body else None, method='POST', ) - if resp.code is 401: + if resp.code == 401: # When auth.pinadmin.com returns a 401 error. remove token and redirect to / page raise OAuthExpiredTokenException("Expired Token") diff --git a/deploy-board/deploy_board/webapp/cluster_view.py b/deploy-board/deploy_board/webapp/cluster_view.py index 86c05a9fab..bf5fb0b1c7 100644 --- a/deploy-board/deploy_board/webapp/cluster_view.py +++ b/deploy-board/deploy_board/webapp/cluster_view.py @@ -32,7 +32,7 @@ from .helpers import baseimages_helper, hosttypes_helper, securityzones_helper, placements_helper, \ autoscaling_groups_helper, groups_helper, cells_helper, arches_helper, accounts_helper -from .helpers import clusters_helper, environs_helper, environ_hosts_helper, baseimages_helper +from .helpers import clusters_helper, environs_helper, environ_hosts_helper from .helpers.exceptions import NotAuthorizedException, TeletraanException, IllegalArgumentException from . import common import traceback @@ -197,6 +197,7 @@ def get(self, request, name, stage): def post(self, request, name, stage): ret = 200 + exception = None log.info("Post to capacity with data {0}".format(request.body)) try: cluster_name = '{}-{}'.format(name, stage) @@ -218,16 +219,18 @@ def post(self, request, name, stage): except NotAuthorizedException as e: log.error("Have an NotAuthorizedException error {}".format(e)) ret = 403 + exception = e except Exception as e: log.error("Have an error {}", e) ret = 500 + exception = e finally: if ret == 200: return HttpResponse("{}", content_type="application/json") else: environs_helper.remove_env_capacity( request, name, stage, capacity_type="GROUP", data=cluster_name) - return HttpResponse(e, status=ret, content_type="application/json") + return HttpResponse(exception, status=ret, content_type="application/json") class ClusterConfigurationView(View): @@ -1137,13 +1140,13 @@ def gen_auto_cluster_refresh_view(request, name, stage): # get default configurations for first time try: - if auto_refresh_config == None: + if auto_refresh_config is None: auto_refresh_config = clusters_helper.get_default_cluster_auto_refresh_config(request, cluster_name) auto_refresh_config["launchBeforeTerminate"] = True auto_refresh_config["config"]["minHealthyPercentage"] = 100 auto_refresh_config["config"]["maxHealthyPercentage"] = 110 else: - if auto_refresh_config["config"]["maxHealthyPercentage"] == None: + if auto_refresh_config["config"]["maxHealthyPercentage"] is None: auto_refresh_config["terminateAndLaunch"] = True elif auto_refresh_config["config"]["minHealthyPercentage"] == 100: auto_refresh_config["launchBeforeTerminate"] = True @@ -1179,7 +1182,7 @@ def gen_auto_cluster_refresh_view(request, name, stage): def sanitize_slack_email_input(input): res = '' - if input == None or len(input) == 0: + if input is None or len(input) == 0: return res tokens = input.strip().split(',') @@ -1264,7 +1267,7 @@ def submit_auto_refresh_config(request, name, stage): group_info["groupInfo"]["chatroom"] = slack_channels autoscaling_groups_helper.update_group_info(request, cluster_name, group_info["groupInfo"]) messages.success(request, "Auto refresh config saved successfully.", "cluster-replacements") - except IllegalArgumentException as e: + except IllegalArgumentException: log.exception("Failed to update refresh config. Some request could succeed.") pass except Exception as e: @@ -1290,13 +1293,13 @@ def gen_replacement_config(request): if params["availabilitySettingRadio"] == "launchBeforeTerminate": rollingUpdateConfig["minHealthyPercentage"] = 100 - if params["maxHealthyPercentage"] == None or len(params["maxHealthyPercentage"]) == 0: + if params["maxHealthyPercentage"] is None or len(params["maxHealthyPercentage"]) == 0: rollingUpdateConfig["maxHealthyPercentage"] = 110 else: rollingUpdateConfig["maxHealthyPercentage"] = params["maxHealthyPercentage"] elif params["availabilitySettingRadio"] == "terminateAndLaunch": rollingUpdateConfig["maxHealthyPercentage"] = None - if params["minHealthyPercentage"] == None or len(params["minHealthyPercentage"]) == 0: + if params["minHealthyPercentage"] is None or len(params["minHealthyPercentage"]) == 0: rollingUpdateConfig["minHealthyPercentage"] = 100 else: rollingUpdateConfig["minHealthyPercentage"] = params["minHealthyPercentage"] @@ -1502,4 +1505,3 @@ def get_current_cluster(request, name, stage, env=None): cluster_name = env.get("clusterName") current_cluster = clusters_helper.get_cluster(request, cluster_name) return current_cluster - diff --git a/deploy-board/deploy_board/webapp/common.py b/deploy-board/deploy_board/webapp/common.py index bf04d0f454..3996d37df4 100644 --- a/deploy-board/deploy_board/webapp/common.py +++ b/deploy-board/deploy_board/webapp/common.py @@ -17,7 +17,6 @@ """ import logging from .helpers import environs_helper, deploys_helper, builds_helper, tags_helper -from deploy_board.settings import IS_PINTEREST DEFAULT_BUILD_SIZE = 30 DEFAULT_COMMITS_SIZE = 30 diff --git a/deploy-board/deploy_board/webapp/deploy_views.py b/deploy-board/deploy_board/webapp/deploy_views.py index f6d11aae97..b20726d753 100644 --- a/deploy-board/deploy_board/webapp/deploy_views.py +++ b/deploy-board/deploy_board/webapp/deploy_views.py @@ -15,6 +15,7 @@ # -*- coding: utf-8 -*- """Collection of all deploy related views """ +import logging from deploy_board.settings import SITE_METRICS_CONFIGS, TELETRAAN_DISABLE_CREATE_ENV_PAGE, TELETRAAN_REDIRECT_CREATE_ENV_PAGE_URL from django.middleware.csrf import get_token from .accounts import get_accounts_from_deploy @@ -23,9 +24,11 @@ from django.views.generic import View from django.template.loader import render_to_string from django.http import HttpResponse -from .helpers import builds_helper, deploys_helper, environs_helper, tags_helper, clusters_helper, accounts_helper +from .helpers import builds_helper, deploys_helper, environs_helper, tags_helper +log = logging.getLogger(__name__) + DEFAULT_PAGE_SIZE = 30 DEFAULT_ONGOING_DEPLOY_SIZE = 10 @@ -145,7 +148,6 @@ def get(self, request, deploy_id): deploy = deploys_helper.get(request, deploy_id) build_with_tag = builds_helper.get_build_and_tag(request, deploy['buildId']) env = None - account = None deploy_accounts = [] if deploy.get('envId'): env = environs_helper.get(request, deploy['envId']) diff --git a/deploy-board/deploy_board/webapp/env_views.py b/deploy-board/deploy_board/webapp/env_views.py index ebeef6332a..614b8793e8 100644 --- a/deploy-board/deploy_board/webapp/env_views.py +++ b/deploy-board/deploy_board/webapp/env_views.py @@ -35,7 +35,6 @@ from .accounts import get_accounts, get_accounts_from_deploy, create_legacy_ui_account, add_account_from_cluster, add_legacy_accounts import random import json -import requests from collections import Counter from .helpers import builds_helper, environs_helper, agents_helper, ratings_helper, deploys_helper, \ systems_helper, environ_hosts_helper, clusters_helper, tags_helper, baseimages_helper, schedules_helper, placements_helper, hosttypes_helper, \ @@ -356,7 +355,7 @@ def check_feedback_eligible(request, username): if num <= 10: return True return False - except: + except Exception: log.error(traceback.format_exc()) return False @@ -605,13 +604,13 @@ def _gen_message_for_refreshing_cluster(request, last_cluster_refresh_status, la any_host_with_outdated_ami = len(amis) > 1 or (len(amis) == 1 and amis[0] != current_AMI) - if any_host_with_outdated_ami and latest_succeeded_base_image_update_event != None: - if last_cluster_refresh_status == None or last_cluster_refresh_status["startTime"] == None or last_cluster_refresh_status["startTime"] <= latest_succeeded_base_image_update_event["finish_time"]: + if any_host_with_outdated_ami and latest_succeeded_base_image_update_event is not None: + if last_cluster_refresh_status is None or last_cluster_refresh_status["startTime"] is None or last_cluster_refresh_status["startTime"] <= latest_succeeded_base_image_update_event["finish_time"]: return "The cluster was updated with a new AMI at {} PST and should be replaced to ensure the AMI is applied to all existing hosts.".format(utils.convertTimestamp(latest_succeeded_base_image_update_event["finish_time"])) return None - except: + except Exception: # in case of any exception, return None instead of showing the error on landing page return None @@ -626,7 +625,7 @@ def _get_last_cluster_refresh_status(request, env): return None return replace_summaries["clusterRollingUpdateStatuses"][0] - except: + except Exception: return None def _is_cluster_auto_refresh_enabled(request, env): @@ -635,7 +634,7 @@ def _is_cluster_auto_refresh_enabled(request, env): cluster_info = clusters_helper.get_cluster(request, cluster_name) return cluster_info["autoRefresh"] - except: + except Exception: return None @@ -689,7 +688,7 @@ def _get_commit_info(request, commit, repo=None, branch='master'): try: commit_info = builds_helper.get_commit(request, repo, commit) return repo, branch, commit_info['date'] - except: + except Exception: log.error(traceback.format_exc()) return None, None, None @@ -1024,7 +1023,7 @@ def post_create_env(request): if external_id: try: environs_helper.delete_nimbus_identifier(request, external_id) - except: + except Exception: message = 'Also failed to delete Nimbus identifier {}. Please verify that identifier no longer exists, Error Message: {}'.format(external_id, detail) log.error(message) raise detail @@ -1322,7 +1321,6 @@ def rollback(request, name, stage): html = render_to_string("environs/env_rollback.html", { "envs": envs, "stages": stages, - "envs": envs, "env": env, "deploy_summaries": deploy_summaries, "to_deploy_id": to_deploy_id, @@ -1368,7 +1366,6 @@ def promote(request, name, stage, deploy_id): html = render_to_string("environs/env_promote.html", { "envs": envs, "stages": stages, - "envs": envs, "env": env, "env_wrappers": env_wrappers, "deploy": deploy, @@ -1806,7 +1803,7 @@ def get_deploy_schedule(request, name, stage): env = environs_helper.get_env_by_stage(request, name, stage) envs = environs_helper.get_all_env_stages(request, name) schedule_id = env.get('scheduleId', None) - if schedule_id != None: + if schedule_id is not None: schedule = schedules_helper.get_schedule(request, name, stage, schedule_id) else: schedule = None diff --git a/deploy-board/deploy_board/webapp/error_views.py b/deploy-board/deploy_board/webapp/error_views.py index 2438d98e00..3f7b75e566 100644 --- a/deploy-board/deploy_board/webapp/error_views.py +++ b/deploy-board/deploy_board/webapp/error_views.py @@ -18,7 +18,7 @@ from django.shortcuts import render from deploy_board.settings import DEBUG from django.http import HttpResponse, HttpResponseRedirect -from .helpers.exceptions import NotAuthorizedException, FailedAuthenticationException +from .helpers.exceptions import IllegalArgumentException, NotAuthorizedException, NotFoundException, FailedAuthenticationException logger = logging.getLogger(__name__) diff --git a/deploy-board/deploy_board/webapp/group_view.py b/deploy-board/deploy_board/webapp/group_view.py index 8be483d7a0..ffe377891b 100644 --- a/deploy-board/deploy_board/webapp/group_view.py +++ b/deploy-board/deploy_board/webapp/group_view.py @@ -23,7 +23,6 @@ from django.contrib.messages import get_messages from collections import Counter import json -import requests import logging import traceback @@ -454,7 +453,7 @@ def get_policy(request, group_name): policies = autoscaling_groups_helper.get_policies(request, group_name) step_scaling_policy = None for policy in policies["scalingPolicies"]: - if policy["policyType"] == "StepScaling" and step_scaling_policy == None: + if policy["policyType"] == "StepScaling" and step_scaling_policy is None: step_scaling_policy = policy if policy["policyType"] in ["TargetTrackingScaling", "StepScaling"]: if policy["instanceWarmup"]: @@ -465,11 +464,11 @@ def get_policy(request, group_name): scale_up_steps = [] scale_down_steps = [] - if step_scaling_policy != None: + if step_scaling_policy is not None: for step in step_scaling_policy["stepAdjustments"]: - if step["metricIntervalUpperBound"] != None and float(step["metricIntervalUpperBound"]) <= 0: + if step["metricIntervalUpperBound"] is not None and float(step["metricIntervalUpperBound"]) <= 0: scale_down_steps.append({"upper_bound": float(step["metricIntervalUpperBound"]), "adjustment": step["scalingAdjustment"]}) - if step["metricIntervalLowerBound"] != None and float(step["metricIntervalLowerBound"]) >= 0: + if step["metricIntervalLowerBound"] is not None and float(step["metricIntervalLowerBound"]) >= 0: scale_up_steps.append({"lower_bound": float(step["metricIntervalLowerBound"]), "adjustment": step["scalingAdjustment"]}) scale_down_steps = sorted(scale_down_steps, key=lambda d: d['upper_bound']) @@ -652,20 +651,20 @@ def _parse_metrics_configs(request, group_name): policies = autoscaling_groups_helper.get_policies(request, group_name) # Use simple scaling for custom metric - if page_data["fromAwsMetric_{}".format(alarm_id)][0] == False: + if page_data["fromAwsMetric_{}".format(alarm_id)][0] is False: policy_type = "simple-scaling" if policy_type == "simple-scaling": if action_type == "GROW": - if policies["scaleupPolicies"] != None: + if policies["scaleupPolicies"] is not None: for i in policies["scaleupPolicies"]: alarm_info["scalingPolicies"].append(i) else: - if policies["scaledownPolicies"] != None: + if policies["scaledownPolicies"] is not None: for i in policies["scaledownPolicies"]: alarm_info["scalingPolicies"].append(i) else: - if policies["scalingPolicies"] != None: + if policies["scalingPolicies"] is not None: for p in policies["scalingPolicies"]: if p["policyType"] == "StepScaling": alarm_info["scalingPolicies"].append(p) @@ -690,7 +689,7 @@ def get_alarms(request, group_name): for alarm in alarms: alarm["scalingType"] = None - if alarm["scalingPolicies"] != None: + if alarm["scalingPolicies"] is not None: if len(alarm["scalingPolicies"]) > 0: # we restrict alarm has only one scaling policy policy = alarm["scalingPolicies"][0] @@ -767,15 +766,15 @@ def add_alarms(request, group_name): if policy_type == "simple-scaling": if action_type == "grow": - if policies["scaleupPolicies"] != None: + if policies["scaleupPolicies"] is not None: for i in policies["scaleupPolicies"]: alarm_info["scalingPolicies"].append(i) else: - if policies["scaledownPolicies"] != None: + if policies["scaledownPolicies"] is not None: for i in policies["scaledownPolicies"]: alarm_info["scalingPolicies"].append(i) else: - if policies["scalingPolicies"] != None: + if policies["scalingPolicies"] is not None: for p in policies["scalingPolicies"]: if p["policyType"] == "StepScaling": alarm_info["scalingPolicies"].append(p) @@ -893,7 +892,7 @@ def get_group_info(request, group_name): "csrf_token": get_token(request), }) return HttpResponse(json.dumps({"html": content}), content_type="application/json") - except: + except Exception: log.error(traceback.format_exc()) @@ -966,7 +965,7 @@ def get_group_size(request, group_name): }) return HttpResponse(json.dumps({"html": content}), content_type="application/json") - except: + except Exception: log.error(traceback.format_exc()) @@ -986,7 +985,7 @@ def get_scaling_activities(request, group_name): "activities": scaling_activities["activities"], }) return HttpResponse(json.dumps({"html": content}), content_type="application/json") - except: + except Exception: log.error(traceback.format_exc()) @@ -1028,7 +1027,7 @@ def get_more_scaling_activities(request, group_name): "disableNext": disableNext, }) return HttpResponse(json.dumps({"html": content}), content_type="application/json") - except: + except Exception: log.error(traceback.format_exc()) @@ -1380,7 +1379,7 @@ def terminate_all_hosts(request, group_name): try: response = autoscaling_groups_helper.terminate_all_hosts(request, group_name) - if response is not None and type(response) is dict: + if response is not None and isinstance(response, dict): success_count = 0 failed_count = 0 @@ -1433,7 +1432,7 @@ def attach_instances(request, group_name): host_ids = hosts.split(',') autoscaling_groups_helper.hosts_action_in_group(request, group_name, host_ids, "ATTACH") return redirect('/groups/{}/'.format(group_name)) - except: + except Exception: log.error(traceback.format_exc()) return redirect('/groups/{}/'.format(group_name)) @@ -1469,12 +1468,12 @@ def get_health_check_activities(request, group_name): def get_charts(request, group_name, type): - if type == "az": + if type == "az": return render(request, 'groups/distribution_charts.tmpl', get_host_az_dist(request, group_name)) - + elif type == "ami": return render(request, 'groups/distribution_charts.tmpl', get_host_ami_dist(request, group_name)) - + elif type == "host-type": return render(request, 'groups/distribution_charts.tmpl', get_host_type_dist(request, group_name)) @@ -1508,7 +1507,7 @@ def get_host_type_dist(request, group_name): query="tags.Autoscaling:{} AND state:running".format(group_name), fields="cloud.aws", account_id=aws_owner_id - ); + ) counter = Counter([x['cloud.aws']['instanceType'] for x in host_type_dist.json()]) labels = list(counter.keys()) @@ -1561,7 +1560,7 @@ def get_host_ami_dist(request, group_name): 'current_AMI': current_AMI, 'any_host_with_outdated_ami': any_host_with_outdated_ami } - + def get_health_check_details(request, id): health_check = autoscaling_groups_helper.get_health_check(request, id) @@ -1627,7 +1626,7 @@ def add_scheduled_actions(request, group_name): schedule_action['capacity'] = params['capacity'] autoscaling_groups_helper.add_scheduled_actions(request, group_name, [schedule_action]) - except: + except Exception: log.error(traceback.format_exc()) return redirect("/groups/{}/config/".format(group_name)) @@ -1704,7 +1703,7 @@ def update_scheduled_actions(request, group_name): configs = _parse_actions_configs(request.POST, group_name) autoscaling_groups_helper.add_scheduled_actions(request, group_name, configs) return get_scheduled_actions(request, group_name) - except: + except Exception: log.error(traceback.format_exc()) return HttpResponse(json.dumps({'content': ""}), content_type="application/json") diff --git a/deploy-board/deploy_board/webapp/helpers/autoscaling_metrics_helper.py b/deploy-board/deploy_board/webapp/helpers/autoscaling_metrics_helper.py index a31d6ec219..654e8a16ad 100644 --- a/deploy-board/deploy_board/webapp/helpers/autoscaling_metrics_helper.py +++ b/deploy-board/deploy_board/webapp/helpers/autoscaling_metrics_helper.py @@ -25,7 +25,7 @@ def get_asg_size_metric(request, cluster_name, start): try: return rodimus_client.get("/metrics/clusters/%s/size" % cluster_name, request.teletraan_user_id.token, params=params) - except: + except Exception: return [] @@ -33,7 +33,7 @@ def get_metric_data(request, cluster_name, metric_name, start): params = {"metricName": metric_name, "start": start} try: return rodimus_client.get("/metrics/clusters/%s" % cluster_name, request.teletraan_user_id.token, params=params) - except: + except Exception: return [] @@ -41,7 +41,7 @@ def get_latency_data(request, env_id, type, start): params = {"envId": env_id, "actionType": type, "start": start} try: return rodimus_client.get("/metrics/latency", request.teletraan_user_id.token, params=params) - except: + except Exception: return [] @@ -49,7 +49,7 @@ def get_raw_metrics(request, metric_name, start): params = {"metricName": metric_name, "start": start} try: return rodimus_client.get("/metrics/raw_metrics", request.teletraan_user_id.token, params=params) - except: + except Exception: return [] @@ -57,5 +57,5 @@ def get_pas_metrics(request, cluster_name, start, type): params = {"clusterName": cluster_name, "start": start, "actionType": type} try: return rodimus_client.get("/metrics/pas", request.teletraan_user_id.token, params=params) - except: + except Exception: return [] diff --git a/deploy-board/deploy_board/webapp/helpers/base_client.py b/deploy-board/deploy_board/webapp/helpers/base_client.py index 1b670832af..4739b1d33a 100644 --- a/deploy-board/deploy_board/webapp/helpers/base_client.py +++ b/deploy-board/deploy_board/webapp/helpers/base_client.py @@ -50,7 +50,7 @@ def api(path, token=None, params=None, data=None): if "access_token=" in response.text: bad_text = response.text.split("access_token=")[1].split('"')[0].replace("\\", "") response.text = response.text.replace(bad_text, "ACCESS_TOKEN") - except: + except Exception: pass if response.status_code == 401: diff --git a/deploy-board/deploy_board/webapp/helpers/baseimages_helper.py b/deploy-board/deploy_board/webapp/helpers/baseimages_helper.py index 921b76fec4..6c18d87421 100644 --- a/deploy-board/deploy_board/webapp/helpers/baseimages_helper.py +++ b/deploy-board/deploy_board/webapp/helpers/baseimages_helper.py @@ -133,7 +133,7 @@ def get_latest_image_update_events(events): return events # Group update events batch by create_time. - # Events are sorted by create_time + # Events are sorted by create_time # create_time is milisecond timestamp and gets increased by 1 per cluster. # The total number of clusters should not be 10K. lastest_timestamp = events[0]['create_time'] @@ -151,7 +151,7 @@ def get_image_update_events_by_cluster(request, cluster_name): def get_latest_succeeded_image_update_event_by_cluster(request, cluster_name): events = rodimus_client.get("/base_images/updates/cluster/%s" % cluster_name, request.teletraan_user_id.token) - events = filter(lambda x: x["state"] == "COMPLETED" and x["finish_time"] != None, events) + events = filter(lambda x: x["state"] == "COMPLETED" and x["finish_time"] is not None, events) return max(events, key=lambda x: x["finish_time"], default=None) def generate_image_update_event_status(event): diff --git a/deploy-board/deploy_board/webapp/helpers/cmdbapiclient.py b/deploy-board/deploy_board/webapp/helpers/cmdbapiclient.py index c02b3b4017..cd04a6e2f7 100644 --- a/deploy-board/deploy_board/webapp/helpers/cmdbapiclient.py +++ b/deploy-board/deploy_board/webapp/helpers/cmdbapiclient.py @@ -1,7 +1,6 @@ import logging import requests from .decorators import singleton -from . import clusters_helper from deploy_board.settings import CMDB_API_HOST, CMDB_INSTANCE_URL requests.packages.urllib3.disable_warnings() @@ -17,7 +16,7 @@ def query(self, query, fields, account_id): }, headers=headers ) - + def get_host_details(self, host_id, account_id): headers = {} if not account_id else {'x-aws-account-id': account_id} return requests.get(CMDB_API_HOST+CMDB_INSTANCE_URL+ host_id, headers=headers) diff --git a/deploy-board/deploy_board/webapp/helpers/environs_helper.py b/deploy-board/deploy_board/webapp/helpers/environs_helper.py index fc097f50f8..6bac48ad34 100644 --- a/deploy-board/deploy_board/webapp/helpers/environs_helper.py +++ b/deploy-board/deploy_board/webapp/helpers/environs_helper.py @@ -85,7 +85,7 @@ def create_identifier_for_new_stage(request, env_name, stage_name): stage_with_external_id = env_stage break - if stage_with_external_id == None: + if stage_with_external_id is None: return None # retrieve Nimbus identifier for existing_stage @@ -319,4 +319,4 @@ def reset_hosts(request, env_name, stage_name, host_ids): def get_env_pindeploy(request, env_name, stage_name): params = [("envName", env_name), ("stageName", stage_name)] - return deployclient.get("/pindeploy", request.teletraan_user_id.token, params=params) \ No newline at end of file + return deployclient.get("/pindeploy", request.teletraan_user_id.token, params=params) diff --git a/deploy-board/deploy_board/webapp/helpers/hosts_helper.py b/deploy-board/deploy_board/webapp/helpers/hosts_helper.py index e7b885d7f6..72ef3a631e 100644 --- a/deploy-board/deploy_board/webapp/helpers/hosts_helper.py +++ b/deploy-board/deploy_board/webapp/helpers/hosts_helper.py @@ -3,9 +3,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,7 +17,6 @@ """ from deploy_board.webapp.helpers.deployclient import DeployClient from deploy_board.webapp.helpers.cmdbapiclient import CmdbApiClient -from deploy_board.webapp.helpers.rodimus_client import RodimusClient deployclient = DeployClient() cmdbapi_client = CmdbApiClient() diff --git a/deploy-board/deploy_board/webapp/helpers/nimbusclient.py b/deploy-board/deploy_board/webapp/helpers/nimbusclient.py index 04f3e891d9..6c81800f37 100644 --- a/deploy-board/deploy_board/webapp/helpers/nimbusclient.py +++ b/deploy-board/deploy_board/webapp/helpers/nimbusclient.py @@ -2,8 +2,8 @@ import logging from .decorators import singleton from deploy_board.settings import IS_PINTEREST, NIMBUS_SERVICE_URL, NIMBUS_SERVICE_VERSION, NIMBUS_USE_EGRESS, NIMBUS_EGRESS_URL, TELETRAAN_PROJECT_URL_FORMAT -from .exceptions import NotAuthorizedException, TeletraanException, FailedAuthenticationException -from urllib.parse import urlparse +from .exceptions import TeletraanException +from urllib.parse import urlparse import requests requests.packages.urllib3.disable_warnings() diff --git a/deploy-board/deploy_board/webapp/host_views.py b/deploy-board/deploy_board/webapp/host_views.py index dc176a2d52..697901fa93 100644 --- a/deploy-board/deploy_board/webapp/host_views.py +++ b/deploy-board/deploy_board/webapp/host_views.py @@ -17,7 +17,7 @@ from django.views.generic import View import logging from .common import is_agent_failed -from .helpers import environs_helper, agents_helper, autoscaling_groups_helper, environ_hosts_helper, hosts_helper, accounts_helper +from .helpers import environs_helper, agents_helper, autoscaling_groups_helper, environ_hosts_helper, hosts_helper from deploy_board.settings import IS_PINTEREST, CMDB_UI_HOST, PHOBOS_URL, AWS_PRIMARY_ACCOUNT from datetime import datetime import pytz @@ -49,7 +49,7 @@ def get_agent_wrapper(request, hostname): agent_wrappers['sidecars'].append(agent_wrapper) else: agent_wrappers['services'].append(agent_wrapper) - + agent_wrappers['sidecars'] = sorted(agent_wrappers['sidecars'], key=lambda x: x["agent"]['lastUpdateDate']) agent_wrappers['services'] = sorted(agent_wrappers['services'], key=lambda x: x["agent"]['lastUpdateDate']) return agent_wrappers, is_unreachable @@ -93,7 +93,7 @@ def get_account_id(hosts): def _get_cloud(json_obj): try: return json_obj.get('cloud', None).get('aws', None) - except: + except Exception: return None @@ -104,7 +104,7 @@ def get_host_details(host_id, account_id): try: instance = response.json() - except: + except Exception: # the host not found in CMDB return None @@ -195,7 +195,7 @@ def get(self, request, name, stage, hostname): wrapper['env']['stageName'] == stage) or is_agent_failed(wrapper['agent']) ) - + host_details = get_host_details(host_id, account_id) termination_limit = environs_helper.get_env_by_stage(request, name, stage).get('terminationLimit') diff --git a/deploy-board/deploy_board/webapp/promote_views.py b/deploy-board/deploy_board/webapp/promote_views.py index f3df822a24..2a58727e7a 100644 --- a/deploy-board/deploy_board/webapp/promote_views.py +++ b/deploy-board/deploy_board/webapp/promote_views.py @@ -23,7 +23,7 @@ from django.views.generic import View from . import common from .helpers import environs_helper -from .helpers.exceptions import TeletraanException, IllegalArgumentException +from .helpers.exceptions import TeletraanException class EnvPromoteConfigView(View): diff --git a/deploy-board/deploy_board/webapp/schedule_views.py b/deploy-board/deploy_board/webapp/schedule_views.py index 9e312ce29b..4c0edbad31 100644 --- a/deploy-board/deploy_board/webapp/schedule_views.py +++ b/deploy-board/deploy_board/webapp/schedule_views.py @@ -38,16 +38,15 @@ def get(self, request, name, stage): envs = environs_helper.get_all_env_stages(request, name) stages, env = common.get_all_stages(envs, stage) agent_count = agents_helper.get_agents_total_by_env(request, env["id"]) - schedule_id = env.get('scheduleId', None); - if schedule_id != None: + schedule_id = env.get('scheduleId', None) + if schedule_id is not None: schedule = schedules_helper.get_schedule(request, name, stage, schedule_id) else: schedule = None - max_parallel_number = env["maxParallel"]; + max_parallel_number = env["maxParallel"] return render(request, 'configs/schedule_config.html', { "env": env, "schedule": schedule, "agent_count": agent_count, "max_parallel_number": max_parallel_number, }) - diff --git a/deploy-board/deploy_board/webapp/security.py b/deploy-board/deploy_board/webapp/security.py index 7de5ccbdde..30a5eaf18d 100644 --- a/deploy-board/deploy_board/webapp/security.py +++ b/deploy-board/deploy_board/webapp/security.py @@ -134,7 +134,7 @@ def login_authorized(request): "message": str(e), }) - except OAuthExpiredTokenException as e: + except OAuthExpiredTokenException: # When auth.pinadmin.com returns a 401 error logger.error(traceback.format_exc()) diff --git a/deploy-board/deploy_board/webapp/service_add_ons.py b/deploy-board/deploy_board/webapp/service_add_ons.py index 66370e7291..bffe23dd27 100644 --- a/deploy-board/deploy_board/webapp/service_add_ons.py +++ b/deploy-board/deploy_board/webapp/service_add_ons.py @@ -13,7 +13,7 @@ # limitations under the License. -from deploy_board.settings import IS_PINTEREST, SERVICE_RATELIMIT_CONFIG_URL, \ +from deploy_board.settings import SERVICE_RATELIMIT_CONFIG_URL, \ STATSBOARD_API_FORMAT, RATELIMIT_ENABLED_METRIC_FORMAT, \ ENABLING_SERVICE_RATELIMIT_URL, KAFKA_MSGS_DELIVERED_METRIC, \ STATSBOARD_HUB_URL_ENDPOINT_FORMAT, STATSBOARD_HOST_TYPE_API_FORMAT @@ -300,7 +300,7 @@ def getRatelimitingReport(serviceName, agentStats): totalHostsOn += 1 else: totalHostsOff += 1 - except: + except Exception: # In any error we abstain from making a claim, including request timeouts. return RateLimitingReport(state=ServiceAddOn.UNKNOWN) @@ -367,7 +367,7 @@ def getLatestLogUnixTime(topics, lognames, hostsOnStage, commonHostPrefix): if dataPoints[k][1] > 0: earliestMessages.append(dataPoints[k]) break - except: + except Exception: return None if not statsboardDataConsistent(statsboardData, hostsOnStage): @@ -448,7 +448,7 @@ def getDashboardReport(env, stage, metricsDashboardUrl, isSidecar): hostType = getStatsboardHostType(env, stage) if hostType is None: state = ServiceAddOn.UNKNOWN - except: + except Exception: state = ServiceAddOn.UNKNOWN return DashboardStateReport(state=state, hostType=hostType) diff --git a/deploy-board/deploy_board/webapp/templatetags/utils.py b/deploy-board/deploy_board/webapp/templatetags/utils.py index ec4afcbb18..b81a3235c4 100644 --- a/deploy-board/deploy_board/webapp/templatetags/utils.py +++ b/deploy-board/deploy_board/webapp/templatetags/utils.py @@ -1035,7 +1035,7 @@ def get_available_tag(tag): @register.filter("tagBuildId") -def get_available_tag(tag): +def get_tag_build_id(tag): if tag is not None: meta_info = tag.get("metaInfo", None) if meta_info is not None: @@ -1070,7 +1070,7 @@ def convertConfigHistoryString(change): try: converted_string = ast.literal_eval(change) return converted_string - except: + except Exception: pass return change diff --git a/deploy-board/deploy_board/webapp/urls.py b/deploy-board/deploy_board/webapp/urls.py index 602c582da4..dbd819e535 100644 --- a/deploy-board/deploy_board/webapp/urls.py +++ b/deploy-board/deploy_board/webapp/urls.py @@ -33,7 +33,6 @@ from . import cluster_view from . import schedule_views from . import host_tags_views -from . import accounts_views urlpatterns = [ # deploy related @@ -201,7 +200,7 @@ host_views.HostDetailView.as_view()), url(r'^env/(?P[a-zA-Z0-9\-_]+)/(?P[a-zA-Z0-9\-_]+)/terminate_hosts/$', cluster_view.terminate_hosts), url(r'^env/(?P[a-zA-Z0-9\-_]+)/(?P[a-zA-Z0-9\-_]+)/force_terminate_hosts/$', cluster_view.force_terminate_hosts), - url(r'^env/(?P[a-zA-Z0-9\-_]+)/(?P[a-zA-Z0-9\-_]+)/reset_environments/(?P[a-zA-Z0-9\-_]+)$', env_views.reset_all_environments), + url(r'^env/(?P[a-zA-Z0-9\-_]+)/(?P[a-zA-Z0-9\-_]+)/reset_environments/(?P[a-zA-Z0-9\-_]+)$', env_views.reset_all_environments), url(r'^hosts/$', host_views.hosts_list), # builds related diff --git a/deploy-board/deploy_board/webapp/util_views.py b/deploy-board/deploy_board/webapp/util_views.py index aa588d1705..f280fc76cc 100644 --- a/deploy-board/deploy_board/webapp/util_views.py +++ b/deploy-board/deploy_board/webapp/util_views.py @@ -21,7 +21,9 @@ from django.template.loader import render_to_string from django.http import HttpResponse import json -import urllib.request, urllib.error, urllib.parse +import urllib.request +import urllib.error +import urllib.parse from deploy_board import settings from .helpers import environs_helper from .helpers import autoscaling_metrics_helper, autoscaling_groups_helper @@ -49,8 +51,8 @@ def _get_latest_metrics(url): # Return the first datapoint in the datapoints list if data: try: - return [datapoint for datapoint in data['data'][0]['datapoints'] if datapoint[1] != None] - except: + return [datapoint for datapoint in data['data'][0]['datapoints'] if datapoint[1] is not None] + except Exception: log.warning(f"No metrics data {data}") pass @@ -58,7 +60,7 @@ def _get_latest_metrics(url): # Check for TSDB response if len(data) > 0 and 'dps' in data[0] and len(data[0]['dps']) != 0: return _convert_opentsdb_data(data[0]['dps']) - except: + except Exception: log.warning(f"No TSDB metrics data {data}") pass @@ -169,7 +171,7 @@ def get_latency_metrics(request, group_name): util_data["stage_names"] = stage_names util_data["launch_latency_th"] = launch_config.get("groupInfo")["launchLatencyTh"] - except: + except Exception: log.error(traceback.format_exc()) return HttpResponse(json.dumps(util_data), content_type="application/json") @@ -194,7 +196,7 @@ def get_launch_rate(request, group_name): util_data[metric_name] = json_data util_data["metric_names"].append(metric_name) - except: + except Exception: log.error(traceback.format_exc()) return HttpResponse(json.dumps(util_data), content_type="application/json") @@ -214,9 +216,6 @@ def get_pas_metrics(request, group_name): timestamp, value = data_point["timestamp"], data_point["value"] json_data3.append([timestamp, value]) util_data['arcee'] = json_data3 - except: + except Exception: log.error(traceback.format_exc()) return HttpResponse(json.dumps(util_data), content_type="application/json") - - - diff --git a/deploy-board/deploy_board/wsgi.py b/deploy-board/deploy_board/wsgi.py index 157d4a0b6f..d49062c791 100644 --- a/deploy-board/deploy_board/wsgi.py +++ b/deploy-board/deploy_board/wsgi.py @@ -3,9 +3,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -22,7 +22,9 @@ """ import os -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "deploy_board.settings") from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "deploy_board.settings") + application = get_wsgi_application() diff --git a/deploy-board/integ_test/env_config_tests.py b/deploy-board/integ_test/env_config_tests.py index b984f204a4..1cf94cd357 100644 --- a/deploy-board/integ_test/env_config_tests.py +++ b/deploy-board/integ_test/env_config_tests.py @@ -3,9 +3,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,7 +15,6 @@ # -*- coding: utf-8 -*- import unittest import commons -import json builds_helper = commons.get_build_helper() environs_helper = commons.get_environ_helper()