~seyeongkim/charms/trusty/neutron-gateway/lp1545886

« back to all changes in this revision

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

  • Committer: Edward Hope-Morley
  • Date: 2016-02-12 09:55:04 UTC
  • mfrom: (162.1.2 neutron-gateway.lp1518975)
  • Revision ID: edward.hope-morley@canonical.com-20160212095504-6oaokktylqcit5m6
[hopem,r=jamespage]

Sync charmhelpers to get support for supplying a Ascii Armor PGP key to
openstack-origin (currently only supports Radix64)
Closes-Bug: 1518975

Show diffs side-by-side

added added

removed removed

Lines of Context:
138
138
        except subprocess.CalledProcessError:
139
139
            return False
140
140
        else:
141
 
            if ("start/running" in output or "is running" in output):
 
141
            if ("start/running" in output or "is running" in output or
 
142
                    "up and running" in output):
142
143
                return True
143
144
            else:
144
145
                return False
160
161
 
161
162
 
162
163
def init_is_systemd():
 
164
    """Return True if the host system uses systemd, False otherwise."""
163
165
    return os.path.isdir(SYSTEMD_SYSTEM)
164
166
 
165
167
 
166
168
def adduser(username, password=None, shell='/bin/bash', system_user=False,
167
169
            primary_group=None, secondary_groups=None):
168
 
    """
169
 
    Add a user to the system.
 
170
    """Add a user to the system.
170
171
 
171
172
    Will log but otherwise succeed if the user already exists.
172
173
 
174
175
    :param str password: Password for user; if ``None``, create a system user
175
176
    :param str shell: The default shell for the user
176
177
    :param bool system_user: Whether to create a login or system user
177
 
    :param str primary_group: Primary group for user; defaults to their username
 
178
    :param str primary_group: Primary group for user; defaults to username
178
179
    :param list secondary_groups: Optional list of additional groups
179
180
 
180
181
    :returns: The password database entry struct, as returned by `pwd.getpwnam`
300
301
 
301
302
 
302
303
def fstab_remove(mp):
303
 
    """Remove the given mountpoint entry from /etc/fstab
304
 
    """
 
304
    """Remove the given mountpoint entry from /etc/fstab"""
305
305
    return Fstab.remove_by_mountpoint(mp)
306
306
 
307
307
 
308
308
def fstab_add(dev, mp, fs, options=None):
309
 
    """Adds the given device entry to the /etc/fstab file
310
 
    """
 
309
    """Adds the given device entry to the /etc/fstab file"""
311
310
    return Fstab.add(dev, mp, fs, options=options)
312
311
 
313
312
 
363
362
 
364
363
 
365
364
def file_hash(path, hash_type='md5'):
366
 
    """
367
 
    Generate a hash checksum of the contents of 'path' or None if not found.
 
365
    """Generate a hash checksum of the contents of 'path' or None if not found.
368
366
 
369
367
    :param str hash_type: Any hash alrgorithm supported by :mod:`hashlib`,
370
368
                          such as md5, sha1, sha256, sha512, etc.
379
377
 
380
378
 
381
379
def path_hash(path):
382
 
    """
383
 
    Generate a hash checksum of all files matching 'path'. Standard wildcards
384
 
    like '*' and '?' are supported, see documentation for the 'glob' module for
385
 
    more information.
 
380
    """Generate a hash checksum of all files matching 'path'. Standard
 
381
    wildcards like '*' and '?' are supported, see documentation for the 'glob'
 
382
    module for more information.
386
383
 
387
384
    :return: dict: A { filename: hash } dictionary for all matched files.
388
385
                   Empty if none found.
394
391
 
395
392
 
396
393
def check_hash(path, checksum, hash_type='md5'):
397
 
    """
398
 
    Validate a file using a cryptographic checksum.
 
394
    """Validate a file using a cryptographic checksum.
399
395
 
400
396
    :param str checksum: Value of the checksum used to validate the file.
401
397
    :param str hash_type: Hash algorithm used to generate `checksum`.
410
406
 
411
407
 
412
408
class ChecksumError(ValueError):
 
409
    """A class derived from Value error to indicate the checksum failed."""
413
410
    pass
414
411
 
415
412
 
