stop.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:vent 作者: CyberReboot 项目源码 文件源码
def POST():
        """
        Send a POST request with a docker container ID and it will be stopped.

        Example input: {'id': "12345"}, {'id': ["123", "456"]
        """
        web.header('Content-Type', 'application/json')

        # verify user input
        data = web.data()
        payload = {}
        try:
            payload = ast.literal_eval(data)
        except Exception as e:  # pragma: no cover
            return (False, 'malformed payload : ' + str(e))

        # verify payload has a container ID
        if 'id' not in payload:
            return (False, 'payload missing container id')

        # connect to docker and stop the given container
        c = None
        try:
            c = docker.from_env()
        except Exception as e:  # pragma: no cover
            return (False, 'unable to connect to docker because: ' + str(e))

        # stop containers chosen from CLI
        try:
            for container_id in payload['id']:
                c.containers.get(container_id).stop()
        except Exception as e:  # pragma: no cover
            return (False, 'unable to stop list of containers because: ' +
                    str(e))

        return (True, 'container successfully stopped: ' + str(payload['id']))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号