We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
chore(*): bump CoreOS to 459.0.0
Only the rackspace image is missing (I don't know if this could be merged without that)

Change to controller scheduler for fleet v0.8.3
Since v0.8.2 fleet return the proper http status code.
In the line https://github.com/deis/deis/blob/master/controller/scheduler/coreos.py#L58 must change 204 to 201.

(founded using the logspout PR in v257.0.0 with fleet v0.8.3)


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `controller/scheduler/coreos.py`
Content:
```
1 import cStringIO
2 import base64
3 import copy
4 import json
5 import httplib
6 import paramiko
7 import socket
8 import re
9 import time
10 
11 
12 MATCH = re.compile(
13     '(?P<app>[a-z0-9-]+)_?(?P<version>v[0-9]+)?\.?(?P<c_type>[a-z-_]+)?.(?P<c_num>[0-9]+)')
14 
15 
16 class UHTTPConnection(httplib.HTTPConnection):
17     """Subclass of Python library HTTPConnection that uses a Unix domain socket.
18     """
19 
20     def __init__(self, path):
21         httplib.HTTPConnection.__init__(self, 'localhost')
22         self.path = path
23 
24     def connect(self):
25         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
26         sock.connect(self.path)
27         self.sock = sock
28 
29 
30 class FleetHTTPClient(object):
31 
32     def __init__(self, cluster_name, hosts, auth, domain, options):
33         self.name = cluster_name
34         self.hosts = hosts
35         self.auth = auth
36         self.domain = domain
37         self.options = options
38         # single global connection
39         self.conn = UHTTPConnection('/var/run/fleet.sock')
40 
41     # scheduler setup / teardown
42 
43     def setUp(self):
44         pass
45 
46     def tearDown(self):
47         pass
48 
49     # connection helpers
50 
51     def _put_unit(self, name, body):
52         headers = {'Content-Type': 'application/json'}
53         self.conn.request('PUT', '/v1-alpha/units/{name}.service'.format(**locals()),
54                           headers=headers, body=json.dumps(body))
55         resp = self.conn.getresponse()
56         data = resp.read()
57         if resp.status != 204:
58             errmsg = "Failed to create unit: {} {} - {}".format(
59                 resp.status, resp.reason, data)
60             raise RuntimeError(errmsg)
61         return data
62 
63     def _delete_unit(self, name):
64         headers = {'Content-Type': 'application/json'}
65         self.conn.request('DELETE', '/v1-alpha/units/{name}.service'.format(**locals()),
66                           headers=headers)
67         resp = self.conn.getresponse()
68         data = resp.read()
69         if resp.status not in (404, 204):
70             errmsg = "Failed to delete unit: {} {} - {}".format(
71                 resp.status, resp.reason, data)
72             raise RuntimeError(errmsg)
73         return data
74 
75     def _get_state(self, name=None):
76         headers = {'Content-Type': 'application/json'}
77         url = '/v1-alpha/state'
78         if name:
79             url += '?unitName={name}.service'.format(**locals())
80         self.conn.request('GET', url, headers=headers)
81         resp = self.conn.getresponse()
82         data = resp.read()
83         if resp.status not in (200,):
84             errmsg = "Failed to retrieve state: {} {} - {}".format(
85                 resp.status, resp.reason, data)
86             raise RuntimeError(errmsg)
87         return json.loads(data)
88 
89     def _get_machines(self):
90         headers = {'Content-Type': 'application/json'}
91         url = '/v1-alpha/machines'
92         self.conn.request('GET', url, headers=headers)
93         resp = self.conn.getresponse()
94         data = resp.read()
95         if resp.status not in (200,):
96             errmsg = "Failed to retrieve machines: {} {} - {}".format(
97                 resp.status, resp.reason, data)
98             raise RuntimeError(errmsg)
99         return json.loads(data)
100 
101     # container api
102 
103     def create(self, name, image, command='', template=None, **kwargs):
104         """Create a container"""
105         self._create_container(name, image, command,
106                                template or copy.deepcopy(CONTAINER_TEMPLATE), **kwargs)
107 
108     def _create_container(self, name, image, command, unit, **kwargs):
109         l = locals().copy()
110         l.update(re.match(MATCH, name).groupdict())
111         # prepare memory limit for the container type
112         mem = kwargs.get('memory', {}).get(l['c_type'], None)
113         if mem:
114             l.update({'memory': '-m {}'.format(mem.lower())})
115         else:
116             l.update({'memory': ''})
117         # prepare memory limit for the container type
118         cpu = kwargs.get('cpu', {}).get(l['c_type'], None)
119         if cpu:
120             l.update({'cpu': '-c {}'.format(cpu)})
121         else:
122             l.update({'cpu': ''})
123         # should a special entrypoint be used
124         entrypoint = kwargs.get('entrypoint')
125         if entrypoint:
126             l.update({'entrypoint': '{}'.format(entrypoint)})
127         # construct unit from template
128         for f in unit:
129             f['value'] = f['value'].format(**l)
130         # prepare tags only if one was provided
131         tags = kwargs.get('tags', {})
132         if tags:
133             tagset = ' '.join(['"{}={}"'.format(k, v) for k, v in tags.items()])
134             unit.append({"section": "X-Fleet", "name": "MachineMetadata",
135                          "value": tagset})
136         # post unit to fleet
137         self._put_unit(name, {"desiredState": "launched", "options": unit})
138 
139     def start(self, name):
140         """Start a container"""
141         self._wait_for_container(name)
142 
143     def _wait_for_container(self, name):
144         # we bump to 20 minutes here to match the timeout on the router and in the app unit files
145         for _ in range(1200):
146             states = self._get_state(name)
147             if states and len(states.get('states', [])) == 1:
148                 state = states.get('states')[0]
149                 subState = state.get('systemdSubState')
150                 if subState == 'running' or subState == 'exited':
151                     break
152                 elif subState == 'failed':
153                     raise RuntimeError('container failed to start')
154             time.sleep(1)
155         else:
156             raise RuntimeError('container failed to start')
157 
158     def _wait_for_destroy(self, name):
159         for _ in range(30):
160             states = self._get_state(name)
161             if not states:
162                 break
163             time.sleep(1)
164         else:
165             raise RuntimeError('timeout on container destroy')
166 
167     def stop(self, name):
168         """Stop a container"""
169         raise NotImplementedError
170 
171     def destroy(self, name):
172         """Destroy a container"""
173         # call all destroy functions, ignoring any errors
174         try:
175             self._destroy_container(name)
176         except:
177             pass
178         self._wait_for_destroy(name)
179 
180     def _destroy_container(self, name):
181         return self._delete_unit(name)
182 
183     def run(self, name, image, entrypoint, command):  # noqa
184         """Run a one-off command"""
185         self._create_container(name, image, command, copy.deepcopy(RUN_TEMPLATE),
186                                entrypoint=entrypoint)
187 
188         # wait for the container to get scheduled
189         for _ in range(30):
190             states = self._get_state(name)
191             if states and len(states.get('states', [])) == 1:
192                 state = states.get('states')[0]
193                 break
194             time.sleep(1)
195         else:
196             raise RuntimeError('container did not report state')
197         machineID = state.get('machineID')
198 
199         # find the machine
200         machines = self._get_machines()
201         if not machines:
202             raise RuntimeError('no available hosts to run command')
203 
204         # find the machine's primaryIP
205         primaryIP = None
206         for m in machines.get('machines', []):
207             if m['id'] == machineID:
208                 primaryIP = m['primaryIP']
209         if not primaryIP:
210             raise RuntimeError('could not find host')
211 
212         # prepare ssh key
213         file_obj = cStringIO.StringIO(base64.b64decode(self.auth))
214         pkey = paramiko.RSAKey(file_obj=file_obj)
215 
216         # grab output via docker logs over SSH
217         ssh = paramiko.SSHClient()
218         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
219         ssh.connect(primaryIP, username="core", pkey=pkey)
220         # share a transport
221         tran = ssh.get_transport()
222 
223         def _do_ssh(cmd):
224             chan = tran.open_session()
225             # get a pty so stdout/stderr look right
226             chan.get_pty()
227             out = chan.makefile()
228             chan.exec_command(cmd)
229             rc, output = chan.recv_exit_status(), out.read()
230             return rc, output
231 
232         # wait for container to start
233         for _ in range(1200):
234             rc, _ = _do_ssh('docker inspect {name}'.format(**locals()))
235             if rc == 0:
236                 break
237             time.sleep(1)
238         else:
239             raise RuntimeError('container failed to start on host')
240 
241         # wait for container to complete
242         for _ in range(1200):
243             _rc, _output = _do_ssh('docker inspect {name}'.format(**locals()))
244             if _rc != 0:
245                 raise RuntimeError('failed to inspect container')
246             _container = json.loads(_output)
247             finished_at = _container[0]["State"]["FinishedAt"]
248             if not finished_at.startswith('0001'):
249                 break
250             time.sleep(1)
251         else:
252             raise RuntimeError('container timed out')
253 
254         # gather container output
255         _rc, output = _do_ssh('docker logs {name}'.format(**locals()))
256         if _rc != 0:
257             raise RuntimeError('could not attach to container')
258 
259         # determine container exit code
260         _rc, _output = _do_ssh('docker inspect {name}'.format(**locals()))
261         if _rc != 0:
262             raise RuntimeError('could not determine exit code')
263         container = json.loads(_output)
264         rc = container[0]["State"]["ExitCode"]
265 
266         # cleanup
267         self._destroy_container(name)
268         self._wait_for_destroy(name)
269 
270         # return rc and output
271         return rc, output
272 
273     def attach(self, name):
274         """
275         Attach to a job's stdin, stdout and stderr
276         """
277         raise NotImplementedError
278 
279 SchedulerClient = FleetHTTPClient
280 
281 
282 CONTAINER_TEMPLATE = [
283     {"section": "Unit", "name": "Description", "value": "{name}"},
284     {"section": "Service", "name": "ExecStartPre", "value": '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker pull $IMAGE"'''},  # noqa
285     {"section": "Service", "name": "ExecStartPre", "value": '''/bin/sh -c "docker inspect {name} >/dev/null 2>&1 && docker rm -f {name} || true"'''},  # noqa
286     {"section": "Service", "name": "ExecStart", "value": '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; port=$(docker inspect -f '{{{{range $k, $v := .ContainerConfig.ExposedPorts }}}}{{{{$k}}}}{{{{end}}}}' $IMAGE | cut -d/ -f1) ; docker run --name {name} {memory} {cpu} -P -e PORT=$port $IMAGE {command}"'''},  # noqa
287     {"section": "Service", "name": "ExecStop", "value": '''/usr/bin/docker rm -f {name}'''},
288     {"section": "Service", "name": "TimeoutStartSec", "value": "20m"},
289     {"section": "Service", "name": "RestartSec", "value": "5"},
290     {"section": "Service", "name": "Restart", "value": "on-failure"},
291 ]
292 
293 
294 RUN_TEMPLATE = [
295     {"section": "Unit", "name": "Description", "value": "{name} admin command"},
296     {"section": "Service", "name": "ExecStartPre", "value": '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker pull $IMAGE"'''},  # noqa
297     {"section": "Service", "name": "ExecStartPre", "value": '''/bin/sh -c "docker inspect {name} >/dev/null 2>&1 && docker rm -f {name} || true"'''},  # noqa
298     {"section": "Service", "name": "ExecStart", "value": '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker run --name {name} --entrypoint={entrypoint} -a stdout -a stderr $IMAGE {command}"'''},  # noqa
299     {"section": "Service", "name": "TimeoutStartSec", "value": "20m"},
300 ]
301
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/controller/scheduler/coreos.py b/controller/scheduler/coreos.py
--- a/controller/scheduler/coreos.py
+++ b/controller/scheduler/coreos.py
@@ -54,7 +54,7 @@
                           headers=headers, body=json.dumps(body))
         resp = self.conn.getresponse()
         data = resp.read()
-        if resp.status != 204:
+        if not 200 <= resp.status <= 299:
             errmsg = "Failed to create unit: {} {} - {}".format(
                 resp.status, resp.reason, data)
             raise RuntimeError(errmsg)

{"golden_diff": "diff --git a/controller/scheduler/coreos.py b/controller/scheduler/coreos.py\n--- a/controller/scheduler/coreos.py\n+++ b/controller/scheduler/coreos.py\n@@ -54,7 +54,7 @@\n                           headers=headers, body=json.dumps(body))\n         resp = self.conn.getresponse()\n         data = resp.read()\n-        if resp.status != 204:\n+        if not 200 <= resp.status <= 299:\n             errmsg = \"Failed to create unit: {} {} - {}\".format(\n                 resp.status, resp.reason, data)\n             raise RuntimeError(errmsg)\n", "issue": "chore(*): bump CoreOS to 459.0.0\nOnly the rackspace image is missing (I don't know if this could be merged without that)\n\nChange to controller scheduler for fleet v0.8.3\nSince v0.8.2 fleet return the proper http status code.\nIn the line https://github.com/deis/deis/blob/master/controller/scheduler/coreos.py#L58 must change 204 to 201.\n\n(founded using the logspout PR in v257.0.0 with fleet v0.8.3)\n\n", "before_files": [{"content": "import cStringIO\nimport base64\nimport copy\nimport json\nimport httplib\nimport paramiko\nimport socket\nimport re\nimport time\n\n\nMATCH = re.compile(\n    '(?P<app>[a-z0-9-]+)_?(?P<version>v[0-9]+)?\\.?(?P<c_type>[a-z-_]+)?.(?P<c_num>[0-9]+)')\n\n\nclass UHTTPConnection(httplib.HTTPConnection):\n    \"\"\"Subclass of Python library HTTPConnection that uses a Unix domain socket.\n    \"\"\"\n\n    def __init__(self, path):\n        httplib.HTTPConnection.__init__(self, 'localhost')\n        self.path = path\n\n    def connect(self):\n        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n        sock.connect(self.path)\n        self.sock = sock\n\n\nclass FleetHTTPClient(object):\n\n    def __init__(self, cluster_name, hosts, auth, domain, options):\n        self.name = cluster_name\n        self.hosts = hosts\n        self.auth = auth\n        self.domain = domain\n        self.options = options\n        # single global connection\n        self.conn = UHTTPConnection('/var/run/fleet.sock')\n\n    # scheduler setup / teardown\n\n    def setUp(self):\n        pass\n\n    def tearDown(self):\n        pass\n\n    # connection helpers\n\n    def _put_unit(self, name, body):\n        headers = {'Content-Type': 'application/json'}\n        self.conn.request('PUT', '/v1-alpha/units/{name}.service'.format(**locals()),\n                          headers=headers, body=json.dumps(body))\n        resp = self.conn.getresponse()\n        data = resp.read()\n        if resp.status != 204:\n            errmsg = \"Failed to create unit: {} {} - {}\".format(\n                resp.status, resp.reason, data)\n            raise RuntimeError(errmsg)\n        return data\n\n    def _delete_unit(self, name):\n        headers = {'Content-Type': 'application/json'}\n        self.conn.request('DELETE', '/v1-alpha/units/{name}.service'.format(**locals()),\n                          headers=headers)\n        resp = self.conn.getresponse()\n        data = resp.read()\n        if resp.status not in (404, 204):\n            errmsg = \"Failed to delete unit: {} {} - {}\".format(\n                resp.status, resp.reason, data)\n            raise RuntimeError(errmsg)\n        return data\n\n    def _get_state(self, name=None):\n        headers = {'Content-Type': 'application/json'}\n        url = '/v1-alpha/state'\n        if name:\n            url += '?unitName={name}.service'.format(**locals())\n        self.conn.request('GET', url, headers=headers)\n        resp = self.conn.getresponse()\n        data = resp.read()\n        if resp.status not in (200,):\n            errmsg = \"Failed to retrieve state: {} {} - {}\".format(\n                resp.status, resp.reason, data)\n            raise RuntimeError(errmsg)\n        return json.loads(data)\n\n    def _get_machines(self):\n        headers = {'Content-Type': 'application/json'}\n        url = '/v1-alpha/machines'\n        self.conn.request('GET', url, headers=headers)\n        resp = self.conn.getresponse()\n        data = resp.read()\n        if resp.status not in (200,):\n            errmsg = \"Failed to retrieve machines: {} {} - {}\".format(\n                resp.status, resp.reason, data)\n            raise RuntimeError(errmsg)\n        return json.loads(data)\n\n    # container api\n\n    def create(self, name, image, command='', template=None, **kwargs):\n        \"\"\"Create a container\"\"\"\n        self._create_container(name, image, command,\n                               template or copy.deepcopy(CONTAINER_TEMPLATE), **kwargs)\n\n    def _create_container(self, name, image, command, unit, **kwargs):\n        l = locals().copy()\n        l.update(re.match(MATCH, name).groupdict())\n        # prepare memory limit for the container type\n        mem = kwargs.get('memory', {}).get(l['c_type'], None)\n        if mem:\n            l.update({'memory': '-m {}'.format(mem.lower())})\n        else:\n            l.update({'memory': ''})\n        # prepare memory limit for the container type\n        cpu = kwargs.get('cpu', {}).get(l['c_type'], None)\n        if cpu:\n            l.update({'cpu': '-c {}'.format(cpu)})\n        else:\n            l.update({'cpu': ''})\n        # should a special entrypoint be used\n        entrypoint = kwargs.get('entrypoint')\n        if entrypoint:\n            l.update({'entrypoint': '{}'.format(entrypoint)})\n        # construct unit from template\n        for f in unit:\n            f['value'] = f['value'].format(**l)\n        # prepare tags only if one was provided\n        tags = kwargs.get('tags', {})\n        if tags:\n            tagset = ' '.join(['\"{}={}\"'.format(k, v) for k, v in tags.items()])\n            unit.append({\"section\": \"X-Fleet\", \"name\": \"MachineMetadata\",\n                         \"value\": tagset})\n        # post unit to fleet\n        self._put_unit(name, {\"desiredState\": \"launched\", \"options\": unit})\n\n    def start(self, name):\n        \"\"\"Start a container\"\"\"\n        self._wait_for_container(name)\n\n    def _wait_for_container(self, name):\n        # we bump to 20 minutes here to match the timeout on the router and in the app unit files\n        for _ in range(1200):\n            states = self._get_state(name)\n            if states and len(states.get('states', [])) == 1:\n                state = states.get('states')[0]\n                subState = state.get('systemdSubState')\n                if subState == 'running' or subState == 'exited':\n                    break\n                elif subState == 'failed':\n                    raise RuntimeError('container failed to start')\n            time.sleep(1)\n        else:\n            raise RuntimeError('container failed to start')\n\n    def _wait_for_destroy(self, name):\n        for _ in range(30):\n            states = self._get_state(name)\n            if not states:\n                break\n            time.sleep(1)\n        else:\n            raise RuntimeError('timeout on container destroy')\n\n    def stop(self, name):\n        \"\"\"Stop a container\"\"\"\n        raise NotImplementedError\n\n    def destroy(self, name):\n        \"\"\"Destroy a container\"\"\"\n        # call all destroy functions, ignoring any errors\n        try:\n            self._destroy_container(name)\n        except:\n            pass\n        self._wait_for_destroy(name)\n\n    def _destroy_container(self, name):\n        return self._delete_unit(name)\n\n    def run(self, name, image, entrypoint, command):  # noqa\n        \"\"\"Run a one-off command\"\"\"\n        self._create_container(name, image, command, copy.deepcopy(RUN_TEMPLATE),\n                               entrypoint=entrypoint)\n\n        # wait for the container to get scheduled\n        for _ in range(30):\n            states = self._get_state(name)\n            if states and len(states.get('states', [])) == 1:\n                state = states.get('states')[0]\n                break\n            time.sleep(1)\n        else:\n            raise RuntimeError('container did not report state')\n        machineID = state.get('machineID')\n\n        # find the machine\n        machines = self._get_machines()\n        if not machines:\n            raise RuntimeError('no available hosts to run command')\n\n        # find the machine's primaryIP\n        primaryIP = None\n        for m in machines.get('machines', []):\n            if m['id'] == machineID:\n                primaryIP = m['primaryIP']\n        if not primaryIP:\n            raise RuntimeError('could not find host')\n\n        # prepare ssh key\n        file_obj = cStringIO.StringIO(base64.b64decode(self.auth))\n        pkey = paramiko.RSAKey(file_obj=file_obj)\n\n        # grab output via docker logs over SSH\n        ssh = paramiko.SSHClient()\n        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n        ssh.connect(primaryIP, username=\"core\", pkey=pkey)\n        # share a transport\n        tran = ssh.get_transport()\n\n        def _do_ssh(cmd):\n            chan = tran.open_session()\n            # get a pty so stdout/stderr look right\n            chan.get_pty()\n            out = chan.makefile()\n            chan.exec_command(cmd)\n            rc, output = chan.recv_exit_status(), out.read()\n            return rc, output\n\n        # wait for container to start\n        for _ in range(1200):\n            rc, _ = _do_ssh('docker inspect {name}'.format(**locals()))\n            if rc == 0:\n                break\n            time.sleep(1)\n        else:\n            raise RuntimeError('container failed to start on host')\n\n        # wait for container to complete\n        for _ in range(1200):\n            _rc, _output = _do_ssh('docker inspect {name}'.format(**locals()))\n            if _rc != 0:\n                raise RuntimeError('failed to inspect container')\n            _container = json.loads(_output)\n            finished_at = _container[0][\"State\"][\"FinishedAt\"]\n            if not finished_at.startswith('0001'):\n                break\n            time.sleep(1)\n        else:\n            raise RuntimeError('container timed out')\n\n        # gather container output\n        _rc, output = _do_ssh('docker logs {name}'.format(**locals()))\n        if _rc != 0:\n            raise RuntimeError('could not attach to container')\n\n        # determine container exit code\n        _rc, _output = _do_ssh('docker inspect {name}'.format(**locals()))\n        if _rc != 0:\n            raise RuntimeError('could not determine exit code')\n        container = json.loads(_output)\n        rc = container[0][\"State\"][\"ExitCode\"]\n\n        # cleanup\n        self._destroy_container(name)\n        self._wait_for_destroy(name)\n\n        # return rc and output\n        return rc, output\n\n    def attach(self, name):\n        \"\"\"\n        Attach to a job's stdin, stdout and stderr\n        \"\"\"\n        raise NotImplementedError\n\nSchedulerClient = FleetHTTPClient\n\n\nCONTAINER_TEMPLATE = [\n    {\"section\": \"Unit\", \"name\": \"Description\", \"value\": \"{name}\"},\n    {\"section\": \"Service\", \"name\": \"ExecStartPre\", \"value\": '''/bin/sh -c \"IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker pull $IMAGE\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"ExecStartPre\", \"value\": '''/bin/sh -c \"docker inspect {name} >/dev/null 2>&1 && docker rm -f {name} || true\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"ExecStart\", \"value\": '''/bin/sh -c \"IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; port=$(docker inspect -f '{{{{range $k, $v := .ContainerConfig.ExposedPorts }}}}{{{{$k}}}}{{{{end}}}}' $IMAGE | cut -d/ -f1) ; docker run --name {name} {memory} {cpu} -P -e PORT=$port $IMAGE {command}\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"ExecStop\", \"value\": '''/usr/bin/docker rm -f {name}'''},\n    {\"section\": \"Service\", \"name\": \"TimeoutStartSec\", \"value\": \"20m\"},\n    {\"section\": \"Service\", \"name\": \"RestartSec\", \"value\": \"5\"},\n    {\"section\": \"Service\", \"name\": \"Restart\", \"value\": \"on-failure\"},\n]\n\n\nRUN_TEMPLATE = [\n    {\"section\": \"Unit\", \"name\": \"Description\", \"value\": \"{name} admin command\"},\n    {\"section\": \"Service\", \"name\": \"ExecStartPre\", \"value\": '''/bin/sh -c \"IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker pull $IMAGE\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"ExecStartPre\", \"value\": '''/bin/sh -c \"docker inspect {name} >/dev/null 2>&1 && docker rm -f {name} || true\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"ExecStart\", \"value\": '''/bin/sh -c \"IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker run --name {name} --entrypoint={entrypoint} -a stdout -a stderr $IMAGE {command}\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"TimeoutStartSec\", \"value\": \"20m\"},\n]\n", "path": "controller/scheduler/coreos.py"}], "after_files": [{"content": "import cStringIO\nimport base64\nimport copy\nimport json\nimport httplib\nimport paramiko\nimport socket\nimport re\nimport time\n\n\nMATCH = re.compile(\n    '(?P<app>[a-z0-9-]+)_?(?P<version>v[0-9]+)?\\.?(?P<c_type>[a-z-_]+)?.(?P<c_num>[0-9]+)')\n\n\nclass UHTTPConnection(httplib.HTTPConnection):\n    \"\"\"Subclass of Python library HTTPConnection that uses a Unix domain socket.\n    \"\"\"\n\n    def __init__(self, path):\n        httplib.HTTPConnection.__init__(self, 'localhost')\n        self.path = path\n\n    def connect(self):\n        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n        sock.connect(self.path)\n        self.sock = sock\n\n\nclass FleetHTTPClient(object):\n\n    def __init__(self, cluster_name, hosts, auth, domain, options):\n        self.name = cluster_name\n        self.hosts = hosts\n        self.auth = auth\n        self.domain = domain\n        self.options = options\n        # single global connection\n        self.conn = UHTTPConnection('/var/run/fleet.sock')\n\n    # scheduler setup / teardown\n\n    def setUp(self):\n        pass\n\n    def tearDown(self):\n        pass\n\n    # connection helpers\n\n    def _put_unit(self, name, body):\n        headers = {'Content-Type': 'application/json'}\n        self.conn.request('PUT', '/v1-alpha/units/{name}.service'.format(**locals()),\n                          headers=headers, body=json.dumps(body))\n        resp = self.conn.getresponse()\n        data = resp.read()\n        if not 200 <= resp.status <= 299:\n            errmsg = \"Failed to create unit: {} {} - {}\".format(\n                resp.status, resp.reason, data)\n            raise RuntimeError(errmsg)\n        return data\n\n    def _delete_unit(self, name):\n        headers = {'Content-Type': 'application/json'}\n        self.conn.request('DELETE', '/v1-alpha/units/{name}.service'.format(**locals()),\n                          headers=headers)\n        resp = self.conn.getresponse()\n        data = resp.read()\n        if resp.status not in (404, 204):\n            errmsg = \"Failed to delete unit: {} {} - {}\".format(\n                resp.status, resp.reason, data)\n            raise RuntimeError(errmsg)\n        return data\n\n    def _get_state(self, name=None):\n        headers = {'Content-Type': 'application/json'}\n        url = '/v1-alpha/state'\n        if name:\n            url += '?unitName={name}.service'.format(**locals())\n        self.conn.request('GET', url, headers=headers)\n        resp = self.conn.getresponse()\n        data = resp.read()\n        if resp.status not in (200,):\n            errmsg = \"Failed to retrieve state: {} {} - {}\".format(\n                resp.status, resp.reason, data)\n            raise RuntimeError(errmsg)\n        return json.loads(data)\n\n    def _get_machines(self):\n        headers = {'Content-Type': 'application/json'}\n        url = '/v1-alpha/machines'\n        self.conn.request('GET', url, headers=headers)\n        resp = self.conn.getresponse()\n        data = resp.read()\n        if resp.status not in (200,):\n            errmsg = \"Failed to retrieve machines: {} {} - {}\".format(\n                resp.status, resp.reason, data)\n            raise RuntimeError(errmsg)\n        return json.loads(data)\n\n    # container api\n\n    def create(self, name, image, command='', template=None, **kwargs):\n        \"\"\"Create a container\"\"\"\n        self._create_container(name, image, command,\n                               template or copy.deepcopy(CONTAINER_TEMPLATE), **kwargs)\n\n    def _create_container(self, name, image, command, unit, **kwargs):\n        l = locals().copy()\n        l.update(re.match(MATCH, name).groupdict())\n        # prepare memory limit for the container type\n        mem = kwargs.get('memory', {}).get(l['c_type'], None)\n        if mem:\n            l.update({'memory': '-m {}'.format(mem.lower())})\n        else:\n            l.update({'memory': ''})\n        # prepare memory limit for the container type\n        cpu = kwargs.get('cpu', {}).get(l['c_type'], None)\n        if cpu:\n            l.update({'cpu': '-c {}'.format(cpu)})\n        else:\n            l.update({'cpu': ''})\n        # should a special entrypoint be used\n        entrypoint = kwargs.get('entrypoint')\n        if entrypoint:\n            l.update({'entrypoint': '{}'.format(entrypoint)})\n        # construct unit from template\n        for f in unit:\n            f['value'] = f['value'].format(**l)\n        # prepare tags only if one was provided\n        tags = kwargs.get('tags', {})\n        if tags:\n            tagset = ' '.join(['\"{}={}\"'.format(k, v) for k, v in tags.items()])\n            unit.append({\"section\": \"X-Fleet\", \"name\": \"MachineMetadata\",\n                         \"value\": tagset})\n        # post unit to fleet\n        self._put_unit(name, {\"desiredState\": \"launched\", \"options\": unit})\n\n    def start(self, name):\n        \"\"\"Start a container\"\"\"\n        self._wait_for_container(name)\n\n    def _wait_for_container(self, name):\n        # we bump to 20 minutes here to match the timeout on the router and in the app unit files\n        for _ in range(1200):\n            states = self._get_state(name)\n            if states and len(states.get('states', [])) == 1:\n                state = states.get('states')[0]\n                subState = state.get('systemdSubState')\n                if subState == 'running' or subState == 'exited':\n                    break\n                elif subState == 'failed':\n                    raise RuntimeError('container failed to start')\n            time.sleep(1)\n        else:\n            raise RuntimeError('container failed to start')\n\n    def _wait_for_destroy(self, name):\n        for _ in range(30):\n            states = self._get_state(name)\n            if not states:\n                break\n            time.sleep(1)\n        else:\n            raise RuntimeError('timeout on container destroy')\n\n    def stop(self, name):\n        \"\"\"Stop a container\"\"\"\n        raise NotImplementedError\n\n    def destroy(self, name):\n        \"\"\"Destroy a container\"\"\"\n        # call all destroy functions, ignoring any errors\n        try:\n            self._destroy_container(name)\n        except:\n            pass\n        self._wait_for_destroy(name)\n\n    def _destroy_container(self, name):\n        return self._delete_unit(name)\n\n    def run(self, name, image, entrypoint, command):  # noqa\n        \"\"\"Run a one-off command\"\"\"\n        self._create_container(name, image, command, copy.deepcopy(RUN_TEMPLATE),\n                               entrypoint=entrypoint)\n\n        # wait for the container to get scheduled\n        for _ in range(30):\n            states = self._get_state(name)\n            if states and len(states.get('states', [])) == 1:\n                state = states.get('states')[0]\n                break\n            time.sleep(1)\n        else:\n            raise RuntimeError('container did not report state')\n        machineID = state.get('machineID')\n\n        # find the machine\n        machines = self._get_machines()\n        if not machines:\n            raise RuntimeError('no available hosts to run command')\n\n        # find the machine's primaryIP\n        primaryIP = None\n        for m in machines.get('machines', []):\n            if m['id'] == machineID:\n                primaryIP = m['primaryIP']\n        if not primaryIP:\n            raise RuntimeError('could not find host')\n\n        # prepare ssh key\n        file_obj = cStringIO.StringIO(base64.b64decode(self.auth))\n        pkey = paramiko.RSAKey(file_obj=file_obj)\n\n        # grab output via docker logs over SSH\n        ssh = paramiko.SSHClient()\n        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n        ssh.connect(primaryIP, username=\"core\", pkey=pkey)\n        # share a transport\n        tran = ssh.get_transport()\n\n        def _do_ssh(cmd):\n            chan = tran.open_session()\n            # get a pty so stdout/stderr look right\n            chan.get_pty()\n            out = chan.makefile()\n            chan.exec_command(cmd)\n            rc, output = chan.recv_exit_status(), out.read()\n            return rc, output\n\n        # wait for container to start\n        for _ in range(1200):\n            rc, _ = _do_ssh('docker inspect {name}'.format(**locals()))\n            if rc == 0:\n                break\n            time.sleep(1)\n        else:\n            raise RuntimeError('container failed to start on host')\n\n        # wait for container to complete\n        for _ in range(1200):\n            _rc, _output = _do_ssh('docker inspect {name}'.format(**locals()))\n            if _rc != 0:\n                raise RuntimeError('failed to inspect container')\n            _container = json.loads(_output)\n            finished_at = _container[0][\"State\"][\"FinishedAt\"]\n            if not finished_at.startswith('0001'):\n                break\n            time.sleep(1)\n        else:\n            raise RuntimeError('container timed out')\n\n        # gather container output\n        _rc, output = _do_ssh('docker logs {name}'.format(**locals()))\n        if _rc != 0:\n            raise RuntimeError('could not attach to container')\n\n        # determine container exit code\n        _rc, _output = _do_ssh('docker inspect {name}'.format(**locals()))\n        if _rc != 0:\n            raise RuntimeError('could not determine exit code')\n        container = json.loads(_output)\n        rc = container[0][\"State\"][\"ExitCode\"]\n\n        # cleanup\n        self._destroy_container(name)\n        self._wait_for_destroy(name)\n\n        # return rc and output\n        return rc, output\n\n    def attach(self, name):\n        \"\"\"\n        Attach to a job's stdin, stdout and stderr\n        \"\"\"\n        raise NotImplementedError\n\nSchedulerClient = FleetHTTPClient\n\n\nCONTAINER_TEMPLATE = [\n    {\"section\": \"Unit\", \"name\": \"Description\", \"value\": \"{name}\"},\n    {\"section\": \"Service\", \"name\": \"ExecStartPre\", \"value\": '''/bin/sh -c \"IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker pull $IMAGE\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"ExecStartPre\", \"value\": '''/bin/sh -c \"docker inspect {name} >/dev/null 2>&1 && docker rm -f {name} || true\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"ExecStart\", \"value\": '''/bin/sh -c \"IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; port=$(docker inspect -f '{{{{range $k, $v := .ContainerConfig.ExposedPorts }}}}{{{{$k}}}}{{{{end}}}}' $IMAGE | cut -d/ -f1) ; docker run --name {name} {memory} {cpu} -P -e PORT=$port $IMAGE {command}\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"ExecStop\", \"value\": '''/usr/bin/docker rm -f {name}'''},\n    {\"section\": \"Service\", \"name\": \"TimeoutStartSec\", \"value\": \"20m\"},\n    {\"section\": \"Service\", \"name\": \"RestartSec\", \"value\": \"5\"},\n    {\"section\": \"Service\", \"name\": \"Restart\", \"value\": \"on-failure\"},\n]\n\n\nRUN_TEMPLATE = [\n    {\"section\": \"Unit\", \"name\": \"Description\", \"value\": \"{name} admin command\"},\n    {\"section\": \"Service\", \"name\": \"ExecStartPre\", \"value\": '''/bin/sh -c \"IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker pull $IMAGE\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"ExecStartPre\", \"value\": '''/bin/sh -c \"docker inspect {name} >/dev/null 2>&1 && docker rm -f {name} || true\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"ExecStart\", \"value\": '''/bin/sh -c \"IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker run --name {name} --entrypoint={entrypoint} -a stdout -a stderr $IMAGE {command}\"'''},  # noqa\n    {\"section\": \"Service\", \"name\": \"TimeoutStartSec\", \"value\": \"20m\"},\n]\n", "path": "controller/scheduler/coreos.py"}]}