Skip to content

Commit 203caac

Browse files
committed
Add get_local_charm_base for charm_origin for local charms
1 parent e8cd614 commit 203caac

2 files changed

Lines changed: 76 additions & 4 deletions

File tree

juju/model.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ async def add_local_charm_dir(self, charm_dir, series):
824824
log.debug('Uploaded local charm: %s -> %s', charm_dir, charm_url)
825825
return charm_url
826826

827-
def add_local_charm(self, charm_file, series, size=None):
827+
def add_local_charm(self, charm_file, series="", size=None):
828828
"""Upload a local charm archive to the model.
829829
830830
Returns the 'local:...' url that should be used to deploy the charm.
@@ -1770,7 +1770,6 @@ async def deploy(
17701770
charm_origin = add_charm_res.get('charm_origin', res.origin)
17711771
else:
17721772
charm_origin = add_charm_res.charm_origin
1773-
17741773
if Schema.CHARM_HUB.matches(url.schema):
17751774
resources = await self._add_charmhub_resources(res.app_name,
17761775
identifier,
@@ -1789,11 +1788,24 @@ async def deploy(
17891788
charm_dir = os.path.abspath(
17901789
os.path.expanduser(identifier))
17911790
charm_origin = res.origin
1791+
17921792
metadata = utils.get_local_charm_metadata(charm_dir)
1793+
# TODO (cderici) : pass the metadata into get_charm_series, as
1794+
# it also reads that file redundantly
1795+
series = series or await get_charm_series(charm_dir, self)
1796+
1797+
# If we're using a newer client, then the CharmOrigin needs a
1798+
# base
1799+
if not self.connection().is_using_old_client:
1800+
charm_origin.base = utils.get_local_charm_base(series,
1801+
channel,
1802+
metadata,
1803+
charm_dir,
1804+
client.Base)
1805+
17931806
if not application_name:
17941807
application_name = metadata['name']
1795-
series = series or await get_charm_series(charm_dir, self)
1796-
if not series:
1808+
if self.connection().is_using_old_client and not series:
17971809
raise JujuError(
17981810
"Couldn't determine series for charm at {}. "
17991811
"Pass a 'series' kwarg to Model.deploy().".format(

juju/utils.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,63 @@ def get_version_series(version):
322322
raise errors.JujuError("Unknown version : %s", version)
323323
return list(UBUNTU_SERIES.keys())[list(UBUNTU_SERIES.values()).index(version)]
324324

325+
326+
def get_local_charm_base(series, channel_from_arg, charm_metadata,
327+
charm_path, baseCls):
328+
"""Deduce the base [channel/osname] of a local charm based on what we
329+
know already
330+
331+
:param str series: This may come from the argument or the metadata.yaml
332+
:param str channel_from_arg: This is channel passed as argument, if any.
333+
:param dict charm_metadata: metadata.yaml
334+
:param str charm_path: Path of charm directory/.charm file
335+
:param class baseCls:
336+
:return: Instance of the baseCls with channel/osname informaiton
337+
"""
338+
339+
channel_for_base = ''
340+
os_name_for_base = ''
341+
# If user passed a channel_arg, then check the supported series against
342+
# the channel's track
343+
chnl_check = origin.Channel.parse(channel_from_arg) if channel_from_arg \
344+
else None
345+
if chnl_check:
346+
not_supported_error = errors.JujuError(
347+
"Given channel [track/risk] is not supported --"
348+
"\n - Given channel : %s"
349+
"\n - Series in Charm Metadata : %s" %
350+
(channel_from_arg, charm_metadata['series']))
351+
channel_for_base = chnl_check.track
352+
intented_series = get_version_series(channel_for_base)
353+
if intented_series not in charm_metadata['series']:
354+
raise not_supported_error
355+
# Also check the manifest if there's one
356+
charm_manifest = get_local_charm_manifest(charm_path)
357+
if 'bases' in charm_manifest:
358+
for base in charm_manifest['bases']:
359+
if channel_for_base == base['channel']:
360+
break
361+
else:
362+
raise not_supported_error
363+
364+
# If we know the series, use it to get a channel
365+
if channel_for_base == '':
366+
channel_for_base = get_series_version(series) if series else ''
367+
if channel_for_base:
368+
# we currently only support ubuntu series (statically)
369+
# TODO (cderici) : go juju/core/series/supported.go and get the
370+
# others here too
371+
os_name_for_base = 'ubuntu'
372+
373+
# Check the charm manifest
374+
if channel_for_base == '':
375+
charm_manifest = get_local_charm_manifest(charm_path)
376+
if 'bases' in charm_manifest:
377+
channel_for_base = charm_manifest['bases'][0]['channel']
378+
os_name_for_base = charm_manifest['bases'][0]['name']
379+
380+
if channel_for_base == '':
381+
raise errors.JujuError("Unable to determine base for charm : %s" %
382+
charm_path)
383+
384+
return baseCls(channel_for_base, os_name_for_base)

0 commit comments

Comments
 (0)