Add support for juju resources

Simplestreams package is now installed via snap. By having the juju
resources for the snap package, it can benefit in offline environment.

Closes-Bug: 1921832
Change-Id: I5f21ca9faff35427281076e142942c3458e71091
This commit is contained in:
Yoshi Kadokawa 2021-08-26 18:35:36 +09:00 committed by Nobuto Murata
parent 60ceb7c5a1
commit bf23ef75b2
4 changed files with 47 additions and 2 deletions

View File

@ -70,6 +70,16 @@ Juju [actions][juju-docs-actions] allow specific operations to be performed on
a per-unit basis. This charm supports the single action `sync-images`, which
allows for a one-time image sync from the currently configured mirror list.
## Juju resources
The charm support juju resources, which is handy in offline deployments. Prefetch the snaps:
snap download --channel=stable simplestreams
Provide downloaded snaps as resources to the application:
juju deploy cs:glance-simplestreams-sync --resource simplestreams=simplestreams_27.snap
# Bugs
Please report bugs on [Launchpad][lp-bugs-charm-glance-simplestreams-sync].

View File

@ -310,8 +310,14 @@ def install():
apt_install(_packages)
snap_install('simplestreams',
*['--channel={}'.format(hookenv.config('snap-channel'))])
snap = 'simplestreams'
res_path = _resource_get(snap)
if res_path is None:
snap_install(snap,
*['--channel={}'.format(hookenv.config('snap-channel'))])
else:
snap_install(res_path,
*['--dangerous'])
install_gss_wrappers()
@ -411,6 +417,19 @@ def certs_changed(relation_id=None, unit=None):
identity_service_changed()
def _resource_get(snapname):
"""Used to fetch the resource path of the given name.
This wrapper obtains a resource path and adds an additional
check to return False if the resource is zero length.
"""
res_path = hookenv.resource_get(snapname)
if res_path and os.stat(res_path).st_size != 0:
return res_path
return None
if __name__ == '__main__':
try:
hooks.execute(sys.argv)

View File

@ -29,3 +29,8 @@ requires:
interface: keystone
certificates:
interface: tls-certificates
resources:
simplestreams:
type: file
filename: simplestreams.snap
description: simplestreams snap

View File

@ -105,3 +105,14 @@ class TestConfigChanged(CharmTestCase):
hooks.CRON_JOB_FILENAME))
remove.assert_any_call(hooks.CRON_POLL_FILEPATH)
update_nrpe_config.assert_called()
@mock.patch("charmhelpers.core.hookenv.resource_get")
@mock.patch("os.stat")
def test_resource_get_simplestreams(self, mock_os_stat, mock_resource_get):
mock_path = "/tmp/file"
mock_resource_get.return_value = mock_path
mock_os_stat.return_value.st_size = 100
self.assertEqual(hooks._resource_get('simplestreams'), mock_path)
mock_os_stat.return_value.st_size = 0
self.assertEqual(hooks._resource_get('simplestreams'), None)