Skip to content

Commit

Permalink
provide true charm origin for charmhub to locate and deploy charms fr…
Browse files Browse the repository at this point in the history
…om bundles

fixes juju#568

Also adds the example from juju#567

closes juju#567
  • Loading branch information
cderici committed Oct 26, 2021
1 parent 1eba708 commit e74c334
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
33 changes: 33 additions & 0 deletions examples/deploy_bundle_charmhub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
This example:
1. Connects to the current model
2. Deploy a bundle from charmhub 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")
# connect to current model with current user, per Juju CLI
await model.connect()

try:
print("Deploying lma-light")
applications = await model.deploy("ch:lma-light", channel="edge", trust=True)

print("Waiting for active")
await model.wait_for_idle(status="active")

print("Removing lma-light")
for application in applications:
await application.remove()
finally:
print("Disconnecting from model")
await model.disconnect()


if __name__ == "__main__":
loop.run(main())
27 changes: 17 additions & 10 deletions juju/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .client import client
from .constraints import parse as parse_constraints, parse_storage_constraint, parse_device_constraint
from .errors import JujuError
from . import utils, jasyncio
from . import utils, jasyncio, charmhub
from .origin import Channel
from .url import Schema, URL

Expand Down Expand Up @@ -488,15 +488,6 @@ async def run(self, context):
options["trust"] = "true"

url = URL.parse(str(charm))
if Schema.CHARM_STORE.matches(url.schema):
resources = await context.model._add_store_resources(
self.application, charm, overrides=self.resources)
elif Schema.CHARM_HUB.matches(url.schema):
resources = await context.model._add_charmhub_resources(
self.application, charm, 'store', overrides=self.resources)
else:
resources = context.bundle.get("applications", {}).get(self.application, {}).get("resources", {})

channel = None
if self.channel is not None and self.channel != "":
channel = Channel.parse(self.channel).normalize()
Expand All @@ -509,6 +500,20 @@ async def run(self, context):
self.series = context.bundle.get("bundle",
context.bundle.get("series", None))

if Schema.CHARM_STORE.matches(url.schema):
resources = await context.model._add_store_resources(
self.application, charm, overrides=self.resources)
elif Schema.CHARM_HUB.matches(url.schema):
c_hub = charmhub.CharmHub(context.model)
info = await c_hub.info(url.name, channel=self.channel)
if info.errors.error_list.code:
raise JujuError("unable to resolve the charm {} with channel {}".format(url.name, channel))
origin.id_ = info.result.id_
resources = await context.model._add_charmhub_resources(
self.application, charm, origin, overrides=self.resources)
else:
resources = context.bundle.get("applications", {}).get(self.application, {}).get("resources", {})

await context.model._deploy(
charm_url=charm,
application=self.application,
Expand All @@ -518,8 +523,10 @@ async def run(self, context):
endpoint_bindings=self.endpoint_bindings,
resources=resources,
storage=self.storage,
channel=self.channel,
devices=self.devices,
num_units=self.num_units,
charm_origin=origin,
)
return self.application

Expand Down

0 comments on commit e74c334

Please sign in to comment.