blob: 4987273883bb3c121eba0a9c2497b7f35d748447 [file] [log] [blame]
Gary Wud04e4402017-07-28 12:26:54 -07001#!/usr/bin/env python
2#
3# Copyright 2016-2017 Huawei Technologies Co., Ltd.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17import sys, csv, subprocess, os, shutil, urllib2, argparse
18
19parser = argparse.ArgumentParser(description='Generate docker image definition for a microservice. The results will be placed under the target/ subdirectory.')
20parser.add_argument('microservice', help='filename of microservice as entered in binaries.csv')
21parser.add_argument('--build', default="snapshot", help='a specific build to use ("autorelease-????")')
22
23args = parser.parse_args()
24
25version = "1.1.0-SNAPSHOT"
26
27root = subprocess.check_output(["git", "rev-parse", "--show-toplevel"]).rstrip()
28path = "{}/test/csit/docker".format(root)
29url_template = "https://nexus.open-o.org/service/local/artifact/maven/redirect?r=snapshots&g={0}&a={1}&e={2}&c={3}&v=LATEST"
30
31found = False
32
33with open( "{}/autorelease/binaries.csv".format(root), "r" ) as f:
34 reader = csv.DictReader(f)
35
36 for row in reader:
37 if row["filename"] == args.microservice:
38 found = True
39 print row["filename"]
40
41 if row["classifier"]:
42 file = "{}-{}-{}.{}".format(row["artifactId"], version, row["classifier"], row["extension"])
43 dest = "{}-{}-{}.{}".format(row["filename"], version, row["classifier"], row["extension"])
44 else:
45 file = "{}-{}.{}".format(row["artifactId"], version, row["extension"])
46 dest = "{}-{}.{}".format(row["filename"], version, row["extension"])
47
48 dir = "{}/{}/target".format(path, row["filename"])
49
50 try:
51 shutil.rmtree(dir, True)
52 os.makedirs(dir)
53 except OSError:
54 pass
55
56 # create empty Dockerfile if not exists
57 open( "{}/Dockerfile".format(dir), "a" ).close()
58
59 outfile = open( "{}/50-microservice.txt".format(dir), "w" )
60
61 outfile.write("# 50-microservice.txt - AUTOGENERATED, DO NOT MODIFY MANUALLY\n\n")
62 outfile.write("# Set up microservice\n")
63 if args.build == "snapshot":
64 redir_url = url_template.format(row["groupId"], row["artifactId"], row["extension"], row["classifier"])
65 outfile.write("# {}\n".format(redir_url))
66 response = urllib2.urlopen(redir_url)
67 url = response.geturl()
68 else:
69 url = "https://nexus.open-o.org/content/repositories/{}/{}/{}/{}/{}".format(args.build, row["groupId"].replace(".","/"), row["artifactId"], version, file )
70
71
72 outfile.write("RUN wget -q -O {} \"{}\"".format(dest, url))
73 # outfile.write("ADD \"{}\" {}\n".format(url, dest))
74
75 unzip_opt = ""
76 if row["extension"] == "tar.gz":
77 if row["unzip-dir"]:
78 unzip_opt = " -C {}".format(row["unzip-dir"])
79 outfile.write(" && tar -xf {}{}".format(dest, unzip_opt))
80 elif row["extension"] == "zip":
81 if row["unzip-dir"]:
82 unzip_opt = " -d {}".format(row["unzip-dir"])
83 outfile.write(" && unzip -q -o -B {}{}".format(dest, unzip_opt))
84 outfile.write(" && rm -f {}\n".format(dest))
85
86 outfile.write("# Set permissions\n")
87 outfile.write("RUN find . -type d -exec chmod o-w {} \;\n")
88 outfile.write("RUN find . -name \"*.sh\" -exec chmod +x {} \;\n")
89
90 if row["ports"]:
91 ports = row["ports"].split()
92 for port in ports:
93 outfile.write("EXPOSE {}\n".format(port))
94 outfile.write("RUN echo Open-O {} {} \"{}\" > OPENO_VERSION\n".format(row["filename"], version, url))
95 outfile.write("\n\n")
96
97 outfile.close()
98
99
100 def symlink(flag, template):
101 try:
102 os.remove("{}/{}".format(dir, template))
103 except OSError:
104 pass
105 if flag:
106 os.symlink("../../templates/{}".format(template), "{}/{}".format(dir, template))
107
108 symlink(True, "10-basebuild.txt")
109 symlink(row["python"], "15-python.txt")
110 symlink(row["mysql"], "20-mysql.txt")
111 if row["tomcat"]:
112 # create empty 30-tomcat.txt as marker for gen-dockerfiles.sh
113 open( "{}/30-tomcat.txt".format(dir), "a" ).close()
114 symlink(row["mongodb"], "25-mongodb.txt")
115 symlink(True, "90-entrypoint.txt")
116
117if not found:
118 print "Error: microservice {} not found in binaries.csv.".format(args.microservice)
119 sys.exit(2)