forked from dattlabs/datt-metadatt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makedatt
executable file
·151 lines (120 loc) · 6.11 KB
/
makedatt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
from sys import argv, exit
from string import Template
from optparse import OptionParser
from shutil import copyfile
import itertools
import subprocess
import glob
import os
import re
isContainerPath = lambda p: os.path.isfile("%s/Dockerfile" % p)
def single(lst):
if not len(lst) == 1:
raise RuntimeError("'single' expects a list containing one item but got %s items" % len(lst))
return lst[0]
trimstart = lambda s, subs: s[len(subs):] if s.startswith(subs) else s
trimend = lambda s, subs: s[:-len(subs)] if s.endswith(subs) else s
trim = lambda s, subs: trimstart(trimend(s, subs), subs)
def getParentImageName(targetPath):
if not isContainerPath(targetPath): return None
try:
dockerfileName = '%s/Dockerfile' % targetPath
with open(dockerfileName) as f:
fromLine = single(filter(lambda s: s.startswith('FROM'), [s.strip() for s in f.readlines()]))
return trim(trim(fromLine, ":latest"), "FROM").strip()
except Exception as e:
print("Malformed file: %s\n\t%s" % (dockerfileName, e.message)); exit(1)
isDattImage = lambda img: img.startswith("datt/datt-")
containerPathToTarget = lambda cp: trimstart(cp, "./containers/datt-")
imageNameToTarget = lambda img: trimstart(img, "datt/datt-")
fst = lambda x: x[0]
snd = lambda x: x[1]
def getMakefileEntry(target, labelLine, cmd):
return (target, "%s\n\t%s\n" % (labelLine, cmd))
def getDefaultMakefileEntry(target, labelLine, containerPath):
return getMakefileEntry(target, labelLine, "pushd %s > /dev/null;make build;popd > /dev/null" % containerPath)
def getContainerMakefileEntry(containerPath, parentImageName):
parent = imageNameToTarget(parentImageName)
target = containerPathToTarget(containerPath)
labelLine = "%s:%s" % (target, " %s" % parent if isDattImage(parentImageName) else "")
return getDefaultMakefileEntry(target, labelLine, containerPath)
def partition(lst, condition):
l1, l2 = itertools.tee((condition(item), item) for item in lst)
return (i for p, i in l1 if p), (i for p, i in l2 if not p)
def copyTemplates(templateDir, destDir, mapping):
from os.path import isfile
files, dirs = partition(os.listdir(templateDir), lambda x: isfile('%s/%s' % (templateDir, x)))
for fi in files:
srcFile = '%s/%s' % (templateDir, fi)
dstFile = '%s/%s' % (destDir, fi)
print 'Transforming %s into %s' % (os.path.relpath(srcFile), os.path.relpath(dstFile))
with open(srcFile, 'r') as f:
replaced = Template(f.read()).substitute(mapping)
with open(dstFile, 'w') as of:
of.write(replaced)
for d in dirs:
dest = '%s/%s' % (destDir, d)
subprocess.call(['mkdir', '-p', dest], stderr=open(os.devnull, 'w'), stdout=open(os.devnull, 'w'))
copyTemplates('%s/%s' % (templateDir, d), dest, mapping)
if __name__ == "__main__":
parser = OptionParser(usage="usage: %prog [options] [build_target]")
parser.add_option("--skip-pull",
action="store_true", default = False,
help="skip pulling from remote git container repos")
(options, args) = parser.parse_args()
target = args[0] if len(args) >= 1 else None
if len(args) > 1: parser.print_help(); exit(1)
metaDattRoot = os.path.dirname(os.path.realpath(__file__))
containersRoot = "%s/containers" % metaDattRoot
getContainerPaths = lambda: glob.glob("%s/datt-*" % containersRoot)
containerPaths = getContainerPaths()
if os.environ.get('DATTDEV') != None:
with open("%s/.gitmodules" % metaDattRoot, "r+") as f:
lines = f.readlines()
f.seek(0)
f.writelines(map(lambda l: re.sub('https://(.*?)/', 'git@\\1:', l), lines))
f.truncate()
if len(containerPaths) == 0:
print('Found no containers in ./containers. Calling ./init.sh.')
subprocess.call(['./init.sh'], stderr=open(os.devnull, 'w'), stdout=open(os.devnull, 'w'))
containerPaths = getContainerPaths()
for path in containerPaths:
if not os.path.exists("%s/.git" % path) or not isContainerPath(path):
print 'Initializing sub-module: %s' % os.path.relpath(path)
subprocess.call(['./init.sh', path], stderr=open(os.devnull, 'w'), stdout=open(os.devnull, 'w'))
if not options.skip_pull:
subprocess.call(['./pull.sh'])
dependencies = map(lambda p: ("./containers%s" % trimstart(p, containersRoot), getParentImageName(p)), containerPaths)
getTargets = lambda t: [getContainerMakefileEntry(fst(t), snd(t))]
targets = list(itertools.chain(*map(getTargets, dependencies)))
allTargets = ' '.join(map(fst, targets))
shellLine = 'SHELL := /bin/bash'
phonyLine = "\n.PHONY: all %s\n" % allTargets
allLine = "all: %s\n" % allTargets
targetEntries = map(snd, targets)
allSections = [shellLine, phonyLine, allLine] + targetEntries
print('Writing ./Makefile')
with open('Makefile', 'w') as f:
f.write("%s\n" % '\n'.join(allSections))
buildLine = "build:\n\t../../scripts/build.sh"
testLine = "test:\n\t../../scripts/test.sh"
runLine = "run:\n\t../../scripts/run.sh"
debugLine = "debug:\n\t../../scripts/run.sh RUN_DEBUG=1"
sections = [shellLine, "\n.PHONY: build run test\n", buildLine, testLine, runLine, debugLine]
for path in containerPaths:
print('Writing %s/Makefile' % os.path.relpath(path))
with open('%s/Makefile' % path, 'w') as f:
f.write("%s\n" % '\n'.join(sections))
filesPath = '%s/files' % path
print('Copying test_server.js to %s' % os.path.relpath(filesPath))
subprocess.call(['mkdir', '-p', '%s/scripts' % filesPath], stderr=open(os.devnull, 'w'), stdout=open(os.devnull, 'w'))
copyfile('./test_server/test_server.js', '%s/test_server.js' % filesPath)
print('Copying scripts/helpers.bash to %s' % os.path.relpath(filesPath))
copyfile('./scripts/helpers.bash', '%s/scripts/helpers.bash' % filesPath)
copyTemplates(os.path.abspath("./templates"), path, {'container': os.path.basename(path)})
if target:
splitted = target.split(":")
if len(splitted) > 2: raise RuntimeError("only one level of nesting is currently supported for target tasks.")
(task, args) = (splitted[1], {'cwd': "./containers/datt-%s" % splitted[0]}) if len(splitted) is 2 else (splitted[0], {})
subprocess.call(['make', task], **args)