Skip to content

Commit cbfc539

Browse files
committed
Docker:
- Added script to automatically build a feel++ app
1 parent 1b127cc commit cbfc539

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

dockerize/feelpp-dockerize-app.py

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import errno
5+
import shutil
6+
import subprocess
7+
import time
8+
import os
9+
10+
debug = 0
11+
12+
# execute a command line
13+
def executeCommand(text, cmd):
14+
out = ""
15+
returnCode = 0
16+
17+
if(debug > 0):
18+
print cmd
19+
20+
# Log execution time
21+
tstart = time.time()
22+
try:
23+
# Launch the current command and also redirect its output to log files
24+
out = subprocess.Popen(cmd.split(' '))
25+
out.communicate()
26+
except subprocess.CalledProcessError, e:
27+
out = e.output
28+
ret = e.returncode
29+
# Log execution time
30+
tend = time.time()
31+
tFinal = (tend - tstart)
32+
33+
return returnCode, tFinal
34+
35+
# make a directory (don't fail if it exists)
36+
def makedir(path):
37+
try:
38+
os.makedirs(path)
39+
except OSError as exc:
40+
if exc.errno == errno.EEXIST and os.path.isdir(path):
41+
pass
42+
else: raise
43+
44+
def writeCMakeLists(fpath, src, cfg, geo, defs):
45+
fl = open(fpath, "w")
46+
47+
# Write header
48+
fl.write("""
49+
cmake_minimum_required(VERSION 2.8)
50+
51+
# This will check for the installed Feel++ library
52+
# If you did not install it in a standard system path,
53+
# you need to specify the FEELPP_DIR variable to the root
54+
# of your installation directory or add the path of your
55+
# installation in the PATHS section
56+
if ( ${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR} )
57+
find_package(Feel++
58+
PATHS $ENV{FEELPP_DIR}/share/feel/cmake/modules
59+
/usr/share/feel/cmake/modules
60+
/usr/local/share/feel/cmake/modules
61+
/opt/share/feel/cmake/modules
62+
)
63+
if(NOT FEELPP_FOUND)
64+
message(FATAL_ERROR "Feel++ was not found on your system. Make sure to install it and specify the FEELPP_DIR to reference the installation directory.")
65+
endif()
66+
endif()
67+
""")
68+
69+
# Generate app build info
70+
fl.write("""
71+
# Then you can add your application and the associated hpp, cpp, geo, msh and cfg files
72+
feelpp_add_application(
73+
app """)
74+
fl.write("""
75+
SRCS """ + src
76+
)
77+
# Use geo if present
78+
if(geo != ""):
79+
fl.write("""
80+
GEO """ + os.path.basename(geo)
81+
)
82+
# Use cfg if present
83+
if(cfg != ""):
84+
fl.write("""
85+
CFG """ + os.path.basename(cfg)
86+
)
87+
if(defs != ""):
88+
fl.write("""
89+
DEFS """ + defs
90+
)
91+
fl.write("""
92+
)
93+
""")
94+
95+
fl.close()
96+
97+
def writeDockerfile(fpath):
98+
fl = open(fpath, "w")
99+
100+
# Write header
101+
fl.write("""
102+
FROM feelpp/develop:insitu
103+
MAINTAINER Feel++ Support <[email protected]>
104+
105+
USER feelpp
106+
ENV HOME /home/feelpp
107+
108+
RUN mkdir -p $HOME/project/build
109+
COPY ./* $HOME/project/
110+
111+
# Install Feel++
112+
WORKDIR $HOME/project/build
113+
RUN bash -c " \\
114+
source $HOME/feelpp.conf.sh \\
115+
&& cmake $HOME/project \\
116+
&& make \\
117+
"
118+
119+
# COPY WELCOME $HOME/WELCOME
120+
USER root
121+
ENTRYPOINT ["/sbin/my_init","--quiet","--","sudo","-u","feelpp","/bin/sh","-c"]
122+
CMD ["/bin/bash"]
123+
""")
124+
125+
fl.close()
126+
127+
def main():
128+
# Parse arguments
129+
parser = argparse.ArgumentParser()
130+
parser.add_argument('--src', required=True, help='Path to the original input file in .mha format')
131+
parser.add_argument('--geo', default="", help='Path to the geo file to use')
132+
parser.add_argument('--cfg', default="", help='Path to the cfg file')
133+
parser.add_argument('--defs', default="", help='Custom definitions to use')
134+
args = parser.parse_args()
135+
136+
# Create workdir
137+
workdir = "dockerize-workdir"
138+
makedir(workdir)
139+
os.chdir(workdir)
140+
141+
# Get app info
142+
srcName = os.path.basename(args.src)
143+
appName, _ = os.path.splitext(srcName)
144+
cfgName = os.path.basename(args.cfg)
145+
geoName = os.path.basename(args.geo)
146+
147+
# Copy files
148+
shutil.copy(args.src, ".")
149+
if(args.cfg != ""):
150+
shutil.copy(args.cfg, ".")
151+
if(args.geo != ""):
152+
shutil.copy(args.geo, ".")
153+
154+
# Write
155+
writeCMakeLists("CMakeLists.txt", srcName, cfgName, geoName, args.defs)
156+
writeDockerfile("Dockerfile")
157+
158+
# Get name of source file to tag
159+
cmd = "docker build -t feelpp/" + appName + " ."
160+
print cmd
161+
retcode, _ = executeCommand("", cmd)
162+
163+
#docker run -ti
164+
165+
main()

0 commit comments

Comments
 (0)