mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-24 15:36:27 +00:00

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
57 lines
2.6 KiB
Python
57 lines
2.6 KiB
Python
from contextlib import contextmanager
|
|
import logging
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
@contextmanager
|
|
def boot_image(manifest, build_server, bootstrap_info):
|
|
image_id = None
|
|
try:
|
|
import os
|
|
from bootstrapvz.common.tools import log_check_call
|
|
docker_machine = build_server.run_settings.get('docker', {}).get('machine', None)
|
|
docker_env = os.environ.copy()
|
|
if docker_machine is not None:
|
|
cmd = ('eval "$(docker-machine env {machine})" && '
|
|
'echo $DOCKER_HOST && echo $DOCKER_CERT_PATH && echo $DOCKER_TLS_VERIFY'
|
|
.format(machine=docker_machine))
|
|
[docker_host, docker_cert_path, docker_tls] = log_check_call([cmd], shell=True)
|
|
docker_env['DOCKER_TLS_VERIFY'] = docker_tls
|
|
docker_env['DOCKER_HOST'] = docker_host
|
|
docker_env['DOCKER_CERT_PATH'] = docker_cert_path
|
|
docker_env['DOCKER_MACHINE_NAME'] = docker_machine
|
|
from bootstrapvz.remote.build_servers.local import LocalBuildServer
|
|
image_id = bootstrap_info._docker['image_id']
|
|
if not isinstance(build_server, LocalBuildServer):
|
|
import tempfile
|
|
handle, image_path = tempfile.mkstemp()
|
|
os.close(handle)
|
|
remote_image_path = os.path.join('/tmp', image_id)
|
|
try:
|
|
log.debug('Saving remote image to file')
|
|
build_server.remote_command([
|
|
'sudo', 'docker', 'save',
|
|
'--output=' + remote_image_path,
|
|
image_id,
|
|
])
|
|
log.debug('Downloading remote image')
|
|
build_server.download(remote_image_path, image_path)
|
|
log.debug('Importing image')
|
|
log_check_call(['docker', 'load', '--input=' + image_path], env=docker_env)
|
|
except (Exception, KeyboardInterrupt):
|
|
raise
|
|
finally:
|
|
log.debug('Deleting exported image from build server and locally')
|
|
build_server.delete(remote_image_path)
|
|
os.remove(image_path)
|
|
log.debug('Deleting image from build server')
|
|
build_server.remote_command(['sudo', 'docker', 'rmi',
|
|
bootstrap_info._docker['image_id']])
|
|
|
|
from image import Image
|
|
with Image(image_id, docker_env) as container:
|
|
yield container
|
|
finally:
|
|
if image_id is not None:
|
|
log.debug('Deleting image')
|
|
log_check_call(['docker', 'rmi', image_id], env=docker_env)
|