#!/usr/bin/python2.7
#
# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
#

import argparse
import httplib
import json
from multiprocessing.dummy import Pool as ThreadPool 
import os
import shutil
import socket
from subprocess import Popen, PIPE
import sys
import tempfile

DOCKER_SOCK = "/var/run/docker/docker.sock"
ROOTFS_ARCHIVE  = "rootfs.tar.gz"
DOCKERFILE = """FROM scratch
ADD %(archive)s /
CMD /bin/bash
"""


class HTTPConnectionSocket(httplib.HTTPConnection):
    """HTTPConnection for local UNIX sockets."""
    def __init__(self, sock_path):
        httplib.HTTPConnection.__init__(self, 'localhost')
        self.sock_path = sock_path

    def connect(self):
        # superclass uses self.sock, he'll handle cleanup
        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        self.sock.connect(self.sock_path)


def do_api_get(url):
    try:
        con = HTTPConnectionSocket(DOCKER_SOCK)
        con.request("GET", url)
        resp = con.getresponse()
    except httplib.HTTPException as err:
        raise RuntimeError("unable to call API: %s" % err)

    # expect standard success status
    if resp.status != 200:
        raise RuntimeError("Unable to query Docker API: status [%s] "
            "reason [%s]" % (resp.status, resp.reason))

    try:
        data = json.loads(resp.read())
    except Exception as err:
        raise RuntimeError("unexpected error parsing JSON: %s" % err)

    con.close()
    return data


def do_api_post(url):
    try:
        con = HTTPConnectionSocket(DOCKER_SOCK)
        con.request("POST", url)
        resp = con.getresponse()
    except httplib.HTTPException as err:
        raise RuntimeError("unable to call API: %s" % err)

    # expect success and no content
    if resp.status != 204:
        raise RuntimeError("Unable to query Docker API: status [%s] "
            "reason [%s]" % (resp.status, resp.reason))
    con.close()


class DockerSupportCmd(object):
    def __init__(self, cmd, verbose=False):
        self.cmd = cmd
        self.verbose = verbose

    def run(self, expect_nonzero=None):
        if self.verbose:
            out = None
        else:
            out = PIPE
        p = Popen(self.cmd, stdout=out, stderr=PIPE)
        output, error = p.communicate()
        if not expect_nonzero and p.returncode != 0:
            raise RuntimeError(error)
        return output


def docker_is_online():
    try:
        status = DockerSupportCmd(['/usr/bin/svcs', '-Ho', 'state',
            'docker']).run().strip()
        return status.startswith('online') or status.startswith('degraded')
    except Exception as err:
        raise RuntimeError("Unable to determine service status: %s" % err)


def get_os_version():
    try:
        output = DockerSupportCmd(['/usr/bin/pkg', 'info', '-r',
            'osnet/osnet-incorporation']).run()
        for line in map(str.strip, output.splitlines()):
            if line.startswith("Branch"):
                return line.split(":")[1].strip()
    except Exception as err:
        raise RuntimeError("Unable to determine version: %s" % err)


def create_rootfs_archive(args):
    cmd = ['/usr/lib/brand/solaris-oci/mkimage-solaris']
    if args.devbuild:
        cmd.append('-D')
    if args.profile:
        if not os.path.exists(args.profile):
            raise RuntimeError("'%s' not found" % args.profile)
        cmd.extend(['-c', args.profile])
    try:
        # build rootfs, send output to stdout
        DockerSupportCmd(cmd, verbose=True).run()
    except Exception as err:
        raise RuntimeError("mkimage-solaris failure: %s" % err)


def create_base_image(args):
    if not docker_is_online():
        raise SystemExit("Docker service not online, is Docker configured?")

    try:
        temp_dir = tempfile.mkdtemp(dir="/system/volatile")
    except Exception as err:
        raise SystemExit("Could not create build directory: %s" % err)

    try:
        print "Creating container rootfs from host publishers..."
        create_rootfs_archive(args)
    except Exception as err:
        raise SystemExit("Failed to create rootfs: %s" % err)

    shutil.move(ROOTFS_ARCHIVE, temp_dir)

    prev_dir = os.getcwd()
    os.chdir(temp_dir)
    try:
        osversion = get_os_version()
        with open("Dockerfile", "w") as dockerfile:
            dockerfile.write(DOCKERFILE %
                {"archive": ROOTFS_ARCHIVE, "osversion": osversion})

        tag = "solaris:%s" % osversion
        print "Creating Docker base image '%s'..." % tag

        DockerSupportCmd(
            ["/usr/bin/docker", "build", "-t", tag, "."], verbose=True).run()
        DockerSupportCmd(
            ["/usr/bin/docker", "tag", tag, "solaris:latest"]).run()
        print "Build complete."
    except Exception as err:
        raise SystemExit("Failed image build: %s" % err)
    finally:
        os.chdir(prev_dir)
        assert os.path.exists(temp_dir)
        shutil.rmtree(temp_dir)


def get_running_container_ids():
    try:
        return [container['Id'] for container in
            do_api_get("http:/containers/json")]
    except RuntimeError as e:
        print "unable to query api for container list: %s" % e


def kill_container(cid):
    try:
        do_api_post("http:/containers/%s/kill" % cid)
        print "shutdown container [%s]" % cid
    except RuntimeError as e:
        print "unable to kill container [%s]: %s" % (cid, e)


def shutdown_containers(args):
    print "Shutting down all running container instances..."
    if not docker_is_online():
        return

    pool = ThreadPool(64)
    pool.map(kill_container, get_running_container_ids())
    pool.close()
    pool.join()

    if get_running_container_ids():
        print "NOTE: unable to gracefully shutdown all containers"


def build_parser():
    parser_main = argparse.ArgumentParser()
    parser_main.add_argument("-v", "--version", action="version",
        version="%(prog)s 0.1")

    subparsers = parser_main.add_subparsers(title="sub-commands", metavar="")

    parser_create = subparsers.add_parser("create-base-image",
        help="create a base image from host publisher content",
        usage=argparse.SUPPRESS)
    parser_create.add_argument("-D", "--devbuild", action="store_true",
        help="use development build options for the package image")
    parser_create.add_argument("-p", "--profile",
        help="TEMPORARY: optional syconfig profile")
    parser_create.set_defaults(func=create_base_image)

    parser_shutdown = subparsers.add_parser("shutdown-containers",
        help="gracefully kill all running container instances",
        usage=argparse.SUPPRESS)
    parser_shutdown.set_defaults(func=shutdown_containers)

    return parser_main


def main():
    parser = build_parser()
    args = parser.parse_args()
    if not vars(args):
        raise SystemExit(parser.print_help())
    return args.func(args)


if __name__ == "__main__":
    sys.exit(main())
