Example #1
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:
                for eth in container['eths']:
                    ContainerStatus.verify_eths(eth[0], eth[2])

                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.add_status(create_containers_json)
            return create_containers_json, 201

        except DockerflyException as e:
            logger.error(traceback.format_exc())
            if container and container.get('id', None):
                ContainerCtl.remove(container['id'])
            return {"errno":e.errno, "errMsg":e.message}, 400

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

            if not container:
                return {"errno":1000, "errMsg":"invalid json request"}, 400
            else:
                return {"errno":1000, "errMsg":e.message}, 400
Example #2
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:
                for eth in container["eths"]:
                    ContainerStatus.verify_eths(eth[0], eth[2])

                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.add_status(create_containers_json)
            return create_containers_json, 201

        except DockerflyException as e:
            logger.error(traceback.format_exc())
            if container and container.get("id", None):
                ContainerCtl.remove(container["id"])
            return {"errno": e.errno, "errMsg": e.message}, 400

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

            if not container:
                return {"errno": 1000, "errMsg": "invalid json request"}, 400
            else:
                return {"errno": 1000, "errMsg": e.message}, 400
Example #3
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