15 log
= logging
.getLogger("websocket")
22 # Connect to an arbitrary api server
23 client = await Connection.connect(
24 api_endpoint, model_uuid, username, password, cacert)
26 # Connect using a controller/model name
27 client = await Connection.connect_model('local.local:default')
29 # Connect to the currently active model
30 client = await Connection.connect_current()
33 def __init__(self
, endpoint
, uuid
, username
, password
, cacert
=None):
34 self
.endpoint
= endpoint
36 self
.username
= username
37 self
.password
= password
40 self
.__request
_id
__ = 0
45 def _get_ssl(self
, cert
):
46 return ssl
.create_default_context(
47 purpose
=ssl
.Purpose
.CLIENT_AUTH
, cadata
=cert
)
49 async def open(self
, addr
, cert
=None):
52 kw
['ssl'] = self
._get
_ssl
(cert
)
54 self
.ws
= await websockets
.connect(addr
, **kw
)
57 async def close(self
):
61 result
= await self
.ws
.recv()
62 if result
is not None:
63 result
= json
.loads(result
)
66 async def rpc(self
, msg
, encoder
=None):
67 self
.__request
_id
__ += 1
68 msg
['request-id'] = self
.__request
_id
__
69 if'params' not in msg
:
71 if "version" not in msg
:
72 msg
['version'] = self
.facades
[msg
['Type']]
73 outgoing
= json
.dumps(msg
, indent
=2, cls
=encoder
)
74 await self
.ws
.send(outgoing
)
75 result
= await self
.recv()
76 log
.debug("send %s got %s", msg
, result
)
77 if result
and 'error' in result
:
78 raise RuntimeError(result
)
81 async def clone(self
):
82 """Return a new Connection, connected to the same websocket endpoint
86 return await Connection
.connect(
95 async def connect(cls
, endpoint
, uuid
, username
, password
, cacert
=None):
96 url
= "wss://{}/model/{}/api".format(endpoint
, uuid
)
97 client
= cls(endpoint
, uuid
, username
, password
, cacert
)
98 await client
.open(url
, cacert
)
99 server_info
= await client
.login(username
, password
)
100 client
.build_facades(server_info
['facades'])
101 log
.info("Driver connected to juju %s", endpoint
)
106 async def connect_current(cls
):
107 """Connect to the currently active model.
110 jujudata
= JujuData()
111 controller_name
= jujudata
.current_controller()
112 controller
= jujudata
.controllers()[controller_name
]
113 endpoint
= controller
['api-endpoints'][0]
114 cacert
= controller
.get('ca-cert')
115 accounts
= jujudata
.accounts()[controller_name
]
116 username
= accounts
['current-account']
117 password
= accounts
['accounts'][username
]['password']
118 models
= jujudata
.models()[controller_name
]['accounts'][username
]
119 model_name
= models
['current-model']
120 model_uuid
= models
['models'][model_name
]['uuid']
122 return await cls
.connect(
123 endpoint
, model_uuid
, username
, password
, cacert
)
126 async def connect_model(cls
, model
):
127 """Connect to a model by name.
129 :param str model: <controller>:<model>
132 controller_name
, model_name
= model
.split(':')
134 jujudata
= JujuData()
135 controller
= jujudata
.controllers()[controller_name
]
136 endpoint
= controller
['api-endpoints'][0]
137 cacert
= controller
.get('ca-cert')
138 accounts
= jujudata
.accounts()[controller_name
]
139 username
= accounts
['current-account']
140 password
= accounts
['accounts'][username
]['password']
141 models
= jujudata
.models()[controller_name
]['accounts'][username
]
142 model_uuid
= models
['models'][model_name
]['uuid']
144 return await cls
.connect(
145 endpoint
, model_uuid
, username
, password
, cacert
)
147 def build_facades(self
, info
):
150 self
.facades
[facade
['name']] = facade
['versions'][-1]
152 async def login(self
, username
, password
):
153 if not username
.startswith('user-'):
154 username
= 'user-{}'.format(username
)
156 result
= await self
.rpc({
161 "auth-tag": username
,
162 "credentials": password
,
163 "nonce": "".join(random
.sample(string
.printable
, 12)),
165 return result
['response']
170 self
.path
= os
.environ
.get('JUJU_DATA') or '~/.local/share/juju'
171 self
.path
= os
.path
.abspath(os
.path
.expanduser(self
.path
))
173 def current_controller(self
):
174 cmd
= shlex
.split('juju show-controller --format yaml')
175 output
= subprocess
.check_output(cmd
)
176 output
= yaml
.safe_load(output
)
177 return list(output
.keys())[0]
179 def controllers(self
):
180 return self
._load
_yaml
('controllers.yaml', 'controllers')
183 return self
._load
_yaml
('models.yaml', 'controllers')
186 return self
._load
_yaml
('accounts.yaml', 'controllers')
188 def _load_yaml(self
, filename
, key
):
189 filepath
= os
.path
.join(self
.path
, filename
)
190 with io
.open(filepath
, 'rt') as f
:
191 return yaml
.safe_load(f
)[key
]