]> source.dussan.org Git - vaadin-framework.git/commitdiff
Build docker image for testing demos (#9826)
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>
Wed, 16 Aug 2017 12:00:58 +0000 (15:00 +0300)
committerHenri Sara <henri.sara@gmail.com>
Wed, 16 Aug 2017 12:00:58 +0000 (15:00 +0300)
This patch allows the demo build script to wrap the resulting war files into docker image. The image contains a jetty with the provided war files deployed on start.

scripts/BuildDemos.py
scripts/BuildHelpers.py
scripts/DeployHelpers.py

index fd4c84fc32aa84bd0772a7bf4e3ccd7de633feb0..f691f851d073fa1074408116c8ecf71a5f558772 100644 (file)
@@ -44,7 +44,7 @@ if __name__ == "__main__":
                log_status("BuildDemos depends on gitpython. Install it with `pip install gitpython`")
                dump_status(True)
                sys.exit(1)
-       from BuildHelpers import updateRepositories, mavenValidate, copyWarFiles, getLogFile, removeDir, getArgs, mavenInstall, resultPath, readPomFile, parser
+       from BuildHelpers import updateRepositories, mavenValidate, copyWarFiles, getLogFile, removeDir, getArgs, mavenInstall, resultPath, readPomFile, parser, dockerWrap
        from DeployHelpers import deployWar
        # Add command line agrument for ignoring failing demos
        parser.add_argument("--ignore", type=str, help="Ignored demos", default="")
@@ -82,12 +82,16 @@ if __name__ == "__main__":
                        pass
                print("")
 
-       for war in wars:
-               try:
-                       deployWar(war)
-               except Exception as e:
-                       log_status("War %s failed to deploy: %s" % (war, e))
-                       demosFailed = True
+       if args.deploy_mode:
+               for war in wars:
+                       try:
+                               deployWar(war)
+                       except Exception as e:
+                               log_status("War %s failed to deploy: %s" % (war, e))
+                               demosFailed = True
+       else:
+               dockerWrap(args.version)
+
 
        if demosFailed:
                dump_status(True)
index 02fddc7af3e5b103565daf533f61b4dc91860b05..9803e310fc68c56dbe91599d507b161fbbe1b187 100644 (file)
@@ -10,7 +10,6 @@ from shutil import copy, rmtree
 from glob import glob
 
 # Directory where the resulting war files are stored
-# TODO: deploy results
 resultPath = join("result", "demos")
 
 if not exists(resultPath):
@@ -39,26 +38,27 @@ def parseArgs():
                args = parser.parse_args()
        return args
 
-# Function for determining the path for maven executable
-def getMavenCommand():
+# Function for determining the path for an executable
+def getCommand(command):
        # This method uses .split("\n")[0] which basically chooses the first result where/which returns.
        # Fixes the case with multiple maven installations available on PATH
        if platform.system() == "Windows":
                try:
-                       return subprocess.check_output(["where", "mvn.cmd"], universal_newlines=True).split("\n")[0]
+                       return subprocess.check_output(["where", "%s.cmd" % (command)], universal_newlines=True).split("\n")[0]
                except:
                        try:
-                               return subprocess.check_output(["where", "mvn.bat"], universal_newlines=True).split("\n")[0]
+                               return subprocess.check_output(["where", "%s.bat" % (command)], universal_newlines=True).split("\n")[0]
                        except:
-                               print("Unable to locate mvn with where. Is the maven executable in your PATH?")
+                               print("Unable to locate command %s with where. Is it in your PATH?" % (command))
        else:
                try:
-                       return subprocess.check_output(["which", "mvn"], universal_newlines=True).split("\n")[0]
+                       return subprocess.check_output(["which", command], universal_newlines=True).split("\n")[0]
                except:
-                       print("Unable to locate maven executable with which. Is the maven executable in your PATH?")
+                       print("Unable to locate command %s with which. Is it in your PATH?" % (command))
        return None
 
-mavenCmd = getMavenCommand()
+mavenCmd = getCommand("mvn")
+dockerCmd = getCommand("docker")
 
 # Get command line arguments. Parses arguments if needed.
 def getArgs():
@@ -176,3 +176,53 @@ def mavenInstall(pomFile, jarFile = None, mvnCmd = mavenCmd, logFile = sys.stdou
        cmd.append("-DpomFile=%s" % (pomFile))
        print("executing: %s" % (" ".join(cmd)))
        subprocess.check_call(cmd, stdout=logFile)      
+
+def dockerWrap(imageVersion, imageName = "demo-validation"):
+       dockerFileContent = """FROM jetty:jre8-alpine
+MAINTAINER FrameworkTeam
+
+RUN apk add --update sed
+
+#Autodeploy folder:
+#/var/lib/jetty/webapps/
+
+COPY ./*.war /var/lib/jetty/webapps/
+COPY ./index-generate.sh /opt/
+RUN chmod +x /opt/index-generate.sh
+
+RUN /opt/index-generate.sh
+
+RUN mkdir -p /var/lib/jetty/webapps/root && \
+    cp /opt/index.html /var/lib/jetty/webapps/root && \
+    chmod 644 /var/lib/jetty/webapps/root/index.html
+
+EXPOSE 8080
+"""
+       indexGenerateScript = """#!/bin/ash
+
+wars="/var/lib/jetty/webapps"
+OUTPUT="/opt/index.html"
+
+echo "<UL>" > $OUTPUT
+cd $wars
+for war in `ls -1 *.war`; do
+  nowar=`echo "$war" | sed -e 's/\(^.*\)\(.war$\)/\\1/'`
+  echo "<LI><a href=\"/$nowar/\">$nowar</a></LI>" >> $OUTPUT
+done
+echo "</UL>" >> $OUTPUT
+"""
+       with open(join(resultPath, "Dockerfile"), "w") as dockerFile:
+               dockerFile.write(dockerFileContent)
+       with open(join(resultPath, "index-generate.sh"), "w") as indexScript:
+               indexScript.write(indexGenerateScript)
+       # build image
+       cmd = [dockerCmd, "build", "-t", "%s:%s" % (imageName, imageVersion), resultPath]
+       subprocess.check_call(cmd)
+       # save to tgz
+       cmd = [dockerCmd, "save", imageName]
+       dockerSave = subprocess.Popen(cmd, stdout=subprocess.PIPE)
+       subprocess.check_call(["gzip"], stdin=dockerSave.stdout, stdout=open(join(resultPath, "%s-%s.tgz" % (imageName, imageVersion)), "w"))
+       dockerSave.wait()
+       # delete from docker
+       cmd = [dockerCmd, "rmi", "%s:%s" % (imageName, imageVersion)]
+       subprocess.check_call(cmd)
index 038e187b8b830507d9c2ed279740c912c0b149bd..e8079c1df1c59f93eca510b5642c466c24caae5d 100644 (file)
@@ -14,9 +14,13 @@ from os.path import join, expanduser, basename
 from BuildHelpers import parser, getArgs
 from time import sleep
 
-parser.add_argument("--deployUrl", help="Wildfly management URL")
-parser.add_argument("--deployUser", help="Deployment user", default=None)
-parser.add_argument("--deployPass", help="Deployment password", default=None)
+group = parser.add_mutually_exclusive_group(required=True)
+group.add_argument("--deploy", dest="deploy_mode", help="Deploy to a remote Wildfly instance", action="store_true")
+group.add_argument("--docker", dest="deploy_mode", help="Wrap results into a Docker image", action="store_false")
+
+parser.add_argument("--deployUrl", help="Wildfly management URL to use with --deploy")
+parser.add_argument("--deployUser", help="Deployment user to use with --deploy", default=None)
+parser.add_argument("--deployPass", help="Deployment password to use with --deploy", default=None)
 
 serverUp = None
 
@@ -120,4 +124,4 @@ def getAuth():
 # Read the deploy url file and return the url
 def getUrl():
        return getArgs().deployUrl
-       
+