+ try:
+ controller = await self.get_controller()
+ model = await self.get_model(controller, model_name)
+ application = self._get_application(
+ model, application_name=application_name,
+ )
+ await application.set_config(config)
+ finally:
+ await self.disconnect_model(model)
+ await self.disconnect_controller(controller)
+
+ def _get_api_endpoints_db(self) -> [str]:
+ """
+ Get API Endpoints from DB
+
+ :return: List of API endpoints
+ """
+ self.log.debug("Getting endpoints from database")
+
+ juju_info = self.db.get_one(
+ DB_DATA.api_endpoints.table,
+ q_filter=DB_DATA.api_endpoints.filter,
+ fail_on_empty=False,
+ )
+ if juju_info and DB_DATA.api_endpoints.key in juju_info:
+ return juju_info[DB_DATA.api_endpoints.key]
+
+ def _update_api_endpoints_db(self, endpoints: [str]):
+ """
+ Update API endpoints in Database
+
+ :param: List of endpoints
+ """
+ self.log.debug("Saving endpoints {} in database".format(endpoints))
+
+ juju_info = self.db.get_one(
+ DB_DATA.api_endpoints.table,
+ q_filter=DB_DATA.api_endpoints.filter,
+ fail_on_empty=False,
+ )
+ # If it doesn't, then create it
+ if not juju_info:
+ try:
+ self.db.create(
+ DB_DATA.api_endpoints.table, DB_DATA.api_endpoints.filter,
+ )
+ except DbException as e:
+ # Racing condition: check if another N2VC worker has created it
+ juju_info = self.db.get_one(
+ DB_DATA.api_endpoints.table,
+ q_filter=DB_DATA.api_endpoints.filter,
+ fail_on_empty=False,
+ )
+ if not juju_info:
+ raise e
+ self.db.set_one(
+ DB_DATA.api_endpoints.table,
+ DB_DATA.api_endpoints.filter,
+ {DB_DATA.api_endpoints.key: endpoints},
+ )
+
+ def handle_exception(self, loop, context):
+ # All unhandled exceptions by libjuju are handled here.
+ pass
+
+ async def health_check(self, interval: float = 300.0):
+ """
+ Health check to make sure controller and controller_model connections are OK
+
+ :param: interval: Time in seconds between checks
+ """
+ while True:
+ try:
+ controller = await self.get_controller()
+ # self.log.debug("VCA is alive")
+ except Exception as e:
+ self.log.error("Health check to VCA failed: {}".format(e))
+ finally:
+ await self.disconnect_controller(controller)
+ await asyncio.sleep(interval)
+
+ async def list_models(self, contains: str = None) -> [str]:
+ """List models with certain names
+
+ :param: contains: String that is contained in model name
+
+ :retur: [models] Returns list of model names
+ """
+
+ controller = await self.get_controller()
+ try:
+ models = await controller.list_models()
+ if contains:
+ models = [model for model in models if contains in model]
+ return models
+ finally:
+ await self.disconnect_controller(controller)