6466N/A#!/usr/bin/python2.7
6466N/A#
6466N/A# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
6466N/A#
6466N/A
6466N/Aimport argparse
7175N/Aimport httplib
7175N/Aimport json
7175N/Afrom multiprocessing.dummy import Pool as ThreadPool
6466N/Aimport os
7116N/Aimport shutil
7175N/Aimport socket
6466N/Afrom subprocess import Popen, PIPE
6466N/Aimport sys
7116N/Aimport tempfile
6466N/A
7175N/ADOCKER_SOCK = "/var/run/docker/docker.sock"
7116N/AROOTFS_ARCHIVE = "rootfs.tar.gz"
6466N/ADOCKERFILE = """FROM scratch
6466N/AADD %(archive)s /
6466N/ACMD /bin/bash
6466N/A"""
6466N/A
7175N/A
7175N/Aclass HTTPConnectionSocket(httplib.HTTPConnection):
7175N/A """HTTPConnection for local UNIX sockets."""
7175N/A def __init__(self, sock_path):
7175N/A httplib.HTTPConnection.__init__(self, 'localhost')
7175N/A self.sock_path = sock_path
7175N/A
7175N/A def connect(self):
7175N/A # superclass uses self.sock, he'll handle cleanup
7175N/A self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
7175N/A self.sock.connect(self.sock_path)
7175N/A
7175N/A
7175N/Adef do_api_get(url):
7175N/A try:
7175N/A con = HTTPConnectionSocket(DOCKER_SOCK)
7175N/A con.request("GET", url)
7175N/A resp = con.getresponse()
7175N/A except httplib.HTTPException as err:
7175N/A raise RuntimeError("unable to call API: %s" % err)
7175N/A
7175N/A # expect standard success status
7175N/A if resp.status != 200:
7175N/A raise RuntimeError("Unable to query Docker API: status [%s] "
7175N/A "reason [%s]" % (resp.status, resp.reason))
7175N/A
7175N/A try:
7175N/A data = json.loads(resp.read())
7175N/A except Exception as err:
7175N/A raise RuntimeError("unexpected error parsing JSON: %s" % err)
7175N/A
7175N/A con.close()
7175N/A return data
7175N/A
7175N/A
7175N/Adef do_api_post(url):
7175N/A try:
7175N/A con = HTTPConnectionSocket(DOCKER_SOCK)
7175N/A con.request("POST", url)
7175N/A resp = con.getresponse()
7175N/A except httplib.HTTPException as err:
7175N/A raise RuntimeError("unable to call API: %s" % err)
7175N/A
7175N/A # expect success and no content
7175N/A if resp.status != 204:
7175N/A raise RuntimeError("Unable to query Docker API: status [%s] "
7175N/A "reason [%s]" % (resp.status, resp.reason))
7175N/A con.close()
7175N/A
7175N/A
6466N/Aclass DockerSupportCmd(object):
6466N/A def __init__(self, cmd, verbose=False):
6466N/A self.cmd = cmd
6466N/A self.verbose = verbose
6466N/A
6466N/A def run(self, expect_nonzero=None):
6466N/A if self.verbose:
6466N/A out = None
6466N/A else:
6466N/A out = PIPE
6466N/A p = Popen(self.cmd, stdout=out, stderr=PIPE)
6466N/A output, error = p.communicate()
6466N/A if not expect_nonzero and p.returncode != 0:
6466N/A raise RuntimeError(error)
6466N/A return output
6466N/A
6466N/A
6466N/Adef docker_is_online():
6466N/A try:
7175N/A status = DockerSupportCmd(['/usr/bin/svcs', '-Ho', 'state',
7175N/A 'docker']).run().strip()
7175N/A return status.startswith('online') or status.startswith('degraded')
6466N/A except Exception as err:
7175N/A raise RuntimeError("Unable to determine service status: %s" % err)
6466N/A
6466N/A
6466N/Adef get_os_version():
6466N/A try:
6523N/A output = DockerSupportCmd(['/usr/bin/pkg', 'info', '-r',
6523N/A 'osnet/osnet-incorporation']).run()
6466N/A for line in map(str.strip, output.splitlines()):
6466N/A if line.startswith("Branch"):
6466N/A return line.split(":")[1].strip()
6466N/A except Exception as err:
6466N/A raise RuntimeError("Unable to determine version: %s" % err)
6466N/A
6466N/A
6524N/Adef create_rootfs_archive(args):
6466N/A cmd = ['/usr/lib/brand/solaris-oci/mkimage-solaris']
6524N/A if args.devbuild:
6524N/A cmd.append('-D')
6524N/A if args.profile:
6524N/A if not os.path.exists(args.profile):
6524N/A raise RuntimeError("'%s' not found" % args.profile)
6524N/A cmd.extend(['-c', args.profile])
6466N/A try:
7116N/A # build rootfs, send output to stdout
6466N/A DockerSupportCmd(cmd, verbose=True).run()
6466N/A except Exception as err:
6466N/A raise RuntimeError("mkimage-solaris failure: %s" % err)
6466N/A
6466N/A
6466N/Adef create_base_image(args):
6466N/A if not docker_is_online():
6466N/A raise SystemExit("Docker service not online, is Docker configured?")
6466N/A
7116N/A try:
7116N/A temp_dir = tempfile.mkdtemp(dir="/system/volatile")
7116N/A except Exception as err:
7116N/A raise SystemExit("Could not create build directory: %s" % err)
6466N/A
6466N/A try:
6466N/A print "Creating container rootfs from host publishers..."
7116N/A create_rootfs_archive(args)
6466N/A except Exception as err:
6466N/A raise SystemExit("Failed to create rootfs: %s" % err)
6466N/A
7116N/A shutil.move(ROOTFS_ARCHIVE, temp_dir)
6466N/A
7116N/A prev_dir = os.getcwd()
7116N/A os.chdir(temp_dir)
7116N/A try:
7116N/A osversion = get_os_version()
7116N/A with open("Dockerfile", "w") as dockerfile:
7116N/A dockerfile.write(DOCKERFILE %
7116N/A {"archive": ROOTFS_ARCHIVE, "osversion": osversion})
7116N/A
7116N/A tag = "solaris:%s" % osversion
7116N/A print "Creating Docker base image '%s'..." % tag
7116N/A
6466N/A DockerSupportCmd(
6466N/A ["/usr/bin/docker", "build", "-t", tag, "."], verbose=True).run()
6466N/A DockerSupportCmd(
6466N/A ["/usr/bin/docker", "tag", tag, "solaris:latest"]).run()
7116N/A print "Build complete."
6466N/A except Exception as err:
6466N/A raise SystemExit("Failed image build: %s" % err)
7116N/A finally:
7116N/A os.chdir(prev_dir)
7116N/A assert os.path.exists(temp_dir)
7116N/A shutil.rmtree(temp_dir)
6466N/A
6466N/A
7175N/Adef get_running_container_ids():
7175N/A try:
7175N/A return [container['Id'] for container in
7175N/A do_api_get("http:/containers/json")]
7175N/A except RuntimeError as e:
7175N/A print "unable to query api for container list: %s" % e
7175N/A
7175N/A
7175N/Adef kill_container(cid):
7175N/A try:
7175N/A do_api_post("http:/containers/%s/kill" % cid)
7175N/A print "shutdown container [%s]" % cid
7175N/A except RuntimeError as e:
7175N/A print "unable to kill container [%s]: %s" % (cid, e)
7175N/A
7175N/A
7175N/Adef shutdown_containers(args):
7175N/A print "Shutting down all running container instances..."
7175N/A if not docker_is_online():
7175N/A return
7175N/A
7175N/A pool = ThreadPool(64)
7175N/A pool.map(kill_container, get_running_container_ids())
7175N/A pool.close()
7175N/A pool.join()
7175N/A
7175N/A if get_running_container_ids():
7175N/A print "NOTE: unable to gracefully shutdown all containers"
7175N/A
7175N/A
6466N/Adef build_parser():
6466N/A parser_main = argparse.ArgumentParser()
6466N/A parser_main.add_argument("-v", "--version", action="version",
6466N/A version="%(prog)s 0.1")
6466N/A
6466N/A subparsers = parser_main.add_subparsers(title="sub-commands", metavar="")
6466N/A
6466N/A parser_create = subparsers.add_parser("create-base-image",
6466N/A help="create a base image from host publisher content",
6466N/A usage=argparse.SUPPRESS)
6524N/A parser_create.add_argument("-D", "--devbuild", action="store_true",
6524N/A help="use development build options for the package image")
6466N/A parser_create.add_argument("-p", "--profile",
6466N/A help="TEMPORARY: optional syconfig profile")
7175N/A parser_create.set_defaults(func=create_base_image)
6466N/A
7175N/A parser_shutdown = subparsers.add_parser("shutdown-containers",
7175N/A help="gracefully kill all running container instances",
7175N/A usage=argparse.SUPPRESS)
7175N/A parser_shutdown.set_defaults(func=shutdown_containers)
6466N/A
6466N/A return parser_main
6466N/A
6466N/A
6466N/Adef main():
6466N/A parser = build_parser()
6466N/A args = parser.parse_args()
6466N/A if not vars(args):
6466N/A raise SystemExit(parser.print_help())
6466N/A return args.func(args)
6466N/A
6466N/A
6466N/Aif __name__ == "__main__":
6466N/A sys.exit(main())