Example #1
0
    def get_list(cls, url, version):
        cli = Client(base_url=url, version=version)

        image_list = cli.images(all=True)
        rel = []

        for image in image_list:
            item = {}

            name_tag = image['RepoTags'][0]

            name = name_tag.split(':')[0]
            if name == '<none>':
                continue

            item['name'] = name
            item['tag'] = name_tag.split(':')[1]

            create_time = time.localtime(image['Created'])
            format_time = time.strftime('%Y-%m-%d %H:%M:%S', create_time)

            item['create_time'] = format_time
            item['size'] = format_size(float(image['VirtualSize']))

            rel.append(item)
Example #2
0
 def started(_):
     docker = Client()
     data = docker.inspect_container(self.namespacing_prefix + name)
     self.assertEqual(
         image_name,
         data[u"Config"][u"Image"],
     )
Example #3
0
    def test_pull_timeout_pull(self):
        """
        Image pull timeout does not affect subsequent pulls.
        """
        # Use an image that isn't likely to be in use by anything, since
        # it's old, and isn't used by other tests.  Note, this is the
        # same image as test_pull_image_if_necessary, but they run at
        # different times.
        image = u"busybox:ubuntu-12.04"
        # Make sure image is gone:
        docker = Client()
        try:
            docker.remove_image(image, force=True)
        except APIError as e:
            if e.response.status_code != 404:
                raise

        name = random_name(self)
        client = DockerClient(namespace=self.namespacing_prefix, long_timeout=1)
        self.addCleanup(client.remove, name)
        d = client.add(name, image)

        def unexpected_success(_):
            self.fail("Image unexpectedly pulled within timeout limit")

        def expected_failure(failure):
            self.assertIsNotNone(failure.check(IOError))
            # We got our failure, now try to successfully pull
            client = DockerClient(namespace=self.namespacing_prefix, long_timeout=600)
            return client.add(name, image)

        d.addCallbacks(unexpected_success, expected_failure)
        return d
    def barman_check(self):
        """Connect to the Barman Docker object and check configuration. Error out on failure."""

        docker_client = Client(base_url=self.url)

        try:
            # TODO: verify that the barman container is running
            docker_client.inspect_container(self.container)
        except Exception:
            print('stats err failed when inspecting the container.')
            sys.exit(1)

        try:
            exec_id = docker_client.exec_create(self.container, self.command)
            response = docker_client.exec_start(exec_id)
        except Exception:
            print('stats err failed to execute barman check command in the container.')
            sys.exit(1)

        failed = False
        for line in response.splitlines()[1:]:
            check, value = line.strip().split(': ', 1)
            slug = check.lower().replace(' ', '_').replace('-', '_')
            print('metric {} string {}'.format(slug, value))
            if value.startswith('FAILED'):
                failed = True
        if failed:
            print('status err failure in barman check')
            sys.exit(1)
        print('status ok all checks passed')
Example #5
0
def find_container(needle):
    cli = Client(base_url='unix://var/run/docker.sock')
    for container in cli.containers():
        for name in container['Names']:
            if needle in name:
                return container
    return False
Example #6
0
def cleanup_host(daemon_url, timeout=5):
    """
    Cleanup a container host when use removes the host

    Maybe we will remove the networks?

    :param daemon_url: Docker daemon url
    :param timeout: timeout to wait
    :return:
    """
    if not daemon_url or not daemon_url.startswith("tcp://"):
        logger.error("Invalid daemon_url={}".format(daemon_url))
        return False
    try:
        client = Client(base_url=daemon_url, version="auto", timeout=timeout)
        net_names = [x["Name"] for x in client.networks()]
        for cs_type in CONSENSUS_PLUGINS:
            net_name = CLUSTER_NETWORK + "_{}".format(cs_type)
            if net_name in net_names:
                logger.debug("Remove network {}".format(net_name))
                client.remove_network(net_name)
            else:
                logger.warning("Network {} not exists!".format(net_name))
    except Exception as e:
        logger.error("Exception happens!")
        logger.error(e)
        return False
    return True
Example #7
0
class DockerInfo(object):
  def __init__(self):
    cfg = config.get_instance()
    self.cfg = cfg
    self.log = aLogger.getLogger(self.__class__.__name__, self.cfg)
    self.__url = self.cfg.get("docker.url", check_type=str)
    self._conn = Client(self.__url)

  def get_ip_info(self, name: str):
    name = name.split(".")
    try:
      info = self._conn.inspect_container(name[0])
      if info is not None and "NetworkSettings" in info and "IPAddress" in info["NetworkSettings"]:
          return info["NetworkSettings"]["IPAddress"] if info["NetworkSettings"]["IPAddress"].strip() != "" else None
    except Exception as e:
      return None
    return None

  def container_name_by_ip(self, ip:str):
    containerIDS = self._conn.containers()
    for containerID in containerIDS:
      container_obj = self._conn.inspect_container(containerID)
      if container_obj is not None and "NetworkSettings" in container_obj and "IPAddress" in container_obj["NetworkSettings"]:
        container_ip = container_obj["NetworkSettings"]["IPAddress"] if container_obj["NetworkSettings"]["IPAddress"].strip() else None
        if container_ip == ip:
          return "%s.%s" % (container_obj["Config"]["Hostname"], container_obj["Config"]["Domainname"])
Example #8
0
def detect_daemon_type(daemon_url, timeout=5):
    """ Try to detect the daemon type

    Only wait for timeout seconds.

    :param daemon_url: Docker daemon url
    :param timeout: Time to wait for the response
    :return: host type info
    """
    if not daemon_url or not daemon_url.startswith("tcp://"):
        return None
    segs = daemon_url.split(":")
    if len(segs) != 3:
        logger.error("Invalid daemon url = ", daemon_url)
        return None
    try:
        client = Client(base_url=daemon_url, version="auto", timeout=timeout)
        server_version = client.info()['ServerVersion']
        if server_version.startswith('swarm'):
            return HOST_TYPES[1]
        else:
            return HOST_TYPES[0]
    except Exception as e:
        logger.error(e)
        return None
Example #9
0
def rm_container(docker_client: docker.Client, args, state: dict):
    containers = args.container

    error = 0

    for container in containers:
        if container == '-':
            container = get_last_container(state)
        try:
            docker_client.remove_container(container, link=args.link, v=args.volumes, force=args.force)
            print(container)
        except docker.errors.NotFound:
            pass
        except docker.errors.APIError as e:
            status_code = e.response.status_code
            if status_code < 500:
                print(e.response.content.decode('utf-8').strip(), file=sys.stderr)
                error = errors.INVALID_INPUT
            else:
                print(e.response.content.decode('utf-8').strip(), file=sys.stderr)
                if error == 0:
                    error = errors.DOCKER_ERROR
        except docker.errors.DockerException as e:
            print(e.explanation.decode('utf-8').strip(), file=sys.stderr)
            if error == 0:
                error = errors.DOCKER_ERROR

    if error > 0:
        raise errors.DkrException("There was an error", error)

    if 'last_container' in state:
        state.pop('last_container')
