~tvansteenburgh/charms/precise/nvp-transport-node/fix-proof

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/host.py

  • Committer: Liam Young
  • Date: 2014-07-30 13:08:44 UTC
  • mfrom: (46.1.2 nvp-transport-node)
  • Revision ID: liam.young@canonical.com-20140730130844-ljfr29rwh62vsfid
[jamespage, r=gnuoy] Add support for using tarball from object storage

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
from collections import OrderedDict
17
17
 
18
18
from hookenv import log
 
19
from fstab import Fstab
19
20
 
20
21
 
21
22
def service_start(service_name):
34
35
 
35
36
 
36
37
def service_reload(service_name, restart_on_failure=False):
37
 
    """Reload a system service, optionally falling back to restart if reload fails"""
 
38
    """Reload a system service, optionally falling back to restart if
 
39
    reload fails"""
38
40
    service_result = service('reload', service_name)
39
41
    if not service_result and restart_on_failure:
40
42
        service_result = service('restart', service_name)
143
145
        target.write(content)
144
146
 
145
147
 
146
 
def mount(device, mountpoint, options=None, persist=False):
 
148
def fstab_remove(mp):
 
149
    """Remove the given mountpoint entry from /etc/fstab
 
150
    """
 
151
    return Fstab.remove_by_mountpoint(mp)
 
152
 
 
153
 
 
154
def fstab_add(dev, mp, fs, options=None):
 
155
    """Adds the given device entry to the /etc/fstab file
 
156
    """
 
157
    return Fstab.add(dev, mp, fs, options=options)
 
158
 
 
159
 
 
160
def mount(device, mountpoint, options=None, persist=False, filesystem="ext3"):
147
161
    """Mount a filesystem at a particular mountpoint"""
148
162
    cmd_args = ['mount']
149
163
    if options is not None:
154
168
    except subprocess.CalledProcessError, e:
155
169
        log('Error mounting {} at {}\n{}'.format(device, mountpoint, e.output))
156
170
        return False
 
171
 
157
172
    if persist:
158
 
        # TODO: update fstab
159
 
        pass
 
173
        return fstab_add(device, mountpoint, filesystem, options=options)
160
174
    return True
161
175
 
162
176
 
168
182
    except subprocess.CalledProcessError, e:
169
183
        log('Error unmounting {}\n{}'.format(mountpoint, e.output))
170
184
        return False
 
185
 
171
186
    if persist:
172
 
        # TODO: update fstab
173
 
        pass
 
187
        return fstab_remove(mountpoint)
174
188
    return True
175
189
 
176
190
 
194
208
        return None
195
209
 
196
210
 
197
 
def restart_on_change(restart_map):
 
211
def restart_on_change(restart_map, stopstart=False):
198
212
    """Restart services based on configuration files changing
199
213
 
200
214
    This function is used a decorator, for example
219
233
            for path in restart_map:
220
234
                if checksums[path] != file_hash(path):
221
235
                    restarts += restart_map[path]
222
 
            for service_name in list(OrderedDict.fromkeys(restarts)):
223
 
                service('restart', service_name)
 
236
            services_list = list(OrderedDict.fromkeys(restarts))
 
237
            if not stopstart:
 
238
                for service_name in services_list:
 
239
                    service('restart', service_name)
 
240
            else:
 
241
                for action in ['stop', 'start']:
 
242
                    for service_name in services_list:
 
243
                        service(action, service_name)
224
244
        return wrapped_f
225
245
    return wrap
226
246
 
245
265
    random_chars = [
246
266
        random.choice(alphanumeric_chars) for _ in range(length)]
247
267
    return(''.join(random_chars))
 
268
 
 
269
 
 
270
def list_nics(nic_type):
 
271
    '''Return a list of nics of given type(s)'''
 
272
    if isinstance(nic_type, basestring):
 
273
        int_types = [nic_type]
 
274
    else:
 
275
        int_types = nic_type
 
276
    interfaces = []
 
277
    for int_type in int_types:
 
278
        cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
 
279
        ip_output = subprocess.check_output(cmd).split('\n')
 
280
        ip_output = (line for line in ip_output if line)
 
281
        for line in ip_output:
 
282
            if line.split()[1].startswith(int_type):
 
283
                interfaces.append(line.split()[1].replace(":", ""))
 
284
    return interfaces
 
285
 
 
286
 
 
287
def set_nic_mtu(nic, mtu):
 
288
    '''Set MTU on a network interface'''
 
289
    cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
 
290
    subprocess.check_call(cmd)
 
291
 
 
292
 
 
293
def get_nic_mtu(nic):
 
294
    cmd = ['ip', 'addr', 'show', nic]
 
295
    ip_output = subprocess.check_output(cmd).split('\n')
 
296
    mtu = ""
 
297
    for line in ip_output:
 
298
        words = line.split()
 
299
        if 'mtu' in words:
 
300
            mtu = words[words.index("mtu") + 1]
 
301
    return mtu
 
302
 
 
303
 
 
304
def get_nic_hwaddr(nic):
 
305
    cmd = ['ip', '-o', '-0', 'addr', 'show', nic]
 
306
    ip_output = subprocess.check_output(cmd)
 
307
    hwaddr = ""
 
308
    words = ip_output.split()
 
309
    if 'link/ether' in words:
 
310
        hwaddr = words[words.index('link/ether') + 1]
 
311
    return hwaddr
 
312
 
 
313
 
 
314
def cmp_pkgrevno(package, revno, pkgcache=None):
 
315
    '''Compare supplied revno with the revno of the installed package
 
316
       1 => Installed revno is greater than supplied arg
 
317
       0 => Installed revno is the same as supplied arg
 
318
      -1 => Installed revno is less than supplied arg
 
319
    '''
 
320
    import apt_pkg
 
321
    if not pkgcache:
 
322
        apt_pkg.init()
 
323
        pkgcache = apt_pkg.Cache()
 
324
    pkg = pkgcache[package]
 
325
    return apt_pkg.version_compare(pkg.current_ver.ver_str, revno)