6 from .client
import client
7 from .client
import connection
8 from .client
import watcher
9 from .model
import Model
11 log
= logging
.getLogger(__name__
)
14 class Controller(object):
15 def __init__(self
, loop
=None):
16 """Instantiate a new Controller.
18 One of the connect_* methods will need to be called before this
19 object can be used for anything interesting.
21 :param loop: an asyncio event loop
24 self
.loop
= loop
or asyncio
.get_event_loop()
25 self
.connection
= None
28 self
, endpoint
, username
, password
, cacert
=None, macaroons
=None):
29 """Connect to an arbitrary Juju controller.
32 self
.connection
= await connection
.Connection
.connect(
33 endpoint
, None, username
, password
, cacert
, macaroons
)
35 async def connect_current(self
):
36 """Connect to the current Juju controller.
40 await connection
.Connection
.connect_current_controller())
42 async def connect_controller(self
, controller_name
):
43 """Connect to a Juju controller by name.
47 await connection
.Connection
.connect_controller(controller_name
))
49 async def disconnect(self
):
50 """Shut down the watcher task and close websockets.
53 if self
.connection
and self
.connection
.is_open
:
54 log
.debug('Closing controller connection')
55 await self
.connection
.close()
56 self
.connection
= None
59 self
, model_name
, cloud_name
=None, credential_name
=None,
60 owner
=None, config
=None, region
=None):
61 """Add a model to this controller.
63 :param str model_name: Name to give the new model.
64 :param str cloud_name: Name of the cloud in which to create the
65 model, e.g. 'aws'. Defaults to same cloud as controller.
66 :param str credential_name: Name of the credential to use when
67 creating the model. Defaults to current credential. If you
68 pass a credential_name, you must also pass a cloud_name,
69 even if it's the default cloud.
70 :param str owner: Username that will own the model. Defaults to
72 :param dict config: Model configuration.
73 :param str region: Region in which to create the model.
76 model_facade
= client
.ModelManagerFacade()
77 model_facade
.connect(self
.connection
)
79 owner
= owner
or self
.connection
.info
['user-info']['identity']
80 cloud_name
= cloud_name
or await self
.get_cloud()
83 credential
= tag
.credential(
85 tag
.untag('user-', owner
),
91 log
.debug('Creating model %s', model_name
)
93 model_info
= await model_facade
.CreateModel(
94 tag
.cloud(cloud_name
),
102 # Add our ssh key to the model, to work around
103 # https://bugs.launchpad.net/juju/+bug/1643076
105 ssh_key
= await utils
.read_ssh_key(loop
=self
.loop
)
106 await utils
.execute_process(
107 'juju', 'add-ssh-key', '-m', model_name
, ssh_key
, log
=log
)
108 except Exception as e
:
110 "Could not add ssh key to model. You will not be able "
111 "to ssh into machines in this model. "
112 "Manually running `juju add-ssh-key <key>` in the cli "
113 "may fix this problem.")
117 self
.connection
.endpoint
,
119 self
.connection
.username
,
120 self
.connection
.password
,
121 self
.connection
.cacert
,
122 self
.connection
.macaroons
,
127 async def destroy_models(self
, *uuids
):
128 """Destroy one or more models.
130 :param str \*uuids: UUIDs of models to destroy
133 model_facade
= client
.ModelManagerFacade()
134 model_facade
.connect(self
.connection
)
137 'Destroying model%s %s',
138 '' if len(uuids
) == 1 else 's',
142 await model_facade
.DestroyModels([
143 client
.Entity(tag
.model(uuid
))
146 destroy_model
= destroy_models
148 def add_user(self
, username
, display_name
=None, acl
=None, models
=None):
149 """Add a user to this controller.
151 :param str username: Username
152 :param str display_name: Display name
153 :param str acl: Access control, e.g. 'read'
154 :param list models: Models to which the user is granted access
159 def change_user_password(self
, username
, password
):
160 """Change the password for a user in this controller.
162 :param str username: Username
163 :param str password: New password
168 def destroy(self
, destroy_all_models
=False):
169 """Destroy this controller.
171 :param bool destroy_all_models: Destroy all hosted models in the
177 def disable_user(self
, username
):
180 :param str username: Username
185 def enable_user(self
):
186 """Re-enable a previously disabled user.
192 """Forcibly terminate all machines and other associated resources for
198 async def get_cloud(self
):
200 Get the name of the cloud that this controller lives on.
202 cloud_facade
= client
.CloudFacade()
203 cloud_facade
.connect(self
.connection
)
205 result
= await cloud_facade
.Clouds()
206 cloud
= list(result
.clouds
.keys())[0] # only lives on one cloud
207 return tag
.untag('cloud-', cloud
)
209 def get_models(self
, all_
=False, username
=None):
210 """Return list of available models on this controller.
212 :param bool all_: List all models, regardless of user accessibilty
214 :param str username: User for which to list models (admin use only)
219 def get_payloads(self
, *patterns
):
220 """Return list of known payloads.
222 :param str \*patterns: Patterns to match against
224 Each pattern will be checked against the following info in Juju::
237 def get_users(self
, all_
=False):
238 """Return list of users that can connect to this controller.
240 :param bool all_: Include disabled users
246 """Log in to this controller.
251 def logout(self
, force
=False):
252 """Log out of this controller.
254 :param bool force: Don't fail even if user not previously logged in
260 def get_model(self
, name
):
261 """Get a model by name.
263 :param str name: Model name
268 def get_user(self
, username
):
269 """Get a user by name.
271 :param str username: Username