Last active
September 3, 2015 18:24
-
-
Save barretts/824fc10337998c0c5907 to your computer and use it in GitHub Desktop.
Register Jenkins Slave from Windows in AWS
This file contains 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 | |
# http://sosuke.com/running-ui-tests-in-internet-explorer-with-jenkins-on-aws | |
import os | |
import httplib | |
import string | |
import json | |
import urllib2 | |
import base64 | |
def decode_base64(data): | |
""" | |
http://stackoverflow.com/a/9807138/604861 | |
Decode base64, padding being optional. | |
:param data: Base64 data as an ASCII byte string | |
:returns: The decoded byte string. | |
""" | |
missing_padding = 4 - len(data) % 4 | |
if missing_padding: | |
data += b'='* missing_padding | |
return base64.decodestring(data) | |
try: | |
os.remove("slave.jar") | |
except OSError: | |
pass | |
try: | |
os.remove("slave-agent.jnlp") | |
except OSError: | |
pass | |
conn = httplib.HTTPConnection("169.254.169.254") | |
conn.request("GET", "/latest/user-data") | |
response = conn.getresponse() | |
userdata = response.read() | |
args = string.split(userdata, "&") | |
for arg in args: | |
if arg.split("=")[0] == "USER_DATA": | |
userdata = arg.split("=")[1] | |
userdata = decode_base64(userdata) | |
print userdata | |
args = string.split(userdata, "&") | |
for arg in args: | |
if arg.split("=")[0] == "JENKINS_URL": | |
jenkinsUrl = arg.split("=")[1] + "/" | |
elif arg.split("=")[0] == "SLAVE_PREFIX": | |
slavePrefix = arg.split("=")[1] | |
elif arg.split("=")[0] == "USER_NAME": | |
userName = arg.split("=")[1] | |
elif arg.split("=")[0] == "USER_TOKEN": | |
userToken = arg.split("=")[1] | |
print jenkinsUrl | |
print slavePrefix | |
print userName | |
print userToken | |
req = urllib2.Request("http://169.254.169.254/latest/dynamic/instance-identity/document") | |
opener = urllib2.build_opener() | |
f = opener.open(req) | |
jsonIdentityData = json.load(f) | |
slaveName = slavePrefix + "%20(" + jsonIdentityData["instanceId"] + ")" | |
print slaveName | |
fullUrl = jenkinsUrl + "computer/" + slaveName + "/slave-agent.jnlp" | |
os.system('wget --auth-no-challenge --http-user=' + userName + ' --http-password=' + userToken + ' "' + fullUrl + '"') | |
os.system("wget " + jenkinsUrl + "jnlpJars/slave.jar -O slave.jar") | |
filePath = os.path.realpath("slave-agent.jnlp") | |
filePath = "file:/"+filePath.replace("/", "\\") | |
os.system("java -jar slave.jar -jnlpUrl "+filePath) | |
# We decided to use User Data instead of tags | |
# import subprocess | |
# proc = subprocess.Popen(["aws", "ec2", "describe-tags"], stdout=subprocess.PIPE, shell=True) | |
# (out, err) = proc.communicate() | |
# jsonIdentityData = json.loads(out) | |
# print jsonIdentityData["Tags"] | |
# for tag in jsonIdentityData["Tags"]: | |
# if tag["Key"] == "Name": | |
# print tag["Value"] |
This file contains 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
JENKINS_URL=http://bld.tld&SLAVE_PREFIX=TestIE&USER_NAME=bsonntag&USER_TOKEN=mytoken |
This file contains 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
cd c:\Users\Test | |
python jenkins-slave-start.py | |
pause >nul |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment