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 1 commit
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
43 changes: 43 additions & 0 deletions examples/upgrade_local_charm_k8s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
This example:

1. Connects to the current model
2. Deploy a bundle and waits until it reports itself active
3. Destroys the units and applications
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing: upgrades the charm from local path.


"""
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!")

await applications[0].upgrade_charm(path='./examples/charms/onos.charm')

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
24 changes: 15 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,16 @@ 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:
model_config = await model.get_config()
default_series = model_config.get("default-series")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my case I have a k8s charm, but the "default-series" it has stored is:
<ConfigValue source='default' value='focal'>
Is that normal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's normal, that's why we get the .value out of it (#L440), but we need to check if it's None first, though maybe I can make the coding a bit clearer there.

if default_series:
series = default_series.value

return series


class ChangeSet:
Expand Down