From bf23ef75b257b0cf1e00c9e7850f8bf10d0378b2 Mon Sep 17 00:00:00 2001 From: Yoshi Kadokawa Date: Thu, 26 Aug 2021 18:35:36 +0900 Subject: [PATCH] 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 --- README.md | 10 ++++++++++ hooks/hooks.py | 23 +++++++++++++++++++++-- metadata.yaml | 5 +++++ unit_tests/test_hooks.py | 11 +++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dd8c2a5..95a9d41 100644 --- a/README.md +++ b/README.md @@ -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]. diff --git a/hooks/hooks.py b/hooks/hooks.py index 120b231..83086df 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -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) diff --git a/metadata.yaml b/metadata.yaml index a45444c..65a9574 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -29,3 +29,8 @@ requires: interface: keystone certificates: interface: tls-certificates +resources: + simplestreams: + type: file + filename: simplestreams.snap + description: simplestreams snap diff --git a/unit_tests/test_hooks.py b/unit_tests/test_hooks.py index ef57ba3..5256d97 100644 --- a/unit_tests/test_hooks.py +++ b/unit_tests/test_hooks.py @@ -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)