| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1 | # Copyright 2020 Canonical Ltd. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import asyncio |
| 16 | import time |
| 17 | from juju.client import client |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 18 | from n2vc.exceptions import EntityInvalidException |
| 19 | from n2vc.n2vc_conn import N2VCConnector |
| 20 | from juju.model import ModelEntity, Model |
| 21 | from juju.client.overrides import Delta |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 22 | from juju.status import derive_status |
| 23 | from juju.application import Application |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 24 | from websockets.exceptions import ConnectionClosed |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 25 | import logging |
| 26 | |
| 27 | logger = logging.getLogger("__main__") |
| 28 | |
| 29 | |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 30 | def status(application: Application) -> str: |
| 31 | unit_status = [] |
| 32 | for unit in application.units: |
| 33 | unit_status.append(unit.workload_status) |
| 34 | return derive_status(unit_status) |
| 35 | |
| 36 | |
| 37 | def entity_ready(entity: ModelEntity) -> bool: |
| David Garcia | c344117 | 2021-03-10 11:50:38 +0100 | [diff] [blame] | 38 | """ |
| 39 | Check if the entity is ready |
| 40 | |
| 41 | :param: entity: Model entity. It can be a machine, action, or application. |
| 42 | |
| 43 | :returns: boolean saying if the entity is ready or not |
| 44 | """ |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 45 | entity_type = entity.entity_type |
| 46 | if entity_type == "machine": |
| 47 | return entity.agent_status in ["started"] |
| 48 | elif entity_type == "action": |
| 49 | return entity.status in ["completed", "failed", "cancelled"] |
| 50 | elif entity_type == "application": |
| 51 | # Workaround for bug: https://github.com/juju/python-libjuju/issues/441 |
| David Garcia | c344117 | 2021-03-10 11:50:38 +0100 | [diff] [blame] | 52 | return entity.status in ["active", "blocked"] |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 53 | else: |
| 54 | raise EntityInvalidException("Unknown entity type: {}".format(entity_type)) |
| 55 | |
| 56 | |
| David Garcia | c344117 | 2021-03-10 11:50:38 +0100 | [diff] [blame] | 57 | def application_ready(application: Application) -> bool: |
| 58 | """ |
| 59 | Check if an application has a leader |
| 60 | |
| 61 | :param: application: Application entity. |
| 62 | |
| 63 | :returns: boolean saying if the application has a unit that is a leader. |
| 64 | """ |
| 65 | ready_status_list = ["active", "blocked"] |
| 66 | application_ready = application.status in ready_status_list |
| 67 | units_ready = all( |
| 68 | unit.workload_status in ready_status_list for unit in application.units |
| 69 | ) |
| 70 | return application_ready and units_ready |
| 71 | |
| 72 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 73 | class JujuModelWatcher: |
| 74 | @staticmethod |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 75 | async def wait_for_model( |
| 76 | model: Model, |
| 77 | timeout: float = 3600 |
| 78 | ): |
| David Garcia | 667696e | 2020-09-22 14:52:32 +0200 | [diff] [blame] | 79 | """ |
| 80 | Wait for all entities in model to reach its final state. |
| 81 | |
| 82 | :param: model: Model to observe |
| 83 | :param: timeout: Timeout for the model applications to be active |
| 84 | |
| 85 | :raises: asyncio.TimeoutError when timeout reaches |
| 86 | """ |
| 87 | |
| 88 | if timeout is None: |
| 89 | timeout = 3600.0 |
| 90 | |
| 91 | # Coroutine to wait until the entity reaches the final state |
| David Garcia | c344117 | 2021-03-10 11:50:38 +0100 | [diff] [blame] | 92 | async def wait_until_model_ready(): |
| 93 | wait_for_entity = asyncio.ensure_future( |
| 94 | asyncio.wait_for( |
| 95 | model.block_until( |
| 96 | lambda: all( |
| 97 | application_ready(application) |
| 98 | for application in model.applications.values() |
| 99 | ), |
| 100 | ), |
| 101 | timeout=timeout, |
| 102 | ) |
| David Garcia | 667696e | 2020-09-22 14:52:32 +0200 | [diff] [blame] | 103 | ) |
| David Garcia | 667696e | 2020-09-22 14:52:32 +0200 | [diff] [blame] | 104 | |
| David Garcia | c344117 | 2021-03-10 11:50:38 +0100 | [diff] [blame] | 105 | tasks = [wait_for_entity] |
| 106 | try: |
| 107 | await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) |
| 108 | finally: |
| 109 | # Cancel tasks |
| 110 | for task in tasks: |
| 111 | task.cancel() |
| 112 | |
| 113 | await wait_until_model_ready() |
| 114 | # Check model is still ready after 10 seconds |
| 115 | |
| 116 | await asyncio.sleep(10) |
| 117 | await wait_until_model_ready() |
| David Garcia | 667696e | 2020-09-22 14:52:32 +0200 | [diff] [blame] | 118 | |
| 119 | @staticmethod |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 120 | async def wait_for( |
| David Garcia | 667696e | 2020-09-22 14:52:32 +0200 | [diff] [blame] | 121 | model: Model, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 122 | entity: ModelEntity, |
| 123 | progress_timeout: float = 3600, |
| 124 | total_timeout: float = 3600, |
| 125 | db_dict: dict = None, |
| 126 | n2vc: N2VCConnector = None, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 127 | vca_id: str = None, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 128 | ): |
| 129 | """ |
| 130 | Wait for entity to reach its final state. |
| 131 | |
| 132 | :param: model: Model to observe |
| 133 | :param: entity: Entity object |
| 134 | :param: progress_timeout: Maximum time between two updates in the model |
| 135 | :param: total_timeout: Timeout for the entity to be active |
| 136 | :param: db_dict: Dictionary with data of the DB to write the updates |
| 137 | :param: n2vc: N2VC Connector objector |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 138 | :param: vca_id: VCA ID |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 139 | |
| 140 | :raises: asyncio.TimeoutError when timeout reaches |
| 141 | """ |
| 142 | |
| 143 | if progress_timeout is None: |
| 144 | progress_timeout = 3600.0 |
| 145 | if total_timeout is None: |
| 146 | total_timeout = 3600.0 |
| 147 | |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 148 | entity_type = entity.entity_type |
| 149 | if entity_type not in ["application", "action", "machine"]: |
| 150 | raise EntityInvalidException("Unknown entity type: {}".format(entity_type)) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 151 | |
| 152 | # Coroutine to wait until the entity reaches the final state |
| 153 | wait_for_entity = asyncio.ensure_future( |
| 154 | asyncio.wait_for( |
| David Garcia | c344117 | 2021-03-10 11:50:38 +0100 | [diff] [blame] | 155 | model.block_until(lambda: entity_ready(entity)), |
| 156 | timeout=total_timeout, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 157 | ) |
| 158 | ) |
| 159 | |
| 160 | # Coroutine to watch the model for changes (and write them to DB) |
| 161 | watcher = asyncio.ensure_future( |
| 162 | JujuModelWatcher.model_watcher( |
| 163 | model, |
| 164 | entity_id=entity.entity_id, |
| 165 | entity_type=entity_type, |
| 166 | timeout=progress_timeout, |
| 167 | db_dict=db_dict, |
| 168 | n2vc=n2vc, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 169 | vca_id=vca_id, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 170 | ) |
| 171 | ) |
| 172 | |
| 173 | tasks = [wait_for_entity, watcher] |
| 174 | try: |
| 175 | # Execute tasks, and stop when the first is finished |
| 176 | # The watcher task won't never finish (unless it timeouts) |
| 177 | await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 178 | finally: |
| 179 | # Cancel tasks |
| 180 | for task in tasks: |
| 181 | task.cancel() |
| 182 | |
| 183 | @staticmethod |
| 184 | async def model_watcher( |
| 185 | model: Model, |
| 186 | entity_id: str, |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 187 | entity_type: str, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 188 | timeout: float, |
| 189 | db_dict: dict = None, |
| 190 | n2vc: N2VCConnector = None, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 191 | vca_id: str = None, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 192 | ): |
| 193 | """ |
| 194 | Observes the changes related to an specific entity in a model |
| 195 | |
| 196 | :param: model: Model to observe |
| 197 | :param: entity_id: ID of the entity to be observed |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 198 | :param: entity_type: Entity Type (p.e. "application", "machine, and "action") |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 199 | :param: timeout: Maximum time between two updates in the model |
| 200 | :param: db_dict: Dictionary with data of the DB to write the updates |
| 201 | :param: n2vc: N2VC Connector objector |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 202 | :param: vca_id: VCA ID |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 203 | |
| 204 | :raises: asyncio.TimeoutError when timeout reaches |
| 205 | """ |
| 206 | |
| 207 | allwatcher = client.AllWatcherFacade.from_connection(model.connection()) |
| 208 | |
| 209 | # Genenerate array with entity types to listen |
| 210 | entity_types = ( |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 211 | [entity_type, "unit"] |
| 212 | if entity_type == "application" # TODO: Add "action" too |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 213 | else [entity_type] |
| 214 | ) |
| 215 | |
| 216 | # Get time when it should timeout |
| 217 | timeout_end = time.time() + timeout |
| 218 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 219 | try: |
| 220 | while True: |
| 221 | change = await allwatcher.Next() |
| 222 | for delta in change.deltas: |
| 223 | write = False |
| 224 | delta_entity = None |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 225 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 226 | # Get delta EntityType |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 227 | delta_entity = delta.entity |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 228 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 229 | if delta_entity in entity_types: |
| 230 | # Get entity id |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 231 | if entity_type == "application": |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 232 | id = ( |
| 233 | delta.data["application"] |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 234 | if delta_entity == "unit" |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 235 | else delta.data["name"] |
| 236 | ) |
| 237 | else: |
| 238 | id = delta.data["id"] |
| 239 | |
| 240 | # Write if the entity id match |
| 241 | write = True if id == entity_id else False |
| 242 | |
| 243 | # Update timeout |
| 244 | timeout_end = time.time() + timeout |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 245 | ( |
| 246 | status, |
| 247 | status_message, |
| 248 | vca_status, |
| 249 | ) = JujuModelWatcher.get_status(delta) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 250 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 251 | if write and n2vc is not None and db_dict: |
| 252 | # Write status to DB |
| 253 | status = n2vc.osm_status(delta_entity, status) |
| 254 | await n2vc.write_app_status_to_db( |
| 255 | db_dict=db_dict, |
| 256 | status=status, |
| 257 | detailed_status=status_message, |
| 258 | vca_status=vca_status, |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 259 | entity_type=delta_entity, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 260 | vca_id=vca_id, |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 261 | ) |
| 262 | # Check if timeout |
| 263 | if time.time() > timeout_end: |
| 264 | raise asyncio.TimeoutError() |
| 265 | except ConnectionClosed: |
| 266 | pass |
| 267 | # This is expected to happen when the |
| 268 | # entity reaches its final state, because |
| 269 | # the model connection is closed afterwards |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 270 | |
| 271 | @staticmethod |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 272 | def get_status(delta: Delta) -> (str, str, str): |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 273 | """ |
| 274 | Get status from delta |
| 275 | |
| 276 | :param: delta: Delta generated by the allwatcher |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 277 | :param: entity_type: Entity Type (p.e. "application", "machine, and "action") |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 278 | |
| 279 | :return (status, message, vca_status) |
| 280 | """ |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 281 | if delta.entity == "machine": |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 282 | return ( |
| 283 | delta.data["agent-status"]["current"], |
| 284 | delta.data["instance-status"]["message"], |
| 285 | delta.data["instance-status"]["current"], |
| 286 | ) |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 287 | elif delta.entity == "action": |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 288 | return ( |
| 289 | delta.data["status"], |
| 290 | delta.data["status"], |
| 291 | delta.data["status"], |
| 292 | ) |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 293 | elif delta.entity == "application": |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 294 | return ( |
| 295 | delta.data["status"]["current"], |
| 296 | delta.data["status"]["message"], |
| 297 | delta.data["status"]["current"], |
| 298 | ) |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 299 | elif delta.entity == "unit": |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 300 | return ( |
| 301 | delta.data["workload-status"]["current"], |
| 302 | delta.data["workload-status"]["message"], |
| 303 | delta.data["workload-status"]["current"], |
| 304 | ) |