|
| 1 | +import logging |
| 2 | +from docker import Client |
| 3 | + |
| 4 | +from django.conf import settings |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +class SwarmClient(object): |
| 9 | + def __init__(self,target, auth, options, pkey): |
| 10 | + self.target = settings.SWARM_HOST |
| 11 | + # single global connection |
| 12 | + self.registry = settings.REGISTRY_HOST+":"+settings.REGISTRY_PORT |
| 13 | + self.docker_cli = Client(base_url='tcp://'+self.target+':'+"2395",timeout=1200) |
| 14 | + |
| 15 | + def create(self, name, image, command='', template=None, **kwargs): |
| 16 | + """Create a container""" |
| 17 | + cimage=self.registry+"/"+image |
| 18 | + cname=name |
| 19 | + ccommand=command |
| 20 | + # self.docker_cli.pull(cimage, stream=False,insecure_registry=True) |
| 21 | + self.docker_cli.create_container(image=cimage,name=cname,command=ccommand)#,hostname=self._get_hostname(cname),ports=self._get_ports(cimage)) |
| 22 | + self.docker_cli.start(cname, port_bindings=self._get_portbindings(cimage),publish_all_ports=True) |
| 23 | + |
| 24 | + def start(self, name): |
| 25 | + """ |
| 26 | + Start a container |
| 27 | + """ |
| 28 | + self.docker_cli.start(name) |
| 29 | + return |
| 30 | + |
| 31 | + def stop(self, name): |
| 32 | + """ |
| 33 | + Stop a container |
| 34 | + """ |
| 35 | + self.docker_cli.stop(name) |
| 36 | + return |
| 37 | + def destroy(self, name): |
| 38 | + """ |
| 39 | + Destroy a container |
| 40 | + """ |
| 41 | + self.docker_cli.stop(name) |
| 42 | + self.docker_cli.remove_container(name) |
| 43 | + return |
| 44 | + |
| 45 | + def run(self, name, image, entrypoint, command): |
| 46 | + """ |
| 47 | + Run a one-off command |
| 48 | + """ |
| 49 | + # dump input into a json object for testing purposes |
| 50 | + return 0, json.dumps({'name': name, |
| 51 | + 'image': image, |
| 52 | + 'entrypoint': entrypoint, |
| 53 | + 'command': command}) |
| 54 | + |
| 55 | + def attach(self, name): |
| 56 | + """ |
| 57 | + Attach to a job's stdin, stdout and stderr |
| 58 | + """ |
| 59 | + return StringIO(), StringIO(), StringIO() |
| 60 | + |
| 61 | + def _get_hostname(self, application_name): |
| 62 | + hostname = settings.UNIT_HOSTNAME |
| 63 | + if hostname == "default": |
| 64 | + return '' |
| 65 | + elif hostname == "application": |
| 66 | + # replace underscore with dots, since underscore is not valid in DNS hostnames |
| 67 | + dns_name = application_name.replace("_", ".") |
| 68 | + return dns_name |
| 69 | + elif hostname == "server": |
| 70 | + raise NotImplementedError |
| 71 | + else: |
| 72 | + raise RuntimeError('Unsupported hostname: ' + hostname) |
| 73 | + |
| 74 | + def _get_portbindings(self,image): |
| 75 | + dictports=self.docker_cli.inspect_image(image)["ContainerConfig"]["ExposedPorts"] |
| 76 | + for port,mapping in dictports.items(): |
| 77 | + dictports[port]=None |
| 78 | + return dictports |
| 79 | + |
| 80 | + def _get_ports(self,image): |
| 81 | + ports=[] |
| 82 | + dictports=self.docker_cli.inspect_image(image)["ContainerConfig"]["ExposedPorts"] |
| 83 | + for port,mapping in dictports.items(): |
| 84 | + ports.append(int(port.split('/')[0])) |
| 85 | + return ports |
| 86 | + |
| 87 | +SchedulerClient = SwarmClient |
0 commit comments