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

Subscribers

People subscribed via source and target branches

to all changes: