Guest User

Script to automatically add a Docker host to Rancher

a guest
Sep 2nd, 2015
1,240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import sys
  5. import getopt
  6. import requests, json
  7. from time import sleep
  8.  
  9. # usage help message
  10. def usage(msg = None):
  11.         if not msg == None:
  12.                 print msg
  13.                 print
  14.         print "Usage: new_host [--url=<RANCHER_URL> --key=<RANCHER_ACCESS_KEY> --secret=<RANCHER_SECRET_KEY>]"
  15.         print
  16.         print "Adds this host to Rancher using the credentials supplied or defined as environment vars"
  17.  
  18. # get credentials form args or env
  19. rancher_url = os.environ.get("RANCHER_URL",None)
  20. rancher_key = os.environ.get("RANCHER_ACCESS_KEY",None)
  21. rancher_secret = os.environ.get("RANCHER_SECRET_KEY",None)
  22.  
  23. opts, args = getopt.getopt(sys.argv[1:],"hu:k:s:",["help","url=","key=","secret="])
  24. for o,a in opts:
  25.         if o in ("-h","--help"):
  26.                 usage()
  27.                 sys.exit(1)
  28.         elif o in ("-u","--url"):
  29.                 rancher_url = a
  30.         elif o in ("-k","--key"):
  31.                 rancher_key = a
  32.         elif o in ("-s","--secret"):
  33.                 rancher_secret = a
  34.  
  35. if rancher_url == None:
  36.         usage("Rancher URL not specified")
  37.         sys.exit(1)
  38. if rancher_key == None:
  39.         usage("Rancher key not specified")
  40.         sys.exit(1)
  41. if rancher_secret == None:
  42.         usage("Rancher secret not specified")
  43.         sys.exit(1)
  44.  
  45. # split url to protocol and host
  46. rancher_protocol,rancher_host = rancher_url.split("://")
  47.  
  48. # get environment we're in
  49. url = "%s://%s:%s@%s/v1/projects" % (rancher_protocol,rancher_key,rancher_secret,rancher_host)
  50. response = requests.get(url)
  51. data = json.loads(response.text)
  52. rancher_environment = data['data'][0]['name']
  53. print "rancher_environment is %s" % rancher_environment
  54.  
  55. # now ask for a new registration key and wait until it becomes active
  56. url = "%s://%s:%s@%s/v1/registrationtokens" % (rancher_protocol,rancher_key,rancher_secret,rancher_host)
  57. response = requests.post(url,json={})
  58. key_active = False
  59. while not key_active:
  60.         url = "%s://%s:%s@%s/v1/registrationtokens/%s" % (rancher_protocol,rancher_key,rancher_secret,rancher_host,response.json()['id'])
  61.         print url
  62.         if response.json()['state'] == 'active':
  63.                 key_active = True
  64.                 command = response.json()['command']
  65.         else:
  66.                 sleep(0.1)
  67.                 response = requests.get(url)
  68.  
  69. print command
  70. os.system(command)
Advertisement
Add Comment
Please, Sign In to add comment