bootstrap-vz/tests/system/providers/docker/image.py
Anders Ingemann f62c8ade99 Convert indentation from tabs to spaces (4)
Up until now I didn't see the point of using spaces for indentation.
However, the previous commit (a18bec3) was quite eye opening.
Given that python is an indentation aware language, the amount of
mistakes that went unnoticed because tabs and spaces were used
at the same time (tabs for indentation and spaces for alignment)
were unacceptable.

E101,W191 have been re-enable in the tox flake8 checker and
the documentation has been modified accordingly.

The following files have been left as-is:
* bootstrapvz/common/assets/extlinux/extlinux.conf
* bootstrapvz/common/assets/init.d/expand-root
* bootstrapvz/common/assets/init.d/generate-ssh-hostkeys
* bootstrapvz/common/assets/init.d/squeeze/generate-ssh-hostkeys
* bootstrapvz/plugins/docker_daemon/assets/init.d/docker
* bootstrapvz/providers/ec2/assets/bin/growpart
* bootstrapvz/providers/ec2/assets/grub.d/40_custom
* bootstrapvz/providers/ec2/assets/init.d/ec2-get-credentials
* bootstrapvz/providers/ec2/assets/init.d/ec2-run-user-data
* docs/_static/taskoverview.coffee
* docs/_static/taskoverview.less
* tests/unit/subprocess.sh
2016-06-04 11:38:16 +02:00

55 lines
1.6 KiB
Python

from bootstrapvz.common.tools import log_check_call
import logging
log = logging.getLogger(__name__)
class Image(object):
def __init__(self, image_id, docker_env):
self.image_id = image_id
self.docker_env = docker_env
def __enter__(self):
self.container = Container(self.image_id, self.docker_env)
self.container.create()
try:
self.container.start()
except:
self.container.destroy()
raise
return self.container
def __exit__(self, exc_type, exc_value, traceback):
try:
self.container.stop()
self.container.destroy()
except Exception as e:
log.exception(e)
class Container(object):
def __init__(self, image_id, docker_env):
self.image_id = image_id
self.docker_env = docker_env
def create(self):
log.debug('Creating container')
[self.container_id] = log_check_call(['docker', 'create', '--tty=true', self.image_id], env=self.docker_env)
def start(self):
log.debug('Starting container')
log_check_call(['docker', 'start', self.container_id], env=self.docker_env)
def run(self, command):
log.debug('Running command in container')
return log_check_call(['docker', 'exec', self.container_id] + command, env=self.docker_env)
def stop(self):
log.debug('Stopping container')
log_check_call(['docker', 'stop', self.container_id], env=self.docker_env)
def destroy(self):
log.debug('Deleting container')
log_check_call(['docker', 'rm', self.container_id], env=self.docker_env)
del self.container_id