Skip to content

Commit 039ad59

Browse files
Gabriel MonroyMatthew Fisher
authored andcommitted
refactor(deis-run): new deis run implementation and celery task
1 parent ac956d1 commit 039ad59

7 files changed

Lines changed: 65 additions & 14 deletions

File tree

client/deis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ def apps_run(self, args):
621621
"/api/apps/{}/run".format(app),
622622
json.dumps(body))
623623
if response.status_code == requests.codes.ok: # @UndefinedVariable
624-
output, rc = json.loads(response.content)
624+
rc, output = json.loads(response.content)
625625
if rc != 0:
626626
print('Warning: non-zero return code {}'.format(rc))
627627
sys.stdout.write(output)

controller/api/models.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,15 @@ def logs(self):
178178
def run(self, command):
179179
"""Run a one-off command in an ephemeral app container."""
180180
# TODO: add support for interactive shell
181-
release = self.release_set.latest()
182-
if not release.build:
183-
raise EnvironmentError('No build exists, please run `git push deis master` first')
184-
# prepare ssh command
185-
version = release.version
186-
image = release.image
187-
docker_args = ' '.join(['-a', 'stdout', '-a', 'stderr', '-rm', image])
188-
env_args = ' '.join(["-e '{k}={v}'".format(**locals())
189-
for k, v in release.config.values.items()])
190181
log_event(self, "deis run '{}'".format(command))
191-
command = "sudo docker run {env_args} {docker_args} {command}".format(**locals())
192-
# TODO: wire up to job dispath
193-
return 0, 'Not implemented yet'
182+
c_num = max([c.num for c in self.container_set.filter(type='admin')] or [0]) + 1
183+
c = Container.objects.create(owner=self.owner,
184+
app=self,
185+
release=self.release_set.latest(),
186+
type='admin',
187+
num=c_num)
188+
rc, output = tasks.run_command.delay(c, command).get()
189+
return rc, output
194190

195191

196192
@python_2_unicode_compatible
@@ -295,6 +291,14 @@ def destroy(self):
295291
self.state = 'destroyed'
296292
self.save()
297293

294+
def run(self, command):
295+
self.state = 'running'
296+
self.save()
297+
rc, output = self._scheduler.run(self._job_id, self.release.image, command)
298+
self.state = 'completed'
299+
self.save()
300+
return rc, output
301+
298302

299303
@python_2_unicode_compatible
300304
class Push(UuidAuditedModel):

controller/api/tasks.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,23 @@ def stop_containers(containers):
7676
c.state = 'error'
7777
c.save()
7878
raise
79+
80+
81+
@task
82+
def run_command(c, command):
83+
release = c.release
84+
version = release.version
85+
image = release.image
86+
try:
87+
# pull the image first
88+
rc, pull_output = c.run("docker pull {image}".format(**locals()))
89+
if rc != 0:
90+
raise EnvironmentError('Could not pull image: {pull_image}'.format(**locals()))
91+
# run the command
92+
docker_args = ' '.join(['-a', 'stdout', '-a', 'stderr', '--rm', image])
93+
env_args = ' '.join(["-e '{k}={v}'".format(**locals())
94+
for k, v in release.config.values.items()])
95+
command = "docker run {env_args} {docker_args} {command}".format(**locals())
96+
return c.run(command)
97+
finally:
98+
c.delete()

controller/scheduler/coreos.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ def run(self, name, image, command):
199199
rc = p.wait()
200200
return rc, p.stdout.read()
201201

202+
def run(self, name, image, command):
203+
"""
204+
Run a one-off command
205+
"""
206+
print 'Running {name}'.format(**locals())
207+
output = subprocess.PIPE
208+
p = subprocess.Popen('fleetrun.sh {command}'.format(**locals()), shell=True, env=self.env,
209+
stdout=output, stderr=subprocess.STDOUT)
210+
rc = p.wait()
211+
return rc, p.stdout.read()
212+
202213
def attach(self, name):
203214
"""
204215
Attach to a job's stdin, stdout and stderr

controller/scheduler/fleetctl.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
set -e
33

4-
SSH_OPTIONS="-i $FLEETW_KEY -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
4+
SSH_OPTIONS="-i $FLEETW_KEY -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR"
55

66
# set debug if provided as an envvar
77
[[ $DEBUG ]] && set -x

controller/scheduler/fleetrun.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -e
3+
4+
SSH_OPTIONS="-i $FLEETW_KEY -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR"
5+
6+
# set debug if provided as an envvar
7+
[[ $DEBUG ]] && set -x
8+
9+
# run the fleetctl command remotely
10+
ssh $SSH_OPTIONS core@$FLEETW_HOST $@

controller/scheduler/mock.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def destroy(self, name):
5050
"""
5151
return {'state': 'inactive'}
5252

53+
def run(self, name, image, command):
54+
"""
55+
Run a one-off command
56+
"""
57+
return 0, ''
58+
5359
def attach(self, name):
5460
"""
5561
Attach to a job's stdin, stdout and stderr

0 commit comments

Comments
 (0)