Skip to content

Commit d75b1f9

Browse files
authored
Merge pull request #873 from cderici/cleaning-tidying-up
#873 #### Description Small change that updates the example in the readme and adds some docstrings for `model.py` and `bundle.py`. #### QA Steps No change in any functionality, so no QA is needed. Just some proof read, maybe.
2 parents b584d27 + 263fa3e commit d75b1f9

3 files changed

Lines changed: 66 additions & 15 deletions

File tree

docs/readme.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ubuntu charm, then exits:
4848
import logging
4949
import sys
5050
51-
from juju import jasyncio
51+
from juju import asyncio
5252
from juju.model import Model
5353
5454
@@ -58,21 +58,20 @@ ubuntu charm, then exits:
5858
model = Model()
5959
6060
# Connect to the currently active Juju model
61-
await model.connect_current()
61+
await model.connect()
6262
6363
try:
6464
# Deploy a single unit of the ubuntu charm, using the latest revision
6565
# from the stable channel of the Charm Store.
6666
ubuntu_app = await model.deploy(
6767
'ubuntu',
6868
application_name='ubuntu',
69-
series='xenial',
70-
channel='stable',
7169
)
7270
7371
if '--wait' in sys.argv:
7472
# optionally block until the application is ready
75-
await model.block_until(lambda: ubuntu_app.status == 'active')
73+
await model.wait_for_idle(status = 'active')
74+
7675
finally:
7776
# Disconnect from the api server and cleanup.
7877
await model.disconnect()
@@ -87,7 +86,7 @@ ubuntu charm, then exits:
8786
8887
# Run the deploy coroutine in an asyncio event loop, using a helper
8988
# that abstracts loop creation and teardown.
90-
jasyncio.run(deploy())
89+
asyncio.run(deploy())
9190
9291
9392
if __name__ == '__main__':

juju/bundle.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,24 @@ def _resolve_include_file_config(self, bundle_dir):
223223

224224
return self.bundle, self.overlays
225225

226-
async def fetch_plan(self, charm_url, origin, overlays=[]):
227-
entity_id = charm_url.path()
228-
is_local = Schema.LOCAL.matches(charm_url.schema)
226+
async def fetch_plan(self, bundle_url, origin, overlays=[]):
227+
"""fetch_plan is called by the model.deploy(). It gathers the information about the
228+
bundle to be deployed (whether local or CharmHub), straightens it up, applies overlays
229+
if any overlays are given. Validates the bundle against known issues. Resolves and adds
230+
local charms if there's any in the bundle. Resolves and adds --include-file configs if
231+
there's any. Finally it calls the BundleFacade.GetChanges() to get the plan for the
232+
bundle to be handed to the execute_plan() by the model.deploy(). Note that it doesn't
233+
return the plan, just saves it in the self (BundleHandler) to be used later.
234+
235+
:param client.URL bundle_url: the url of the bundle to be deployed
236+
:param client.CharmOrigin origin: the origin of the bundle to be deployed
237+
:param [string] overlays: paths for the yaml files containing overlays to be applied to
238+
the bundle during deployment
239+
240+
:returns: None
241+
"""
242+
entity_id = bundle_url.path()
243+
is_local = Schema.LOCAL.matches(bundle_url.schema)
229244
bundle_dir = None
230245

231246
if is_local and os.path.isfile(entity_id):
@@ -235,8 +250,8 @@ async def fetch_plan(self, charm_url, origin, overlays=[]):
235250
bundle_yaml = (Path(entity_id) / "bundle.yaml").read_text()
236251
bundle_dir = Path(entity_id)
237252

238-
if Schema.CHARM_HUB.matches(charm_url.schema):
239-
bundle_yaml = await self._download_bundle(charm_url, origin)
253+
if Schema.CHARM_HUB.matches(bundle_url.schema):
254+
bundle_yaml = await self._download_bundle(bundle_url, origin)
240255

