Merge lp:~james-page/charms/trusty/ceph-radosgw/fix-apt-race-stable into lp:~openstack-charmers-archive/charms/trusty/ceph-radosgw/trunk

Proposed by James Page
Status: Merged
Merged at revision: 22
Proposed branch: lp:~james-page/charms/trusty/ceph-radosgw/fix-apt-race-stable
Merge into: lp:~openstack-charmers-archive/charms/trusty/ceph-radosgw/trunk
Diff against target: 222 lines (+143/-8)
4 files modified
charm-helpers-sync.yaml (+1/-1)
hooks/charmhelpers/core/fstab.py (+116/-0)
hooks/charmhelpers/core/host.py (+24/-6)
hooks/charmhelpers/fetch/bzrurl.py (+2/-1)
To merge this branch: bzr merge lp:~james-page/charms/trusty/ceph-radosgw/fix-apt-race-stable
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+228705@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Liam Young (gnuoy) wrote :

Approved

review: Approve

Preview Diff

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

Subscribers

People subscribed via source and target branches