summaryrefslogtreecommitdiffstats
path: root/packaging/docker/scripts/gen-dockerfiles.py
blob: 4987273883bb3c121eba0a9c2497b7f35d748447 (plain)
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
#!/usr/bin/env python
#
# Copyright 2016-2017 Huawei Technologies Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys, csv, subprocess, os, shutil, urllib2, argparse

parser = argparse.ArgumentParser(description='Generate docker image definition for a microservice.  The results will be placed under the target/ subdirectory.')
parser.add_argument('microservice', help='filename of microservice as entered in binaries.csv')
parser.add_argument('--build', default="snapshot", help='a specific build to use ("autorelease-????")')

args = parser.parse_args()

version = "1.1.0-SNAPSHOT"

root = subprocess.check_output(["git", "rev-parse", "--show-toplevel"]).rstrip()
path = "{}/test/csit/docker".format(root)
url_template = "https://nexus.open-o.org/service/local/artifact/maven/redirect?r=snapshots&g={0}&a={1}&e={2}&c={3}&v=LATEST"

found = False

with open( "{}/autorelease/binaries.csv".format(root), "r" ) as f:
    reader = csv.DictReader(f)

    for row in reader:
        if row["filename"] == args.microservice:
            found = True
            print row["filename"]

            if row["classifier"]:
                file = "{}-{}-{}.{}".format(row["artifactId"], version, row["classifier"], row["extension"])
                dest = "{}-{}-{}.{}".format(row["filename"], version, row["classifier"], row["extension"])
            else:
                file = "{}-{}.{}".format(row["artifactId"], version, row["extension"])
                dest = "{}-{}.{}".format(row["filename"], version, row["extension"])

            dir = "{}/{}/target".format(path, row["filename"])

            try:
                shutil.rmtree(dir, True)
                os.makedirs(dir)
            except OSError:
                pass

            # create empty Dockerfile if not exists
            open( "{}/Dockerfile".format(dir), "a" ).close()

            outfile = open( "{}/50-microservice.txt".format(dir), "w" )
            
            outfile.write("# 50-microservice.txt - AUTOGENERATED, DO NOT MODIFY MANUALLY\n\n")
            outfile.write("# Set up microservice\n")
            if args.build == "snapshot":
                redir_url = url_template.format(row["groupId"], row["artifactId"], row["extension"], row["classifier"])
                outfile.write("# {}\n".format(redir_url))
                response = urllib2.urlopen(redir_url)
                url = response.geturl()
            else:
                url = "https://nexus.open-o.org/content/repositories/{}/{}/{}/{}/{}".format(args.build, row["groupId"].replace(".","/"), row["artifactId"], version, file )

            
            outfile.write("RUN wget -q -O {} \"{}\"".format(dest, url))
            # outfile.write("ADD \"{}\" {}\n".format(url, dest))

            unzip_opt = ""
            if row["extension"] == "tar.gz":
                if row["unzip-dir"]:
                    unzip_opt = " -C {}".format(row["unzip-dir"])
                outfile.write(" && tar -xf {}{}".format(dest, unzip_opt))
            elif row["extension"] == "zip":
                if row["unzip-dir"]:
                    unzip_opt = " -d {}".format(row["unzip-dir"])
                outfile.write(" && unzip -q -o -B {}{}".format(dest, unzip_opt))
            outfile.write(" && rm -f {}\n".format(dest))

            outfile.write("# Set permissions\n")
            outfile.write("RUN find . -type d -exec chmod o-w {} \;\n")
            outfile.write("RUN find . -name \"*.sh\" -exec chmod +x {} \;\n")
            
            if row["ports"]:
                ports = row["ports"].split()
                for port in ports:
                    outfile.write("EXPOSE {}\n".format(port))
            outfile.write("RUN echo Open-O {} {} \"{}\" > OPENO_VERSION\n".format(row["filename"], version, url))
            outfile.write("\n\n")
            
            outfile.close()


            def symlink(flag, template):
                try:
                    os.remove("{}/{}".format(dir, template))
                except OSError:
                    pass
                if flag:
                    os.symlink("../../templates/{}".format(template), "{}/{}".format(dir, template))

            symlink(True, "10-basebuild.txt")
            symlink(row["python"], "15-python.txt")
            symlink(row["mysql"], "20-mysql.txt")
            if row["tomcat"]:
                # create empty 30-tomcat.txt as marker for gen-dockerfiles.sh
                open( "{}/30-tomcat.txt".format(dir), "a" ).close()
            symlink(row["mongodb"], "25-mongodb.txt")
            symlink(True, "90-entrypoint.txt")

if not found:
    print "Error: microservice {} not found in binaries.csv.".format(args.microservice)
    sys.exit(2)