Merge lp:~xavpaice/landscape-client-charm/add_tags into lp:landscape-client-charm

Proposed by Xav Paice
Status: Rejected
Rejected by: Haw Loeung
Proposed branch: lp:~xavpaice/landscape-client-charm/add_tags
Merge into: lp:landscape-client-charm
Diff against target: 144 lines (+69/-2)
3 files modified
hooks/common.py (+7/-2)
hooks/hooks.py (+58/-0)
metadata.yaml (+4/-0)
To merge this branch: bzr merge lp:~xavpaice/landscape-client-charm/add_tags
Reviewer Review Type Date Requested Status
Landscape Pending
Review via email: mp+407578@code.launchpad.net

Commit message

Add list of Juju units as tags to client config, and a tags relation for consumption by https://launchpad.net/charm-launchpad-tagger

To post a comment you must log in.
74. By Xav Paice

Add Juju availabilty zone to tags

75. By Xav Paice

Add tags relation

Revision history for this message
Stephan Pampel (stephanpampel) :
Revision history for this message
Stephan Pampel (stephanpampel) wrote :

Added a comment

Revision history for this message
Stephan Pampel (stephanpampel) wrote :

Unmerged revisions

75. By Xav Paice

Add tags relation

74. By Xav Paice

Add Juju availabilty zone to tags

73. By Xav Paice

Add list of Juju units as tags to client config

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/common.py'
2--- hooks/common.py 2021-02-18 02:37:59 +0000
3+++ hooks/common.py 2021-08-25 21:10:55 +0000
4@@ -64,8 +64,13 @@
5 return check_output(["juju-log", message])
6
7 def _run_juju_tool(self, command):
8- """Run specified Juju tool and parse json output."""
9- output = check_output([command, "--format", "json"])
10+ """Run specified Juju tool and parse json output.
11+
12+ @param json: boolean, whether to return output formatted in json.
13+ @param switches: list, additional arguments to the command to be run
14+ """
15+ cmd = [command, "--format", "json"]
16+ output = check_output(cmd)
17 return json.loads(output) if output else {}
18
19
20
21=== modified file 'hooks/hooks.py'
22--- hooks/hooks.py 2019-04-04 16:07:48 +0000
23+++ hooks/hooks.py 2021-08-25 21:10:55 +0000
24@@ -1,4 +1,5 @@
25 import apt_pkg
26+import re
27 import sys
28 import socket
29 import os
30@@ -11,6 +12,7 @@
31 from subprocess import CalledProcessError
32
33 from charmhelpers import fetch
34+from charmhelpers.core.unitdata import kv
35 from charmhelpers.core import hookenv
36
37 from common import (
38@@ -21,6 +23,7 @@
39 from install import install
40
41 CERT_FILE = "/etc/ssl/certs/landscape_server_ca.crt"
42+JUJU_AGENT_DIR = "/var/lib/juju/agents"
43
44 JUJU_BROKER = JujuBroker()
45 LANDSCAPE_BROKER = LandscapeBroker()
46@@ -38,6 +41,32 @@
47 landscape_broker=landscape_broker)
48
49
50+@hooks.hook("tags-relation-changed")
51+def tags_relation(juju_broker=JUJU_BROKER, hookenv=hookenv):
52+ """Run hook to populate tags relation data."""
53+ tags_list = get_tags(hookenv.config())
54+
55+ for relation in hookenv.relation_ids("tags"):
56+ hookenv.relation_set(relation_id=relation,
57+ tags=",".join(tags_list),
58+ computer_title=socket.gethostname()
59+ )
60+
61+
62+@hooks.hook("update-status")
63+def update_status():
64+ """Update status hook."""
65+
66+ kvstore = kv()
67+ stored_tags = set(kvstore.get("tags"))
68+ current_tags = set(get_tags(hookenv.config()))
69+ if stored_tags == current_tags:
70+ # Assumption that we have already set relation data to match stored tags
71+ return
72+ config_changed()
73+ tags_relation()
74+
75+
76 @hooks.hook("config-changed")
77 def config_changed(relation_data=None, hookenv=hookenv, fetch=fetch,
78 landscape_broker=LANDSCAPE_BROKER):
79@@ -61,8 +90,16 @@
80 set_config[key] = value.strip()
81 else:
82 set_config[key] = value
83+ # Add tags for Juju units installed on this machine
84+ tags_list = get_tags(set_config)
85
86+ kvstore = kv()
87+ kvstore.set(key="tags", value=tags_list)
88+ kvstore.flush()
89+ set_config["tags"] = ",".join(tags_list)
90+ tags_relation()
91 relation_data.update(set_config)
92+
93 if service_config.get("disable-unattended-upgrades"):
94 Cnf = apt_pkg.Configuration()
95 apt_dir = '/etc/apt/apt.conf.d'
96@@ -72,13 +109,34 @@
97 hookenv.log("Disabling unattended upgrades")
98 with open(apt_dir + '/99landscapeoverride', 'w') as override_file:
99 override_file.write(disable_upgrades)
100+
101 if relation_data.get("ssl-public-key", "").startswith("base64:"):
102 _write_certificate(relation_data["ssl-public-key"][7:], CERT_FILE)
103 relation_data["ssl-public-key"] = CERT_FILE
104+
105 if "account-name" in relation_data:
106 return landscape_broker.update_client_config(relation_data)
107
108
109+def get_tags(config):
110+ """Return a list of tags to use for this unit."""
111+ tags_list = [tag.strip() for tag in config.get("tags").split(",")]
112+ tags_list.extend(juju_application_tags())
113+ zone = os.getenv("JUJU_AVAILABILITY_ZONE")
114+ if zone:
115+ tags_list.append(zone)
116+ return tags_list
117+
118+
119+def juju_application_tags():
120+ """Return a list of tags to add for each Juju application."""
121+ agent_dirs = os.listdir(JUJU_AGENT_DIR)
122+ regex = re.compile(r"^unit-(\w+-*\w*)-\d+")
123+ app_tags = [regex.match(agent)[1] for agent in
124+ agent_dirs if regex.match(agent)]
125+ return app_tags
126+
127+
128 @hooks.hook("upgrade-charm")
129 def upgrade_charm(landscape_broker=LANDSCAPE_BROKER):
130 """Idempotently upgrades state from older charms."""
131
132=== added symlink 'hooks/update-status'
133=== target is 'hooks'
134=== modified file 'metadata.yaml'
135--- metadata.yaml 2020-02-19 17:40:49 +0000
136+++ metadata.yaml 2021-08-25 21:10:55 +0000
137@@ -22,3 +22,7 @@
138 ceph-client:
139 interface: ceph-client
140 scope: container
141+provides:
142+ tags:
143+ interface: landscape-tags
144+

Subscribers

People subscribed via source and target branches

to all changes: