Merge lp:~stub/charms/precise/postgresql/charm-helpers into lp:charms/postgresql

Proposed by Stuart Bishop
Status: Merged
Approved by: Mark Mims
Approved revision: 65
Merged at revision: 60
Proposed branch: lp:~stub/charms/precise/postgresql/charm-helpers
Merge into: lp:charms/postgresql
Diff against target: 214 lines (+67/-38)
3 files modified
charm-helpers.yaml (+1/-1)
hooks/charmhelpers/core/hookenv.py (+11/-2)
hooks/charmhelpers/core/host.py (+55/-35)
To merge this branch: bzr merge lp:~stub/charms/precise/postgresql/charm-helpers
Reviewer Review Type Date Requested Status
Mark Mims (community) Approve
Review via email: mp+181062@code.launchpad.net

Description of the change

Import charm-helpers trunk.

To post a comment you must log in.
Revision history for this message
Mark Mims (mark-mims) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charm-helpers.yaml'
2--- charm-helpers.yaml 2013-06-20 09:28:16 +0000
3+++ charm-helpers.yaml 2013-08-20 14:58:24 +0000
4@@ -1,4 +1,4 @@
5 destination: hooks/charmhelpers
6-branch: lp:~stub/charm-helpers/bug-1182959-no-implicit-serializable-magic
7+branch: lp:charm-helpers
8 include:
9 - core
10
11=== modified file 'hooks/charmhelpers/core/hookenv.py'
12--- hooks/charmhelpers/core/hookenv.py 2013-06-28 17:17:51 +0000
13+++ hooks/charmhelpers/core/hookenv.py 2013-08-20 14:58:24 +0000
14@@ -143,6 +143,11 @@
15 return os.environ['JUJU_REMOTE_UNIT']
16
17
18+def service_name():
19+ "The name service group this unit belongs to"
20+ return local_unit().split('/')[0]
21+
22+
23 @cached
24 def config(scope=None):
25 "Juju charm configuration"
26@@ -192,7 +197,7 @@
27 relid_cmd_line = ['relation-ids', '--format=json']
28 if reltype is not None:
29 relid_cmd_line.append(reltype)
30- return json.loads(subprocess.check_output(relid_cmd_line))
31+ return json.loads(subprocess.check_output(relid_cmd_line)) or []
32 return []
33
34
35@@ -203,7 +208,7 @@
36 units_cmd_line = ['relation-list', '--format=json']
37 if relid is not None:
38 units_cmd_line.extend(('-r', relid))
39- return json.loads(subprocess.check_output(units_cmd_line))
40+ return json.loads(subprocess.check_output(units_cmd_line)) or []
41
42
43 @cached
44@@ -329,3 +334,7 @@
45 decorated.__name__.replace('_', '-'), decorated)
46 return decorated
47 return wrapper
48+
49+
50+def charm_dir():
51+ return os.environ.get('CHARM_DIR')
52
53=== modified file 'hooks/charmhelpers/core/host.py'
54--- hooks/charmhelpers/core/host.py 2013-06-28 08:39:20 +0000
55+++ hooks/charmhelpers/core/host.py 2013-08-20 14:58:24 +0000
56@@ -9,12 +9,14 @@
57 import os
58 import pwd
59 import grp
60+import random
61+import string
62 import subprocess
63 import hashlib
64
65 from collections import OrderedDict
66
67-from hookenv import log, execution_environment
68+from hookenv import log
69
70
71 def service_start(service_name):
72@@ -39,6 +41,18 @@
73 return subprocess.call(cmd) == 0
74
75
76+def service_running(service):
77+ try:
78+ output = subprocess.check_output(['service', service, 'status'])
79+ except subprocess.CalledProcessError:
80+ return False
81+ else:
82+ if ("start/running" in output or "is running" in output):
83+ return True
84+ else:
85+ return False
86+
87+
88 def adduser(username, password=None, shell='/bin/bash', system_user=False):
89 """Add a user"""
90 try:
91@@ -48,13 +62,13 @@
92 log('creating user {0}'.format(username))
93 cmd = ['useradd']
94 if system_user or password is None:
95- cmd.append('--system')
96+ cmd.append('--system')
97 else:
98- cmd.extend([
99- '--create-home',
100- '--shell', shell,
101- '--password', password,
102- ])
103+ cmd.extend([
104+ '--create-home',
105+ '--shell', shell,
106+ '--password', password,
107+ ])
108 cmd.append(username)
109 subprocess.check_call(cmd)
110 user_info = pwd.getpwnam(username)
111@@ -74,36 +88,33 @@
112
113 def rsync(from_path, to_path, flags='-r', options=None):
114 """Replicate the contents of a path"""
115- context = execution_environment()
116 options = options or ['--delete', '--executability']
117 cmd = ['/usr/bin/rsync', flags]
118 cmd.extend(options)
119- cmd.append(from_path.format(**context))
120- cmd.append(to_path.format(**context))
121+ cmd.append(from_path)
122+ cmd.append(to_path)
123 log(" ".join(cmd))
124 return subprocess.check_output(cmd).strip()
125
126
127 def symlink(source, destination):
128 """Create a symbolic link"""
129- context = execution_environment()
130 log("Symlinking {} as {}".format(source, destination))
131 cmd = [
132 'ln',
133 '-sf',
134- source.format(**context),
135- destination.format(**context)
136+ source,
137+ destination,
138 ]
139 subprocess.check_call(cmd)
140
141
142 def mkdir(path, owner='root', group='root', perms=0555, force=False):
143 """Create a directory"""
144- context = execution_environment()
145 log("Making dir {} {}:{} {:o}".format(path, owner, group,
146 perms))
147- uid = pwd.getpwnam(owner.format(**context)).pw_uid
148- gid = grp.getgrnam(group.format(**context)).gr_gid
149+ uid = pwd.getpwnam(owner).pw_uid
150+ gid = grp.getgrnam(group).gr_gid
151 realpath = os.path.abspath(path)
152 if os.path.exists(realpath):
153 if force and not os.path.isdir(realpath):
154@@ -114,28 +125,15 @@
155 os.chown(realpath, uid, gid)
156
157
158-def write_file(path, fmtstr, owner='root', group='root', perms=0444, **kwargs):
159+def write_file(path, content, owner='root', group='root', perms=0444):
160 """Create or overwrite a file with the contents of a string"""
161- context = execution_environment()
162- context.update(kwargs)
163- log("Writing file {} {}:{} {:o}".format(path, owner, group,
164- perms))
165- uid = pwd.getpwnam(owner.format(**context)).pw_uid
166- gid = grp.getgrnam(group.format(**context)).gr_gid
167- with open(path.format(**context), 'w') as target:
168+ log("Writing file {} {}:{} {:o}".format(path, owner, group, perms))
169+ uid = pwd.getpwnam(owner).pw_uid
170+ gid = grp.getgrnam(group).gr_gid
171+ with open(path, 'w') as target:
172 os.fchown(target.fileno(), uid, gid)
173 os.fchmod(target.fileno(), perms)
174- target.write(fmtstr.format(**context))
175-
176-
177-def render_template_file(source, destination, **kwargs):
178- """Create or overwrite a file using a template"""
179- log("Rendering template {} for {}".format(source,
180- destination))
181- context = execution_environment()
182- with open(source.format(**context), 'r') as template:
183- write_file(destination.format(**context), template.read(),
184- **kwargs)
185+ target.write(content)
186
187
188 def filter_installed_packages(packages):
189@@ -261,3 +259,25 @@
190 service('restart', service_name)
191 return wrapped_f
192 return wrap
193+
194+
195+def lsb_release():
196+ '''Return /etc/lsb-release in a dict'''
197+ d = {}
198+ with open('/etc/lsb-release', 'r') as lsb:
199+ for l in lsb:
200+ k, v = l.split('=')
201+ d[k.strip()] = v.strip()
202+ return d
203+
204+
205+def pwgen(length=None):
206+ '''Generate a random pasword.'''
207+ if length is None:
208+ length = random.choice(range(35, 45))
209+ alphanumeric_chars = [
210+ l for l in (string.letters + string.digits)
211+ if l not in 'l0QD1vAEIOUaeiou']
212+ random_chars = [
213+ random.choice(alphanumeric_chars) for _ in range(length)]
214+ return(''.join(random_chars))

Subscribers

People subscribed via source and target branches