Example #10
0
def stop_container(docker_client: docker.Client, args, state: dict):
    containers = args.container

    error = 0

    for container in containers:
        if container == '-':
            container = get_last_container(state)
        try:
            docker_client.stop(container, timeout=args.timeout)
            print(container)
        except docker.errors.APIError as e:
            status_code = e.response.status_code
            if status_code < 500:
                print(e.response.content.decode('utf-8').strip(), file=sys.stderr)
                error = errors.INVALID_INPUT
            else:
                print(e.response.content.decode('utf-8').strip(), file=sys.stderr)
                if error == 0:
                    error = errors.DOCKER_ERROR
        except docker.errors.DockerException as e:
            print(e.explanation.decode('utf-8').strip(), file=sys.stderr)
            if error == 0:
                error = errors.DOCKER_ERROR

    state['last_container'] = containers[-1]

    if error > 0:
        raise errors.DkrException("There was an error", error)
Example #11
0
def StopContainer():
    if not os.geteuid() == 0:
        print "Command must be run as root"
        sys.exit(-1)
    cli = Client()
    cli.stop('suri-buildbot')
    sys.exit(0)
Example #12
0
    def get_client(self, verbose=False):
        url = docker_url()

        tls = url.split(':')[-1] == '2376'
        if tls:
            url = 'https://' + url.split('://')[-1]

        import requests.packages.urllib3 as urllib3
        urllib3.__version__ = '1.9'

        client = Client(base_url=url, tls=tls)

        if tls:
            client.verify = False

            import os
            cert_home = os.path.join(os.getenv('HOME'), '.docker')
            client.cert = (
                os.path.join(cert_home, 'cert.pem'),
                os.path.join(cert_home, 'key.pem'),
            )

        if verbose:
            version_info = six.iteritems(client.version())
            log.info("Fig version %s", __version__)
            log.info("Docker base_url: %s", client.base_url)
            log.info("Docker version: %s",
                     ", ".join("%s=%s" % item for item in version_info))
            return verbose_proxy.VerboseProxy('docker', client)
        return client
Example #13
0
def getlogs(params):
    
    c = Client(base_url = 'unix://var/run/docker.sock')
    
    if 'stdout' in params:
        params['stdout'] = bool(params['stdout'])
    
    if 'stdin' in params:
        params['stdin'] = bool(params['stdin'])
        
    if 'stream' in params:
        params['stream'] = bool(params['stream'])

    if 'stderr' in params:
        params['stderr'] = bool(params['stderr'])
    
    if 'timestamps' in params:
        params['timestamps'] = bool(params['timestamps'])
    
    if 'tail' in params:
        if isdigit(params['tail']):
            params['tail'] = int(params['tail'])
    
    container = c.logs(**params)
    
    yield  container
           
Example #14
0
 def _pull_image_to_docker_host(self, base_url, image_name, image_version):
     """
     Pull image from private registry to docker host.
     """
     client = Client(base_url=base_url)
     response = [line for line in client.pull(repository=image_name,
             tag=image_version, stream=True)]
Example #15
0
    def _push_image_to_registry(self, base_url, image_name, image_version,
        image_token):
        """
        Push image from docker host to private registry.

        Returns the sha256 digest of the image.
        """
        image_complete_name = '%s:%s' %(image_name, image_version)

        client = Client(base_url=base_url)
        try:
            response = [res for res in client.push(image_complete_name,
                stream=True)]
        except Exception:
            logger.error('Push image %s to registry failed.' %
                image_complete_name)
            return None

        try:
            digest = fetch_digest_from_response(response[-1])
        except Exception:
            logger.error('Parse the digest response error.')
            return None

        return digest
Example #16
0
    def _tag_image_with_new_name(self, base_url, old_image_name,
            old_image_version, image_name, image_version):
        """
        Docker tag old_image_name:old_image_version image_name:image_version.
        """
        client = Client(base_url=base_url)
        old_image = "{}:{}".format(old_image_name, old_image_version)
        try:
            response = client.tag(image=old_image, repository=image_name,
                tag=image_version)
        except Exception as e:
            logger.debug(e)
            response = False
        if not response:
            logger.info("Tag image {} to {}:{} failed.".format(old_image,
                image_name, image_version))
            return None

        image_token = self._get_image_token_on_docker_host(base_url,
            image_name, image_version)

        self._delete_image_on_docker_host(base_url, old_image_name,
            old_image_version)

        return image_token
Example #17
0
    def _import_snapshot_on_docker_host(self, base_url, build_file, image_name,
                image_version='latest'):
        """
        Import container snapshot on the selected docker host.

        'base_url': the url of docker host.
        'build_file': the name of the build file in absolute path.
        'image_name': the name of the image, containing registry address, user
        name and image name.
        'image_version': the version of the image.

        Returns:
        'token': the image token
        """
        self._delete_image_on_docker_host(base_url, image_name, image_version)

        client = Client(base_url=base_url)
        try:
            res_json = client.import_image_from_file(build_file, image_name,
                image_version)
            res = json.loads(res_json)
        except Exception:
            logger.error('import snapshot on docker host %s failed.' % base_url)
            return None

        return res.get('status', None)
Example #18
0
    def _push_image_to_registry(self, base_url, image_name, image_version,
        image_token):
        """
        Push image from docker host to private registry.

        Returns the sha256 digest of the image.
        """
        image_complete_name = '%s:%s' %(image_name, image_version)

        if not self._is_image_on_docker_host(base_url, image_token):
            logger.error('There is no image called %s on docker host %s' %
                (image_complete_name, base_url))
            return None

        client = Client(base_url=base_url)
        try:
            response = [res for res in client.push(image_complete_name,
                stream=True)]
        except Exception:
            logger.error('Communicate with %s failed.' % base_url)
            return None

        try:
            digest = fetch_digest_from_response(response[-1])
        except Exception:
            logger.error('Parse the digest response error.')
            return None

        return digest
Example #19
0
def CreateContainer():
    cli = Client()
    # FIXME check if existing
    print "Pulling docking image, first run should take long"
    cli.pull('regit/suri-buildbot')
    cli.create_container(name='suri-buildbot', image='regit/suri-buildbot', ports=[8010, 22], volumes=['/data/oisf', '/data/buildbot/master/master.cfg'])
    sys.exit(0)
Example #20
0
    def _load_image_on_docker_host(self, base_url, build_file, image_name,
                image_version='latest'):
        """
        Import container snapshot on the selected docker host.

        'base_url': the url of docker host.
        'build_file': the name of the build file in absolute path.
        'image_name': the name of the image, containing registry address, user
        name and image name.
        'image_version': the version of the image.

        Returns:
        'token': the image token
        """
        self._delete_image_on_docker_host(base_url, self.old_image_name,
            self.old_image_version)
        self._delete_image_on_docker_host(base_url, image_name, image_version)

        client = Client(base_url=base_url)
        try:
            with open(build_file, 'rb') as fileobj:
                client.load_image(fileobj)
        except Exception:
            logger.error('load image file on docker host %s failed.' % base_url)
            return None

        return self._tag_image_with_new_name(base_url, self.old_image_name,
            self.old_image_version, image_name, image_version)
