-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from redcanari/adding-dockerize-subcommand
Adding dockerize subcommand
- Loading branch information
Showing
11 changed files
with
212 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
FROM alpine | ||
COPY . /root/sdist | ||
RUN apk add --no-cache \ | ||
ca-certificates \ | ||
alpine-sdk \ | ||
python-dev \ | ||
py-lxml \ | ||
py-setuptools \ | ||
py-twisted && \ | ||
cd /root/sdist && \ | ||
python setup.py install && \ | ||
cd /root && \ | ||
rm -rf sdist && \ | ||
apk del \ | ||
alpine-sdk \ | ||
python-dev | ||
|
||
ENTRYPOINT ["canari"] |
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 |
---|---|---|
|
@@ -25,7 +25,8 @@ | |
'argparse', | ||
'flask', | ||
'Twisted', | ||
'safedexml' | ||
'safedexml', | ||
'lxml' | ||
] | ||
|
||
if os.name == 'nt': | ||
|
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,104 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import subprocess | ||
|
||
import re | ||
|
||
from canari import question | ||
from mrbob.configurator import Configurator | ||
|
||
from canari.project import CanariProject | ||
from canari.utils.fs import PushDir | ||
from common import canari_main | ||
from distutils.spawn import find_executable | ||
from framework import SubCommand, Argument | ||
|
||
__author__ = 'Nadeem Douba' | ||
__copyright__ = 'Copyright 2012, Canari Project' | ||
__credits__ = ['Nadeem Douba'] | ||
|
||
__license__ = 'GPLv3' | ||
__version__ = '0.5' | ||
__maintainer__ = 'Nadeem Douba' | ||
__email__ = 'ndouba@gmail.com' | ||
__status__ = 'Development' | ||
|
||
|
||
def run_command(args, **kwargs): | ||
print 'Running command: %s' % ' '.join(args) | ||
return subprocess.Popen(args, **kwargs) | ||
|
||
@SubCommand( | ||
canari_main, | ||
help='Creates a Docker build file pre-configured with Plume.', | ||
description='Creates a Docker build file that can be used to run remote transforms using Plume.' | ||
) | ||
@Argument( | ||
'-H', | ||
'--host', | ||
metavar='[host]', | ||
action='append', | ||
default=[], | ||
help='Docker daemon socket(s) to connect to' | ||
) | ||
def dockerize_package(args): | ||
|
||
project = CanariProject() | ||
|
||
print('Dockerizing %s transform package...' % project.name) | ||
|
||
configurator = Configurator( | ||
'canari.resources.templates:dockerize_package', | ||
project.root_dir, | ||
{'non_interactive': True}, | ||
variables={'project.name': project.name} | ||
) | ||
|
||
print('Creating Dockerfile for %s...' % project.name) | ||
configurator.render() | ||
print('done!') | ||
|
||
if not find_executable('docker'): | ||
print """Could not find 'docker' in your system path. Please download and install Docker from http://docker.com | ||
and rerun this command again. | ||
""" | ||
|
||
exit(-1) | ||
|
||
docker_hosts = [j for sublist in [('-H', i) for i in args.host] for j in sublist] | ||
container = '%s/%s' % (project.name, project.name) | ||
|
||
if not args.host: | ||
if not find_executable('docker-machine'): | ||
print """Could not find 'docker-machine' in your system path. Please download and install Docker Machine from | ||
http://docker.com and rerun this command again or manually specify a Docker host using the '-H' parameter, | ||
instead. | ||
""" | ||
exit(-1) | ||
|
||
print 'Attempting to discover available Docker machines.' | ||
machines = run_command(['docker-machine', 'ls', '-q'], stdout=subprocess.PIPE).communicate()[0].split('\n') | ||
machines.remove('') | ||
|
||
machine = question.parse_int('More than one Docker machine was detected. Which one would you like to use to' | ||
'build and run this container?', machines) if len(machines) != 1 else 0 | ||
|
||
print 'Setting up environment for Docker machine %s' % machines[machine] | ||
|
||
# Inject docker environment variables | ||
env = run_command(['docker-machine', 'env', machines[machine]], stdout=subprocess.PIPE).communicate()[0] | ||
os.environ.update(re.findall(r'export ([^=]+)="([^"]+)', env)) | ||
|
||
with PushDir(project.root_dir): | ||
p = run_command(['docker'] + docker_hosts + ['build', '-t', container, '.']) | ||
p.communicate() | ||
if p.returncode: | ||
print 'An error occurred while building the Docker container.' | ||
exit(-1) | ||
|
||
if question.parse_bool('Would you like to run this container now?'): | ||
port = question.parse_int_range('Which port would you like Plume to listen on externally?', 0, 65535, 8080) | ||
print 'Plume will be listening on http://%s:%s' % (re.findall('://([^:]+)', os.environ['DOCKER_HOST'])[0], port) | ||
run_command(['docker'] + docker_hosts + ['run', '-it', '-p', '8080:%s' % port, container]).communicate() | ||
|
||
print 'done!' |
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
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
Empty file.
22 changes: 22 additions & 0 deletions
22
src/canari/resources/templates/dockerize_package/Dockerfile.bob
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,22 @@ | ||
FROM redcanari/canari | ||
ADD . /root/sdist | ||
RUN apk add --no-cache \ | ||
ca-certificates \ | ||
alpine-sdk \ | ||
python-dev \ | ||
py-lxml \ | ||
py-setuptools \ | ||
py-twisted && \ | ||
cd /root/sdist && \ | ||
python setup.py install && \ | ||
cd /root && \ | ||
rm -rf /root/sdist && \ | ||
mkdir -p /var/plume && \ | ||
cd /var/plume && \ | ||
canari install-plume -y && \ | ||
canari load-plume-package -y {{{ project.name }}} && \ | ||
apk del \ | ||
alpine-sdk \ | ||
python-dev | ||
EXPOSE 8080 | ||
ENTRYPOINT /etc/init.d/plume start-docker |
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