From 3a185f3fce49eba2b92d36e51fadd2e41c92009f Mon Sep 17 00:00:00 2001 From: Michiel Date: Mon, 16 Apr 2018 14:03:50 +0200 Subject: [PATCH 1/8] Bugfix: add application. --- files/sojobo_api/scripts/add_application.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/files/sojobo_api/scripts/add_application.py b/files/sojobo_api/scripts/add_application.py index ba9d8de..6eb8168 100644 --- a/files/sojobo_api/scripts/add_application.py +++ b/files/sojobo_api/scripts/add_application.py @@ -70,6 +70,14 @@ async def add_application(c_name, m_key, username, password, units, machine, con conf = {} config = yaml.dump({application: conf}, default_flow_style=False) + if machine: + placement = [client.Placement( + scope="#", + directive=machine + )] + else: + placement = None + app = client.ApplicationDeploy( charm_url=entity_id, application=application, @@ -81,7 +89,7 @@ async def add_application(c_name, m_key, username, password, units, machine, con num_units=int(units), resources=None, storage=None, - placement=machine + placement=placement ) await app_facade.Deploy([app]) From c2a3aa6c56cf98e47220010cf014a738639e59ab Mon Sep 17 00:00:00 2001 From: Michiel Date: Mon, 16 Apr 2018 15:42:15 +0200 Subject: [PATCH 2/8] Add monitoring_enabled to model_info --- files/sojobo_api/api/w_juju.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/files/sojobo_api/api/w_juju.py b/files/sojobo_api/api/w_juju.py index 9e46a37..8222fb1 100644 --- a/files/sojobo_api/api/w_juju.py +++ b/files/sojobo_api/api/w_juju.py @@ -385,10 +385,16 @@ async def get_model_info(connection, data): applications = get_applications_info(connection) machines = get_machines_info(connection) gui = get_gui_url(data) - credentials = {'cloud': data['controller']['type'], 'credential-name': data['model']['credential']} - return {'name': data['model']['name'], 'users': users, - 'applications': applications, 'machines': machines, 'juju-gui-url' : gui, - 'state': state, 'credentials' : credentials} + credentials = {'cloud': data['controller']['type'], + 'credential-name': data['model']['credential']} + return {'name': data['model']['name'], + 'users': users, + 'applications': applications, + 'machines': machines, + 'juju-gui-url': gui, + 'state': state, + 'credentials': credentials, + 'monitoring_enabled': data['model']['monitoring_enabled']} def get_ssh_keys_user(username): From 253efe50927dd3a563e43d7155dc9294ed3adc36 Mon Sep 17 00:00:00 2001 From: Michiel Date: Tue, 17 Apr 2018 08:33:38 +0200 Subject: [PATCH 3/8] Bugfix: workspace type functionality was removed during merge. --- files/sojobo_api/api/w_juju.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/files/sojobo_api/api/w_juju.py b/files/sojobo_api/api/w_juju.py index 5424556..4268f35 100644 --- a/files/sojobo_api/api/w_juju.py +++ b/files/sojobo_api/api/w_juju.py @@ -469,14 +469,15 @@ 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') + if workspace_type: + 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]) + 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') From 0b78ab39a030ddc61b8f9eae06802924bead0a7d Mon Sep 17 00:00:00 2001 From: Michiel Date: Tue, 17 Apr 2018 09:58:10 +0200 Subject: [PATCH 4/8] Addition of monitoring_enabled field. --- files/sojobo_api/api/w_datastore.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/files/sojobo_api/api/w_datastore.py b/files/sojobo_api/api/w_datastore.py index 6b26e19..658a136 100644 --- a/files/sojobo_api/api/w_datastore.py +++ b/files/sojobo_api/api/w_datastore.py @@ -872,3 +872,13 @@ def get_model_id(m_key): def get_workspace_type_id(ws_type): return "workspace_types/" + ws_type + + +############################################################################### +# MONITORING # +############################################################################### + +def enable_monitoring(m_key): + """Insert or updates the field of a model that indicates if monitoring is enabled.""" + aql = "UPDATE {_key: @m_key, monitoring_enabled: @enabled} IN models" + execute_aql_query(aql, m_key=m_key, enabled=True) From ffb3042e2e968fa55f6a873475df97d4604fb0d8 Mon Sep 17 00:00:00 2001 From: Michiel Date: Tue, 17 Apr 2018 10:50:32 +0200 Subject: [PATCH 5/8] Add relation between monitoring telegraf and new application if monitoring is enabled in workspace. --- files/sojobo_api/api/w_juju.py | 12 ++++++++++++ files/sojobo_api/scripts/add_application.py | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/files/sojobo_api/api/w_juju.py b/files/sojobo_api/api/w_juju.py index 4268f35..c09c681 100644 --- a/files/sojobo_api/api/w_juju.py +++ b/files/sojobo_api/api/w_juju.py @@ -1080,3 +1080,15 @@ 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(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) diff --git a/files/sojobo_api/scripts/add_application.py b/files/sojobo_api/scripts/add_application.py index e09c5a8..2ca39b0 100644 --- a/files/sojobo_api/scripts/add_application.py +++ b/files/sojobo_api/scripts/add_application.py @@ -92,6 +92,18 @@ 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 "monitoring_enabled" in auth_data["model"] and auth_data["model"]["monitoring_enabled"]: + endpoint = auth_data["controller"]["endpoints"][0] + cacert = auth_data["controller"]["ca_cert"] + m_name = auth_data["model"]["name"] + uuid = auth_data["model"]["uuid"] + juju_username = auth_data["user"]["juju_username"] + juju.add_monitoring(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: From 5248bd7637ef5c50de4ab0d385b664ae2fe20faa Mon Sep 17 00:00:00 2001 From: Michiel Date: Tue, 17 Apr 2018 16:43:02 +0200 Subject: [PATCH 6/8] Update datastore function that enables monitoring. --- files/sojobo_api/api/w_datastore.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/files/sojobo_api/api/w_datastore.py b/files/sojobo_api/api/w_datastore.py index 658a136..3d8c99a 100644 --- a/files/sojobo_api/api/w_datastore.py +++ b/files/sojobo_api/api/w_datastore.py @@ -878,7 +878,7 @@ def get_workspace_type_id(ws_type): # MONITORING # ############################################################################### -def enable_monitoring(m_key): +def toggle_monitoring(m_key, enabled): """Insert or updates the field of a model that indicates if monitoring is enabled.""" aql = "UPDATE {_key: @m_key, monitoring_enabled: @enabled} IN models" - execute_aql_query(aql, m_key=m_key, enabled=True) + execute_aql_query(aql, m_key=m_key, enabled=enabled) From 43321aff27aae655508d2b9f0f14ab12f4cc221c Mon Sep 17 00:00:00 2001 From: Michiel Date: Wed, 18 Apr 2018 14:58:38 +0200 Subject: [PATCH 7/8] Add state field for monitoring. --- files/sojobo_api/api/w_datastore.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/files/sojobo_api/api/w_datastore.py b/files/sojobo_api/api/w_datastore.py index 3d8c99a..3fee279 100644 --- a/files/sojobo_api/api/w_datastore.py +++ b/files/sojobo_api/api/w_datastore.py @@ -878,7 +878,6 @@ def get_workspace_type_id(ws_type): # MONITORING # ############################################################################### -def toggle_monitoring(m_key, enabled): - """Insert or updates the field of a model that indicates if monitoring is enabled.""" - aql = "UPDATE {_key: @m_key, monitoring_enabled: @enabled} IN models" - execute_aql_query(aql, m_key=m_key, enabled=enabled) +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) From 70020198996fddf3361c16c0c1092e0067a85f9e Mon Sep 17 00:00:00 2001 From: Michiel Date: Thu, 19 Apr 2018 14:23:16 +0200 Subject: [PATCH 8/8] Improve the deployment of monitoring. --- files/sojobo_api/api/w_juju.py | 19 ++++++++++++++++++- files/sojobo_api/scripts/add_application.py | 13 +++++++++---- files/sojobo_api/scripts/bundle_deployment.py | 14 ++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/files/sojobo_api/api/w_juju.py b/files/sojobo_api/api/w_juju.py index c09c681..9f3167e 100644 --- a/files/sojobo_api/api/w_juju.py +++ b/files/sojobo_api/api/w_juju.py @@ -1086,9 +1086,26 @@ def log_event(event_type, tags): ############################################################################### -def add_monitoring(c_name, endpoint, cacert, m_name, uuid, juju_username, password, application): +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 2ca39b0..4e56ea4 100644 --- a/files/sojobo_api/scripts/add_application.py +++ b/files/sojobo_api/scripts/add_application.py @@ -95,14 +95,19 @@ async def add_application(c_name, m_key, username, password, units, machine, con # If monitoring is enabled for the workspace then we need to add a # relation between the application and the tengu monitoring telegraf. - if "monitoring_enabled" in auth_data["model"] and auth_data["model"]["monitoring_enabled"]: + 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"] - m_name = auth_data["model"]["name"] uuid = auth_data["model"]["uuid"] juju_username = auth_data["user"]["juju_username"] - juju.add_monitoring(c_name, endpoint, cacert, m_name, uuid, - juju_username, password, application) + 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) diff --git a/files/sojobo_api/scripts/bundle_deployment.py b/files/sojobo_api/scripts/bundle_deployment.py index e455f93..6294d09 100644 --- a/files/sojobo_api/scripts/bundle_deployment.py +++ b/files/sojobo_api/scripts/bundle_deployment.py @@ -71,6 +71,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)