241256
if not bundle_yaml:
242257
raise JujuError('empty bundle, nothing to deploy')

juju/model.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,17 +1675,15 @@ async def deploy(
16751675
'24/lxd/3' - place in container 3 on machine 24
16761676
16771677
If None, a new machine is provisioned.
1678+
:param devices: charm device constraints
16781679
:param bool trust: Trust signifies that the charm should be deployed
16791680
with access to trusted credentials. Hooks run by the charm can access
16801681
cloud credentials and other trusted access credentials.
16811682
16821683
:param str[] attach_storage: Existing storage to attach to the deployed unit
16831684
(not available on k8s models)
1684-
TODO::
1685-
1686-
- support local file resources
1687-
16881685
"""
1686+
16891687
if storage:
16901688
storage = {
16911689
k: client.Constraints(**v)
@@ -1817,6 +1815,15 @@ async def deploy(
18171815
)
18181816

18191817
async def _add_charm(self, charm_url, origin):
1818+
"""_add_charm sends the given origin and the url to the Juju API too add the charm to the
1819+
state. Either calls the CharmsFacade.AddCharm for (> version 2), or the
1820+
ClientFacade.AddCharm (for older versions).
1821+
1822+
:param str charm_url: the url of the charm to be added
1823+
:param client.CharmOrigin origin: the origin for the charm to be added
1824+
1825+
:returns client.CharmOriginResult
1826+
"""
18201827
# client facade is deprecated with in Juju, and smaller, more focused
18211828
# facades have been created and we'll use that if it's available.
18221829
charms_cls = client.CharmsFacade
@@ -1880,6 +1887,12 @@ async def _resolve_charm(self, url, origin, force=False, series=None, model_conf
18801887
return str(charm_url), resolved_origin
18811888

18821889
async def _resolve_architecture(self, url):
1890+
"""_resolve_architecture returns the architecture for a given charm url.
1891+
:param str url: the client.URL to determine the arch for
1892+
1893+
:returns str architecture for the given url
1894+
"""
1895+
18831896
if url.architecture:
18841897
return url.architecture
18851898

@@ -1893,6 +1906,16 @@ async def _add_charmhub_resources(self, application,
18931906
entity_url,
18941907
origin,
18951908
overrides=None):
1909+
"""_add_charmhub_resources is called by the deploy to add pending resources requested by
1910+
the charm being deployed. It calls the ResourcesFacade.AddPendingResources.
1911+
1912+
:param str application: the name of the application
1913+
:param client.CharmURL entity_url: url for the charm that we add resources for
1914+
:param client.CharmOrigin origin: origin for the charm that we add resources for
1915+
1916+
:returns [string]string resource_map that is a map of resources to their assigned
1917+
pendingIDs.
1918+
"""
18961919
charm_facade = client.CharmsFacade.from_connection(self.connection())
18971920
res = await charm_facade.CharmInfo(entity_url)
18981921

@@ -1936,6 +1959,20 @@ async def _add_charmhub_resources(self, application,
19361959
return resource_map
19371960

19381961
async def add_local_resources(self, application, entity_url, metadata, resources):
1962+
"""_add_local_resources is called by the deploy to add pending local resources requested by
1963+
the charm being deployed. It calls the ResourcesFacade.AddPendingResources. After getting
1964+
the pending IDs from the controller it sends an HTTP PUT request to actually upload local
1965+
resources.
1966+
1967+
:param str application: the name of the application
1968+
:param client.CharmURL entity_url: url for the charm that we add resources for
1969+
:param [string]string metadata: metadata for the charm that we add resources for
1970+
:param [string] resources: the paths for the local files (or oci-images) to be added as
1971+
local resources
1972+
1973+
:returns [string]string resource_map that is a map of resources to their assigned
1974+
pendingIDs.
1975+
"""
19391976
if not resources:
19401977
return None
19411978

0 commit comments

Comments
 (0)