Merge lp:~facundo/locolander/ns2df into lp:locolander

Proposed by Facundo Batista
Status: Merged
Approved by: Loco Lander
Approved revision: 5
Merged at revision: 8
Proposed branch: lp:~facundo/locolander/ns2df
Merge into: lp:locolander
Diff against target: 308 lines (+297/-0)
2 files modified
ns2df.py (+129/-0)
tests/test_ns2df.py (+168/-0)
To merge this branch: bzr merge lp:~facundo/locolander/ns2df
Reviewer Review Type Date Requested Status
Loco Lander Approve
Review via email: mp+170959@code.launchpad.net

Commit message

The transformer from locolander config to docker stuff

Description of the change

The transformer from locolander config to docker stuff

To post a comment you must log in.
Revision history for this message
Loco Lander (locolander) wrote :

l. 31-33: this should be

run pip install --download=/var/cache/locolander/pip --no-install foobar
run ip link set dev eth0 down
run pip install --find-links=file:///var/cache/locolander/pip --no-index foobar

Revision history for this message
Loco Lander (locolander) wrote :

l. 20: I'd use pip instead of pypi
l. 29: I'd split each dependency into it's own line to make better use of the underlying caching algorithm of docker
l. 29: use apt-get install -q -y to avoid apt-get asking questions interactively
l. 30: no longer needed, part of the base image (assume /var/cache/locolander exists and will be used as the pip cache)

lp:~facundo/locolander/ns2df updated
3. By Facundo Batista

Some changes.

4. By Facundo Batista

More changes.

5. By Facundo Batista

Merged trunk in.

