charmhelpers.core.host

ChecksumError
add_group Add a group to the system
add_user_to_group Add a user to a group
adduser Add a user to the system
chdir
check_hash Validate a file using a cryptographic checksum.
chownr
cmp_pkgrevno Compare supplied revno with the revno of the installed package
file_hash Generate a hash checksum of the contents of ‘path’ or None if not found.
fstab_add Adds the given device entry to the /etc/fstab file
fstab_remove Remove the given mountpoint entry from /etc/fstab
get_nic_hwaddr
get_nic_mtu
lchownr
list_nics Return a list of nics of given type(s)
lsb_release Return /etc/lsb-release in a dict
mkdir Create a directory
mount Mount a filesystem at a particular mountpoint
mounts Get a list of all mounted volumes as [[mountpoint,device],[...]]
path_hash Generate a hash checksum of all files matching ‘path’.
pwgen Generate a random pasword.
restart_on_change Restart services based on configuration files changing
rsync Replicate the contents of a path
service Control a system service
service_available Determine whether a system service is available
service_reload Reload a system service, optionally falling back to restart if
service_restart Restart a system service
service_running Determine whether a system service is running
service_start Start a system service
service_stop Stop a system service
set_nic_mtu Set MTU on a network interface
symlink Create a symbolic link
umount Unmount a filesystem
write_file Create or overwrite a file with the contents of a byte string.

Tools for working with the host system

exception charmhelpers.core.host.ChecksumError

Bases: exceptions.ValueError

charmhelpers.core.host.add_group(group_name, system_group=False)

Add a group to the system

charmhelpers.core.host.add_user_to_group(username, group)

Add a user to a group

charmhelpers.core.host.adduser(username, password=None, shell='/bin/bash', system_user=False)

Add a user to the system

charmhelpers.core.host.chdir(*args, **kwds)
charmhelpers.core.host.check_hash(path, checksum, hash_type='md5')

Validate a file using a cryptographic checksum.

Parameters:
  • checksum (str) – Value of the checksum used to validate the file.
  • hash_type (str) – Hash algorithm used to generate checksum. Can be any hash alrgorithm supported by hashlib, such as md5, sha1, sha256, sha512, etc.
Raises:

ChecksumError – If the file fails the checksum

charmhelpers.core.host.chownr(path, owner, group, follow_links=True)
charmhelpers.core.host.cmp_pkgrevno(package, revno, pkgcache=None)

Compare supplied revno with the revno of the installed package

  • 1 => Installed revno is greater than supplied arg
  • 0 => Installed revno is the same as supplied arg
  • -1 => Installed revno is less than supplied arg

This function imports apt_cache function from charmhelpers.fetch if the pkgcache argument is None. Be sure to add charmhelpers.fetch if you call this function, or pass an apt_pkg.Cache() instance.

charmhelpers.core.host.file_hash(path, hash_type='md5')

Generate a hash checksum of the contents of ‘path’ or None if not found.

Parameters:hash_type (str) – Any hash alrgorithm supported by hashlib, such as md5, sha1, sha256, sha512, etc.
charmhelpers.core.host.fstab_add(dev, mp, fs, options=None)

Adds the given device entry to the /etc/fstab file

charmhelpers.core.host.fstab_remove(mp)

Remove the given mountpoint entry from /etc/fstab

charmhelpers.core.host.get_nic_hwaddr(nic)
charmhelpers.core.host.get_nic_mtu(nic)
charmhelpers.core.host.lchownr(path, owner, group)
charmhelpers.core.host.list_nics(nic_type)

Return a list of nics of given type(s)

charmhelpers.core.host.lsb_release()

Return /etc/lsb-release in a dict

charmhelpers.core.host.mkdir(path, owner='root', group='root', perms=365, force=False)

Create a directory

charmhelpers.core.host.mount(device, mountpoint, options=None, persist=False, filesystem='ext3')

Mount a filesystem at a particular mountpoint

charmhelpers.core.host.mounts()

Get a list of all mounted volumes as [[mountpoint,device],[...]]

charmhelpers.core.host.path_hash(path)

Generate a hash checksum of all files matching ‘path’. Standard wildcards like ‘*’ and ‘?’ are supported, see documentation for the ‘glob’ module for more information.

Returns:dict: A { filename: hash } dictionary for all matched files. Empty if none found.
charmhelpers.core.host.pwgen(length=None)

Generate a random pasword.

charmhelpers.core.host.restart_on_change(restart_map, stopstart=False)

Restart services based on configuration files changing

This function is used a decorator, for example:

@restart_on_change({
    '/etc/ceph/ceph.conf': [ 'cinder-api', 'cinder-volume' ]
    '/etc/apache/sites-enabled/*': [ 'apache2' ]
    })
def config_changed():
    pass  # your code here

In this example, the cinder-api and cinder-volume services would be restarted if /etc/ceph/ceph.conf is changed by the ceph_client_changed function. The apache2 service would be restarted if any file matching the pattern got changed, created or removed. Standard wildcards are supported, see documentation for the ‘glob’ module for more information.

charmhelpers.core.host.rsync(from_path, to_path, flags='-r', options=None)

Replicate the contents of a path

charmhelpers.core.host.service(action, service_name)

Control a system service

charmhelpers.core.host.service_available(service_name)

Determine whether a system service is available

charmhelpers.core.host.service_reload(service_name, restart_on_failure=False)

Reload a system service, optionally falling back to restart if reload fails

charmhelpers.core.host.service_restart(service_name)

Restart a system service

charmhelpers.core.host.service_running(service)

Determine whether a system service is running

charmhelpers.core.host.service_start(service_name)

Start a system service

charmhelpers.core.host.service_stop(service_name)

Stop a system service

charmhelpers.core.host.set_nic_mtu(nic, mtu)

Set MTU on a network interface

Create a symbolic link

charmhelpers.core.host.umount(mountpoint, persist=False)

Unmount a filesystem

charmhelpers.core.host.write_file(path, content, owner='root', group='root', perms=292)

Create or overwrite a file with the contents of a byte string.