-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docker is now the default driver, subprocess is used to communicate with the docker itself. runbook_runner image is used as a base image for all the runs and each run spawns a new image, that is later removed.
- Loading branch information
Showing
4 changed files
with
138 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Copyright 2016: Mirantis Inc. | ||
# All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import logging | ||
import os | ||
import shutil | ||
import subprocess | ||
import tempfile | ||
import uuid | ||
|
||
from runbook.drivers import base | ||
|
||
LOG = logging.getLogger("runner.docker") | ||
|
||
|
||
RUNNER_DOCKERFILE = """ | ||
FROM ubuntu:14.04 | ||
RUN apt-get update | ||
RUN apt-get install python -y | ||
""" | ||
|
||
RUN_DOCKERFILE = """ | ||
FROM runbook_runner | ||
COPY . /runbook | ||
WORKDIR /runbook | ||
ENTRYPOINT ["{interpreter}"] | ||
CMD ["runbook"] | ||
""" | ||
|
||
|
||
class Driver(base.Driver): | ||
|
||
@classmethod | ||
def initialise(cls): | ||
dirname = tempfile.mkdtemp() | ||
with open(os.path.join(dirname, 'Dockerfile'), "w") as f: | ||
f.write(RUNNER_DOCKERFILE) | ||
output = subprocess.check_output( | ||
["docker", "build", "-t", "runbook_runner", dirname], | ||
stderr=subprocess.STDOUT,) | ||
LOG.info("Built 'runbook_runner' image, {}".format(output)) | ||
|
||
shutil.rmtree(dirname) | ||
|
||
@classmethod | ||
def run(cls, runbook, parameters): | ||
LOG.info("Running runbook '{}' with parameters '{}'".format( | ||
runbook, parameters)) | ||
|
||
interpreter = cls.interpreters.get(runbook.get("type")) | ||
if not interpreter: | ||
return { | ||
"return_code": -1, | ||
"output": "Don't know how to run '{}' type runbook".format( | ||
runbook.get("type")), | ||
} | ||
|
||
dirname = tempfile.mkdtemp() | ||
|
||
with open(os.path.join(dirname, 'runbook'), "w") as f: | ||
f.write(runbook["runbook"]) | ||
|
||
with open(os.path.join(dirname, 'Dockerfile'), "w") as f: | ||
f.write(RUN_DOCKERFILE.format( | ||
interpreter=interpreter)) | ||
|
||
run_name = "run-{}".format(uuid.uuid4().hex) | ||
output = subprocess.check_output( | ||
["docker", "build", "-t", run_name, dirname], | ||
stderr=subprocess.STDOUT,) | ||
LOG.info("Built 'runbook_runner' image, {}".format(output)) | ||
|
||
returncode = 0 | ||
try: | ||
output = subprocess.check_output( | ||
["docker", "run", "--name", run_name, run_name], | ||
stderr=subprocess.STDOUT, | ||
) | ||
except subprocess.CalledProcessError as e: | ||
output = e.output | ||
returncode = e.returncode | ||
|
||
# cleanup | ||
try: | ||
subprocess.check_output( | ||
["docker", "rm", run_name], | ||
stderr=subprocess.STDOUT, | ||
) | ||
LOG.info("Removed container {}".format(run_name)) | ||
except subprocess.CalledProcessError as e: | ||
LOG.warning("Could not remove container {}".format( | ||
run_name, e.output)) | ||
|
||
try: | ||
subprocess.check_output( | ||
["docker", "rmi", run_name], | ||
stderr=subprocess.STDOUT, | ||
) | ||
LOG.info("Removed image {}".format(run_name)) | ||
except subprocess.CalledProcessError as e: | ||
LOG.warning("Could not remove image {}: {}".format( | ||
run_name, e.output)) | ||
|
||
shutil.rmtree(dirname, ignore_errors=True) | ||
return { | ||
"return_code": returncode, | ||
"output": output, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters