| Adam Israel | dcdf82b | 2017-08-15 15:26:43 -0400 | [diff] [blame^] | 1 | import asyncio |
| 2 | import logging |
| 3 | |
| 4 | from . import tag |
| 5 | from . import utils |
| 6 | from .client import client |
| 7 | from .client import connection |
| 8 | from .model import Model |
| 9 | |
| 10 | log = logging.getLogger(__name__) |
| 11 | |
| 12 | |
| 13 | class Controller(object): |
| 14 | def __init__(self, loop=None, |
| 15 | max_frame_size=connection.Connection.DEFAULT_FRAME_SIZE): |
| 16 | """Instantiate a new Controller. |
| 17 | |
| 18 | One of the connect_* methods will need to be called before this |
| 19 | object can be used for anything interesting. |
| 20 | |
| 21 | :param loop: an asyncio event loop |
| 22 | |
| 23 | """ |
| 24 | self.loop = loop or asyncio.get_event_loop() |
| 25 | self.max_frame_size = None |
| 26 | self.connection = None |
| 27 | self.controller_name = None |
| 28 | |
| 29 | async def connect( |
| 30 | self, endpoint, username, password, cacert=None, macaroons=None): |
| 31 | """Connect to an arbitrary Juju controller. |
| 32 | |
| 33 | """ |
| 34 | self.connection = await connection.Connection.connect( |
| 35 | endpoint, None, username, password, cacert, macaroons, |
| 36 | max_frame_size=self.max_frame_size) |
| 37 | |
| 38 | async def connect_current(self): |
| 39 | """Connect to the current Juju controller. |
| 40 | |
| 41 | """ |
| 42 | self.connection = ( |
| 43 | await connection.Connection.connect_current_controller( |
| 44 | max_frame_size=self.max_frame_size)) |
| 45 | |
| 46 | async def connect_controller(self, controller_name): |
| 47 | """Connect to a Juju controller by name. |
| 48 | |
| 49 | """ |
| 50 | self.connection = ( |
| 51 | await connection.Connection.connect_controller( |
| 52 | controller_name, max_frame_size=self.max_frame_size)) |
| 53 | self.controller_name = controller_name |
| 54 | |
| 55 | async def disconnect(self): |
| 56 | """Shut down the watcher task and close websockets. |
| 57 | |
| 58 | """ |
| 59 | if self.connection and self.connection.is_open: |
| 60 | log.debug('Closing controller connection') |
| 61 | await self.connection.close() |
| 62 | self.connection = None |
| 63 | |
| 64 | async def add_model( |
| 65 | self, model_name, cloud_name=None, credential_name=None, |
| 66 | owner=None, config=None, region=None): |
| 67 | """Add a model to this controller. |
| 68 | |
| 69 | :param str model_name: Name to give the new model. |
| 70 | :param str cloud_name: Name of the cloud in which to create the |
| 71 | model, e.g. 'aws'. Defaults to same cloud as controller. |
| 72 | :param str credential_name: Name of the credential to use when |
| 73 | creating the model. Defaults to current credential. If you |
| 74 | pass a credential_name, you must also pass a cloud_name, |
| 75 | even if it's the default cloud. |
| 76 | :param str owner: Username that will own the model. Defaults to |
| 77 | the current user. |
| 78 | :param dict config: Model configuration. |
| 79 | :param str region: Region in which to create the model. |
| 80 | |
| 81 | """ |
| 82 | model_facade = client.ModelManagerFacade.from_connection( |
| 83 | self.connection) |
| 84 | |
| 85 | owner = owner or self.connection.info['user-info']['identity'] |
| 86 | cloud_name = cloud_name or await self.get_cloud() |
| 87 | |
| 88 | if credential_name: |
| 89 | credential = tag.credential( |
| 90 | cloud_name, |
| 91 | tag.untag('user-', owner), |
| 92 | credential_name |
| 93 | ) |
| 94 | else: |
| 95 | credential = None |
| 96 | |
| 97 | log.debug('Creating model %s', model_name) |
| 98 | |
| 99 | model_info = await model_facade.CreateModel( |
| 100 | tag.cloud(cloud_name), |
| 101 | config, |
| 102 | credential, |
| 103 | model_name, |
| 104 | owner, |
| 105 | region |
| 106 | ) |
| 107 | |
| 108 | # Add our ssh key to the model, to work around |
| 109 | # https://bugs.launchpad.net/juju/+bug/1643076 |
| 110 | try: |
| 111 | ssh_key = await utils.read_ssh_key(loop=self.loop) |
| 112 | |
| 113 | if self.controller_name: |
| 114 | model_name = "{}:{}".format(self.controller_name, model_name) |
| 115 | |
| 116 | cmd = ['juju', 'add-ssh-key', '-m', model_name, ssh_key] |
| 117 | |
| 118 | await utils.execute_process(*cmd, log=log, loop=self.loop) |
| 119 | except Exception: |
| 120 | log.exception( |
| 121 | "Could not add ssh key to model. You will not be able " |
| 122 | "to ssh into machines in this model. " |
| 123 | "Manually running `juju add-ssh-key <key>` in the cli " |
| 124 | "may fix this problem.") |
| 125 | |
| 126 | model = Model() |
| 127 | await model.connect( |
| 128 | self.connection.endpoint, |
| 129 | model_info.uuid, |
| 130 | self.connection.username, |
| 131 | self.connection.password, |
| 132 | self.connection.cacert, |
| 133 | self.connection.macaroons, |
| 134 | loop=self.loop, |
| 135 | ) |
| 136 | |
| 137 | return model |
| 138 | |
| 139 | async def destroy_models(self, *uuids): |
| 140 | """Destroy one or more models. |
| 141 | |
| 142 | :param str \*uuids: UUIDs of models to destroy |
| 143 | |
| 144 | """ |
| 145 | model_facade = client.ModelManagerFacade.from_connection( |
| 146 | self.connection) |
| 147 | |
| 148 | log.debug( |
| 149 | 'Destroying model%s %s', |
| 150 | '' if len(uuids) == 1 else 's', |
| 151 | ', '.join(uuids) |
| 152 | ) |
| 153 | |
| 154 | await model_facade.DestroyModels([ |
| 155 | client.Entity(tag.model(uuid)) |
| 156 | for uuid in uuids |
| 157 | ]) |
| 158 | destroy_model = destroy_models |
| 159 | |
| 160 | async def add_user(self, username, password=None, display_name=None): |
| 161 | """Add a user to this controller. |
| 162 | |
| 163 | :param str username: Username |
| 164 | :param str display_name: Display name |
| 165 | :param str acl: Access control, e.g. 'read' |
| 166 | :param list models: Models to which the user is granted access |
| 167 | |
| 168 | """ |
| 169 | if not display_name: |
| 170 | display_name = username |
| 171 | user_facade = client.UserManagerFacade.from_connection(self.connection) |
| 172 | users = [{'display_name': display_name, |
| 173 | 'password': password, |
| 174 | 'username': username}] |
| 175 | return await user_facade.AddUser(users) |
| 176 | |
| 177 | async def change_user_password(self, username, password): |
| 178 | """Change the password for a user in this controller. |
| 179 | |
| 180 | :param str username: Username |
| 181 | :param str password: New password |
| 182 | |
| 183 | """ |
| 184 | user_facade = client.UserManagerFacade.from_connection(self.connection) |
| 185 | entity = client.EntityPassword(password, tag.user(username)) |
| 186 | return await user_facade.SetPassword([entity]) |
| 187 | |
| 188 | async def destroy(self, destroy_all_models=False): |
| 189 | """Destroy this controller. |
| 190 | |
| 191 | :param bool destroy_all_models: Destroy all hosted models in the |
| 192 | controller. |
| 193 | |
| 194 | """ |
| 195 | controller_facade = client.ControllerFacade.from_connection( |
| 196 | self.connection) |
| 197 | return await controller_facade.DestroyController(destroy_all_models) |
| 198 | |
| 199 | async def disable_user(self, username): |
| 200 | """Disable a user. |
| 201 | |
| 202 | :param str username: Username |
| 203 | |
| 204 | """ |
| 205 | user_facade = client.UserManagerFacade.from_connection(self.connection) |
| 206 | entity = client.Entity(tag.user(username)) |
| 207 | return await user_facade.DisableUser([entity]) |
| 208 | |
| 209 | async def enable_user(self, username): |
| 210 | """Re-enable a previously disabled user. |
| 211 | |
| 212 | """ |
| 213 | user_facade = client.UserManagerFacade.from_connection(self.connection) |
| 214 | entity = client.Entity(tag.user(username)) |
| 215 | return await user_facade.EnableUser([entity]) |
| 216 | |
| 217 | def kill(self): |
| 218 | """Forcibly terminate all machines and other associated resources for |
| 219 | this controller. |
| 220 | |
| 221 | """ |
| 222 | raise NotImplementedError() |
| 223 | |
| 224 | async def get_cloud(self): |
| 225 | """ |
| 226 | Get the name of the cloud that this controller lives on. |
| 227 | """ |
| 228 | cloud_facade = client.CloudFacade.from_connection(self.connection) |
| 229 | |
| 230 | result = await cloud_facade.Clouds() |
| 231 | cloud = list(result.clouds.keys())[0] # only lives on one cloud |
| 232 | return tag.untag('cloud-', cloud) |
| 233 | |
| 234 | async def get_models(self, all_=False, username=None): |
| 235 | """Return list of available models on this controller. |
| 236 | |
| 237 | :param bool all_: List all models, regardless of user accessibilty |
| 238 | (admin use only) |
| 239 | :param str username: User for which to list models (admin use only) |
| 240 | |
| 241 | """ |
| 242 | controller_facade = client.ControllerFacade.from_connection( |
| 243 | self.connection) |
| 244 | return await controller_facade.AllModels() |
| 245 | |
| 246 | def get_payloads(self, *patterns): |
| 247 | """Return list of known payloads. |
| 248 | |
| 249 | :param str \*patterns: Patterns to match against |
| 250 | |
| 251 | Each pattern will be checked against the following info in Juju:: |
| 252 | |
| 253 | - unit name |
| 254 | - machine id |
| 255 | - payload type |
| 256 | - payload class |
| 257 | - payload id |
| 258 | - payload tag |
| 259 | - payload status |
| 260 | |
| 261 | """ |
| 262 | raise NotImplementedError() |
| 263 | |
| 264 | def get_users(self, all_=False): |
| 265 | """Return list of users that can connect to this controller. |
| 266 | |
| 267 | :param bool all_: Include disabled users |
| 268 | |
| 269 | """ |
| 270 | raise NotImplementedError() |
| 271 | |
| 272 | def login(self): |
| 273 | """Log in to this controller. |
| 274 | |
| 275 | """ |
| 276 | raise NotImplementedError() |
| 277 | |
| 278 | def logout(self, force=False): |
| 279 | """Log out of this controller. |
| 280 | |
| 281 | :param bool force: Don't fail even if user not previously logged in |
| 282 | with a password |
| 283 | |
| 284 | """ |
| 285 | raise NotImplementedError() |
| 286 | |
| 287 | def get_model(self, name): |
| 288 | """Get a model by name. |
| 289 | |
| 290 | :param str name: Model name |
| 291 | |
| 292 | """ |
| 293 | raise NotImplementedError() |
| 294 | |
| 295 | async def get_user(self, username, include_disabled=False): |
| 296 | """Get a user by name. |
| 297 | |
| 298 | :param str username: Username |
| 299 | |
| 300 | """ |
| 301 | client_facade = client.UserManagerFacade.from_connection( |
| 302 | self.connection) |
| 303 | user = tag.user(username) |
| 304 | return await client_facade.UserInfo([client.Entity(user)], |
| 305 | include_disabled) |
| 306 | |
| 307 | async def grant(self, username, acl='login'): |
| 308 | """Set access level of the given user on the controller |
| 309 | |
| 310 | :param str username: Username |
| 311 | :param str acl: Access control ('login', 'add-model' or 'superuser') |
| 312 | |
| 313 | """ |
| 314 | controller_facade = client.ControllerFacade.from_connection( |
| 315 | self.connection) |
| 316 | user = tag.user(username) |
| 317 | await self.revoke(username) |
| 318 | changes = client.ModifyControllerAccess(acl, 'grant', user) |
| 319 | return await controller_facade.ModifyControllerAccess([changes]) |
| 320 | |
| 321 | async def revoke(self, username): |
| 322 | """Removes all access from a controller |
| 323 | |
| 324 | :param str username: username |
| 325 | |
| 326 | """ |
| 327 | controller_facade = client.ControllerFacade.from_connection( |
| 328 | self.connection) |
| 329 | user = tag.user(username) |
| 330 | changes = client.ModifyControllerAccess('login', 'revoke', user) |
| 331 | return await controller_facade.ModifyControllerAccess([changes]) |