Пример #1
0
    def delete(self, container_id):
        container_id = abort_if_container_doesnt_exist(container_id)
        try:
            ContainerCtl.remove(container_id)
            ContainerStatus.remove_status([container_id])

        except DockerflyException, e:
            return {"errno": e.errno, "errMsg": e.message}, 400
Пример #2
0
    def delete(self, container_id):
        container_id = abort_if_container_doesnt_exist(container_id)
        try:
            ContainerCtl.remove(container_id)
            ContainerStatus.remove_status([container_id])

        except DockerflyException, e:
            return {"errno":e.errno, "errMsg":e.message}, 400
Пример #3
0
 def test_remove_container_status(self):
     remove_container_ids = [1]
     remove_status(remove_container_ids)
     self._containers.pop(0)
     self.assertEqual(get_all_status(), self._containers)
Пример #4
0
    def post(self):
        """json template

            {
                "eths": [
                    [
                        "testDockerflyv0",
                        "eth0",
                        "172.16.11.239/24"
                    ]
                ],
                "gateway": "172.16.11.1",
                "id": null,
                "pid": null,
                "image_name": "centos:centos6_sshd",
                "container_name": "testDockerflyxxx",
                "last_modify_time": 0,
                "run_cmd": "/usr/sbin/sshd -D",
                "status": "running",
                "desc": "create a container by template"
            }
        """
        try:
            container = None
            create_containers_json = request.get_json()
            for container in create_containers_json:
                with FileLock(join(RUN_ROOT, 'verify_ips.lock')):
                    for eth in container['eths']:
                        ContainerStatus.verify_ips(eth[2])

                    eth_names = [eth[0] for eth in container['eths']]
                    if len(eth_names) > len(set(eth_names)):
                        raise VEthStatusException('You set duplicate eth!')

                    container['uuid'] = str(uuid.uuid1())
                    container['id'] = None
                    container['pid'] = None
                    ContainerStatus.add_status([container])
                    container['id'] = ContainerCtl.create(
                        container['image_name'], container['run_cmd'],
                        container['container_name'])

                    ContainerCtl.start(container['id'], container['eths'],
                                       container['gateway'])

                    if container.get('resize', None):
                        ContainerCtl.resize(container['id'],
                                            container['resize'])
                    container['pid'] = ContainerCtl.get_pid(container['id'])
                    container['last_modify_time'] = time.time()
                    ContainerStatus.update_status([container], key='uuid')

            return create_containers_json, 201

        except Exception as e:
            logger.error(traceback.format_exc())
            if container and container.get('id', None):
                ContainerCtl.remove(container['id'])

            ContainerStatus.remove_status([container.get('uuid', None)],
                                          key='uuid')

            if not container:
                return {"errno": 1000, "errMsg": "invalid json request"}, 400
            else:
                return {"errno": 1000, "errMsg": e.message}, 400
Пример #5
0
 def test_remove_container_status_by_uuid(self):
     remove_container_uuids = (['aa-bb-cc-dd'])
     remove_status(remove_container_uuids, key='uuid')
     self._containers.pop(0)
     self.assertEqual(get_all_status(), self._containers)
Пример #6
0
 def test_remove_container_status(self):
     remove_container_ids = ([1])
     remove_status(remove_container_ids)
     self._containers.pop(0)
     self.assertEqual(get_all_status(), self._containers)
Пример #7
0
def main():
    arguments = docopt(__doc__, version=dockerfly_version)
    docker_cli = dockerpy.Client(base_url='unix://var/run/docker.sock')

    container_json_exp = [{
        'image_name':
        'centos:centos6',
        'run_cmd':
        '/bin/sleep 300',
        'eths': [
            ('testDockerflyv0', 'eth0', '192.168.159.10/24'),
            ('testDockerflyv1', 'eth0', '192.168.159.11/24'),
        ],
        'gateway':
        '192.168.159.2',
        'container_name':
        None,
        'status':
        'stopped',
        'last_modify_time':
        0,
        'id':
        0,
        'pid':
        0,
    }]

    if arguments['ps']:
        print docker('ps')

    if arguments['gen']:
        with open(arguments['<config_json>'], 'w') as config:
            json.dump(container_json_exp, config, indent=4, encoding='utf-8')

    if arguments['run']:
        with open(arguments['<config_json>'], 'r') as config:
            container_json = json.load(config, encoding='utf-8')
            for container in container_json:
                container_id = Container.run(container['image_name'],
                                             container['run_cmd'],
                                             container['eths'],
                                             container['gateway'])
                print "Container running:ContainerId(%s) Pid(%s)" % (
                    container_id,
                    docker_cli.inspect_container(container_id)['State']['Pid'])

    if arguments['sync']:
        containers = []
        for container in docker_cli.containers(all=True):
            db_container = {}
            inspect_status = docker_cli.inspect_container(container['Id'])
            db_container['id'] = inspect_status['Id']
            db_container['pid'] = inspect_status['State']['Pid']
            db_container['image_name'] = inspect_status['Config']['Image']
            db_container['container_name'] = inspect_status['Name'].strip('/')
            db_container['run_cmd'] = ' '.join(inspect_status['Args'])
            db_container['status'] = 'running' if inspect_status['State'][
                'Running'] else 'stopped'
            containers.append(db_container)

        #remove no exist container
        for container in ContainerStatus.get_all_status():
            if container['id'] not in [co['id'] for co in containers]:
                ContainerStatus.remove_status([container['id']])
                print "remove:====================="
                print container

        #update or remove container status
        for container in containers:
            try:
                modify_container = ContainerStatus.get_status(container['id'])
                if container['status'] != modify_container['status']:
                    modify_container['status'] = container['status']
                    ContainerStatus.update_status([modify_container])
                    print "update:====================="
                    print modify_container
            except LookupError:
                ContainerStatus.add_status([container])
                print "add:====================="
                print container

    if arguments['rm']:
        Container.remove(arguments['<container_id>'])

    if arguments['resize']:
        Container.resize(arguments['<container_id>'], arguments['<new_size>'])

    if arguments['getpid']:
        print docker_cli.inspect_container(
            arguments['<container_id>'])['State']['Pid']

    if arguments['getpid']:
        print docker_cli.inspect_container(
            arguments['<container_id>'])['State']['Pid']
        print "run dockerflyd server %s:%s" % (arguments['<ip>'],
                                               arguments['<port>'])
        rundaemon(arguments['<ip>'], arguments['<port>'])