Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import os
  2. import requests
  3. import json
  4. import time
  5.  
  6. environmentName = 'babyloania-dev'
  7. serviceName = 'auth'
  8. newImage = 'docker:arkka/babyloania:auth-develop-8'
  9.  
  10. # Find stack based on their name
  11. r = requests.get(os.environ['RANCHER_URL'] + 'v1/environments?name=' + environmentName,
  12. auth=(os.environ['RANCHER_ACCESS_KEY'], os.environ['RANCHER_SECRET_KEY']))
  13.  
  14. environment = r.json()['data'][0]
  15.  
  16. # Find service based on their name and environmentId
  17. r = requests.get(os.environ['RANCHER_URL'] + 'v1/services?name=' + serviceName + '&environmentId=' + environment['id'],
  18. auth=(os.environ['RANCHER_ACCESS_KEY'], os.environ['RANCHER_SECRET_KEY']))
  19.  
  20. service = r.json()['data'][0]
  21. launchConfig = service['launchConfig']
  22.  
  23. # Update launchConfig with newImage
  24. launchConfig['imageUuid'] = newImage
  25.  
  26. # Construct payload for upgrade
  27. payload = {
  28. 'inServiceStrategy': {
  29. 'batchSize': 1,
  30. 'intervalMillis': 2000,
  31. 'startFirst': False,
  32. 'launchConfig': launchConfig
  33. }
  34. }
  35. headers = {'content-type': 'application/json'}
  36.  
  37. # Upgrade the service with payload
  38. r = requests.post(os.environ['RANCHER_URL'] + 'v1/services/' + service['id'] + '/?action=upgrade',
  39. data=json.dumps(payload), headers=headers,
  40. auth=(os.environ['RANCHER_ACCESS_KEY'], os.environ['RANCHER_SECRET_KEY']))
  41.  
  42. # Pool service upgrade status
  43. state = 'upgrading'
  44. sleep = 30
  45. retry = 10
  46.  
  47. while (state != 'upgraded'):
  48. print "service: " + service['name'] + " [upgrading]"
  49. time.sleep(sleep)
  50. r = requests.get(os.environ['RANCHER_URL'] + 'v1/services/' + service['id'],
  51. auth=(os.environ['RANCHER_ACCESS_KEY'], os.environ['RANCHER_SECRET_KEY']))
  52. state = r.json()['state']
  53. retry -= 1
  54. if (retry <= 0): sys.exit()
  55.  
  56. print "service: " + service['name'] + " [upgraded]"
  57.  
  58. # Finish Upgrade
  59. r = requests.post(os.environ['RANCHER_URL'] + 'v1/services/' + service['id'] + '/?action=finishupgrade',
  60. headers=headers, auth=(os.environ['RANCHER_ACCESS_KEY'], os.environ['RANCHER_SECRET_KEY']));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement