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
=== modified file 'charm-helpers.yaml'
--- charm-helpers.yaml 2013-06-20 09:28:16 +0000
+++ charm-helpers.yaml 2013-08-20 14:58:24 +0000
@@ -1,4 +1,4 @@
1destination: hooks/charmhelpers1destination: hooks/charmhelpers
2branch: lp:~stub/charm-helpers/bug-1182959-no-implicit-serializable-magic2branch: lp:charm-helpers
3include:3include:
4 - core4 - core
55
=== modified file 'hooks/charmhelpers/core/hookenv.py'
--- hooks/charmhelpers/core/hookenv.py 2013-06-28 17:17:51 +0000
+++ hooks/charmhelpers/core/hookenv.py 2013-08-20 14:58:24 +0000
@@ -143,6 +143,11 @@
143 return os.environ['JUJU_REMOTE_UNIT']143 return os.environ['JUJU_REMOTE_UNIT']
144144
145145
146def service_name():
147 "The name service group this unit belongs to"
148 return local_unit().split('/')[0]
149
150
146@cached151@cached
147def config(scope=None):152def config(scope=None):
148 "Juju charm configuration"153 "Juju charm configuration"
@@ -192,7 +197,7 @@
192 relid_cmd_line = ['relation-ids', '--format=json']197 relid_cmd_line = ['relation-ids', '--format=json']
193 if reltype is not None:198 if reltype is not None:
194 relid_cmd_line.append(reltype)199 relid_cmd_line.append(reltype)
195 return json.loads(subprocess.check_output(relid_cmd_line))200 return json.loads(subprocess.check_output(relid_cmd_line)) or []
196 return []201 return []
197202
198203
@@ -203,7 +208,7 @@
203 units_cmd_line = ['relation-list', '--format=json']208 units_cmd_line = ['relation-list', '--format=json']
204 if relid is not None:209 if relid is not None:
205 units_cmd_line.extend(('-r', relid))210 units_cmd_line.extend(('-r', relid))
206 return json.loads(subprocess.check_output(units_cmd_line))211 return json.loads(subprocess.check_output(units_cmd_line)) or []
207212
208213
209@cached214@cached
@@ -329,3 +334,7 @@
329 decorated.__name__.replace('_', '-'), decorated)334 decorated.__name__.replace('_', '-'), decorated)
330 return decorated335 return decorated
331 return wrapper336 return wrapper
337
338
339def charm_dir():
340 return os.environ.get('CHARM_DIR')
332341
=== modified file 'hooks/charmhelpers/core/host.py'
--- hooks/charmhelpers/core/host.py 2013-06-28 08:39:20 +0000
+++ hooks/charmhelpers/core/host.py 2013-08-20 14:58:24 +0000
@@ -9,12 +9,14 @@
9import os9import os
10import pwd10import pwd
11import grp11import grp
12import random
13import string
12import subprocess14import subprocess
13import hashlib15import hashlib
1416
15from collections import OrderedDict17from collections import OrderedDict
1618
17from hookenv import log, execution_environment19from hookenv import log
1820
1921
20def service_start(service_name):22def service_start(service_name):
@@ -39,6 +41,18 @@
39 return subprocess.call(cmd) == 041 return subprocess.call(cmd) == 0
4042
4143
44def service_running(service):
45 try:
46 output = subprocess.check_output(['service', service, 'status'])
47 except subprocess.CalledProcessError:
48 return False
49 else:
50 if ("start/running" in output or "is running" in output):
51 return True
52 else:
53 return False
54
55
42def adduser(username, password=None, shell='/bin/bash', system_user=False):56def adduser(username, password=None, shell='/bin/bash', system_user=False):
43 """Add a user"""57 """Add a user"""
44 try:58 try:
@@ -48,13 +62,13 @@
48 log('creating user {0}'.format(username))62 log('creating user {0}'.format(username))
49 cmd = ['useradd']63 cmd = ['useradd']
50 if system_user or password is None:64 if system_user or password is None:
51 cmd.append('--system')65 cmd.append('--system')
52 else:66 else:
53 cmd.extend([67 cmd.extend([
54 '--create-home',68 '--create-home',
55 '--shell', shell,69 '--shell', shell,
56 '--password', password,70 '--password', password,
57 ])71 ])
58 cmd.append(username)72 cmd.append(username)
59 subprocess.check_call(cmd)73 subprocess.check_call(cmd)
60 user_info = pwd.getpwnam(username)74 user_info = pwd.getpwnam(username)
@@ -74,36 +88,33 @@
7488
75def rsync(from_path, to_path, flags='-r', options=None):89def rsync(from_path, to_path, flags='-r', options=None):
76 """Replicate the contents of a path"""90 """Replicate the contents of a path"""
77 context = execution_environment()
78 options = options or ['--delete', '--executability']91 options = options or ['--delete', '--executability']
79 cmd = ['/usr/bin/rsync', flags]92 cmd = ['/usr/bin/rsync', flags]
80 cmd.extend(options)93 cmd.extend(options)
81 cmd.append(from_path.format(**context))94 cmd.append(from_path)
82 cmd.append(to_path.format(**context))95 cmd.append(to_path)
83 log(" ".join(cmd))96 log(" ".join(cmd))
84 return subprocess.check_output(cmd).strip()97 return subprocess.check_output(cmd).strip()
8598
8699
87def symlink(source, destination):100def symlink(source, destination):
88 """Create a symbolic link"""101 """Create a symbolic link"""
89 context = execution_environment()
90 log("Symlinking {} as {}".format(source, destination))102 log("Symlinking {} as {}".format(source, destination))
91 cmd = [103 cmd = [
92 'ln',104 'ln',
93 '-sf',105 '-sf',
94 source.format(**context),106 source,
95 destination.format(**context)107 destination,
96 ]108 ]
97 subprocess.check_call(cmd)109 subprocess.check_call(cmd)
98110
99111
100def mkdir(path, owner='root', group='root', perms=0555, force=False):112def mkdir(path, owner='root', group='root', perms=0555, force=False):
101 """Create a directory"""113 """Create a directory"""
102 context = execution_environment()
103 log("Making dir {} {}:{} {:o}".format(path, owner, group,114 log("Making dir {} {}:{} {:o}".format(path, owner, group,
104 perms))115 perms))
105 uid = pwd.getpwnam(owner.format(**context)).pw_uid116 uid = pwd.getpwnam(owner).pw_uid
106 gid = grp.getgrnam(group.format(**context)).gr_gid117 gid = grp.getgrnam(group).gr_gid
107 realpath = os.path.abspath(path)118 realpath = os.path.abspath(path)
108 if os.path.exists(realpath):119 if os.path.exists(realpath):
109 if force and not os.path.isdir(realpath):120 if force and not os.path.isdir(realpath):
@@ -114,28 +125,15 @@
114 os.chown(realpath, uid, gid)125 os.chown(realpath, uid, gid)
115126
116127
117def write_file(path, fmtstr, owner='root', group='root', perms=0444, **kwargs):128def write_file(path, content, owner='root', group='root', perms=0444):
118 """Create or overwrite a file with the contents of a string"""129 """Create or overwrite a file with the contents of a string"""
119 context = execution_environment()130 log("Writing file {} {}:{} {:o}".format(path, owner, group, perms))
120 context.update(kwargs)131 uid = pwd.getpwnam(owner).pw_uid
121 log("Writing file {} {}:{} {:o}".format(path, owner, group,132 gid = grp.getgrnam(group).gr_gid
122 perms))133 with open(path, 'w') as target:
123 uid = pwd.getpwnam(owner.format(**context)).pw_uid
124 gid = grp.getgrnam(group.format(**context)).gr_gid
125 with open(path.format(**context), 'w') as target:
126 os.fchown(target.fileno(), uid, gid)134 os.fchown(target.fileno(), uid, gid)
127 os.fchmod(target.fileno(), perms)135 os.fchmod(target.fileno(), perms)
128 target.write(fmtstr.format(**context))136 target.write(content)
129
130
131def render_template_file(source, destination, **kwargs):
132 """Create or overwrite a file using a template"""
133 log("Rendering template {} for {}".format(source,
134 destination))
135 context = execution_environment()
136 with open(source.format(**context), 'r') as template:
137 write_file(destination.format(**context), template.read(),
138 **kwargs)
139137
140138
141def filter_installed_packages(packages):139def filter_installed_packages(packages):
@@ -261,3 +259,25 @@
261 service('restart', service_name)259 service('restart', service_name)
262 return wrapped_f260 return wrapped_f
263 return wrap261 return wrap
262
263
264def lsb_release():
265 '''Return /etc/lsb-release in a dict'''
266 d = {}
267 with open('/etc/lsb-release', 'r') as lsb:
268 for l in lsb:
269 k, v = l.split('=')
270 d[k.strip()] = v.strip()
271 return d
272
273
274def pwgen(length=None):
275 '''Generate a random pasword.'''
276 if length is None:
277 length = random.choice(range(35, 45))
278 alphanumeric_chars = [
279 l for l in (string.letters + string.digits)
280 if l not in 'l0QD1vAEIOUaeiou']
281 random_chars = [
282 random.choice(alphanumeric_chars) for _ in range(length)]
283 return(''.join(random_chars))

Subscribers

People subscribed via source and target branches