Merge ~aieri/charm-juju-lxd/+git/charm-juju-lxd:fix/109728 into charm-juju-lxd:master

Proposed by Andrea Ieri
Status: Merged
Merged at revision: 2e18adb772cced0f12a85e39d1315e112ce0f8ab
Proposed branch: ~aieri/charm-juju-lxd/+git/charm-juju-lxd:fix/109728
Merge into: charm-juju-lxd:master
Diff against target: 88 lines (+25/-37)
1 file modified
reactive/juju_lxd.py (+25/-37)
Reviewer Review Type Date Requested Status
Alvaro Uria (community) Approve
Review via email: mp+341347@code.launchpad.net

Description of the change

This commit fixes RT 109728 by making sure that the group specified as lxd-group in the charm configuration will be fully synced with group lxd, i.e. after a config change both groups will contain exactly the same users.
If lxd-group is set to an empty string, all users in group lxd will be removed.

To post a comment you must log in.
Revision history for this message
Alvaro Uria (aluria) wrote :

lgtm

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reactive/juju_lxd.py b/reactive/juju_lxd.py
2index 15250c9..c48a917 100644
3--- a/reactive/juju_lxd.py
4+++ b/reactive/juju_lxd.py
5@@ -19,9 +19,10 @@
6 from charms.reactive import when, when_not, set_state, remove_state
7 from charms.reactive.helpers import data_changed
8 from charmhelpers.core import hookenv
9-from charmhelpers.core.host import add_user_to_group
10+from charmhelpers.core.host import group_exists
11 import subprocess
12 import os
13+import grp
14
15
16 @when_not('juju-lxd.installed')
17@@ -49,15 +50,21 @@ def status_set(state, message):
18 @when_not('juju-lxd.configured')
19 def juju_lxd_configure():
20 config = hookenv.config()
21- group = config.get('lxd-group')
22+ src_group = config.get('lxd-group')
23+ dst_group = 'lxd'
24
25- if group is not None and group != '' and \
26- is_valid_group(group):
27-
28- add_group_to_lxd(group)
29- data_changed('juju-lxd.config', config)
30- set_state('juju-lxd.configured')
31- status_set('active', 'Ready')
32+ if group_exists(dst_group):
33+ try:
34+ sync_groups(src=src_group, dst='lxd')
35+ except KeyError:
36+ status_set('blocked',
37+ 'lxd-group "{0}" does not exist'.format(src_group))
38+ finally:
39+ data_changed('juju-lxd.config', config)
40+ set_state('juju-lxd.configured')
41+ status_set('active', 'Ready')
42+ else:
43+ status_set('blocked', 'group "{0}" does not exist'.format(dst_group))
44
45
46 @when('juju-lxd.configured')
47@@ -110,32 +117,13 @@ def git_repo():
48 pass
49
50
51-def add_group_to_lxd(group):
52- users = getent_group_users(group)
53- status_set("maintenance", "configuring users in lxd group")
54- for user in users:
55- try:
56- add_user_to_group(user, 'lxd')
57- except:
58- pass
59-
60-
61-def is_valid_group(group):
62- output = ''
63-
64- try:
65- output = subprocess.check_output(['getent', 'group', group])
66- except:
67- return False
68-
69- if output is None or output == '':
70- return False
71+def sync_groups(src, dst):
72+ ''' Makes the dst group contain the same users as the src group.
73+ If src is empty, the dst group will be emptied too'''
74+ # Will raise KeyError if src is empty or non-existent
75+ if src in [None, '']:
76+ users = ''
77 else:
78- return True
79-
80-
81-def getent_group_users(group):
82- output = str(subprocess.check_output(['getent', 'group',
83- group])).rstrip("'")
84- members = output.split(':')[3]
85- return members.split(',')
86+ users = ','.join(grp.getgrnam(src).gr_mem)
87+ status_set("maintenance", "configuring users in lxd group")
88+ subprocess.call(['gpasswd', '-M', users, dst])

Subscribers

People subscribed via source and target branches

to all changes: