6 from .client
import client
7 from .client
import connection
8 from .model
import Model
10 log
= logging
.getLogger(__name__
)
13 class Controller(object):
14 def __init__(self
, loop
=None):
15 """Instantiate a new Controller.
17 One of the connect_* methods will need to be called before this
18 object can be used for anything interesting.
20 :param loop: an asyncio event loop
23 self
.loop
= loop
or asyncio
.get_event_loop()
24 self
.connection
= None
25 self
.controller_name
= 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
))
48 self
.controller_name
= controller_name
50 async def disconnect(self
):
51 """Shut down the watcher task and close websockets.
54 if self
.connection
and self
.connection
.is_open
:
55 log
.debug('Closing controller connection')
56 await self
.connection
.close()
57 self
.connection
= None
60 self
, model_name
, cloud_name
=None, credential_name
=None,
61 owner
=None, config
=None, region
=None):
62 """Add a model to this controller.
64 :param str model_name: Name to give the new model.
65 :param str cloud_name: Name of the cloud in which to create the
66 model, e.g. 'aws'. Defaults to same cloud as controller.
67 :param str credential_name: Name of the credential to use when
68 creating the model. Defaults to current credential. If you
69 pass a credential_name, you must also pass a cloud_name,
70 even if it's the default cloud.
71 :param str owner: Username that will own the model. Defaults to
73 :param dict config: Model configuration.
74 :param str region: Region in which to create the model.
77 model_facade
= client
.ModelManagerFacade
.from_connection(
80 owner
= owner
or self
.connection
.info
['user-info']['identity']
81 cloud_name
= cloud_name
or await self
.get_cloud()
84 credential
= tag
.credential(
86 tag
.untag('user-', owner
),
92 log
.debug('Creating model %s', model_name
)
94 model_info
= await model_facade
.CreateModel(
95 tag
.cloud(cloud_name
),
103 # Add our ssh key to the model, to work around
104 # https://bugs.launchpad.net/juju/+bug/1643076
106 ssh_key
= await utils
.read_ssh_key(loop
=self
.loop
)
108 if self
.controller_name
:
109 model_name
= "{}:{}".format(self
.controller_name
, model_name
)
111 cmd
= ['juju', 'add-ssh-key', '-m', model_name
, ssh_key
]
113 await utils
.execute_process(*cmd
, log
=log
, loop
=self
.loop
)
116 "Could not add ssh key to model. You will not be able "
117 "to ssh into machines in this model. "
118 "Manually running `juju add-ssh-key <key>` in the cli "
119 "may fix this problem.")
123 self
.connection
.endpoint
,
125 self
.connection
.username
,
126 self
.connection
.password
,
127 self
.connection
.cacert
,
128 self
.connection
.macaroons
,
134 async def destroy_models(self
, *uuids
):
135 """Destroy one or more models.
137 :param str \*uuids: UUIDs of models to destroy
140 model_facade
= client
.ModelManagerFacade
.from_connection(
144 'Destroying model%s %s',
145 '' if len(uuids
) == 1 else 's',
149 await model_facade
.DestroyModels([
150 client
.Entity(tag
.model(uuid
))
153 destroy_model
= destroy_models
155 async def add_user(self
, username
, password
=None, display_name
=None):
156 """Add a user to this controller.
158 :param str username: Username
159 :param str display_name: Display name
160 :param str acl: Access control, e.g. 'read'
161 :param list models: Models to which the user is granted access
165 display_name
= username
166 user_facade
= client
.UserManagerFacade
.from_connection(self
.connection
)
167 users
= [{'display_name': display_name
,
168 'password': password
,
169 'username': username
}]
170 return await user_facade
.AddUser(users
)
172 async def change_user_password(self
, username
, password
):
173 """Change the password for a user in this controller.
175 :param str username: Username
176 :param str password: New password
179 user_facade
= client
.UserManagerFacade
.from_connection(self
.connection
)
180 entity
= client
.EntityPassword(password
, tag
.user(username
))
181 return await user_facade
.SetPassword([entity
])
183 async def destroy(self
, destroy_all_models
=False):
184 """Destroy this controller.
186 :param bool destroy_all_models: Destroy all hosted models in the
190 controller_facade
= client
.ControllerFacade
.from_connection(
192 return await controller_facade
.DestroyController(destroy_all_models
)
194 async def disable_user(self
, username
):
197 :param str username: Username
200 user_facade
= client
.UserManagerFacade
.from_connection(self
.connection
)
201 entity
= client
.Entity(tag
.user(username
))
202 return await user_facade
.DisableUser([entity
])
204 async def enable_user(self
, username
):
205 """Re-enable a previously disabled user.
208 user_facade
= client
.UserManagerFacade
.from_connection(self
.connection
)
209 entity
= client
.Entity(tag
.user(username
))
210 return await user_facade
.EnableUser([entity
])
213 """Forcibly terminate all machines and other associated resources for
217 raise NotImplementedError()
219 async def get_cloud(self
):
221 Get the name of the cloud that this controller lives on.
223 cloud_facade
= client
.CloudFacade
.from_connection(self
.connection
)
225 result
= await cloud_facade
.Clouds()
226 cloud
= list(result
.clouds
.keys())[0] # only lives on one cloud
227 return tag
.untag('cloud-', cloud
)
229 async def get_models(self
, all_
=False, username
=None):
230 """Return list of available models on this controller.
232 :param bool all_: List all models, regardless of user accessibilty
234 :param str username: User for which to list models (admin use only)
237 controller_facade
= client
.ControllerFacade
.from_connection(
239 return await controller_facade
.AllModels()
242 def get_payloads(self
, *patterns
):
243 """Return list of known payloads.
245 :param str \*patterns: Patterns to match against
247 Each pattern will be checked against the following info in Juju::
258 raise NotImplementedError()
260 def get_users(self
, all_
=False):
261 """Return list of users that can connect to this controller.
263 :param bool all_: Include disabled users
266 raise NotImplementedError()
269 """Log in to this controller.
272 raise NotImplementedError()
274 def logout(self
, force
=False):
275 """Log out of this controller.
277 :param bool force: Don't fail even if user not previously logged in
281 raise NotImplementedError()
283 def get_model(self
, name
):
284 """Get a model by name.
286 :param str name: Model name
289 raise NotImplementedError()
291 async def get_user(self
, username
, include_disabled
=False):
292 """Get a user by name.
294 :param str username: Username
297 client_facade
= client
.UserManagerFacade
.from_connection(
299 user
= tag
.user(username
)
300 return await client_facade
.UserInfo([client
.Entity(user
)], include_disabled
)
302 async def grant(self
, username
, acl
='login'):
303 """Set access level of the given user on the controller
305 :param str username: Username
306 :param str acl: Access control ('login', 'add-model' or 'superuser')
309 controller_facade
= client
.ControllerFacade
.from_connection(
311 user
= tag
.user(username
)
312 await self
.revoke(username
)
313 changes
= client
.ModifyControllerAccess(acl
, 'grant', user
)
314 return await controller_facade
.ModifyControllerAccess([changes
])
316 async def revoke(self
, username
):
317 """Removes all access from a controller
319 :param str username: username
322 controller_facade
= client
.ControllerFacade
.from_connection(
324 user
= tag
.user(username
)
325 changes
= client
.ModifyControllerAccess('login', 'revoke', user
)
326 return await controller_facade
.ModifyControllerAccess([changes
])