Manage docker instance
965925
def docker(gandi, vm, args):
    
    if not [basedir for basedir in os.getenv('PATH', '.:/usr/bin').split(':')
            if os.path.exists('%s/docker' % basedir)]:
        gandi.echo("""'docker' not found in $PATH, required for this command \
to work
See https://docs.docker.com/installation/#installation to install, or use:
    # curl https://get.docker.io/ | sh""")
        return

    if vm:
        gandi.configure(True, 'dockervm', vm)
    else:
        vm = gandi.get('dockervm')

    if not vm:
        gandi.echo("""
No docker vm specified. You can create one:
    $ gandi vm create --hostname docker --image "Ubuntu 14.04 64 bits LTS (HVM)" \\
        --run 'wget -O - https://get.docker.io/ | sh'

Then configure it using:
    $ gandi docker --vm docker ps

Or to both change target vm and spawn a process (note the -- separator):
    $ gandi docker --vm myvm -- run -i -t debian bash
""")  # noqa
        return

    return gandi.docker.handle(vm, args)
671722
def clone_with_new_dockerfile(self, conf, docker_file):
        """"""
        log.info("Copying context to add a different dockerfile")
        self.close()
        with a_temp_file() as tmpfile:
            old_t = os.stat(self.tmpfile.name).st_size > 0
            if old_t:
                shutil.copy(self.tmpfile.name, tmpfile.name)

            with tarfile.open(tmpfile.name, mode="a") as t:
                conf.add_docker_file_to_tarfile(docker_file, t)
                yield ContextWrapper(t, tmpfile)
278091
def prepare(self):
        ''''''
        self.__make_scubadir()

        if self.is_remote_docker:
            '''
            Docker is running remotely (e.g. boot2docker on OSX).
            We don't need to do any user setup whatsoever.

            TODO: For now, remote instances won't have any .scubainit

            See:
            https://github.com/JonathonReinhart/scuba/issues/17
            '''
            raise ScubaError('Remote docker not supported (DOCKER_HOST is set)')

        # Docker is running natively
        self.__setup_native_run()

        # Apply environment vars from .scuba.yml
        self.env_vars.update(self.context.environment)
404548
def cli(env, **args):
    """"""
    create_args = _parse_create_args(env.client, args)

    create_args['primary_disk'] = args.get('primary_disk')
    manager = CapacityManager(env.client)
    capacity_id = args.get('capacity_id')
    test = args.get('test')

    result = manager.create_guest(capacity_id, test, create_args)

    env.fout(_build_receipt(result, test))
82853
def build_docker(ctx, image):
    
    if image not in DOCKER_IMGS:
        print('Error: unknown docker image "{0}"!'.format(image), file=sys.stderr)
        sys.exit(1)

    dinfo = DOCKER_IMGS[image]
    ctx.run('docker rmi -f {0}'.format(dinfo['name']), warn=True)
    dinfo_work_dir = os.path.join(dinfo['path'], '_work')
    ctx.run('mkdir -p {0}'.format(dinfo_work_dir))
    ctx.run('cp {0} {1}'.format(os.path.join(DOCKER_COMMON_DIR, 'sudo-as-user.sh'), dinfo_work_dir))
    ctx.run('docker build -t {0} {1}'.format(dinfo['name'], dinfo['path']))
731573
def connect(self):
        """"""
        try:
            ret = docker.from_env()
        except Exception as e:
            logger.error("docker plugin - Can not connect to Docker ({})".format(e))
            ret = None

        return ret
789036
def create(self):
        """"""

        try:
            image_infos = yield from self._get_image_information()
        except DockerHttp404Error:
            log.info("Image %s is missing pulling it from docker hub", self._image)
            yield from self.pull_image(self._image)
            image_infos = yield from self._get_image_information()
            if image_infos is None:
                raise DockerError("Can't get image informations, please try again.")

        params = {
            "Hostname": self._name,
            "Name": self._name,
            "Image": self._image,
            "NetworkDisabled": True,
            "Tty": True,
            "OpenStdin": True,
            "StdinOnce": False,
            "HostConfig": {
                "CapAdd": ["ALL"],
                "Privileged": True,
                "Binds": self._mount_binds(image_infos)
            },
            "Volumes": {},
            "Env": ["container=docker"],  # Systemd compliant: https://github.com/GNS3/gns3-server/issues/573
            "Cmd": [],
            "Entrypoint": image_infos.get("Config", {"Entrypoint": []})["Entrypoint"]
        }

        if params["Entrypoint"] is None:
            params["Entrypoint"] = []
        if self._start_command:
            params["Cmd"] = shlex.split(self._start_command)
        if len(params["Cmd"]) == 0:
            params["Cmd"] = image_infos.get("Config", {"Cmd": []})["Cmd"]
            if params["Cmd"] is None:
                params["Cmd"] = []
        if len(params["Cmd"]) == 0 and len(params["Entrypoint"]) == 0:
            params["Cmd"] = ["/bin/sh"]
        params["Entrypoint"].insert(0, "/gns3/init.sh")  # FIXME /gns3/init.sh is not found?

        # Give the information to the container on how many interface should be inside
        params["Env"].append("GNS3_MAX_ETHERNET=eth{}".format(self.adapters - 1))
        # Give the information to the container the list of volume path mounted
        params["Env"].append("GNS3_VOLUMES={}".format(":".join(self._volumes)))

        if self._environment:
            for e in self._environment.strip().split("\n"):
                e = e.strip()
                if not e.startswith("GNS3_"):
                    params["Env"].append(e)

        if self._console_type == "vnc":
            yield from self._start_vnc()
            params["Env"].append("QT_GRAPHICSSYSTEM=native")  # To fix a Qt issue: https://github.com/GNS3/gns3-server/issues/556
            params["Env"].append("DISPLAY=:{}".format(self._display))
            params["HostConfig"]["Binds"].append("/tmp/.X11-unix/:/tmp/.X11-unix/")

        result = yield from self.manager.query("POST", "containers/create", data=params)
        self._cid = result['Id']
        log.info("Docker container '{name}' [{id}] created".format(
            name=self._name, id=self._id))
        return True
521667
def handle(self, *args, **options):
        """"""
        verbosity = int(options.get('verbosity'))

        # Check that the specified output format is valid
        if options['format'] != 'plain' and options['format'] != 'yaml':
            raise CommandError("Unknown output format: %s" % options['format'])

        # Gather only unique latest custom Docker requirements that the processes are using
        # The 'image' field is optional, so be careful about that as well
        unique_docker_images = set(
            p.requirements['executor']['docker']['image']
            for p in Process.objects.filter(is_active=True).order_by(
                'slug', '-version'
            ).distinct(
                'slug'
            ).only(
                'requirements'
            ).filter(
                requirements__icontains='docker'
            )
            if 'image' in p.requirements.get('executor', {}).get('docker', {})
        )

        # Add the default image.
        unique_docker_images.add(DEFAULT_CONTAINER_IMAGE)

        # Pull images if requested or just output the list in specified format
        if options['pull']:
            # Remove set of already pulled images.
            with PULLED_IMAGES_LOCK:
                unique_docker_images.difference_update(PULLED_IMAGES)

            # Get the desired 'docker' command from settings or use the default
            docker = getattr(settings, 'FLOW_DOCKER_COMMAND', 'docker')

            # Pull each image
            for img in unique_docker_images:
                ret = subprocess.call(
                    shlex.split('{} pull {}'.format(docker, img)),
                    stdout=None if verbosity > 0 else subprocess.DEVNULL,
                    stderr=None if verbosity > 0 else subprocess.DEVNULL,
                )

                # Update set of pulled images.
                with PULLED_IMAGES_LOCK:
                    PULLED_IMAGES.add(img)

                if ret != 0:
                    errmsg = "Failed to pull Docker image '{}'!".format(img)

                    if not options['ignore_pull_errors']:
                        # Print error and stop execution
                        raise CommandError(errmsg)
                    else:
                        # Print error, but keep going
                        logger.error(errmsg)
                        if verbosity > 0:
                            self.stderr.write(errmsg)
                else:
                    msg = "Docker image '{}' pulled successfully!".format(img)
                    logger.info(msg)
                    if verbosity > 0:
                        self.stdout.write(msg)
        else:
            # Sort the set of unique Docker images for nicer output.
            unique_docker_images = sorted(unique_docker_images)

            # Convert the set of unique Docker images into a list of dicts for easier output
            imgs = [
                dict(name=s[0], tag=s[1] if len(s) == 2 else 'latest')
                for s in (img.split(':') for img in unique_docker_images)
            ]

            # Output in YAML or plaintext (one image per line), as requested
            if options['format'] == 'yaml':
                out = yaml.safe_dump(imgs, default_flow_style=True, default_style="'")
            else:
                out = functools.reduce(operator.add,
                                       ('{name}:{tag}\n'.format(**i) for i in imgs), '')

            self.stdout.write(out, ending='')
492815
def start(name, config_file=None):
    '''
    
    '''
    if not exists(name):
        raise ContainerNotExists("The container (%s) does not exist!" % name)
    if name in running():
        raise ContainerAlreadyRunning('The container %s is already started!' % name)
    cmd = ['lxc-start', '-n', name, '-d']
    if config_file:
        cmd += ['-f', config_file]
    subprocess.check_call(cmd)
14780
def load(self):
        
        if self.exists():
            with open(self.dot_file, 'r') as handle:
                self.update(json.load(handle))
        if self.options['context'] is not None:
            self['context'] = self.options['context']
        else:
            self.options['context'] = self['context']
        if self.options['defaults'] is not None:
            self['defaults'] = self.options['defaults']
        else:
            self.options['defaults'] = self['defaults']
        if self.options['output'] is not None:
            self['output'] = self.options['output']

        if self.options.get('inclusive', False):
            self['inclusive'] = True

        if self.options.get('exclude', []):
            self['exclude'].extend(self.options['exclude'])

        if self['output'] is None:
            self['output'] = os.path.join(os.getcwd(), 'dockerstache-output')
        self['output_path'] = self.abs_output_dir()
        self['input_path'] = self.abs_input_dir()
        if self['context'] is not None:
            self['context_path'] = absolute_path(self['context'])
        if self['defaults'] is not None:
            self['defaults_path'] = absolute_path(self['defaults'])
1105667
def start(self):
        """"""
        if self.container_id is not None:
            msg = 'The Docker container is already running with ID {}'
            raise Exception(msg.format(self.container_id))

        process = Popen(['docker ps | grep ":{}"'.format(self.port)],
                        shell=True, stdout=PIPE)
        (grep_output, _grep_error) = process.communicate()
        lines = grep_output.split('\n')
        for line in lines:
            if ':{}'.format(self.port) in line:
                other_id = line.split()[0]
                msg = 'Port {} is already being used by container {}'
                raise Exception(msg.format(self.port, other_id))

        self.container_id = check_output(self.command, shell=True).strip()
        try:
            self.ip_address = check_output(['docker-machine', 'ip']).strip()
        except (CalledProcessError, OSError):
            self.ip_address = '127.0.0.1'

        output = OutputMonitor()
        logs_process = Popen(['docker', 'logs', '-f', self.container_id],
                             stdout=output.stream.input,
                             stderr=open(os.devnull, 'w'))
        ready_log_line = 'Selenium Server is up and running'
        if not output.wait_for(ready_log_line, 10):
            logs_process.kill()
            msg = 'Timeout starting the Selenium server Docker container:\n'
            msg += '\n'.join(output.lines)
            raise Exception(msg)
        logs_process.kill()
514299
async def _init_clean(self):
        
        # Data about running containers
        self._containers_running = {}
        self._container_for_job = {}

        self._student_containers_running = {}
        self._student_containers_for_job = {}

        self._containers_killed = dict()

        # Delete tmp_dir, and recreate-it again
        try:
            await self._ashutil.rmtree(self._tmp_dir)
        except OSError:
            pass

        try:
            await self._aos.mkdir(self._tmp_dir)
        except OSError:
            pass

        # Docker
        self._docker = AsyncProxy(DockerInterface())

        # Auto discover containers
        self._logger.info("Discovering containers")
        self._containers = await self._docker.get_containers()

        self._assigned_external_ports = {}  # container_id : [external_ports]

        if self._address_host is None and len(self._containers) != 0:
            self._logger.info("Guessing external host IP")
            self._address_host = await self._docker.get_host_ip(next(iter(self._containers.values()))["id"])
        if self._address_host is None:
            self._logger.warning(
                "Cannot find external host IP. Please indicate it in the configuration. Remote SSH debug has been deactivated.")
            self._external_ports = None
        else:
            self._logger.info("External address for SSH remote debug is %s", self._address_host)

        # Watchers
        self._timeout_watcher = TimeoutWatcher(self._docker)
793778
def instance(instance_group_name, index_or_id, options = { deployment_name: Deployments::DEFAULT_DEPLOYMENT_NAME })
      find_instance(instances(options), instance_group_name, index_or_id)
    end
1022364
def check_docker_access(self):
        
        try:
            if self.client is None:
                self.client = docker.from_env()
            self.client.ping()  # check that docker is running and user is permitted to access it
        except ConnectionError as e:
            logger.exception(e)
            raise BuildError("Docker is not running or the current user doesn't have permissions to access docker.")
891084
def generate_docker_compose(self):
        
        example = {}
        example['app'] = {}
        example['app']['environment'] = []
        for key in sorted(list(self.spec.keys())):
            if self.spec[key]['type'] in (dict, list):
                value = f"\'{json.dumps(self.spec[key].get('example', ''))}\'"
            else:
                value = f"{self.spec[key].get('example', '')}"
            example['app']['environment'].append(f"{self.env_prefix}_{key.upper()}={value}")
        print(yaml.dump(example, default_flow_style=False))
382103
protected function shareInstancesInContainer() {
        $this->container->set([Router::class, IRouter::class], $this->router);
        $this->container->set(
            [RequestHandler::class, IRequestHandler::class], $this->requestHandler
        );
    }
691832
def get_init_container(self,
                           init_command,
                           init_args,
                           env_vars,
                           context_mounts,
                           persistence_outputs,
                           persistence_data):
        """"""
        env_vars = to_list(env_vars, check_none=True)
        if self.original_name is not None and self.cloning_strategy == CloningStrategy.RESUME:
            return []
        if self.original_name is not None and self.cloning_strategy == CloningStrategy.COPY:
            command = InitCommands.COPY
            original_outputs_path = stores.get_experiment_outputs_path(
                persistence=persistence_outputs,
                experiment_name=self.original_name)
        else:
            command = InitCommands.CREATE
            original_outputs_path = None

        outputs_path = stores.get_experiment_outputs_path(
            persistence=persistence_outputs,
            experiment_name=self.experiment_name)
        _, outputs_volume_mount = get_pod_outputs_volume(persistence_outputs=persistence_outputs)
        volume_mounts = outputs_volume_mount + to_list(context_mounts, check_none=True)
        init_command = init_command or ["/bin/sh", "-c"]
        init_args = init_args or to_list(
            get_output_args(command=command,
                            outputs_path=outputs_path,
                            original_outputs_path=original_outputs_path))
        init_args += to_list(get_auth_context_args(entity='experiment',
                                                   entity_name=self.experiment_name))
        return [
            client.V1Container(
                name=self.init_container_name,
                image=self.init_docker_image,
                image_pull_policy=self.init_docker_image_pull_policy,
                command=init_command,
                args=[''.join(init_args)],
                env=env_vars,
                volume_mounts=volume_mounts)
        ]