Revision history for this message
Loco Lander (locolander) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'ns2df.py'
--- ns2df.py 1970-01-01 00:00:00 +0000
+++ ns2df.py 2013-06-22 23:34:26 +0000
@@ -0,0 +1,129 @@
1"""Nessita Syntax to Docker File.
2
3Example:
4
5 precise:
6 apt:
7 apache2: 3.3-4
8 bzr:
9 pip:
10 foobar:
11 metadata:
12 test_script: ./test
13
14
15will be converted to
16
17 from ubuntu:precise
18 run apt-get -q -y install apache2=3.3-4
19 run apt-get -q -y install bzr
20 run pip install --download=/tmp/pipcache --no-install foobar
21 run ip link set dev eth0 down
22 run pip install --find-links=file:///tmp/pipcache --no-index foobar
23
24and also the system will use "./test" as script
25"""
26
27import yaml
28
29# the conversion between our nice base names and those that are
30# needed by docker
31BASE_TRANSLATIONS = {
32 'precise': 'ubuntu:precise',
33}
34
35def _get_base(config, **params):
36 """Process the system base stuff."""
37 # support only one base for now
38 if len(config) > 1:
39 raise NotImplementedError("Too many base machines.")
40 base = config.keys()[0]
41 return ["from " + BASE_TRANSLATIONS[base]]
42
43
44def _get_depends_prev(config, **params):
45 """All the dependencies before securing machine."""
46 # support only one base for now
47 config = config.values()[0]
48 dependencies = []
49
50 # apt
51 items = config.get('apt', [])
52 if items:
53 for name, ver in sorted(items.items()):
54 if ver is None:
55 p = name
56 else:
57 p = "%s=%s" % (name, ver)
58 dep = "run apt-get -q -y install " + p
59 dependencies.append(dep)
60
61 # pip
62 items = config.get('pip', [])
63 pip_cache_dir = params["pip_cache_dir"]
64 if items:
65 for name, ver in sorted(items.items()):
66 if ver is None:
67 p = name
68 else:
69 p = "%s==%s" % (name, ver)
70 dep = "run pip install --download=%s --no-install %s" % (
71 pip_cache_dir, p)
72 dependencies.append(dep)
73
74 return dependencies
75
76def _get_secure(config, **params):
77 """All that needs to be done to secure the machine."""
78 return ["run ip link set dev eth0 down"]
79
80def _get_depends_post(config, **params):
81 """All the dependencies stuff after the machine was secured."""
82 # support only one base for now
83 config = config.values()[0]
84 dependencies = []
85
86 # pip
87 items = config.get('pip', [])
88 pip_cache_dir = params["pip_cache_dir"]
89 if items:
90 for name, ver in sorted(items.items()):
91 if ver is None:
92 p = name
93 else:
94 p = "%s==%s" % (name, ver)
95 dep = "run pip install --find-links=file://%s --no-index %s" % (
96 pip_cache_dir, p)
97 dependencies.append(dep)
98
99 return dependencies
100
101
102def _get_rest(config, **params):
103 """The final stuff."""
104 return []
105
106
107def parse(config_text, pip_cache_dir):
108 """Convert Nessita Syntax and return the text for Docker."""
109 config = yaml.load(config_text)
110 metadata = config.pop("metadata")
111
112 # the order for calling these functions is VERY important
113 functions = [
114 _get_base,
115 _get_depends_prev,
116 _get_secure,
117 _get_depends_post,
118 _get_rest,
119 ]
120 data = []
121 for func in functions:
122 items = func(config, pip_cache_dir=pip_cache_dir)
123 data.extend(items)
124 docker = "\n".join(data)
125
126 # get stuff from the metadata
127 script = metadata["test_script"]
128
129 return script, docker
0130
=== added directory 'tests'
=== added file 'tests/__init__.py'
=== added file 'tests/test_ns2df.py'
--- tests/test_ns2df.py 1970-01-01 00:00:00 +0000
+++ tests/test_ns2df.py 2013-06-22 23:34:26 +0000
@@ -0,0 +1,168 @@
1"""Tests for the ns2df convertor."""
2
3import unittest
4
5import ns2df
6
7class DependenciesTestCase(unittest.TestCase):
8 """Tests for the dependencies transformer."""
9
10 def test_no_dependencies(self):
11 config = dict(somebase={})
12 res = ns2df._get_depends_prev(config, pip_cache_dir="/tmp/pipcache")
13 self.assertEqual(res, [])
14 res = ns2df._get_depends_post(config, pip_cache_dir="/tmp/pipcache")
15 self.assertEqual(res, [])
16
17 def test_apt_one(self):
18 config = dict(somebase={
19 'apt': {'bzr': None}
20 })
21 res = ns2df._get_depends_prev(config, pip_cache_dir="/tmp/pipcache")
22 self.assertEqual(res, ["run apt-get -q -y install bzr"])
23
24 def test_apt_several(self):
25 config = dict(somebase={
26 'apt': {'bzr': None, 'apache2': None}
27 })
28 res = ns2df._get_depends_prev(config, pip_cache_dir="/tmp/pipcache")
29 self.assertEqual(res, [
30 "run apt-get -q -y install apache2",
31 "run apt-get -q -y install bzr",
32 ])
33
34 def test_apt_versions(self):
35 config = dict(somebase={
36 'apt': {'bzr': None, 'apache2': '3.3-4'}
37 })
38 res = ns2df._get_depends_prev(config, pip_cache_dir="/tmp/pipcache")
39 self.assertEqual(res, [
40 "run apt-get -q -y install apache2=3.3-4",
41 "run apt-get -q -y install bzr",
42 ])
43
44 def test_pip_prev_one(self):
45 config = dict(somebase={
46 'pip': {'foo': None}
47 })
48 res = ns2df._get_depends_prev(config, pip_cache_dir="/tmp/pipcache")
49 self.assertEqual(res, [
50 "run pip install --download=/tmp/pipcache --no-install foo",
51 ])
52
53 def test_pip_prev_several(self):
54 config = dict(somebase={
55 'pip': {'foo': None, 'bar': None}
56 })
57 res = ns2df._get_depends_prev(config, pip_cache_dir="/tmp/pipcache")
58 self.assertEqual(res, [
59 "run pip install --download=/tmp/pipcache --no-install bar",
60 "run pip install --download=/tmp/pipcache --no-install foo",
61 ])
62
63 def test_pip_prev_versions(self):
64 config = dict(somebase={
65 'pip': {'foo': None, 'bar': '2.1'}
66 })
67 res = ns2df._get_depends_prev(config, pip_cache_dir="/tmp/pipcache")
68 self.assertEqual(res, [
69 "run pip install --download=/tmp/pipcache --no-install bar==2.1",
70 "run pip install --download=/tmp/pipcache --no-install foo",
71 ])
72
73 def test_pip_post_one(self):
74 config = dict(somebase={
75 'pip': {'foo': None}
76 })
77 res = ns2df._get_depends_post(config, pip_cache_dir="/tmp/pipcache")
78 self.assertEqual(res, [
79 "run pip install --find-links=file:///tmp/pipcache --no-index foo"
80 ])
81
82 def test_pip_post_several(self):
83 config = dict(somebase={
84 'pip': {'foo': None, 'bar': None}
85 })
86 res = ns2df._get_depends_post(config, pip_cache_dir="/tmp/pipcache")
87 self.assertEqual(res, [
88 "run pip install --find-links=file:///tmp/pipcache --no-index bar",
89 "run pip install --find-links=file:///tmp/pipcache --no-index foo",
90 ])
91
92 def test_pip_post_versions(self):
93 config = dict(somebase={
94 'pip': {'foo': None, 'bar': '2.1'}
95 })
96 res = ns2df._get_depends_post(config, pip_cache_dir="/tmcache")
97 self.assertEqual(res, [
98 "run pip install --find-links=file:///tmcache --no-index bar==2.1",
99 "run pip install --find-links=file:///tmcache --no-index foo",
100 ])
101
102 def test_mixed_prev(self):
103 config = dict(somebase={
104 'apt': {'bzr': None, 'apache2': '3.3-4'},
105 'pip': {'foo': None, 'bar': None},
106 })
107 res = ns2df._get_depends_prev(config, pip_cache_dir="/tmp/pipcache")
108 self.assertEqual(res, [
109 "run apt-get -q -y install apache2=3.3-4",
110 "run apt-get -q -y install bzr",
111 "run pip install --download=/tmp/pipcache --no-install bar",
112 "run pip install --download=/tmp/pipcache --no-install foo",
113 ])
114
115 def test_mixed_post(self):
116 config = dict(somebase={
117 'apt': {'bzr': None, 'apache2': '3.3-4'},
118 'pip': {'foo': None, 'bar': None},
119 })
120 res = ns2df._get_depends_post(config, pip_cache_dir="/tmp/pipcache")
121 self.assertEqual(res, [
122 "run pip install --find-links=file:///tmp/pipcache --no-index bar",
123 "run pip install --find-links=file:///tmp/pipcache --no-index foo",
124 ])
125
126
127class BoilerplateTestCase(unittest.TestCase):
128 """Tests for the setup and the rest of commands."""
129
130 def test_one_base(self):
131 config = dict(precise={})
132 res = ns2df._get_base(config)
133 self.assertEqual(res, ["from ubuntu:precise"])
134
135 def test_several_bases(self):
136 config = dict(base1={}, base2={})
137 self.assertRaises(NotImplementedError, ns2df._get_base, config)
138
139 def test_secure_actions(self):
140 res = ns2df._get_secure({})
141 self.assertEqual(res, ["run ip link set dev eth0 down"])
142
143 def test_rest(self):
144 res = ns2df._get_rest({})
145 self.assertEqual(res, [])
146
147 def test_all_mixed(self):
148 config_text = """
149 precise:
150 apt:
151 apache2: 3.3-4
152 bzr:
153 pip:
154 foobar:
155 metadata:
156 test_script: foo
157 """
158 script, docker = ns2df.parse(config_text, "/tmp/pipcache")
159 self.assertEqual(script, "foo")
160 self.assertEqual(docker,
161 "from ubuntu:precise\n"
162 "run apt-get -q -y install apache2=3.3-4\n"
163 "run apt-get -q -y install bzr\n"
164 "run pip install --download=/tmp/pipcache --no-install foobar\n"
165 "run ip link set dev eth0 down\n"
166 "run pip install --find-links=file:///tmp/pipcache "
167 "--no-index foobar"
168 )

Subscribers

People subscribed via source and target branches

to all changes: