diff --git a/files/sojobo_api/api/w_datastore.py b/files/sojobo_api/api/w_datastore.py index 7817dee..4497605 100644 --- a/files/sojobo_api/api/w_datastore.py +++ b/files/sojobo_api/api/w_datastore.py @@ -899,3 +899,12 @@ def get_all_bundle_types(): def clear_bundle_types(): aql = "FOR b IN bundleTypes REMOVE b IN bundleTypes" execute_aql_query(aql, rawResults=True) + +############################################################################### +# MONITORING # +############################################################################### + + +def set_monitoring_state(m_key, state): + aql = "UPDATE {_key: @m_key, monitoring_state: @state} IN models" + execute_aql_query(aql, m_key=m_key, state=state) diff --git a/files/sojobo_api/api/w_juju.py b/files/sojobo_api/api/w_juju.py index 81eef2e..f177f5e 100644 --- a/files/sojobo_api/api/w_juju.py +++ b/files/sojobo_api/api/w_juju.py @@ -477,8 +477,6 @@ def create_model(authorization, m_name, cred_name, c_name, workspace_type=None): # Create the model in ArangoDB. Add model key to controller and # set the model access level of the user. new_model = datastore.create_model(m_key, m_name, state='deploying') - # TODO: Maybe put these 3 datastore methods in one so you do not have - # to create a connection with ArangoDB each time. datastore.add_model_to_controller(c_name, m_key) datastore.set_model_state(m_key, 'accepted') datastore.set_model_access(m_key, authorization.username, 'admin') @@ -486,7 +484,8 @@ def create_model(authorization, m_name, cred_name, c_name, workspace_type=None): datastore.add_edge_between_model_and_workspace_type(new_model["_key"], workspace_type) # Run the background script, this creates the model in JuJu. Popen(["python3", "{}/scripts/add_model.py".format(settings.SOJOBO_API_DIR), - c_name, m_key, m_name, authorization.username, authorization.password, cred_name, str(workspace_type)]) + c_name, m_key, m_name, authorization.username, + authorization.password, cred_name, str(workspace_type)]) return 202, "Model is being deployed." else: return errors.already_exists('model') @@ -1090,3 +1089,32 @@ def log_event(event_type, tags): current_time = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') from sojobo_api.api import w_events as events events.log_event(event_type, current_time, tags) + +############################################################################### +# MONITORING # +############################################################################### + + +def add_monitoring_to_app(c_name, endpoint, cacert, m_name, uuid, juju_username, password, application): + if os.path.isfile("{}/monitoring_settings.py".format(settings.SOJOBO_API_DIR)): + from sojobo_api.api import w_monitoring_logic + w_monitoring_logic.add_monitoring_to_app(c_name, endpoint, cacert, + m_name, uuid, juju_username, + password, application) + + +def monitoring_enabled(model_info): + if os.path.isfile("{}/monitoring_settings.py".format(settings.SOJOBO_API_DIR)): + from sojobo_api.api import w_monitoring_logic + return w_monitoring_logic.monitoring_enabled(model_info) + else: + return False + + +def update_monitoring_relations(c_name, endpoint, cacert, m_name, uuid, + juju_username, password, applications_info): + if os.path.isfile("{}/monitoring_settings.py".format(settings.SOJOBO_API_DIR)): + from sojobo_api.api import w_monitoring_logic + w_monitoring_logic.update_monitoring_relations(c_name, endpoint, cacert, + m_name, uuid, juju_username, + password, applications_info) diff --git a/files/sojobo_api/scripts/add_application.py b/files/sojobo_api/scripts/add_application.py index 4ab8a18..c273886 100644 --- a/files/sojobo_api/scripts/add_application.py +++ b/files/sojobo_api/scripts/add_application.py @@ -94,6 +94,23 @@ async def add_application(c_name, m_key, username, password, units, machine, con ) await app_facade.Deploy([app]) + + # If monitoring is enabled for the workspace then we need to add a + # relation between the application and the tengu monitoring telegraf. + if juju.monitoring_enabled(auth_data["model"]): + m_name = auth_data["model"]["name"] + logger.info('Updating monitoring relations for %s:%s', c_name, m_name) + applications_info = juju.get_applications_info(model_connection) + endpoint = auth_data["controller"]["endpoints"][0] + cacert = auth_data["controller"]["ca_cert"] + uuid = auth_data["model"]["uuid"] + juju_username = auth_data["user"]["juju_username"] + juju.update_monitoring_relations(c_name, endpoint, cacert, m_name, + uuid, juju_username, password, + applications_info) + juju.add_monitoring_to_app(c_name, endpoint, cacert, m_name, + uuid, juju_username, password, application) + await model_connection.disconnect() logger.info('Application %s succesfully added!', application) except Exception as e: diff --git a/files/sojobo_api/scripts/bundle_deployment.py b/files/sojobo_api/scripts/bundle_deployment.py index 73f624d..d524938 100644 --- a/files/sojobo_api/scripts/bundle_deployment.py +++ b/files/sojobo_api/scripts/bundle_deployment.py @@ -72,6 +72,20 @@ async def deploy_bundle(username, password, c_name, m_name, bundle): for app_name in pending_apps ], loop=model.loop) logger.info('Bundle successfully deployed for %s:%s', c_name, m_name) + + + if juju.monitoring_enabled(auth_data["model"]): + logger.info('Updating monitoring relations for %s:%s', c_name, m_name) + applications_info = juju.get_applications_info(model) + endpoint = auth_data["controller"]["endpoints"][0] + cacert = auth_data["controller"]["ca_cert"] + uuid = auth_data["model"]["uuid"] + juju_username = auth_data["user"]["juju_username"] + juju.update_monitoring_relations(c_name, endpoint, cacert, m_name, + uuid, juju_username, password, + applications_info) + + await model.disconnect() logger.info('Successfully disconnected %s', m_name) shutil.rmtree(dirpath)