515
512
 
516
513
 
517
514
def list_nics(nic_type=None):
518
 
    '''Return a list of nics of given type(s)'''
 
515
    """Return a list of nics of given type(s)"""
519
516
    if isinstance(nic_type, six.string_types):
520
517
        int_types = [nic_type]
521
518
    else:
557
554
 
558
555
 
559
556
def set_nic_mtu(nic, mtu):
560
 
    '''Set MTU on a network interface'''
 
557
    """Set the Maximum Transmission Unit (MTU) on a network interface."""
561
558
    cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
562
559
    subprocess.check_call(cmd)
563
560
 
564
561
 
565
562
def get_nic_mtu(nic):
 
563
    """Return the Maximum Transmission Unit (MTU) for a network interface."""
566
564
    cmd = ['ip', 'addr', 'show', nic]
567
565
    ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
568
566
    mtu = ""
574
572
 
575
573
 
576
574
def get_nic_hwaddr(nic):
 
575
    """Return the Media Access Control (MAC) for a network interface."""
577
576
    cmd = ['ip', '-o', '-0', 'addr', 'show', nic]
578
577
    ip_output = subprocess.check_output(cmd).decode('UTF-8')
579
578
    hwaddr = ""
584
583
 
585
584
 
586
585
def cmp_pkgrevno(package, revno, pkgcache=None):
587
 
    '''Compare supplied revno with the revno of the installed package
 
586
    """Compare supplied revno with the revno of the installed package
588
587
 
589
588
    *  1 => Installed revno is greater than supplied arg
590
589
    *  0 => Installed revno is the same as supplied arg
593
592
    This function imports apt_cache function from charmhelpers.fetch if
594
593
    the pkgcache argument is None. Be sure to add charmhelpers.fetch if
595
594
    you call this function, or pass an apt_pkg.Cache() instance.
596
 
    '''
 
595
    """
597
596
    import apt_pkg
598
597
    if not pkgcache:
599
598
        from charmhelpers.fetch import apt_cache
603
602
 
604
603
 
605
604
@contextmanager
606
 
def chdir(d):
 
605
def chdir(directory):
 
606
    """Change the current working directory to a different directory for a code
 
607
    block and return the previous directory after the block exits. Useful to
 
608
    run commands from a specificed directory.
 
609
 
 
610
    :param str directory: The directory path to change to for this context.
 
611
    """
607
612
    cur = os.getcwd()
608
613
    try:
609
 
        yield os.chdir(d)
 
614
        yield os.chdir(directory)
610
615
    finally:
611
616
        os.chdir(cur)
612
617
 
613
618
 
614
619
def chownr(path, owner, group, follow_links=True, chowntopdir=False):
615
 
    """
616
 
    Recursively change user and group ownership of files and directories
 
620
    """Recursively change user and group ownership of files and directories
617
621
    in given path. Doesn't chown path itself by default, only its children.
618
622
 
 
623
    :param str path: The string path to start changing ownership.
 
624
    :param str owner: The owner string to use when looking up the uid.
 
625
    :param str group: The group string to use when looking up the gid.
619
626
    :param bool follow_links: Also Chown links if True
620
627
    :param bool chowntopdir: Also chown path itself if True
621
628
    """
639
646
 
640
647
 
641
648
def lchownr(path, owner, group):
 
649
    """Recursively change user and group ownership of files and directories
 
650
    in a given path, not following symbolic links. See the documentation for
 
651
    'os.lchown' for more information.
 
652
 
 
653
    :param str path: The string path to start changing ownership.
 
654
    :param str owner: The owner string to use when looking up the uid.
 
655
    :param str group: The group string to use when looking up the gid.
 
656
    """
642
657
    chownr(path, owner, group, follow_links=False)
643
658
 
644
659
 
645
660
def get_total_ram():
646
 
    '''The total amount of system RAM in bytes.
 
661
    """The total amount of system RAM in bytes.
647
662
 
648
663
    This is what is reported by the OS, and may be overcommitted when
649
664
    there are multiple containers hosted on the same machine.
650
 
    '''
 
665
    """
651
666
    with open('/proc/meminfo', 'r') as f:
652
667
        for line in f.readlines():
653
668
            if line: