Merge lp:~hopem/charms/precise/cinder/stable.lp1337266 into lp:~openstack-charmers-archive/charms/precise/cinder/trunk

Proposed by Edward Hope-Morley
Status: Merged
Merged at revision: 38
Proposed branch: lp:~hopem/charms/precise/cinder/stable.lp1337266
Merge into: lp:~openstack-charmers-archive/charms/precise/cinder/trunk
Diff against target: 240 lines (+147/-11)
4 files modified
hooks/charmhelpers/contrib/openstack/context.py (+9/-4)
hooks/charmhelpers/core/fstab.py (+116/-0)
hooks/charmhelpers/core/host.py (+20/-6)
hooks/charmhelpers/fetch/bzrurl.py (+2/-1)
To merge this branch: bzr merge lp:~hopem/charms/precise/cinder/stable.lp1337266
Reviewer Review Type Date Requested Status
OpenStack Charmers Pending
Review via email: mp+225621@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Edward Hope-Morley (hopem) wrote :

synced from stable charm-helpers

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/charmhelpers/contrib/openstack/context.py'
2--- hooks/charmhelpers/contrib/openstack/context.py 2014-05-19 11:43:29 +0000
3+++ hooks/charmhelpers/contrib/openstack/context.py 2014-07-04 09:22:30 +0000
4@@ -24,6 +24,7 @@
5 unit_get,
6 unit_private_ip,
7 ERROR,
8+ INFO
9 )
10
11 from charmhelpers.contrib.hahelpers.cluster import (
12@@ -657,7 +658,7 @@
13 self.interface = interface
14
15 def __call__(self):
16- ctxt = {}
17+ ctxt = {'sections': {}}
18 for rid in relation_ids(self.interface):
19 for unit in related_units(rid):
20 sub_config = relation_get('subordinate_configuration',
21@@ -683,10 +684,14 @@
22
23 sub_config = sub_config[self.config_file]
24 for k, v in sub_config.iteritems():
25- ctxt[k] = v
26+ if k == 'sections':
27+ for section, config_dict in v.iteritems():
28+ log("adding section '%s'" % (section))
29+ ctxt[k][section] = config_dict
30+ else:
31+ ctxt[k] = v
32
33- if not ctxt:
34- ctxt['sections'] = {}
35+ log("%d section(s) found" % (len(ctxt['sections'])), level=INFO)
36
37 return ctxt
38
39
40=== added file 'hooks/charmhelpers/core/fstab.py'
41--- hooks/charmhelpers/core/fstab.py 1970-01-01 00:00:00 +0000
42+++ hooks/charmhelpers/core/fstab.py 2014-07-04 09:22:30 +0000
43@@ -0,0 +1,116 @@
44+#!/usr/bin/env python
45+# -*- coding: utf-8 -*-
46+
47+__author__ = 'Jorge Niedbalski R. <jorge.niedbalski@canonical.com>'
48+
49+import os
50+
51+
52+class Fstab(file):
53+ """This class extends file in order to implement a file reader/writer
54+ for file `/etc/fstab`
55+ """
56+
57+ class Entry(object):
58+ """Entry class represents a non-comment line on the `/etc/fstab` file
59+ """
60+ def __init__(self, device, mountpoint, filesystem,
61+ options, d=0, p=0):
62+ self.device = device
63+ self.mountpoint = mountpoint
64+ self.filesystem = filesystem
65+
66+ if not options:
67+ options = "defaults"
68+
69+ self.options = options
70+ self.d = d
71+ self.p = p
72+
73+ def __eq__(self, o):
74+ return str(self) == str(o)
75+
76+ def __str__(self):
77+ return "{} {} {} {} {} {}".format(self.device,
78+ self.mountpoint,
79+ self.filesystem,
80+ self.options,
81+ self.d,
82+ self.p)
83+
84+ DEFAULT_PATH = os.path.join(os.path.sep, 'etc', 'fstab')
85+
86+ def __init__(self, path=None):
87+ if path:
88+ self._path = path
89+ else:
90+ self._path = self.DEFAULT_PATH
91+ file.__init__(self, self._path, 'r+')
92+
93+ def _hydrate_entry(self, line):
94+ # NOTE: use split with no arguments to split on any
95+ # whitespace including tabs
96+ return Fstab.Entry(*filter(
97+ lambda x: x not in ('', None),
98+ line.strip("\n").split()))
99+
100+ @property
101+ def entries(self):
102+ self.seek(0)
103+ for line in self.readlines():
104+ try:
105+ if not line.startswith("#"):
106+ yield self._hydrate_entry(line)
107+ except ValueError:
108+ pass
109+
110+ def get_entry_by_attr(self, attr, value):
111+ for entry in self.entries:
112+ e_attr = getattr(entry, attr)
113+ if e_attr == value:
114+ return entry
115+ return None
116+
117+ def add_entry(self, entry):
118+ if self.get_entry_by_attr('device', entry.device):
119+ return False
120+
121+ self.write(str(entry) + '\n')
122+ self.truncate()
123+ return entry
124+
125+ def remove_entry(self, entry):
126+ self.seek(0)
127+
128+ lines = self.readlines()
129+
130+ found = False
131+ for index, line in enumerate(lines):
132+ if not line.startswith("#"):
133+ if self._hydrate_entry(line) == entry:
134+ found = True
135+ break
136+
137+ if not found:
138+ return False
139+
140+ lines.remove(line)
141+
142+ self.seek(0)
143+ self.write(''.join(lines))
144+ self.truncate()
145+ return True
146+
147+ @classmethod
148+ def remove_by_mountpoint(cls, mountpoint, path=None):
149+ fstab = cls(path=path)
150+ entry = fstab.get_entry_by_attr('mountpoint', mountpoint)
151+ if entry:
152+ return fstab.remove_entry(entry)
153+ return False
154+
155+ @classmethod
156+ def add(cls, device, mountpoint, filesystem, options=None, path=None):
157+ return cls(path=path).add_entry(Fstab.Entry(device,
158+ mountpoint, filesystem,
159+ options=options))
160
161=== modified file 'hooks/charmhelpers/core/host.py'
162--- hooks/charmhelpers/core/host.py 2014-05-19 11:43:29 +0000
163+++ hooks/charmhelpers/core/host.py 2014-07-04 09:22:30 +0000
164@@ -17,6 +17,7 @@
165 from collections import OrderedDict
166
167 from hookenv import log
168+from fstab import Fstab
169
170
171 def service_start(service_name):
172@@ -35,7 +36,8 @@
173
174
175 def service_reload(service_name, restart_on_failure=False):
176- """Reload a system service, optionally falling back to restart if reload fails"""
177+ """Reload a system service, optionally falling back to restart if
178+ reload fails"""
179 service_result = service('reload', service_name)
180 if not service_result and restart_on_failure:
181 service_result = service('restart', service_name)
182@@ -144,7 +146,19 @@
183 target.write(content)
184
185
186-def mount(device, mountpoint, options=None, persist=False):
187+def fstab_remove(mp):
188+ """Remove the given mountpoint entry from /etc/fstab
189+ """
190+ return Fstab.remove_by_mountpoint(mp)
191+
192+
193+def fstab_add(dev, mp, fs, options=None):
194+ """Adds the given device entry to the /etc/fstab file
195+ """
196+ return Fstab.add(dev, mp, fs, options=options)
197+
198+
199+def mount(device, mountpoint, options=None, persist=False, filesystem="ext3"):
200 """Mount a filesystem at a particular mountpoint"""
201 cmd_args = ['mount']
202 if options is not None:
203@@ -155,9 +169,9 @@
204 except subprocess.CalledProcessError, e:
205 log('Error mounting {} at {}\n{}'.format(device, mountpoint, e.output))
206 return False
207+
208 if persist:
209- # TODO: update fstab
210- pass
211+ return fstab_add(device, mountpoint, filesystem, options=options)
212 return True
213
214
215@@ -169,9 +183,9 @@
216 except subprocess.CalledProcessError, e:
217 log('Error unmounting {}\n{}'.format(mountpoint, e.output))
218 return False
219+
220 if persist:
221- # TODO: update fstab
222- pass
223+ return fstab_remove(mountpoint)
224 return True
225
226
227
228=== modified file 'hooks/charmhelpers/fetch/bzrurl.py'
229--- hooks/charmhelpers/fetch/bzrurl.py 2013-11-06 03:53:17 +0000
230+++ hooks/charmhelpers/fetch/bzrurl.py 2014-07-04 09:22:30 +0000
231@@ -39,7 +39,8 @@
232 def install(self, source):
233 url_parts = self.parse_url(source)
234 branch_name = url_parts.path.strip("/").split("/")[-1]
235- dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched", branch_name)
236+ dest_dir = os.path.join(os.environ.get('CHARM_DIR'), "fetched",
237+ branch_name)
238 if not os.path.exists(dest_dir):
239 mkdir(dest_dir, perms=0755)
240 try:

Subscribers

People subscribed via source and target branches