发表于 2015-4-18 06:48:49

docker-api

__author__ = 'zxp'
import docker
import sys
class DockerManager_Slave(object):
def __init__(self):
self.idict={}
self.rinfo={}
try:
self.c = docker.Client(base_url='unix://var/run/docker.sock',version='1.0.1',timeout=15)
except Exception,e:
print "Connection docker server error:"+str(e)
sys.exit()
def Create_Container(self,image,command=None,mem_limit=0,ports=None,name=None,cpu_shares=None):
"""
:param image (str): The image to run
:param command (str or list): The command to be run in the container
:param mem_limit (float or str): Memory limit (format: , where unit = b, k, m, or g)
:param ports (list of ints): A list of port numbers
:param name (str): A name for the container
:param cpu_shares (int or float): CPU shares (relative weight)
Returns (dict): A dictionary with an image 'Id' key and a 'Warnings' key.
return container id
"""
try:
self.container = self.c.create_container(image=image, command=command,mem_limit=mem_limit,ports=ports,\
name=name,cpu_shares=cpu_shares)
except Exception,e:
print "Create Container error:"+str(e)
sys.exit()
return self.container['Id']
def Inspect_Container(self,containerid):
"""
:param containerid:The container to inspect
:return:Nearly the same output as docker inspect, just as a single dict
"""
try:
self.container_info=self.c.inspect_container(containerid)
except Exception,e:
print "Inspect Container"+containerid+"error:"+str(e)
sys.exit()
def Pull(self,repository,tag=None):
"""
:param repository (str): The repository to pull
:param tag (str): The tag to pull
:return (generator or str): The output
"""
try:
self.pull_detail=self.c.pull(repository,tag)
except Exception,e:
print "Pull images"+repository+":"+tag+"error:"+str(e)
sys.exit()
def Start(self,containerid):
try:
self.c.start(containerid)
except Exception,e:
print "Start Container"+containerid+"error:"+str(e)
def Stop(self,containerid,timeout=10):
"""
:param container (str): The container to stop
:param timeout (int): Timeout in seconds to wait for the container to stop before sending a SIGKILL
:return:
"""
try:
self.c.stop(containerid,timeout=timeout)
except Exception,e:
print "Stop"+containerid+"error:"+str(e)
sys.exit()
def Remove_Container(self,containerid):
"""
container (str): The container to remove
v (bool): Remove the volumes associated with the container
link (bool): Remove the specified link and not the underlying container
force (bool): Force the removal of a running container (uses SIGKILL)
"""
try:
self.c.remove_container(containerid)
except Exception,e:
print "Remove container"+containerid+"error:"+str(e)
sys.exit()

  
页: [1]
查看完整版本: docker-api