Example #1
0
    def run(self, ticket_id, command, size=None):

        image_name = yield self.image_builder.build_image(ticket_id=ticket_id, service=self)

        config = yield self._generate_config(image_name, for_run=True)

        config['Cmd'] = command
        config['Tty'] = True
        config['AttachStdin'] = True
        config['AttachStdout'] = True
        config['AttachStderr'] = True
        config['OpenStdin'] = True

        name = '%s_pty_%s' % (self.name, ticket_id)

        yield self.client.create_container(config, name, ticket_id=ticket_id)

        if self.ports:
            config['PortBindings'] = self.prepare_ports()



        if self.volumes and len(self.volumes):
            config['Binds'] = ['%s:%s' % (x['local'], x['remote'] + (':ro' if self.is_read_only(x['remote']) else '')) for x in self.volumes]

        for plugin in enumerate_plugins(IServiceBuilder):
            yield plugin.configure_container_on_start(self, config)

        yield self.client.start_container(name, ticket_id=ticket_id, config=config)

        if size:
            yield self.client.resize(name, width=size[1], height=size[0])
            yield self.client.attach(name, ticket_id)
        else:
            yield self.client.attach(name, ticket_id, skip_terminal=True)
Example #2
0
    def unpublish_app(self, deployment, domain, ticket_id=None):

        if not isinstance(deployment, Deployment):
            deployment = yield self.get(deployment)

        if domain in deployment.exports:
            del deployment.exports[domain]

        yield self._persist_dployment(deployment)

        for plugin in enumerate_plugins(IDeploymentPublishListener):
            yield plugin.on_domain_unpublish(deployment, domain, ticket_id=ticket_id)
Example #3
0
    def publish_app(self, deployment, domain, app_name, service_name, custom_port=None, ticket_id=None):
        if not isinstance(deployment, Deployment):
            deployment = yield self.get(deployment)

        deployment.exports[domain] = {
            'public_app': app_name,
            'public_service': service_name,
            'custom_port': custom_port
        }

        yield self._persist_dployment(deployment)

        for plugin in enumerate_plugins(IDeploymentPublishListener):
            yield plugin.on_domain_publish(deployment, domain, ticket_id=ticket_id)
Example #4
0
    def _generate_config(self, image_name, for_run=False):
        config = {
            "Image": image_name
        }

        image_info = None
        # TODO: improve tests
        if hasattr(self.client, 'inspect_image'):
            image_info = yield self.client.inspect_image(image_name)

        vlist = yield self.redis.hgetall('vars')

        if self.env:
            vlist.update(self.env)

        if len(vlist) > 0:
            config['Env'] = ['%s=%s' % x for x in vlist.items()]


        if self.ports:
            all_ports = {}
            for port in self.ports:
                if isinstance(port, basestring) and ':' in port:
                    all_ports[port.split(':')[0]] = {}
                else:
                    all_ports[port] = {}

            config['ExposedPorts'] = all_ports


        if self.entrypoint:
            config['Entrypoint'] = self.entrypoint

        if self.workdir:
            config['WorkingDir'] = self.workdir

        if not for_run:
            config['Hostname'] = self.name

            if self.command:
                config['Cmd'] = self.command.split(' ')

        for plugin in enumerate_plugins(IServiceBuilder):
            yield plugin.configure_container_on_create(self, config)

        defer.returnValue(config)
Example #5
0
    def _generate_config(self, image_name, for_run=False):
        config = {"Image": image_name}

        image_info = None
        # TODO: improve tests
        if hasattr(self.client, 'inspect_image'):
            image_info = yield self.client.inspect_image(image_name)

        vlist = yield self.redis.hgetall('vars')

        if self.env:
            vlist.update(self.env)

        if len(vlist) > 0:
            config['Env'] = ['%s=%s' % x for x in vlist.items()]

        if self.ports:
            all_ports = {}
            for port in self.ports:
                if isinstance(port, basestring) and ':' in port:
                    all_ports[port.split(':')[0]] = {}
                else:
                    all_ports[port] = {}

            config['ExposedPorts'] = all_ports

        if self.entrypoint:
            config['Entrypoint'] = self.entrypoint

        if self.workdir:
            config['WorkingDir'] = self.workdir

        if not for_run:
            config['Hostname'] = self.name

            if self.command:
                config['Cmd'] = self.command.split(' ')

        for plugin in enumerate_plugins(IServiceBuilder):
            yield plugin.configure_container_on_create(self, config)

        defer.returnValue(config)
