Beispiel #1
0
    def start(self):
        if self.running:
            log.info('container is already running')
            return

        node = self.get_node()
        if not node:
            raise TaskException("container's node does not exist")
        if not node.pulled:
            raise TaskException("container's node has not been pulled yet")

        # Compile templates for new run
        self.compile_templates(node)

        # Run container
        cmd = ['docker', 'run', '-d'] + self.get_run_args(node)
        cid = utils.run_cmd(cmd)[0].strip()

        # Get ports
        ports = {}
        for name, port in self.data['ports'].iteritems():
            # TODO: Use API when it can handle port mapping
            host = utils.run_cmd(['docker', 'port', cid, str(port)])
            ports[name] = int(host.split(':')[1])

        self.data['cid'] = cid
        self.data['endpoint'] = json.dumps({
            'host': self.agent_host, 'ports': ports
        })
        self.save()
Beispiel #2
0
    def start(self):
        if self.running:
            log.info('container is already running')
            return

        node = self.get_node()
        if not node:
            raise TaskException("container's node does not exist")
        if not node.pulled:
            raise TaskException("container's node has not been pulled yet")

        # Compile templates for new run
        self.compile_templates(node)

        # Run container
        cmd = ['docker', 'run', '-d'] + self.get_run_args(node)
        cid = utils.run_cmd(cmd)[0].strip()

        # Get ports
        ports = {}
        for name, port in self.data['ports'].iteritems():
            # TODO: Use API when it can handle port mapping
            host = utils.run_cmd(['docker', 'port', cid, str(port)])
            ports[name] = int(host.split(':')[1])

        self.data['cid'] = cid
        self.data['endpoint'] = json.dumps({
            'host': self.agent_host,
            'ports': ports
        })
        self.save()
Beispiel #3
0
    def stop(self):
        if not self.running:
            log.info('container is already stopped')
            return

        # Remove from config
        self.config_manager.delete(self.data['config_key'])

        # Stop container
        utils.run_cmd(['docker', 'stop', self.data['cid']])
        self.data['cid'] = None
        self.data['endpoint'] = None
        self.save()
Beispiel #4
0
    def stop(self):
        if not self.running:
            log.info('container is already stopped')
            return

        # Remove from config
        self.config_manager.delete(self.data['config_key'])

        # Stop container
        utils.run_cmd(['docker', 'stop', self.data['cid']])
        self.data['cid'] = None
        self.data['endpoint'] = None
        self.save()
Beispiel #5
0
    def pull(self, args):
        # Pull image
        if not args['app_path']:
            utils.run_cmd(['docker', 'pull', args['image']])

        # Prepare to pull templates
        templates_path = self.get_templates_path()
        src = 'salt://templates/%s/%s' % (args['env_id'], self.data['_id'])

        # Remove all contents before adding new templates
        utils.clear_path(templates_path)

        # Pull templates
        caller_client().function('cp.get_dir', src, templates_path)

        node.update(args)
Beispiel #6
0
    def pull(self, args):
        # Pull image
        if not args['app_path']:
            utils.run_cmd(['docker', 'pull', args['image']])

        # Prepare to pull templates
        templates_path = self.get_templates_path()
        src = 'salt://templates/%s/%s' % (args['env_id'], self.data['_id'])

        # Remove all contents before adding new templates
        utils.clear_path(templates_path)

        # Pull templates
        caller_client().function('cp.get_dir', src, templates_path)

        node.update(args)
Beispiel #7
0
    def reload(self):
        if not self.running:
            raise TaskException('container is not running')

        code = utils.run_cmd(['lxc-attach', '-n', self.data['cid'], '--',
                        '/bin/bash', os.path.join(container_dir, 'files',
                        'autoload.sh')], allow_errors=True)[1]

        if code != 0:
            # No user-defined autoload.sh or script wants to reload; restart.
            self.restart()
Beispiel #8
0
    def reload(self):
        if not self.running:
            raise TaskException('container is not running')

        code = utils.run_cmd([
            'lxc-attach', '-n', self.data['cid'], '--', '/bin/bash',
            os.path.join(container_dir, 'files', 'autoload.sh')
        ],
                             allow_errors=True)[1]

        if code != 0:
            # No user-defined autoload.sh or script wants to reload; restart.
            self.restart()
Beispiel #9
0
def check_instances():
    cids = utils.run_cmd(['docker', 'ps', '-q'])[0].splitlines()
    for instance in objects.Instance.get_instances():
        cid = instance.data['cid']
        if cid in cids:
            # Instance is running
            # Set endpoint key
            instance.set_endpoint()
        else:
            # Instance is down
            instance.data['cid'] = None
            instance.data['endpoint'] = None
            instance.save()
            # Log the event and start the instance
            instance.start()