Last active
December 2, 2017 02:57
-
-
Save rossigee/204dee2f52e04ab1afa7d2c6923895ab to your computer and use it in GitHub Desktop.
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/python2.7 | |
import os, sys | |
import requests | |
import json | |
import time | |
projectId = os.environ['RANCHER_ENVIRONMENT'] | |
serviceId = os.environ['RANCHER_SERVICE'] | |
newImage = sys.argv[1] | |
# Service endpoint (v2-beta) | |
endpoint_url = os.environ['RANCHER_URL'] + '/v2-beta/projects/' + projectId + '/services/' + serviceId | |
auth = (os.environ['RANCHER_ACCESS_KEY'], os.environ['RANCHER_SECRET_KEY']) | |
# Find service based on the ids provided | |
r = requests.get(endpoint_url, auth=auth) | |
service = r.json() | |
if 'status' in service and service['status'] != 200: | |
sys.exit(1) | |
try: | |
launchConfig = service['launchConfig'] | |
secondaryLaunchConfigs = service['secondaryLaunchConfigs'] | |
except KeyError as ke: | |
print "Bad key: %s" % str(ke) | |
sys.exit(1) | |
# Update launchConfig with newImage | |
launchConfig['imageUuid'] = "docker:" + newImage | |
# Construct payload for upgrade | |
payload = { | |
'inServiceStrategy': { | |
'batchSize': 1, | |
'intervalMillis': 2000, | |
'startFirst': False, | |
'launchConfig': launchConfig, | |
'secondaryLaunchConfigs': secondaryLaunchConfigs, | |
} | |
} | |
headers = {'content-type': 'application/json'} | |
# Upgrade the service with payload | |
r = requests.post(endpoint_url + '?action=upgrade', | |
data=json.dumps(payload), headers=headers, | |
auth=auth) | |
# Pool service upgrade status | |
r = requests.get(endpoint_url, auth=auth) | |
state = r.json()['state'] | |
sleep = 30 | |
retry = 10 | |
while (state != 'upgraded'): | |
print("service: " + service['name'] + " [" + state + "]") | |
time.sleep(sleep) | |
r = requests.get(endpoint_url, auth=auth) | |
state = r.json()['state'] | |
retry -= 1 | |
if (retry <= 0): sys.exit() | |
print("service: " + service['name'] + " [upgraded]") | |
# Finish Upgrade | |
r = requests.post(endpoint_url + '/?action=finishupgrade', | |
headers=headers, auth=auth) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment