From 771da038480192a2d49a3672a33850ec3f8e3269 Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Fri, 12 Jun 2015 11:23:39 +0300 Subject: Add DeployHelpers python module for adding wildfly auto deployment Change-Id: I1b3570a96d8406aa502171eb8065f1359d458450 --- scripts/BuildArchetypes.py | 28 ++++++++++------ scripts/BuildDemos.py | 39 ++++++++++++++--------- scripts/BuildHelpers.py | 13 ++++++-- scripts/DeployHelpers.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 26 deletions(-) create mode 100644 scripts/DeployHelpers.py diff --git a/scripts/BuildArchetypes.py b/scripts/BuildArchetypes.py index 80dd745bfb..ffdbbe1c7a 100644 --- a/scripts/BuildArchetypes.py +++ b/scripts/BuildArchetypes.py @@ -11,7 +11,8 @@ # import subprocess -from BuildHelpers import mavenValidate, copyWarFiles, repo, getLogFile, parseArgs, mavenCmd, updateRepositories +from BuildHelpers import mavenValidate, copyWarFiles, repo, getLogFile, mavenCmd, updateRepositories, getArgs, removeDir +from DeployHelpers import deployWar ## DEFAULT VARIABLES ## @@ -22,7 +23,7 @@ archetypeGroup = "com.vaadin" archetypes = [ "vaadin-archetype-widget", "vaadin-archetype-application", - "vaadin-archetype-application-example", + "vaadin-archetype-application-example", "vaadin-archetype-application-multimodule" ] @@ -59,11 +60,20 @@ def generateArchetype(archetype): ## DO THIS IF RUN AS A SCRIPT (not import) ## if __name__ == "__main__": - args = parseArgs() + args = getArgs() for archetype in archetypes: - log = getLogFile(archetype) - artifactId = generateArchetype(archetype) - updateRepositories(artifactId) - mavenValidate(artifactId, logFile=log) - copyWarFiles(artifactId, name=archetype) - + try: + log = getLogFile(archetype) + artifactId = generateArchetype(archetype) + updateRepositories(artifactId) + mavenValidate(artifactId, logFile=log) + warFiles = copyWarFiles(artifactId, name=archetype) + for war in warFiles: + try: + deployWar(war, "%s.war" % (archetype.split("-", 2)[2])) + except Exception as e: + print("War %s failed to deploy: %s" % (war, e)) + except: + print("Archetype %s build failed" % (archetype)) + removeDir(artifactId) + print("") diff --git a/scripts/BuildDemos.py b/scripts/BuildDemos.py index f9f2ed1b48..396863350d 100644 --- a/scripts/BuildDemos.py +++ b/scripts/BuildDemos.py @@ -4,38 +4,47 @@ # BuildDemos needs git in PATH and depends on gitpython library # gitpython can be installed with python installer script "pip": # pip install gitpython +# +# Deployment dependency: requests +# pip install requests +# Deploy depends on .deployUrl and .deployCredentials files in home folder -from git import Repo -from BuildHelpers import updateRepositories, mavenValidate, copyWarFiles, VersionObject, getLogFile, parseArgs - -## Example of a non-staging test. -#version = VersionObject() -#version.version = "7.4.8" - -# Uncomment lines before this, and comment following line to make a non-staging test -version = None +import sys +try: + from git import Repo +except: + print("BuildDemos depends on gitpython. Install it with `pip install gitpython`") + sys.exit(1) +from BuildHelpers import updateRepositories, mavenValidate, copyWarFiles, getLogFile, removeDir +from DeployHelpers import deployWar +# Validated demos. name -> git url demos = { "dashboard" : "https://github.com/vaadin/dashboard-demo.git", "parking" : "https://github.com/vaadin/parking-demo.git", "addressbook" : "https://github.com/vaadin/addressbook.git", - "confirmdialog" : "https://github.com/samie/Vaadin-ConfirmDialog.git" + "confirmdialog" : "https://github.com/samie/Vaadin-ConfirmDialog.git", + "grid-gwt" : "https://github.com/vaadin/grid-gwt.git" } def checkout(folder, url): Repo.clone_from(url, folder) if __name__ == "__main__": - if version is None: - version = parseArgs() for demo in demos: print("Validating demo %s" % (demo)) try: checkout(demo, demos[demo]) - updateRepositories(demo, repoIds = version) - mavenValidate(demo, repoIds = version, logFile = getLogFile(demo)) - copyWarFiles(demo) + updateRepositories(demo) + mavenValidate(demo, logFile=getLogFile(demo)) + resultWars = copyWarFiles(demo) + for war in resultWars: + try: + deployWar(war) + except Exception as e: + print("War %s failed to deploy: %s" % (war, e)) print("%s demo validation succeeded!" % (demo)) except: print("%s demo validation failed" % (demo)) + removeDir(demo) print("") diff --git a/scripts/BuildHelpers.py b/scripts/BuildHelpers.py index be21c0f721..7349c9b367 100644 --- a/scripts/BuildHelpers.py +++ b/scripts/BuildHelpers.py @@ -6,7 +6,7 @@ import sys, argparse, subprocess, platform from xml.etree import ElementTree from os.path import join, isdir, isfile, basename, exists from os import listdir, getcwd, mkdir -from shutil import copy +from shutil import copy, rmtree from glob import glob class VersionObject(object): @@ -89,6 +89,7 @@ def mavenValidate(artifactId, mvnCmd = mavenCmd, logFile = sys.stdout, repoIds = def copyWarFiles(artifactId, resultDir = resultPath, name = None): if name is None: name = artifactId + copiedWars = [] warFiles = glob(join(getcwd(), artifactId, "target", "*.war")) warFiles.extend(glob(join(getcwd(), artifactId, "*", "target", "*.war"))) for warFile in warFiles: @@ -97,7 +98,9 @@ def copyWarFiles(artifactId, resultDir = resultPath, name = None): else: deployName = "%s-%d.war" % (name, warFiles.index(warFile)) print("Copying .war file %s as %s to result folder" % (basename(warFile), deployName)) - copy(warFile, join(resultDir, "%s" % (deployName))) + copy(warFile, join(resultDir, deployName)) + copiedWars.append(join(resultDir, deployName)) + return copiedWars # Recursive pom.xml update script def updateRepositories(path, repoIds = None, repoUrl = repo): @@ -158,3 +161,9 @@ def addRepo(repoNode, repoType, id, url): # Get a logfile for given artifact def getLogFile(artifact, resultDir = resultPath): return open(join(resultDir, "%s.log" % (artifact)), 'w') + +def removeDir(subdir): + if '..' in subdir or '/' in subdir: + # Dangerous relative paths. + return + rmtree(join(getcwd(), subdir)) diff --git a/scripts/DeployHelpers.py b/scripts/DeployHelpers.py new file mode 100644 index 0000000000..c12c38d293 --- /dev/null +++ b/scripts/DeployHelpers.py @@ -0,0 +1,79 @@ +#coding=UTF-8 + +### Helper class for wildfly deployments. ### +# Related files $HOME/.deploy-url $HOME/.deploy-credentials + +import sys, json +try: + import requests +except Exception as e: + print("DeployHelpers depends on requests library. Install it with `pip install requests`") + sys.exit(1) +from requests.auth import HTTPDigestAuth +from os.path import join, expanduser, basename + +# .deploy-url in home folder +deployUrlFile = join(expanduser("~"), ".deploy-url") + +# .deploy-credentials in home folder +deployCredFile = join(expanduser("~"), ".deploy-credentials") + +# Helper for handling the full deployment +# name should end with .war +def deployWar(warFile, name=None): + if name is None: + name = basename(warFile) + + print("Deploying to context %s" % (name[:-4])) + # Undeploy/Remove old version if needed + if deploymentExists(name): + removeDeployment(name) + # Do upload war file + hash = doUploadWarFile(warFile) + # Do deployment under name + doDeploy(hash, name) + +def deploymentExists(name): + # Deployment existence check data + data = {"operation" : "read-attribute", "name": "runtime-name", "address": [{"deployment" : name}]} + result = doPostJson(url=getUrl(), auth=getAuth(), data=json.dumps(data)) + return result.json()["outcome"] == "success" + +def doDeploy(hash, name): + # Deployment data + data = {} + data["content"] = [{"hash" : hash}] + data["address"] = [{"deployment" : name}] + data["operation"] = "add" + data["enabled"] = True + return doPostJson(data=json.dumps(data), auth=getAuth(), url=getUrl()) + +# Helper for adding Content-Type to headers +def doPostJson(**kwargs): + return requests.post(headers={"Content-Type" : "application/json"}, **kwargs) + +def doUploadWarFile(warFile): + # Upload request, just see the outcome + result = requests.post("%s/add-content" % (getUrl()), files={"file" : open(warFile, 'rb')}, auth=getAuth()).json() + if "outcome" not in result or result["outcome"] != "success": + raise Exception("File upload failed.", result) + return result["result"] + +# Method for removing an existing deployment +def removeDeployment(name): + data = {} + data["address"] = [{"deployment" : name}] + for i in ["undeploy", "remove"]: + print("%s old deployment of %s" % (i, name)) + data["operation"] = i + doPostJson(data=json.dumps(data), auth=getAuth(), url=getUrl()) + +# Read credentials file and return a HTTPDigestAuth object +def getAuth(): + (deployUser, deployPass) = open(deployCredFile).read().strip().split(",") + return HTTPDigestAuth(deployUser, deployPass) + +# Read the deploy url file and return the url +def getUrl(): + return open(deployUrlFile).read().strip() + -- cgit v1.2.3