class PrometheusHelper(object):
    def __init__(self, template_dir):
        self.clusters = []
        self.template_dir = template_dir
        self.template = self.get_template_path('prometheus/prometheus.conf.tmpl')
        self.target_path = '/etc/gluu/prometheus/prometheus.conf' #TODO: must come from flask config
        self.docker = Client("unix:///var/run/docker.sock")
        self.prometheus_cid = "/var/run/prometheus.cid" #TODO: must come from flask config

    def __load_clusters(self):
        self.clusters = db.all("clusters")

    def __render(self):
        with open(self.template, 'r') as fp:
            tmpl = fp.read()
        template = Template(tmpl)
        rtxt = template.render(clusters = self.clusters)
        with open(self.target_path, 'w') as fp:
            fp.write(rtxt)

    def __restart(self):
        with open(self.prometheus_cid, 'r') as fp:
            cid = fp.read()
        self.docker.restart(container=cid)

    def update(self):
        self.__load_clusters()
        self.__render()
        self.__restart()

    def get_template_path(self, path):
        template_path = os.path.join(self.template_dir, path)
        return template_path
Example #22
0
 def __init__(self, *args, **kw):
     """
     :param timedelta long_timeout: A timeout to use for any request that
         doesn't have any other timeout specified.
     """
     self._long_timeout = kw.pop('long_timeout', None)
     Client.__init__(self, *args, **kw)
Example #23
0
 def _get_container_state(self, host, name):
     docker_cli = Client("tcp://{}:2375".format(host), timeout=1200, version='1.17')
     try:
         if docker_cli.inspect_container(name)['State']['Running']:
             return JobState.up
     except:
         return JobState.destroyed
Example #24
0
    def deploy_project(self, build_id, **kwargs):
        build = Build.objects.get(id=build_id)
        DOCKER_HOST = os.environ['DOCKER_HOST']
        DOCKER_HOST = DOCKER_HOST.replace('tcp', 'https')
        DOCKER_CERT_PATH = os.environ['DOCKER_CERT_PATH']
        cert_dir = os.environ['DOCKER_CERT_PATH'] + '/'
        tls_config = TLSConfig(
            client_cert=(cert_dir + 'cert.pem', cert_dir + 'key.pem'), verify=False
        )
        docker_client = Client(base_url=DOCKER_HOST, tls = tls_config)  
 
        try:
            Build.objects.get(project = build.project, is_active = True)
        except ObjectDoesNotExist:
            pass
        else:  
            old_build = Build.objects.get(project = build.project, is_active = True)
            old_build.is_active = False
            old_build.save()
            docker_client.stop(container = old_build.container)

        response = docker_client.start(container = build.container, port_bindings = { 8080 : build.port }) 
        build.is_active = True
        build.save()
        return response
Example #25
0
    def _get_image_token_on_docker_host(self, base_url, image_name,
        image_version):
        """
        Given the image name and version, return the token of the image on the
        docker host.
        """
        image_complete_name = '%s:%s' %(image_name, image_version)

        logger.debug(image_complete_name)

        client = Client(base_url=base_url)
        try:
            images = client.images()
        except Exception as e:
            logger.debug(e)
            logger.debug("Communicate with docker host {} failed.".format(
                base_url))
            return None

        tokens = [image['Id'] for image in images
            if image_complete_name in image['RepoTags']]

        if not tokens:
            logger.info("The docker host {} has no image {}:{}".format(base_url,
                image_name, image_version))
            return None
        return tokens[0]
def main():
    cli = Client(base_url="unix://var/run/docker.sock")
    hosts = {"static": OrderedDict(), "dynamic": OrderedDict()}

    for container in cli.containers():
        data = container["Image"].split("/")

        if len(data) != 2:
            continue

        host, name = data

        if host == "tellendil":
            if name in ["static", "dynamic"]:
                hosts[name][container["Id"]] = container["NetworkSettings"]["Networks"]["bridge"]["IPAddress"]

    update_conf(safe_get(hosts, "static"), safe_get(hosts, "dynamic"))

    for event in cli.events(decode=True):
        data = event.get("from", "").split("/")

        if len(data) != 2:
            continue

        host, name = data

        if event["Action"] == "die" and host == "tellendil":
            if name in ["static", "dynamic"]:
                hosts[name].pop(event["id"])
                update_conf(safe_get(hosts, "static"), safe_get(hosts, "dynamic"))

        elif event["Action"] == "start" and host == "tellendil":
            if name in ["static", "dynamic"]:
                hosts[name][event["id"]] = cli.inspect_container(event["id"])["NetworkSettings"]["Networks"]["bridge"]["IPAddress"]
                update_conf(safe_get(hosts, "static"), safe_get(hosts, "dynamic"))
Example #27
0
 def __connect_to_default_socket(self):
     try:
         c = Client(base_url='unix:///var/run/docker.sock')
         c.info()
         return c
     except Exception:
         return None
Example #28
0
def main():
    # register the exit signals
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    args = parse_args()
    global hosts_path
    hosts_path = args.file

    docker = Client(base_url='unix://%s'%args.socket)
    events = docker.events(decode=True)
    #get running containers
    for c in docker.containers(quiet=True,all=False):
        container_id = c["Id"]
        container = get_container_data(docker, container_id)
        hosts[container_id] = container

    update_hosts_file()

    #listen for events to keep the hosts file updated
    for e in events:
        status = e["status"];
        if status =="start":
            container_id = e["id"]
            container = get_container_data(docker, container_id)
            hosts[container_id] = container
            update_hosts_file()

        if status=="stop" or status=="die" or status=="destroy":
            container_id = e["id"]
            if container_id in hosts:
                hosts.pop(container_id)
                update_hosts_file()
Example #29
0
def _get_docker():
    global _docker

    if not _docker:
        if sys.platform.startswith('darwin'):
            _machine_check_connectivity()

        # Create the Docker client
        version_client = Client(version=MINIMUM_API_VERSION, **_docker_kwargs)
        try:
            api_version = version_client.version()['ApiVersion']
        except ConnectionError:
            try:
                # workaround for connection issue when old version specified
                # on some clients
                version_client = Client(**_docker_kwargs)
                api_version = version_client.version()['ApiVersion']
            except:
                raise DatacatsError(DOCKER_FAIL_STRING)
        except:
            raise DatacatsError(DOCKER_FAIL_STRING)

        version = get_api_version(DEFAULT_DOCKER_API_VERSION, api_version)
        _docker = Client(version=version, **_docker_kwargs)

    return _docker
Example #30
0
def _get_container(name):
    client = Client()
    for c in client.containers(all=True):
        for container_name in c['Names']:
            if name == container_name:
                return c
    return None
Example #31
0
from flask import Flask, request
from docker import Client
import json
app = Flask(__name__)
docker_cli = Client(base_url='unix://var/run/docker.sock')


@app.route('/start', methods=['POST'])
def start():
    for line in docker_cli.pull('itxtech/docker-env-genisys', stream=True):
        print(json.dumps(json.loads(line), indent=4))

    container = docker_cli.create_container(
        image='itxtech/docker-env-genisys',
        ports=['19133:19132/udp'],
        host_config=docker_cli.create_host_config(binds=[
            '/home/ubuntu/genisys.phar:/srv/genisys/genisys.phar',
            '/home/ubuntu/server.properties:/srv/genisys/server.properties',
        ], ),
        name='mcpe',
        tty=True,
        stdin_open=True,
    )

    response = docker_cli.start(container=container)

    return 'ok'


if __name__ == '__main__':
    app.run('0.0.0.0', debug=True)
Example #32
0
 def __init__(self):
     self.cli = Client(base_url="tcp://192.168.205.125:2376", version="auto")
Example #33
0
import subprocess
import os

from docker import Client

from utils import build_args

cli = Client(base_url='unix:///var/run/docker.sock', tls=False, version='1.18')


def create_subprocess(cmd):
    """
    Create a new subprocess in the OS
    :param cmd: command to execute in the subprocess
    :return: the output and errors of the subprocess
    """
    process = subprocess.Popen(cmd,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               env=os.environ,
                               shell=True)
    return process.communicate()


def docker_run(cmd):
    """
    r = envoy.run(command)
    if r.std_err:
        return r.std_err
    return r.std_out
Example #34
0
 def setUpClass(cls):
     cls.client = Client(docker_url())
     cls.client.pull('busybox', tag='latest')
Example #35
0
class Docker(object):
    def __init__(self,
                 socket,
                 remove_intermediate=False,
                 registry=None,
                 insecure_registry=False):
        log.debug('DockerClient connecting to {}'.format(socket))
        self._socket = socket
        self._client = Client(base_url=socket)
        self._remove_intermediate = remove_intermediate
        self._registry = registry
        self._insecure_registry = insecure_registry

    @exception_safe(ConnectionError, False)
    def build(self, dockerfile, image):
        """/
        :param dockerfile: The dockerfile full path
        :param image: the image name (eg: midolman:1.9)
        """
        log.info('Now building {}'.format(image))
        log.debug('Invoking docker build on {}'.format(dockerfile))

        response = self._client.build(path=os.path.dirname(dockerfile),
                                      tag=image,
                                      pull=False,
                                      rm=self._remove_intermediate,
                                      dockerfile=os.path.basename(dockerfile))

        last_line = None
        for line in response:
            eval_line = eval(line)
            if 'stream' in eval_line:
                print(eval_line['stream']),
                last_line = eval_line['stream']
        # Check for ensure success after building
        if 'Successfully built' not in last_line:
            log.error('Error building the image {}.'.format(image))
            return False
        return True

    @exception_safe(ConnectionError, None)
    def pull(self, image):
        """
        :param image: The image name with its tag
        :param repository: The repository where to pull the image
                          (dockerhub if none defined)
        """
        name, tag = image.split(':')
        repository = '{}/{}'.format(self._registry, name) \
            if self._registry else name
        log.info('Now Pulling {}:{}'.format(repository, tag))
        response = self._client.pull(repository=repository,
                                     tag=tag,
                                     insecure_registry=self._insecure_registry,
                                     stream=True)
        for line in response:
            eval_line = eval(line)
            if 'error' in eval_line:
                log.error('Error pulling image: {}'.format(eval_line['error']))
                return False
            if 'status' in eval_line:
                log.info('[{}:{}] Status: {}'.format(repository, tag,
                                                     eval_line['status']))
        if self._registry:
            # If pulling from an external repo, we need to tag with the
            # actual image name used in the flavours definition.
            images = self.list_images(repository)
            for image in images:
                for repotag in image['RepoTags']:
                    if '{}:{}'.format(repository, tag) == repotag:
                        self._client.tag(image['Id'], name, tag, force=True)
        return True

    @exception_safe(ConnectionError, None)
    def push(self, image):
        """
        :param image: The image name with its tag
        :param repository: The repository where to push the image
                           (dockerhub if none defined)
        """
        name, tag = image.split(':')
        if self._registry:
            # First tag the local image to map to the new registry
            repository = '{}/{}'.format(self._registry, name)
            images = self.list_images(name)
            for image in images:
                for repotag in image['RepoTags']:
                    if '{}:{}'.format(name, tag) == repotag:
                        self._client.tag(image['Id'],
                                         repository,
                                         tag,
                                         force=True)
        else:
            repository = name
        log.info('Now pushing {}:{}'.format(repository, tag))
        response = self._client.push(repository=repository,
                                     tag=tag,
                                     insecure_registry=self._insecure_registry,
                                     stream=True)
        for line in response:
            eval_line = eval(line)
            if 'error' in eval_line:
                log.error('Error pushing image: {}'.format(eval_line['error']))
                return False
            if 'status' in eval_line:
                if 'Pushing' not in eval_line['status'] \
                        and 'Buffering' not in eval_line['status']:
                    log.info('[{}:{}] Status: {}'.format(
                        repository, tag, eval_line['status']))
        return True

    @exception_safe(ConnectionError, [])
    def list_images(self, prefix=None):
        """
        List the available images
        :param prefix: Filter the images by a prefix (eg: "sandbox/")
        :return: the images list
        """
        images = self._client.images()

        if prefix:
            filtered = list()
            for image in images:
                for tag in image['RepoTags']:
                    if tag.startswith(prefix):
                        filtered.append(image)

            images = filtered

        return images

    def list_containers(self, prefix=None):
        """
        List the running containers, prefixed with prefix
        :param prefix: The container's name prefix
        :return: The list of containers
        """
        containers = self._client.containers()
        filtered = list()
        if prefix:
            for container_ref in containers:
                if prefix in container_ref['Names'][0]:
                    filtered.append(container_ref)

            containers = filtered

        return containers

    def container_by_name(self, name):
        containers = self.list_containers()
        for container_ref in containers:
            if name == self.principal_container_name(container_ref):
                return container_ref

    @staticmethod
    def principal_container_name(container_ref):
        for name in container_ref['Names']:
            if '/' not in name[1:]:
                return name[1:]

    def container_ip(self, container_ref):
        return self._client.inspect_container(
            container_ref)['NetworkSettings']['IPAddress']

    def stop_container(self, container_ref):
        self._client.stop(container_ref)

    def kill_container(self, container_ref):
        self._client.kill(container_ref)

    def remove_container(self, container_ref):
        self._client.remove_container(container_ref)

    @exception_safe(OSError, False)
    def execute(self, container_ref, command):
        """
        Execute a command inside the container.

        NOTE: Needs the 'docker' binary installed in the host
        """
        cmd = [
            'docker', 'exec', '-it',
            self.principal_container_name(container_ref), 'env', 'TERM=xterm',
            command
        ]
        log.debug('Running command: "{}"'.format(' '.join(cmd)))
        p = subprocess.Popen(cmd, stderr=subprocess.STDOUT)
        p.wait()

    def ssh(self, container_ref):
        self.execute(container_ref, 'bash')
Example #36
0
class Docker_interface:
    def __init__(self,
                 net_name='tosker_net',
                 tmp_dir='/tmp',
                 socket='unix://var/run/docker.sock'):
        self._log = Logger.get(__name__)
        self._net_name = net_name
        self._cli = Client(base_url=os.environ.get('DOCKER_HOST') or socket)
        self._tmp_dir = tmp_dir

    # TODO: aggiungere un parametro per eliminare i container se esistono gia'!
    def create(self, con, cmd=None, entrypoint=None, saved_image=False):
        def create_container():
            tmp_dir = path.join(self._tmp_dir, con.name)
            try:
                os.makedirs(tmp_dir)
            except:
                pass
            saved_img_name = '{}/{}'.format(self._net_name, con.name)
            img_name = con.image
            if saved_image and self.inspect(saved_img_name):
                img_name = saved_img_name

            self._log.debug('container: {}'.format(con.get_str_obj()))

            con.id = self._cli.create_container(
                name=con.name,
                image=img_name,
                entrypoint=entrypoint if entrypoint else con.entrypoint,
                command=cmd if cmd else con.cmd,
                environment=con.env,
                detach=True,
                # stdin_open=True,
                ports=[key for key in con.ports.keys()] if con.ports else None,
                volumes=['/tmp/dt'] +
                ([k for k, v in con.volume.items()] if con.volume else []),
                networking_config=self._cli.create_networking_config({
                    self._net_name:
                    self._cli.create_endpoint_config(links=con.link
                                                     # ,aliases=['db']
                                                     )
                }),
                host_config=self._cli.create_host_config(
                    port_bindings=con.ports,
                    # links=con.link,
                    binds=[tmp_dir + ':/tmp/dt'] +
                    ([v + ':' + k
                      for k, v in con.volume.items()] if con.volume else []),
                )).get('Id')

        assert isinstance(con, Container)

        if con.to_build:
            self._log.debug('start building..')
            # utility.print_json(
            self._cli.build(path='/'.join(con.dockerfile.split('/')[0:-1]),
                            dockerfile='./' + con.dockerfile.split('/')[-1],
                            tag=con.image,
                            pull=True,
                            quiet=True)
            # )
            self._log.debug('stop building..')
        elif not saved_image:
            # TODO: da evitare se si deve utilizzare un'immagine custom
            self._log.debug('start pulling.. {}'.format(con.image))
            utility.print_json(self._cli.pull(con.image, stream=True),
                               self._log.debug)
            self._log.debug('end pulling..')

        try:
            create_container()
        except errors.APIError as e:
            self._log.debug(e)
            # self.stop(con)
            self.delete(con)
            create_container()
            # raise e

    def stop(self, container):
        name = self._get_name(container)
        try:
            return self._cli.stop(name)
        except errors.NotFound as e:
            self._log.error(e)

    def start(self, container, wait=False):
        name = self._get_name(container)
        self._cli.start(name)
        if wait:
            self._log.debug('wait container..')
            self._cli.wait(name)
            utility.print_byte(self._cli.logs(name, stream=True),
                               self._log.debug)

    def delete(self, container):
        name = self._get_name(container)
        try:
            self._cli.remove_container(name, v=True)
        except (errors.NotFound, errors.APIError) as e:
            self._log.error(e)
            raise e

    def exec_cmd(self, container, cmd):
        name = self._get_name(container)
        if not self.is_running(name):
            return False
        try:
            exec_id = self._cli.exec_create(name, cmd)
            status = self._cli.exec_start(exec_id)

            # TODO: verificare attendibilita' di questo check!
            check = 'rpc error:' != status[:10].decode("utf-8")
            self._log.debug('check: {}'.format(check))
            return check
        except errors.APIError as e:
            self._log.error(e)
            return False
        except requests.exceptions.ConnectionError as e:
            # TODO: questo errore arriva dopo un timeout di 10 secodi
            self._log.error(e)
            return False

    def create_volume(self, volume):
        assert isinstance(volume, Volume)
        self._log.debug('volume opt: {}'.format(volume.get_all_opt()))
        return self._cli.create_volume(volume.name, volume.driver,
                                       volume.get_all_opt())

    def delete_volume(self, volume):
        name = self._get_name(volume)
        return self._cli.remove_volume(name)

    def get_containers(self, all=False):
        return self._cli.containers(all=all)

    def get_volumes(self):
        volumes = self._cli.volumes()
        return volumes['Volumes'] or []

    def inspect(self, item):
        name = self._get_name(item)
        try:
            return self._cli.inspect_container(name)
        except errors.NotFound:
            pass
        try:
            return self._cli.inspect_image(name)
        except errors.NotFound:
            pass
        try:
            return self._cli.inspect_volume(name)
        except errors.NotFound:
            return None

    def remove_all_containers(self):
        for c in self.get_containers(all=True):
            self.stop(c['Id'])
            self.delete(c['Id'])

    def remove_all_volumes(self):
        for v in self.get_volumes():
            self.delete_volume(v['Name'])

    def create_network(self, name, subnet='172.25.0.0/16'):
        # docker network create -d bridge --subnet 172.25.0.0/16 isolated_nw
        # self.delete_network(name)
        try:
            self._cli.create_network(name=name,
                                     driver='bridge',
                                     ipam={'subnet': subnet},
                                     check_duplicate=True)
        except errors.APIError:
            self._log.debug('network already exists!')

    def delete_network(self, name):
        assert isinstance(name, str)
        try:
            self._cli.remove_network(name)
        except errors.APIError:
            self._log.debug('network not exists!')

    def delete_image(self, name):
        assert isinstance(name, str)
        try:
            self._cli.remove_image(name)
        except errors.NotFound:
            pass

    # TODO: splittare questo metodo in due, semantica non chiara!
    def update_container(self, node, cmd, saved_image=True):
        assert isinstance(node, Container)
        # self._log.debug('container_conf: {}'.format(node.host_container))
        stat = self.inspect(node.image)
        old_cmd = stat['Config']['Cmd'] or None
        old_entry = stat['Config']['Entrypoint'] or None

        if self.inspect(node):
            self.stop(node)
            self.delete(node)
        self.create(node, cmd=cmd, entrypoint='', saved_image=saved_image)

        self.start(node.id, wait=True)
        self.stop(node.id)

        name = '{}/{}'.format(self._net_name, node.name)

        self._cli.commit(node.id, name)

        self.stop(node)
        self.delete(node)
        self.create(node,
                    cmd=node.cmd or old_cmd,
                    entrypoint=node.entrypoint or old_entry,
                    saved_image=True)

        self._cli.commit(node.id, name)

    def is_running(self, container):
        name = self._get_name(container)
        stat = self.inspect(name)
        stat = stat is not None and stat['State']['Running'] is True
        self._log.debug('State: {}'.format(stat))
        return stat

    def _get_name(self, name):
        if isinstance(name, six.string_types):
            return name
        else:
            assert isinstance(name, (Container, Volume))
            return name.name
Example #37
0
class Docker:
    def __init__(self):
        self.client = Client(base_url='tcp://192.168.0.2:2376')

    def listar_containers(self):
        containers = self.client.containers(all=True)
        return containers

    def criar_container(self,
                        nome='novo',
                        imagem='ubuntu',
                        comando='/bin/bash'):
        container = self.client.create_container(
            image=imagem,
            command=comando,
            name=nome,
            stdin_open=True,
            tty=True,
            detach=True,
            ports=[80],
            host_config=self.client.create_host_config(port_bindings={80: 80}))
        return container

    def iniciar_container(self, id):
        self.client.start(container=id)
        print 'Container iniciado'

    def parar_container(self, id):
        self.client.stop(container=id)
        print 'Container parado.'

    def rem_container(self, id):
        self.client.stop(container=id)
        self.client.remove_container(container=id)
        print 'Container removido.'

    def exec_comando(self, id, comando):
        exec_id = self.client.exec_create(container=id, cmd=comando)
        resultado = self.client.exec_start(exec_id)
        return resultado

    def inspec_container(self, id):
        container = self.client.inspect_container(container=id)
        return container
Example #38
0
 def __init__(self, url):
     self._cli = Client(base_url=url, version='auto')
Example #39
0
#coding:utf-8

from django.test import TestCase

# Create your tests here.
try:
    from docker import Client
except:
    from docker import APIClient as Client

from registry_api import BASE_REGISTRY_API
c = Client(base_url='tcp://192.168.62.200:2375', version='1.14', timeout=10)
# c = Client(base_url='unix://var/run/docker.sock',version='1.14',timeout=10)
# print c.images()
# import docker

#sssss

url = 'http://192.168.62.200:5000/'  ##私有仓库地址

b = BASE_REGISTRY_API()
print b.get_imageId_info(
    ImageId=
    "sha256:9087edac75d18fbcaffbf6ed3f0fa34e726bd5e6abefe2d4cd0fdf4a493eb43b",
    url=url,
    version=2,
    tag="latest",
    ImageName="shipyard")['data']['history']
# client = docker.from_env()
# print client.containers.run("alpine", ["echo", "hello", "world"])
 def __init__(self, base_url, semaphore):
     self._semaphore = semaphore
     # FIXME we can reuse the client once we know for sure that it is Thread safe...
     self._client = Client(base_url, version='1.19')
Example #41
0
def attach_to_docker_client():
    if os.getenv('DOCKER_HOST') == 'tcp://192.168.59.103:2376':
        c = Client(**kwargs_from_env(assert_hostname=False))
    else:
        c = Client()
    return c
Example #42
0
    def get_client(self, daemon_client):

        return Client(**daemon_client)
Example #43
0
def docker_monitor(host_ip, container_id, network_card):
    try:
        lists = []
        network_alls = []
        memory_alls = []
        cpu_alls = []
        io_alls = []
        times = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        datetimes = int(time.mktime(time.strptime(times, '%Y-%m-%d %H:%M:%S')))
        #    datetimes = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        #    timeStamp = int(time.mktime(datatimes))
        cli = Client(base_url='tcp://%s:5555' % (host_ip), version='1.20')
        stats_obj = cli.stats('%s' % (container_id))
        for i in stats_obj:
            lists.append(i)
            break
        for i in lists:
            i_json = json.loads(i)
            for k, v in i_json.items():
                #            print k
                if re.search('network', k):
                    network = {'network_stats': [v]}
                    network_alls.append(network)
                if re.search('memory_stats', k):
                    memory = {'memory_stats': [v]}
                    #               print memory
                    for memory_i in memory.get('memory_stats'):
                        memory_stats = {
                            'mem_usage':
                            str(round(memory_i.get('usage')) / 1000000),
                            'mem_limit':
                            str(memory_i.get('limit') / 1000000000 * 1000)
                        }

#               memory_alls.append(memory)
                if re.search('cpu_stats', k):
                    cpu = {'cpu_stats': [v]}
                    for cpu_i in cpu.get('cpu_stats'):
                        #                    print cpu
                        #                   for k,v in cpu_i.items():
                        #                       print v
                        #                   if re.search('throttling_data',cpu_i.get('cpu_usage').get('throttling_data')):
                        #                 print cpu_i
                        #                    print cpu_i.get('cpu_usage').get('total_usage')
                        #                    print cpu_i.get('cpu_usage').get('percpu_usage')
                        if int(cpu_i.get('cpu_usage').get(
                                'total_usage')) != 0 and cpu_i.get(
                                    'cpu_usage').get('total_usage') != None:
                            #stats = cpu_i.get('cpu_usage').get('total_usage') / cpu_i.get('cpu_usage').get('percpu_usage')[0]
                            cpu_stats = str(
                                float(
                                    cpu_i.get('cpu_usage').get('total_usage') /
                                    cpu_i.get('cpu_usage').get(
                                        'percpu_usage')[0]) / 100) + '%'
#                        print stats
#                        print cpu_i.get('cpu_usage').get('total_usage')
#                        print cpu_i.get('cpu_usage').get('percpu_usage')[0]
#                        print stats
                        else:
                            pass

#               cpu_alls.append(cpu)
                if re.search('blkio_stats', k):
                    io = {'io_stats': [v]}
                    io_alls.append(io)

        linux = 'python /home/zeusadmin/monitor/monitor_flow.py %s' % network_card
        cli = docker.Client(base_url='tcp://%s:5555' % (host_ip),
                            version='1.20',
                            timeout=10)
        ex = cli.exec_create(container=container_id, cmd=linux, user='******')
        ls = cli.exec_start(exec_id=ex["Id"], tty=True).strip('\r\n')
        if len(ls) == 0:
            flow = u"监控无数据"

        else:
            flow = eval(ls)
#        print ls

#    print memory_stats
#    print cpu_stats
#    print io_alls
#    print flow
        all_stats = {
            'code':
            200,
            'monitor_time':
            datetimes,
            'message': [{
                'memory_stats': memory_stats
            }, {
                'flow_stats': flow
            }, {
                'cpu_stats': {
                    'cpu_usage': cpu_stats
                }
            }]
        }
        return (json.dumps(all_stats, sort_keys=True, indent=4))
#    print stats
#   status = {'code':200,'message': [network_alls,memory_alls,cpu_alls,io_alls]}
#    return (json.dumps(status,sort_keys=True,indent=4))
    except docker.errors.NotFound, e:
        return e
from docker import Client
from json import JSONEncoder
from platform import node

# Generates image inventory data in the same format as the provider (used in unit tests to validate OMI provider)

NUMBYTESPERMB = 1048576

c = Client(base_url="unix://var/run/docker.sock")

imageDict = c.images()
tempDict = dict()

for image in imageDict:
    result = dict()
    result["InstanceID"] = image["Id"]

    name = image["RepoTags"][-1].replace("/", ":").split(":")
    result["Image"] = name[-2]
    result["ImageTag"] = name[-1]
    result["Repository"] = name[0] if len(name) == 3 else ""
    result["Computer"] = node()
    result["Running"] = 0
    result["Stopped"] = 0
    result["Failed"] = 0
    result["Paused"] = 0
    result["Total"] = 0
    result["ImageSize"] = str(image["Size"] / NUMBYTESPERMB) + " MB"
    result["VirtualSize"] = str(image["VirtualSize"] / NUMBYTESPERMB) + " MB"

    tempDict[image["Id"]] = result
Example #45
0
from docker import Client
import json, os, time

# work against docker socket
cli = Client(base_url='unix://var/run/docker.sock', version="auto")


# list containers based on said image, if no app_name provided gets all
def list_containers(app_name=""):
    if app_name == "":
        try:
            return cli.containers(filters="")
        except:
            print "failed getting list of all containers"
            os._exit(2)
    else:
        try:
            app_label = "app_name=" + app_name
            return cli.containers(filters={"label": app_label}, all=True)
        except:
            print "failed getting list of containers where label is app_name=" + app_name
            os._exit(2)


# pull image with optional version tag and registry auth
def pull_image(image_name,
               version_tag="latest",
               registry_user="",
               registry_pass="",
               registry_host=""):
    print "logging in to registry"
                    _id, _id),  # TODO: Detect format
            ])
        }
        container = self.client.create_container(**container)
        work = Work(_id, container, self, fuente)
        work.run()
        return work

    @property
    def name(self):
        return repo_origin(self.repo.name, self.schema["RepoTags"])


if __name__ == "__main__":
    from docker import Client
    c = Client("unix:///var/run/docker.sock")
    esquema = {
        'Created': 1441279808,
        'Labels': {},
        'VirtualSize': 795865269,
        'ParentId':
        '9cce8d0ab3ceadd880b97330a259d362e51e303089fc096db5094f2642585bee',
        'RepoTags': ['iso8601:latest', 'src/iso8601:latest'],
        'RepoDigests': [],
        'Id':
        '15ef989c576bdc0ae8ed721f9c58a850ee050620ed8e9d816dacaffdb050e497',
        'Size': 0
    }

    from utils import make_id
Example #47
0
class SwarmClient(object):

    def __init__(self, target, auth, options, pkey):
        self.target = settings.SWARM_HOST
        # single global connection
        self.registry = settings.REGISTRY_HOST + ':' + settings.REGISTRY_PORT
        self.docker_cli = Client("tcp://{}:2395".format(self.target),
                                 timeout=1200, version='1.17')

    def create(self, name, image, command='', template=None, **kwargs):
        """Create a container"""
        cimage = self.registry + '/' + image
        affinity = "affinity:container!=~/{}*/".format(re.split(r'_v\d.', name)[0])
        l = locals().copy()
        l.update(re.match(MATCH, name).groupdict())
        mem = kwargs.get('memory', {}).get(l['c_type'])
        if mem:
            mem = mem.lower()
            if mem[-2:-1].isalpha() and mem[-1].isalpha():
                mem = mem[:-1]
        cpu = kwargs.get('cpu', {}).get(l['c_type'])
        self.docker_cli.create_container(image=cimage, name=name,
                                         command=command.encode('utf-8'), mem_limit=mem,
                                         cpu_shares=cpu,
                                         environment=[affinity],
                                         host_config={'PublishAllPorts': True})

    def start(self, name):
        """
        Start a container
        """
        self.docker_cli.start(name)

    def stop(self, name):
        """
        Stop a container
        """
        self.docker_cli.stop(name)

    def destroy(self, name):
        """
        Destroy a container
        """
        self.stop(name)
        self.docker_cli.remove_container(name)

    def run(self, name, image, entrypoint, command):
        """
        Run a one-off command
        """
        cimage = self.registry + '/' + image
        # use affinity for nodes that already have the image
        affinity = "affinity:image==~{}".format(cimage)
        self.docker_cli.create_container(image=cimage, name=name,
                                         command=command.encode('utf-8'),
                                         environment=[affinity],
                                         entrypoint=[entrypoint])
        time.sleep(2)
        self.start(name)
        rc = 0
        while (True):
            if self._get_container_state(name) == JobState.created:
                break
            time.sleep(1)
        try:
            output = self.docker_cli.logs(name)
            return rc, output
        except:
            rc = 1
            return rc, output

    def _get_container_state(self, name):
        try:
            if self.docker_cli.inspect_container(name)['State']['Running']:
                return JobState.up
            else:
                return JobState.created
        except:
            return JobState.destroyed

    def state(self, name):
        try:
            for _ in range(30):
                return self._get_container_state(name)
                time.sleep(1)
            # FIXME (smothiki): should be able to send JobState.crashed
        except KeyError:
            return JobState.error
        except RuntimeError:
            return JobState.destroyed

    def attach(self, name):
        """
        Attach to a job's stdin, stdout and stderr
        """
        raise NotImplementedError

    def _get_hostname(self, application_name):
        hostname = settings.UNIT_HOSTNAME
        if hostname == 'default':
            return ''
        elif hostname == 'application':
            # replace underscore with dots, since underscore is not valid in DNS hostnames
            dns_name = application_name.replace('_', '.')
            return dns_name
        elif hostname == 'server':
            raise NotImplementedError
        else:
            raise RuntimeError('Unsupported hostname: ' + hostname)

    def _get_portbindings(self, image):
        dictports = self.docker_cli.inspect_image(image)['ContainerConfig']['ExposedPorts']
        for port, mapping in dictports.items():
            dictports[port] = None
        return dictports

    def _get_ports(self, image):
        dictports = self.docker_cli.inspect_image(image)['ContainerConfig']['ExposedPorts']
        return [int(port.split('/')[0]) for port in dictports.iterkeys()]
Example #48
0
 def __init__(self, target, auth, options, pkey):
     self.target = settings.SWARM_HOST
     # single global connection
     self.registry = settings.REGISTRY_HOST + ':' + settings.REGISTRY_PORT
     self.docker_cli = Client("tcp://{}:2395".format(self.target),
                              timeout=1200, version='1.17')
Example #49
0
 def __init__(self):
     try:
         self.client = Client(base_url="tcp://192.168.0.2:2376",
                              version="auto")
     except Exception as e:
         print "Falhou ao conectar no Docker %s", e
#
# Copyright (C) 2016 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

try:
    from docker import Client
except ImportError:
    from docker import APIClient as Client

dckr = Client(version='auto')
Example #51
0
#coding=utf-8
from flask import render_template, redirect, request, url_for, flash
from . import ws

import time, json
from docker import Client

client = Client(base_url='unix://run/docker.sock')


@ws.route('/echo')
def echo_socket(socket):
    while not socket.closed:
        message = socket.receive()
        if message != '':
            for i in range(10):
                socket.send(message + ':' + str(i))
                time.sleep(1)


@ws.route('/pull')
def pull_images(socket):
    while not socket.closed:
        try:
            image = socket.receive()
            if image == '' or image == None:
                socket.send('Please tell me which image you want to pull.')
            else:
                im = image.split(':')
                if im[1] == '':
                    image += 'latest'
Example #52
0
def get_docker_client():
    if os.environ.get('DOCKER_HOST'):
        kwargs = kwargs_from_env()
        return Client(**kwargs)
    return Client(base_url=get_docker_base_url())
Example #53
0
 def __init__(self, base_master_name, base_slave_name, base_tag,
              tag_decoration):
     super(TagBareImageProvider, self).__init__(tag_decoration)
     self.base_master_name = base_master_name + ":" + base_tag
     self.base_slave_name = base_slave_name + ":" + base_tag
     self.client = Client()
Example #54
0
def pg_server(unused_port_factory, docker: DockerClient, request):
    pg_name = request.config.getoption('--pg-name')
    pg_image = request.config.getoption('--pg-image')
    pg_reuse = request.config.getoption('--pg-reuse')

    container = None
    port = None
    if not pg_name:
        pg_name = 'db-{}'.format(str(uuid.uuid4()))

    if pg_name:
        for item in docker.containers(all=True):
            for name in item['Names']:
                if pg_name in name:
                    container = item
                    break

    if not container:
        port = unused_port_factory()
        docker.pull(pg_image)
        container = docker.create_container(
            image=pg_image,
            name=pg_name,
            ports=[5432],
            host_config=docker.create_host_config(port_bindings={5432: port}),
            detach=True)

    docker.start(container=container['Id'])

    inspection = docker.inspect_container(container['Id'])
    host = inspection['NetworkSettings']['IPAddress']

    if not port:
        ports = inspection['NetworkSettings']['Ports']
        if '5432/tcp' in ports:
            port = ports['5432/tcp'][0]['HostPort']

    pg_params = {
        'database': 'postgres',
        'user': '******',
        'password': '******',
        'host': 'localhost',
        'port': port
    }

    delay = 0.001
    for i in range(100):
        try:
            with psycopg2.connect(**pg_params) as conn:
                with conn.cursor() as cursor:
                    cursor.execute('SELECT version();')
            break
        except psycopg2.Error:
            time.sleep(delay)
            delay *= 2
    else:
        pytest.fail('Cannot start postgres server')

    container['host'] = host
    container['port'] = port
    container['pg_params'] = pg_params

    yield container

    if not pg_reuse:
        docker.kill(container=container['Id'])
        docker.remove_container(container['Id'])
Example #55
0
class Haproxyctl:
    def __init__(self):
        self.__haproxyctl_config_file = '/root/.haproxyctl.cfg'
        self.__haproxy_template_file = 'haproxy.jinja'
        self.__haproxy_config_file = 'haproxy.cfg'
        self.__haproxy_config_path = '/usr/local/etc/haproxy/'
        self.__haproxy_container_id = None
        self.__docker_client = Client(base_url='unix://var/run/docker.sock')

    def get_haproxy_container(self):
        containers = self.__docker_client.containers()
        for container in containers:
            for name in container['Names']:
                if 'haproxy' in name.lower():
                    self.__haproxy_container_id = container['Id']
                    return self.__haproxy_container_id
        return None

    def get_container_ip(self, container_name):
        try:
            container = self.__docker_client.inspect_container(container_name)
            return container['NetworkSettings']['IPAddress']
        except errors.NotFound:
            return None

    def read_config_file(self):
        with open(self.__haproxyctl_config_file, 'w+') as f_handle:
            try:
                json_data = json.load(f_handle)
                return json_data
            except ValueError:
                return []
        return []

    def write_config_file(self, existing_config):
        with open(self.__haproxyctl_config_file, 'w') as f_handle:
            json.dump(existing_config,
                      f_handle,
                      sort_keys=True,
                      indent=4,
                      ensure_ascii=False)

    def add_url(self, url, container_name, port, existing_config):
        if port is None:
            port = 80

        for item in existing_config:
            if type(item) is dict:
                if 'url' in item:
                    if item['url'] == url:
                        item['container_name'] = container_name
                        item['port'] = port
                        return existing_config

        existing_config.append({
            'url': url,
            'container_name': container_name,
            'port': port
        })
        return existing_config

    def remove_url(self, url, existing_config):
        for item in existing_config:
            if type(item) is dict:
                if 'url' in item:
                    if item['url'] == url:
                        existing_config.remove(item)

        return existing_config

    def generate_haproxy_config(self, new_config):
        env = Environment(loader=FileSystemLoader(
            pkg_resources.resource_filename('haproxyctl', 'templates')),
                          trim_blocks=True)
        template = env.get_template(self.__haproxy_template_file)
        f = open(self.__haproxy_config_file, 'w')
        items = []
        counter = 1
        for item in new_config:
            ip = self.get_container_ip(item['container_name'])
            if ip:
                items.append({
                    'id': counter,
                    'url': item['url'],
                    'port': item['port'],
                    'ip': ip
                })
                counter += 1
                print " => [ OK ] Url: '%s', Container: '%s', IP:%s, Port:%s" % (
                    item['url'], item['container_name'], ip, item['port'])
            else:
                print " => [SKIP] Url: '%s', Container: '%s'(not found), IP:N/A, Port:%s" % (
                    item['url'], item['container_name'], item['port'])

        f.write(template.render(items=items))
        f.close()

    def update_haproxy_config(self):
        si = StringIO()
        tar = tarfile.open(mode='w', fileobj=si)
        tar.add(self.__haproxy_config_file)
        tar.close()
        tar_content = si.getvalue()
        self.__docker_client.put_archive(self.__haproxy_container_id,
                                         self.__haproxy_config_path,
                                         tar_content)

    def test_haproxy_config(self):
        cmd = "haproxy -c -f %s%s 2>&1" % (self.__haproxy_config_path,
                                           self.__haproxy_config_file)
        res = self.__docker_client.exec_create(
            container=self.__haproxy_container_id, cmd=['bash', '-c', cmd])
        output = self.__docker_client.exec_start(res)
        output = output.strip()
        if output == "Configuration file is valid":
            print " => Haproxy config is OK"
            return True
        else:
            print " => Haproxy config contains error!"
        return False

    def restart_haproxy_container(self):
        print " => Restarting haproxy..."
        self.__docker_client.restart(container=self.__haproxy_container_id,
                                     timeout=0)
        print " => Restarted."
Example #56
0
from jinja2 import Environment, FileSystemLoader
from docker import Client
import os, shutil, tarfile, io
import xml.etree.ElementTree as ET
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-u",
                    "--update",
                    action='store_true',
                    help="force recreate the keys")
args = parser.parse_args()

client = Client()

HOSTS = {"rpi3": "hypriot", "win-dev": "alpine", "mac-dev": "alpine"}

# build/run the syncthing_cli image to get the configuration
key_pathname = os.path.join(os.getcwd(), "keys")

if args.update:
    key_container = client.create_container(
        "johncclayton/syncthing-cli",
        "/generate-identities.sh",
        environment={"KEY_HOSTS": " ".join(HOSTS.keys())})
    print("Generating keys...")
    key_response = client.start(container=key_container.get('Id'))
    client.wait(container=key_container.get('Id'))

    # now copy the files from within the container to the local disk
    if os.path.exists(key_pathname):
Example #57
0
def get_docker_port(container):
    ports = docker.execute(["netstat", "-tulpn"])[0].split("\n")

    for line in ports:
        if "docker" in line:
            port = line.split()[3].split(":::")[1]

    return port

#Get the port the docker host is listening on
port = get_docker_port(docker)

######## Connect to the docker host ########

#Connect to the remote docker host in the LXC container
dockerclient = Dclient(base_url="tcp://{}:{}".format(host,port), version="auto")

print "Connected to Docker host on {}:{}".format(host,port)

######## Create an docker image ########

print "\nCreating image from Dockerfile\n"

#Create a docker container image from the Dockerfile in this directory
response = dockerclient.build(path=".", dockerfile="Dockerfile", tag="pyne/helloworld")

#Show the output from the build process
for line in response:
    print line

#Show the images now avalible on the docker host
Example #58
0
from docker import Client


cli = Client(base_url="tcp://192.168.205.125:2376",version="auto")


class Docker:

    def __init__(self):
        self.cli = Client(base_url="tcp://192.168.205.125:2376", version="auto")
    
    def get_containers(self, all=True):
        return [ c for c in self.cli.containers(all=all)]

    def create_container(self, name, command, image):
        return self.cli.create_container(
            name=name,
            command=command,
            image=image
    )

    def start_container(self, name):
        return self.cli.start(name)

    def stop_container(self, name):
        return self.cli.stop(name)

    def remove_container(self, name):
        return self.cli.remove_container(name)

    
Example #59
0
 def __init__(self):
     self.client = Client(base_url='tcp://192.168.0.2:2376')
Example #60
0
 def setUpClass(cls):
     cls.client = Client(docker_url())