docker-support revision 6524
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
6466N/Aimport os
6466N/Afrom subprocess import Popen, PIPE
6466N/Aimport sys
6466N/A
6466N/A
6466N/ADOCKERFILE = """FROM scratch
6466N/AADD %(archive)s /
6466N/ALABEL vendor="Oracle USA"
6466N/ALABEL com.oracle.solaris.version.release="beta"
6466N/ALABEL com.oracle.solaris.version.branch="%(osversion)s"
6466N/ACMD /bin/bash
6466N/A"""
6466N/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:
6466N/A return DockerSupportCmd(['/usr/bin/svcs', '-Ho', 'state',
6466N/A 'docker']).run().strip() == 'online'
6466N/A except Exception as err:
6466N/A raise RuntimeError("Unable to determine version: %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 # we'll build the default archive, make sure we don't clobber one
6466N/A if os.path.exists("rootfs.tar.gz"):
6466N/A raise RuntimeError("archive already exists 'rootfs.tar.gz'")
6466N/A
6466N/A # build here with mkimage, send output to stdout
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:
6466N/A DockerSupportCmd(cmd, verbose=True).run()
6466N/A return "rootfs.tar.gz"
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
6466N/A if os.path.exists("Dockerfile"):
6466N/A raise SystemExit("Dockerfile already exists in working directory.")
6466N/A
6466N/A try:
6466N/A print "Creating container rootfs from host publishers..."
6524N/A rootfs = create_rootfs_archive(args)
6466N/A except Exception as err:
6466N/A raise SystemExit("Failed to create rootfs: %s" % err)
6466N/A
6466N/A osversion = get_os_version()
6466N/A with open("Dockerfile", "w") as dockerfile:
6466N/A dockerfile.write(
6466N/A DOCKERFILE % {"archive": rootfs, "osversion": osversion})
6466N/A
6466N/A tag = "solaris:%s" % osversion
6466N/A print "Creating Docker base image '%s'..." % tag
6466N/A try:
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()
6466N/A except Exception as err:
6466N/A raise SystemExit("Failed image build: %s" % err)
6522N/A
6522N/A assert os.path.exists("rootfs.tar.gz")
6522N/A os.unlink("rootfs.tar.gz")
6466N/A print "Build complete."
6466N/A
6466N/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")
6466N/A
6466N/A parser_create.set_defaults(func=create_base_image)
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())