Skip to content

Commit

Permalink
Adds resource support for charmhub deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
tlm committed Aug 17, 2021
1 parent ef05ad8 commit 13934c8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
35 changes: 35 additions & 0 deletions examples/charmhub_deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
This example:
1. Connects to the current model
2. Deploy a charm and waits until it reports itself active
3. Destroys the unit and application
"""
from juju import loop
from juju.model import Model


async def main():
model = Model()
print('Connecting to model')
await model.connect()

try:
print('Deploying ')
application = await model.deploy(
'ch:snappass-test',
application_name='snappass'
)

print('Waiting for active')
await model.block_until(
lambda: all(unit.workload_status == 'active'
for unit in application.units))

finally:
print('Disconnecting from model')
await model.disconnect()

if __name__ == '__main__':
loop.run(main())
9 changes: 7 additions & 2 deletions juju/client/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ class ResourcesFacade(Type):
"""

@ReturnMapping(_client.AddPendingResourcesResult)
async def AddPendingResources(self, application_tag="", charm_url="", resources=None):
async def AddPendingResources(self,
application_tag="",
charm_url="",
charm_origin=None,
resources=None):
"""Fix the calling signature of AddPendingResources.
The ResourcesFacade doesn't conform to the standard facade pattern in
Expand All @@ -81,11 +85,12 @@ async def AddPendingResources(self, application_tag="", charm_url="", resources=
_params = dict()
msg = dict(type='Resources',
request='AddPendingResources',
version=1,
version=2,
params=_params)
_params['tag'] = application_tag
_params['url'] = charm_url
_params['resources'] = resources
_params['charm-origin'] = charm_origin
reply = await self.rpc(msg)
return reply

Expand Down
51 changes: 47 additions & 4 deletions juju/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,10 +1595,13 @@ async def deploy(
# XXX: we're dropping local resources here, but we don't
# actually support them yet anyway
if not res.is_local:
await self._add_charm(identifier, res.origin)
add_charm_res = await self._add_charm(identifier, res.origin)

if Schema.CHARM_HUB.matches(url.schema):
resources = await self._add_charmhub_resources(res.app_name,
identifier,
add_charm_res.charm_origin)

# TODO (stickupkid): Handle charmhub charms, for now we'll only
# handle charmstore charms.
if Schema.CHARM_STORE.matches(url.schema):
resources = await self._add_store_resources(res.app_name,
identifier)
Expand Down Expand Up @@ -1632,6 +1635,7 @@ async def deploy(
num_units=num_units,
placement=parse_placement(to),
devices=devices,
charm_origin=add_charm_res.charm_origin,
)

async def _add_charm(self, charm_url, origin):
Expand Down Expand Up @@ -1684,6 +1688,44 @@ async def _resolve_architecture(self, url):

return DEFAULT_ARCHITECTURE

async def _add_charmhub_resources(self, application,
entity_url,
origin,
overrides=None):
charm_facade = client.CharmsFacade.from_connection(self.connection())
res = await charm_facade.CharmInfo(entity_url)

resources = []
for resource in res.meta.resources.values():
print("%^%%%%%%%%%%%%%%%%%")
print("%^%%%%%%%%%%%%%%%%%")
print("%^%%%%%%%%%%%%%%%%%")
print("%^%%%%%%%%%%%%%%%%%")
print("%^%%%%%%%%%%%%%%%%%")
print(res)
resources.append({
'description': resource.description,
'name': resource.name,
'path': resource.path,
'type_': resource.type_,
'origin': 'store',
'revision': -1,
})

resources_facade = client.ResourcesFacade.from_connection(
self.connection())
response = await resources_facade.AddPendingResources(
application_tag=tag.application(application),
charm_url=entity_url,
charm_origin=origin,
resources=[client.CharmResource(**resource) for resource in resources],
)

resource_map = {resource['name']: pid
for resource, pid
in zip(resources, response.pending_ids)}
return resource_map

async def _add_store_resources(self, application, entity_url,
overrides=None):
entity = await self.charmstore.entity(entity_url,
Expand Down Expand Up @@ -1729,7 +1771,7 @@ async def _add_store_resources(self, application, entity_url,
async def _deploy(self, charm_url, application, series, config,
constraints, endpoint_bindings, resources, storage,
channel=None, num_units=None, placement=None,
devices=None):
devices=None, charm_origin=None):
"""Logic shared between `Model.deploy` and `BundleHandler.deploy`.
"""
log.info('Deploying %s', charm_url)
Expand All @@ -1747,6 +1789,7 @@ async def _deploy(self, charm_url, application, series, config,
application=application,
series=series,
channel=channel,
charm_origin=charm_origin,
config_yaml=config,
constraints=parse_constraints(constraints),
endpoint_bindings=endpoint_bindings,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async def test_deploy_charmstore_charm(event_loop):
@pytest.mark.asyncio
async def test_deploy_charmhub_charm(event_loop):
async with base.CleanModel() as model:
app = await model.deploy('hello-juju')
app = await model.deploy('ch:hello-juju')
await model.block_until(lambda: (len(app.units) > 0 and
app.units[0].machine))
assert 'hello-juju' in app.data['charm-url']
Expand Down

0 comments on commit 13934c8

Please sign in to comment.