Example #6
0
    def run(self, ticket_id, command, size=None):

        image_name = yield self.image_builder.build_image(ticket_id=ticket_id,
                                                          service=self)

        config = yield self._generate_config(image_name, for_run=True)

        config['Cmd'] = command
        config['Tty'] = True
        config['AttachStdin'] = True
        config['AttachStdout'] = True
        config['AttachStderr'] = True
        config['OpenStdin'] = True

        name = '%s_pty_%s' % (self.name, ticket_id)

        yield self.client.create_container(config, name, ticket_id=ticket_id)

        if self.ports:
            config['PortBindings'] = self.prepare_ports()

        if self.volumes and len(self.volumes):
            config['Binds'] = [
                '%s:%s' % (x['local'], x['remote'] +
                           (':ro' if self.is_read_only(x['remote']) else ''))
                for x in self.volumes
            ]

        for plugin in enumerate_plugins(IServiceBuilder):
            yield plugin.configure_container_on_start(self, config)

        yield self.client.start_container(name,
                                          ticket_id=ticket_id,
                                          config=config)

        if size:
            yield self.client.resize(name, width=size[1], height=size[0])
            yield self.client.attach(name, ticket_id)
        else:
            yield self.client.attach(name, ticket_id, skip_terminal=True)
Example #7
0
    def start(self, ticket_id=None):

        id_ = yield self.client.find_container_by_name(self.name)

        self.task_log(ticket_id,
                      '[%s][%s] Starting service' % (ticket_id, self.name))
        self.task_log(
            ticket_id, '[%s][%s] Service resolve by name result: %s' %
            (ticket_id, self.name, id_))

        # container is not created yet
        if not id_:
            self.task_log(
                ticket_id, '[%s][%s] Service not created. Creating ...' %
                (ticket_id, self.name))
            yield self.create(ticket_id)
            id_ = yield self.client.find_container_by_name(self.name)

        current_config = yield self.inspect()
        image_id = current_config['Image']
        image_info = yield self.client.inspect_image(image_id)

        self.task_log(ticket_id,
                      '[%s][%s] Starting service...' % (ticket_id, self.name))

        config = {
            # "Dns": [self.dns_server],
            # "DnsSearch": '%s.%s' % (self.app_name, self.dns_search_suffix)
        }

        for plugin in enumerate_plugins(IServiceBuilder):
            yield plugin.configure_container_on_start(self, config)

        if self.volumes_from:
            config['VolumesFrom'] = self.volumes_from

        if self.ports:
            config['PortBindings'] = self.prepare_ports()

        mounted_volumes = []
        config['Binds'] = []
        if self.volumes and len(self.volumes):
            for x in self.volumes:
                mounted_volumes.append(x['remote'])
                config['Binds'].append(
                    '%s:%s' %
                    (x['local'], x['remote'] +
                     (':ro' if self.is_read_only(x['remote']) else '')))

        if image_info['ContainerConfig']['Volumes']:
            for vpath, vinfo in image_info['ContainerConfig']['Volumes'].items(
            ):

                if not vpath in mounted_volumes:
                    dir_ = os.path.expanduser(
                        '%s/volumes/%s/%s' %
                        (self.settings.home_dir, self.name,
                         re.sub('[^a-z0-9]+', '_', vpath)))

                    if self.settings.btrfs:
                        dir_ += '_btrfs'

                    if not os.path.exists(dir_):
                        if self.settings.btrfs:
                            os.system('btrfs subvolume create %s' % dir_)
                        else:
                            os.makedirs(dir_)

                    mounted_volumes.append(vpath)
                    config['Binds'].append('%s:%s' % (dir_, vpath))

        #config['Binds'] = ["/home/alex/dev/mcloud/examples/static_site1/public:/var/www"]

        self.task_log(ticket_id, 'Startng container. config: %s' % config)

        yield self.client.start_container(id_,
                                          ticket_id=ticket_id,
                                          config=config)

        yield self.inspect()

        if self.is_running():
            content = """
#!/bin/sh

echo -e "\n@mcloud $@\n"
"""

            print 'Load file into container: ' + '/usr/bin/@me'
            yield self.client.put_file(id_,
                                       '/usr/bin/@me',
                                       content,
                                       ticket_id=ticket_id)

        # lifecycle events

        self.task_log(ticket_id, 'Emit startup event')
        for plugin in enumerate_plugins(IServiceLifecycleListener):
            self.task_log(ticket_id, 'Call start listener %s' % plugin)
            yield plugin.on_service_start(self, ticket_id=ticket_id)

        # inspect and return result
        ret = yield self.inspect()

        defer.returnValue(ret)
