def backup(container, sr_type='local', destination='/var/lxc-backup/'): """ Backup container with tar to a storage repository (SR). E.g: localy or with nfs If SR is localy then the path is /var/lxc-backup/ otherwise if SR is NFS type then we just check if the SR is mounted in host side in /mnt/lxc-backup Returns path/filename of the backup instances """ prefix = time.strftime("%Y-%m-%d__%H-%M.tar.gz") filename = '{}/{}-{}'.format(destination, container, prefix) was_running = False if not exists(container): raise ContainerDoesntExists('Container {} does not exist!'.format(container)) if sr_type == 'local': if not os.path.isdir(destination): raise DirectoryDoesntExists('Directory {} does not exist !'.format(destination)) if sr_type == 'nfs': if not os.path.ismount(destination): raise NFSDirectoryNotMounted('NFS {} is not mounted !'.format(destination)) if info(container)['state'] == 'RUNNING': was_running = True freeze(container) _run('tar czf {} -C /var/lib/lxc {}'.format(filename, container)) if was_running is True: unfreeze(container) return filename
def destroy(container): """ Destroys a container """ if not exists(container): raise ContainerDoesntExists('Container {} does not exists!'.format(container)) return _run('lxc-destroy -n {}'.format(container))
def unfreeze(container): """ Unfreezes a container """ if not exists(container): raise ContainerDoesntExists('Container {} does not exists!'.format(container)) if container not in frozen(): raise ContainerNotRunning('Container {} is not frozen!'.format(container)) return _run('lxc-unfreeze -n {}'.format(container))
def freeze(container): """ Freezes a container """ if not exists(container): raise ContainerDoesntExists('Container {} does not exists!'.format(container)) if container not in running(): raise ContainerNotRunning('Container {} is not running!'.format(container)) return _run('lxc-freeze -n {}'.format(container))
def stop(container): """ Stops a container """ if not exists(container): raise ContainerDoesntExists('Container {} does not exists!'.format(container)) if container in stopped(): raise ContainerNotRunning('Container {} is not running!'.format(container)) return _run('lxc-stop -n {}'.format(container))
def start(container): """ Starts a container """ if not exists(container): raise ContainerDoesntExists('Container {} does not exists!'.format(container)) if container in running(): raise ContainerAlreadyRunning('Container {} is already running!'.format(container)) return _run('lxc-start -dn {}'.format(container))
def snapshot(container, delete_snapshot=False, restore_snapshot=False): """ Create container snapshot """ if not exists(container): raise ContainerDoesntExists('Container {} does not exist!'.format(container)) if delete_snapshot: command = 'lxc-snapshot -n {} -d {}'.format(container, delete_snapshot) elif restore_snapshot: command = 'lxc-snapshot -n {} -r {}'.format(container, restore_snapshot) else: command = 'lxc-snapshot -n {}'.format(container) return _run(command)
def info(container): """ Check info from lxc-info """ if not exists(container): raise ContainerDoesntExists('Container {} does not exist!'.format(container)) output = _run('lxc-info -qn {}'.format(container), output=True).splitlines() state = {'pid': 0} for val in output: key = val.split(b':')[0].lower().strip().replace(b" ", b"_") value = val.split(b':')[1].strip() state[key.decode('utf-8')] = value.decode('utf-8') return state
def snapshots(container): """ List container snapshots """ if not exists(container): raise ContainerDoesntExists('Container {} does not exist!'.format(container)) output = _run('lxc-snapshot -LCn {}'.format(container), output=True) sn_list = [] if output and b'No snapshots' not in output: for val in output.splitlines(): splitted = val.split() snap = {'name':splitted[0].decode('utf-8'),'date':'{} {}'.format(splitted[-2].decode('utf-8'),splitted[-1].decode('utf-8')),'path':splitted[1][1:-1].decode('utf-8')} if os.path.exists(os.path.join(snap['path'],snap['name'])): pass #size = _run('du -sh {}/{}'.format(snap['path'],snap['name']), output=True) #if size: #snap['size'] = size.splitlines()[0].split(b'\t')[0].decode('utf-8') sn_list.append(snap) return sn_list
def cgroup(container, key, value): if not exists(container): raise ContainerDoesntExists('Container {} does not exist!'.format(container)) return _run('lxc-cgroup -n {} {} {}'.format(container, key, value))