cluster 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. # vim: expandtab:tabstop=4:shiftwidth=4
  3. import argparse
  4. import ConfigParser
  5. import sys
  6. import os
  7. class Cluster(object):
  8. """Python wrapper to ensure environment is correct for running ansible playbooks
  9. """
  10. def __init__(self, args):
  11. self.args = args
  12. # setup ansible ssh environment
  13. if 'ANSIBLE_SSH_ARGS' not in os.environ:
  14. os.environ['ANSIBLE_SSH_ARGS'] = (
  15. '-o ForwardAgent=yes'
  16. '-o StrictHostKeyChecking=no'
  17. '-o UserKnownHostsFile=/dev/null'
  18. '-o ControlMaster=auto'
  19. '-o ControlPersist=600s'
  20. )
  21. def apply(self):
  22. # setup ansible playbook environment
  23. config = ConfigParser.ConfigParser()
  24. if 'gce' == self.args.provider:
  25. config.readfp(open('inventory/gce/gce.ini'))
  26. for key in config.options('gce'):
  27. os.environ[key] = config.get('gce', key)
  28. inventory = '-i inventory/gce/gce.py'
  29. elif 'aws' == self.args.provider:
  30. config.readfp(open('inventory/aws/ec2.ini'))
  31. for key in config.options('ec2'):
  32. os.environ[key] = config.get('ec2', key)
  33. inventory = '-i inventory/aws/ec2.py'
  34. else:
  35. assert False, "invalid PROVIDER {}".format(self.args.provider)
  36. env = {'cluster_id': self.args.cluster_id}
  37. if 'create' == self.args.action:
  38. playbook = "playbooks/{}/openshift-cluster/launch.yml".format(self.args.provider)
  39. env['masters'] = self.args.masters
  40. env['nodes'] = self.args.nodes
  41. elif 'terminate' == self.args.action:
  42. playbook = "playbooks/{}/openshift-cluster/terminate.yml".format(self.args.provider)
  43. elif 'list' == self.args.action:
  44. # todo: implement cluster list
  45. argparse.ArgumentError("ACTION {} not implemented".format(self.args.action))
  46. elif 'update' == self.args.action:
  47. # todo: implement cluster update
  48. argparse.ArgumentError("ACTION {} not implemented".format(self.args.action))
  49. else:
  50. assert False, "invalid ACTION {}".format(self.args.action)
  51. verbose = ''
  52. if self.args.verbose > 0:
  53. verbose = '-{}'.format('v' * self.args.verbose)
  54. ansible_env = '-e \'{}\''.format(
  55. ' '.join(['%s=%s' % (key, value) for (key, value) in env.items()])
  56. )
  57. command = 'ansible-playbook {} {} {} {}'.format(
  58. verbose, inventory, ansible_env, playbook
  59. )
  60. if self.args.verbose > 1:
  61. command = 'time {}'.format(command)
  62. if self.args.verbose > 0:
  63. sys.stderr.write('RUN [{}]\n'.format(command))
  64. sys.stderr.flush()
  65. os.system(command)
  66. if __name__ == '__main__':
  67. parser = argparse.ArgumentParser(description='Manage OpenShift Cluster')
  68. parser.add_argument('-p', '--provider', default='gce', choices=['gce', 'aws'],
  69. help='One of the supported cloud providers')
  70. parser.add_argument('-m', '--masters', default=1, type=int, help='number of masters to create in cluster')
  71. parser.add_argument('-n', '--nodes', default=2, type=int, help='number of nodes to create in cluster')
  72. parser.add_argument('-v', '--verbose', action='count', help='Multiple -v options increase the verbosity')
  73. parser.add_argument('--version', action='version', version='%(prog)s 0.1')
  74. parser.add_argument('action', choices=['create', 'terminate', 'update', 'list'])
  75. parser.add_argument('provider', choices=['gce', 'aws'])
  76. parser.add_argument('cluster_id', help='prefix for cluster VM names')
  77. args = parser.parse_args()
  78. Cluster(args).apply()