Example #8
0
    def start(self, ticket_id=None):

        id_ = yield self.client.find_container_by_name(self.name)

        self.task_log(ticket_id, '[%s][%s] Starting service' % (ticket_id, self.name))
        self.task_log(ticket_id, '[%s][%s] Service resolve by name result: %s' % (ticket_id, self.name, id_))

        # container is not created yet
        if not id_:
            self.task_log(ticket_id, '[%s][%s] Service not created. Creating ...' % (ticket_id, self.name))
            yield self.create(ticket_id)
            id_ = yield self.client.find_container_by_name(self.name)

        current_config = yield self.inspect()
        image_id = current_config['Image']
        image_info = yield self.client.inspect_image(image_id)

        self.task_log(ticket_id, '[%s][%s] Starting service...' % (ticket_id, self.name))

        config = {
            # "Dns": [self.dns_server],
            # "DnsSearch": '%s.%s' % (self.app_name, self.dns_search_suffix)
        }

        for plugin in enumerate_plugins(IServiceBuilder):
            yield plugin.configure_container_on_start(self, config)


        if self.volumes_from:
            config['VolumesFrom'] = self.volumes_from

        if self.ports:
            config['PortBindings'] = self.prepare_ports()

        mounted_volumes = []
        config['Binds'] = []
        if self.volumes and len(self.volumes):
            for x in self.volumes:
                mounted_volumes.append(x['remote'])
                config['Binds'].append('%s:%s' % (x['local'], x['remote'] + (':ro' if self.is_read_only(x['remote']) else '')))


        if image_info['ContainerConfig']['Volumes']:
            for vpath, vinfo in image_info['ContainerConfig']['Volumes'].items():

                if not vpath in mounted_volumes:
                    dir_ = os.path.expanduser('%s/volumes/%s/%s' % (self.settings.home_dir, self.name, re.sub('[^a-z0-9]+', '_', vpath)))

                    if self.settings.btrfs:
                        dir_ += '_btrfs'

                    if not os.path.exists(dir_):
                        if self.settings.btrfs:
                            os.system('btrfs subvolume create %s' % dir_)
                        else:
                            os.makedirs(dir_)

                    mounted_volumes.append(vpath)
                    config['Binds'].append('%s:%s' % (dir_, vpath))

        #config['Binds'] = ["/home/alex/dev/mcloud/examples/static_site1/public:/var/www"]

        self.task_log(ticket_id, 'Startng container. config: %s' % config)

        yield self.client.start_container(id_, ticket_id=ticket_id, config=config)

        yield self.inspect()

        if self.is_running():
            content = """
#!/bin/sh

echo -e "\n@mcloud $@\n"
"""

            print 'Load file into container: ' + '/usr/bin/@me'
            yield self.client.put_file(id_, '/usr/bin/@me', content,  ticket_id=ticket_id)

        # lifecycle events

        self.task_log(ticket_id, 'Emit startup event')
        for plugin in enumerate_plugins(IServiceLifecycleListener):
            self.task_log(ticket_id, 'Call start listener %s' % plugin)
            yield plugin.on_service_start(self, ticket_id=ticket_id)

        # inspect and return result
        ret = yield self.inspect()

        defer.returnValue(ret)