Merge ~hloeung/ubuntu-repository-cache:master into ubuntu-repository-cache:master

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 9f85be2b03aa4c64c2bcd7571114128e7d3044af
Merged at revision: f0c23229db01e90e52bba9ac3f660117d645229a
Proposed branch: ~hloeung/ubuntu-repository-cache:master
Merge into: ubuntu-repository-cache:master
Diff against target: 107 lines (+62/-2)
5 files modified
lib/ubuntu_repository_cache/service.py (+1/-0)
lib/ubuntu_repository_cache/util.py (+11/-0)
templates/cron/ubuntu-repository-cache-cron (+2/-2)
tests/unit/files/cloud-id-azure (+1/-0)
tests/unit/test_util.py (+47/-0)
Reviewer Review Type Date Requested Status
James Simpson Approve
Canonical IS Reviewers Pending
Review via email: mp+432106@code.launchpad.net

Commit message

Include cloud name in u-r-c cron emails

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
James Simpson (jsimpso) wrote :

LGTM

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision f0c23229db01e90e52bba9ac3f660117d645229a

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/ubuntu_repository_cache/service.py b/lib/ubuntu_repository_cache/service.py
2index 615c407..c6cec42 100644
3--- a/lib/ubuntu_repository_cache/service.py
4+++ b/lib/ubuntu_repository_cache/service.py
5@@ -291,6 +291,7 @@ def render_configs():
6 cron_context['MirrorSeries'] = config['mirror-series'].strip()
7 cron_context['ConfigPath'] = urc_config_path
8 cron_context['CharmEnvPath'] = os.path.join(hookenv.charm_dir(), 'bin', 'charm-env')
9+ cron_context['Cloud'] = util.get_cloud_name()
10 # Add Juju model name and AZ so it's easier to work out which region sent out cron emails.
11 cron_context['JujuModelName'] = os.environ.get('JUJU_MODEL_NAME')
12 cron_context['JujuAvailabilityZone'] = os.environ.get('JUJU_AVAILABILITY_ZONE')
13diff --git a/lib/ubuntu_repository_cache/util.py b/lib/ubuntu_repository_cache/util.py
14index 297a297..c90817c 100644
15--- a/lib/ubuntu_repository_cache/util.py
16+++ b/lib/ubuntu_repository_cache/util.py
17@@ -277,3 +277,14 @@ def send_to_influx(metrics, send_to='127.0.0.1:8094'):
18 except Exception:
19 # We don't care if it fails to send metrics.
20 pass
21+
22+
23+def get_cloud_name(cloud_init_path='/run/cloud-init'):
24+ cloud_id_path = os.path.join(cloud_init_path, 'cloud-id')
25+
26+ try:
27+ with open(cloud_id_path, 'r') as f:
28+ cloud_name = f.readline()
29+ return cloud_name.strip()
30+ except FileNotFoundError:
31+ return ''
32diff --git a/templates/cron/ubuntu-repository-cache-cron b/templates/cron/ubuntu-repository-cache-cron
33index 186c6cc..9368631 100644
34--- a/templates/cron/ubuntu-repository-cache-cron
35+++ b/templates/cron/ubuntu-repository-cache-cron
36@@ -4,9 +4,9 @@ JUJU_AVAILABILITY_ZONE={{ JujuAvailabilityZone }}
37 {%- endif %}
38 {%- if Peers != '' %}
39 # This cronjob will make the leader sync its view of the metadata with upstream
40-{{ Minutes }} * * * * root . {{ ConfigPath }} && run-one {{ CharmEnvPath }} --charm ubuntu-repository-cache python3 -m ubuntu_repository_cache.metadata_sync # {{ JujuModelName }} {% if JujuAvailabilityZone %}{{ JujuAvailabilityZone }}{% endif %}
41+{{ Minutes }} * * * * root . {{ ConfigPath }} && run-one {{ CharmEnvPath }} --charm ubuntu-repository-cache python3 -m ubuntu_repository_cache.metadata_sync # {{ JujuModelName }} {{ Cloud }}{% if JujuAvailabilityZone %} {{ JujuAvailabilityZone }}{% endif %}
42
43 {% else %}
44-{{ Minutes }} * * * * root . {{ ConfigPath }} && run-one {{ CharmEnvPath }} --charm ubuntu-repository-cache python3 -m ubuntu_repository_cache.metadata_cleanup # {{ JujuModelName }} {% if JujuAvailabilityZone %}{{ JujuAvailabilityZone }}{% endif %}
45+{{ Minutes }} * * * * root . {{ ConfigPath }} && run-one {{ CharmEnvPath }} --charm ubuntu-repository-cache python3 -m ubuntu_repository_cache.metadata_cleanup # {{ JujuModelName }} {{ Cloud }}{% if JujuAvailabilityZone %} {{ JujuAvailabilityZone }}{% endif %}
46
47 {% endif %}
48diff --git a/tests/unit/files/cloud-id-azure b/tests/unit/files/cloud-id-azure
49new file mode 100644
50index 0000000..9271abf
51--- /dev/null
52+++ b/tests/unit/files/cloud-id-azure
53@@ -0,0 +1 @@
54+azure
55diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py
56new file mode 100644
57index 0000000..86ee8db
58--- /dev/null
59+++ b/tests/unit/test_util.py
60@@ -0,0 +1,47 @@
61+import os
62+import shutil
63+import sys
64+import tempfile
65+import unittest
66+
67+from unittest import mock
68+
69+# Add path to where our reactive layer lives and import.
70+sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
71+from lib.ubuntu_repository_cache import util # NOQA: E402
72+
73+
74+class TestCharm(unittest.TestCase):
75+ def setUp(self):
76+ self.maxDiff = None
77+ self.tmpdir = tempfile.mkdtemp(prefix='charm-unittests-')
78+ self.addCleanup(shutil.rmtree, self.tmpdir)
79+
80+ self.charm_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
81+
82+ patcher = mock.patch('charmhelpers.core.hookenv.log')
83+ self.mock_log = patcher.start()
84+ self.addCleanup(patcher.stop)
85+ self.mock_log.return_value = ''
86+
87+ patcher = mock.patch('charmhelpers.core.hookenv.charm_dir')
88+ self.mock_charm_dir = patcher.start()
89+ self.addCleanup(patcher.stop)
90+ self.mock_charm_dir.return_value = self.charm_dir
91+
92+ patcher = mock.patch('charmhelpers.core.hookenv.local_unit')
93+ self.mock_local_unit = patcher.start()
94+ self.addCleanup(patcher.stop)
95+ self.mock_local_unit.return_value = 'mock-ubuntu-repository-cache/0'
96+
97+ patcher = mock.patch('charmhelpers.core.host.log')
98+ self.mock_log = patcher.start()
99+ self.addCleanup(patcher.stop)
100+ self.mock_log.return_value = ''
101+
102+ def test_get_cloud_name(self):
103+ self.assertEqual(util.get_cloud_name(self.tmpdir), '')
104+
105+ shutil.copy2('tests/unit/files/cloud-id-azure', self.tmpdir)
106+ os.symlink(os.path.join(self.tmpdir, 'cloud-id-azure'), os.path.join(self.tmpdir, 'cloud-id'))
107+ self.assertEqual(util.get_cloud_name(self.tmpdir), 'azure')

Subscribers

People subscribed via source and target branches