1
0
mirror of https://github.com/projectatomic/atomic.git synced 2026-02-05 18:45:01 +01:00
Files
atomic/Atomic/Import.py
Dan Walsh a8f0e7dca7 Use close_fds=True for all subprocess invocations
The Python default is to keep all FDs open, which is how Unix
traditionally worked. This default behavior is particularly
problematic for the atomic command, which e.g. may
hold a reference to the Docker socket, and we don't
necessarily want to give that reference to all
child processes.

This patch changes most calls to subprocess to be through
util package.  And the util subprocess calls all close_fds=True.

Closes: #370
Approved by: cgwalters
2016-05-03 17:54:31 +00:00

81 lines
2.8 KiB
Python

"""
import docker images, containers and volumes from a filesystem directory.
"""
import os
import sys
from . import util
ATOMIC_LIBEXEC = os.environ.get('ATOMIC_LIBEXEC', '/usr/libexec/atomic')
def import_docker(graph, import_location):
"""
This is a wrapper function for importing docker images, containers
and volumes.
"""
if not os.path.isdir(import_location):
sys.exit("{0} does not exist".format(import_location))
#import docker images
import_images(import_location)
#import docker containers
import_containers(graph, import_location)
#import docker volumes
import_volumes(graph, import_location)
util.write_out("atomic import completed successfully")
util.write_out("Would you like to cleanup (rm -rf {0}) the temporary directory [y/N]"
.format(import_location))
choice = sys.stdin.read(1)
if choice.lower() == 'y':
util.write_out("Deleting {0}".format(import_location))
util.check_call(['/usr/bin/rm', '-rf', import_location])
util.write_out("Please restart docker daemon for the changes to take effect")
def import_images(import_location):
"""
Method for importing docker images from a filesystem directory.
"""
subdir = import_location + '/images'
images = os.listdir(subdir)
for image in images:
util.write_out("Importing image: {0}".format(image[:12]))
with open(subdir + '/' + image) as f:
util.check_call([util.default_docker(), "load"], stdin=f)
def import_containers(graph, import_location):
"""
Method for importing docker containers from a filesystem directory.
"""
subdir = import_location + '/containers'
containers = os.listdir(subdir)
for cnt in containers:
cnt = cnt[8:] # strip off the "migrate-" prefix
util.write_out("Importing container: {0}".format(cnt[:12]))
util.check_call([ATOMIC_LIBEXEC + '/migrate.sh',
'import',
'--container-id=' + cnt,
'--graph=' + graph,
'--import-location=' + import_location])
def tar_extract(srcfile, destdir):
util.check_call(['/usr/bin/tar', '--extract', '--gzip', '--selinux',
'--file', srcfile, '--directory', destdir])
def import_volumes(graph, import_location):
"""
Method for importing docker volumes from a filesystem directory.
"""
volfile = import_location + '/volumes/volumeData.tar.gz'
if os.path.isfile(volfile):
util.write_out("Importing volumes")
tar_extract(srcfile=volfile,
destdir=graph + '/volumes')
vfsfile = import_location + '/volumes/vfsData.tar.gz'
if os.path.isfile(vfsfile) and os.path.isdir(graph + "/vfs"):
tar_extract(srcfile=vfsfile,
destdir=graph + '/vfs')