Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JUJU-367] Improve get_charm_series to check the model for series for a local charm #607

Merged
merged 5 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions examples/upgrade_local_charm_k8s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
This example:

1. Connects to the current model
2. Deploy a bundle and waits until it reports itself active
3. Upgrades the charm with a local path
4. Destroys the units and applications

"""
from juju import jasyncio
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 bundle')
applications = await model.deploy(
'./examples/k8s-local-bundle/bundle.yaml',
)

print('Waiting for active')
await model.wait_for_idle(status='active')
print("Successfully deployed!")

local_path = './examples/charms/onos.charm'
print('Upgrading charm with %s' % local_path)
await applications[0].upgrade_charm(path=local_path)

await model.wait_for_idle(status='active')

print('Removing bundle')
for application in applications:
await application.remove()
finally:
print('Disconnecting from model')
await model.disconnect()
print("Success")


if __name__ == '__main__':
jasyncio.run(main())
2 changes: 1 addition & 1 deletion juju/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ async def local_refresh(
charm_dir = path.expanduser().resolve()
model_config = await self.get_config()

series = get_charm_series(charm_dir)
series = await get_charm_series(charm_dir, self.model)
if not series:
model_config = await self.get_config()
default_series = model_config.get("default-series")
Expand Down
27 changes: 18 additions & 9 deletions juju/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,8 @@ async def _handle_local_charms(self, bundle, bundle_dir):
series = (
app_dict.get('series') or
default_series or
get_charm_series(charm_dir)
await get_charm_series(charm_dir, self.model)
)
if not series:
model_config = await self.model.get_config()
default_series = model_config.get("default-series")
if default_series:
default_series = series = default_series.value
if not series:
raise JujuError(
"Couldn't determine series for charm at {}. "
Expand Down Expand Up @@ -410,11 +405,14 @@ def is_local_charm(charm_url):
return charm_url.startswith('.') or charm_url.startswith('local:') or os.path.isabs(charm_url)


def get_charm_series(path):
async def get_charm_series(path, model):
"""Inspects the charm directory at ``path`` and returns a default
series from its metadata.yaml (the first item in the 'series' list).

Tries to extract the informiation from the given model if no
series is determined from the path.
Returns None if no series can be determined.

"""
path = Path(path)
try:
Expand All @@ -432,8 +430,19 @@ def get_charm_series(path):
mark = exc.problem_mark
log.error("Error parsing YAML file {}, line {}, column: {}".format(md, mark.line, mark.column))
raise
series = data.get('series')
return series[0] if series else None
_series = data.get('series')
series = _series[0] if _series else None

if series is None:
# get the ConfigValue for the 'default-series' from the model
model_config = await model.get_config()
_default_series = model_config.get("default-series")

if _default_series is not None:
# then update the series with its value
series = _default_series.value

return series


class ChangeSet:
Expand Down
2 changes: 1 addition & 1 deletion juju/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ async def deploy(
metadata = utils.get_local_charm_metadata(charm_dir)
if not application_name:
application_name = metadata['name']
series = series or get_charm_series(charm_dir)
series = series or await get_charm_series(charm_dir, self)
if not series:
model_config = await self.get_config()
default_series = model_config.get("default-series")
Expand Down