diff options
Diffstat (limited to 'api/resources/v1/env.py')
-rw-r--r-- | api/resources/v1/env.py | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/api/resources/v1/env.py b/api/resources/v1/env.py index 7c831fd74..6c9eb8324 100644 --- a/api/resources/v1/env.py +++ b/api/resources/v1/env.py @@ -10,18 +10,24 @@ from __future__ import absolute_import import errno import logging + +import ipaddress import os import subprocess import threading import time import uuid import glob + +import six import yaml import collections from six.moves import configparser from oslo_serialization import jsonutils from docker import Client +from docker.errors import APIError +from requests.exceptions import HTTPError from api.database.v1.handlers import AsyncTaskHandler from api.utils import influx @@ -44,7 +50,7 @@ class V1Env(ApiResource): def post(self): return self._dispatch_post() - def create_grafana(self, args): + def create_grafana(self, *args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=self._create_grafana, args=(task_id,)) @@ -82,7 +88,7 @@ class V1Env(ApiResource): self._update_task_status(task_id) LOG.info('Finished') - except Exception as e: + except (APIError, HTTPError) as e: self._update_task_error(task_id, str(e)) LOG.exception('Create grafana failed') @@ -117,7 +123,7 @@ class V1Env(ApiResource): "isDefault": True, } try: - HttpClient().post(url, data, timeout=10) + HttpClient().post(url, data, timeout=60) except Exception: LOG.exception('Create datasources failed') raise @@ -145,7 +151,7 @@ class V1Env(ApiResource): return any(t in a['RepoTags'][0] for a in client.images() if a['RepoTags']) - def create_influxdb(self, args): + def create_influxdb(self, *args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=self._create_influxdb, args=(task_id,)) @@ -185,7 +191,7 @@ class V1Env(ApiResource): self._update_task_status(task_id) LOG.info('Finished') - except Exception as e: + except APIError as e: self._update_task_error(task_id, str(e)) LOG.exception('Creating influxdb failed') @@ -217,7 +223,7 @@ class V1Env(ApiResource): consts.INFLUXDB_DB_NAME) client.create_database(consts.INFLUXDB_DB_NAME) LOG.info('Success to config influxDB') - except Exception: + except HTTPError: LOG.exception('Config influxdb failed') def _change_output_to_influxdb(self, ip): @@ -236,7 +242,7 @@ class V1Env(ApiResource): with open(consts.CONF_FILE, 'w') as f: parser.write(f) - def prepare_env(self, args): + def prepare_env(self, *args): task_id = str(uuid.uuid4()) thread = threading.Thread(target=self._prepare_env_daemon, @@ -267,6 +273,8 @@ class V1Env(ApiResource): LOG.info('Openrc file not found') installer_ip = os.environ.get('INSTALLER_IP', '192.168.200.2') + # validate installer_ip is a valid ipaddress + installer_ip = str(ipaddress.IPv4Address(six.u(installer_ip))) installer_type = os.environ.get('INSTALLER_TYPE', 'compass') LOG.info('Getting openrc file from %s', installer_type) self._get_remote_rc_file(rc_file, @@ -287,7 +295,7 @@ class V1Env(ApiResource): self._update_task_status(task_id) LOG.info('Finished') - except Exception as e: + except (subprocess.CalledProcessError, OSError) as e: self._update_task_error(task_id, str(e)) LOG.exception('Prepare env failed') @@ -373,7 +381,7 @@ class V1Env(ApiResource): LOG.info('Source openrc: Sourcing') try: self._source_file(consts.OPENRC) - except Exception as e: + except subprocess.CalledProcessError as e: LOG.exception('Failed to source openrc') return result_handler(consts.API_ERROR, str(e)) LOG.info('Source openrc: Done') |