Created
September 27, 2018 09:25
-
-
Save wolfkang/c01f186def43017b5ab88140ea3ed728 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import subprocess, sys | |
import datetime | |
from string import Template | |
from time import sleep | |
from time import gmtime, strftime | |
def log(msg): | |
print(strftime("%Y-%m-%d %H:%M:%S", gmtime()), msg) | |
def command(cmd, params): | |
template = Template(cmd) | |
comm = template.substitute(params) | |
print('command: '+comm) | |
return subprocess.check_output(comm, shell=True).decode('utf-8') | |
log("Building") | |
result = command("mvn clean package -Dmaven.test.skip=true", {}) | |
if not "BUILD SUCCESS" in result: | |
log ("Build failure") | |
sys.exit(0) | |
log ("Build success") | |
log ("Deploying") | |
params = {'host': '', | |
'port': 80, | |
'username':'', | |
'password':'', | |
'war': r'danbi-api/target/ROOT.war', | |
'path': '/', | |
'version': datetime.datetime.now().strftime("%Y%m%d-%H%M")} | |
manager = dict( | |
deploy = 'curl --user $username:$password --upload-file $war "http://$host:$port/manager/text/deploy?path=$path&version=$version"', | |
list = 'curl --user $username:$password "http://$host:$port/manager/text/list"', | |
undeploy = 'curl --user $username:$password "http://$host:$port/manager/text/undeploy?path=$path&version=$oldVersion"' | |
) | |
result = command(manager['deploy'], params) | |
log(result) | |
log ("Deploy success") | |
listCmd = 'curl --user $username:$password "http://$host:$port/manager/text/list"' | |
result = command(manager['list'], params) | |
print() | |
#print(result) | |
log ("Undeploying the old verions in 60 seconds.") | |
sleep(30) | |
undeployCmd = 'curl --user $username:$password "http://$host:$port/manager/text/undeploy?path=$path&version=$oldVersion"' | |
for line in result.split(): | |
oldVersion = line[line.rfind('#')+1:] | |
if (line.startswith(params['path']+':running:0:') and oldVersion != params['version']): | |
log('Undeploying '+params['path']+'##' + oldVersion) | |
params['oldVersion'] = oldVersion | |
log(command(manager['undeploy'], params)) | |
log ('Undeploy success') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment