cluster 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #!/usr/bin/env python2
  2. # vim: expandtab:tabstop=4:shiftwidth=4
  3. import argparse
  4. import ConfigParser
  5. import sys
  6. import os
  7. class Cluster(object):
  8. """
  9. Provide Command, Control and Configuration (c3) Interface for OpenShift Clusters
  10. """
  11. def __init__(self):
  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 get_deployment_type(self, args):
  22. """
  23. Get the deployment_type based on the environment variables and the
  24. command line arguments
  25. :param args: command line arguments provided by the user
  26. :return: string representing the deployment type
  27. """
  28. deployment_type = 'origin'
  29. if args.deployment_type:
  30. deployment_type = args.deployment_type
  31. elif 'OS_DEPLOYMENT_TYPE' in os.environ:
  32. deployment_type = os.environ['OS_DEPLOYMENT_TYPE']
  33. return deployment_type
  34. def create(self, args):
  35. """
  36. Create an OpenShift cluster for given provider
  37. :param args: command line arguments provided by user
  38. :return: exit status from run command
  39. """
  40. env = {'cluster_id': args.cluster_id,
  41. 'deployment_type': self.get_deployment_type(args)}
  42. playbook = "playbooks/{}/openshift-cluster/launch.yml".format(args.provider)
  43. inventory = self.setup_provider(args.provider)
  44. env['num_masters'] = args.masters
  45. env['num_nodes'] = args.nodes
  46. return self.action(args, inventory, env, playbook)
  47. def terminate(self, args):
  48. """
  49. Destroy OpenShift cluster
  50. :param args: command line arguments provided by user
  51. :return: exit status from run command
  52. """
  53. env = {'cluster_id': args.cluster_id,
  54. 'deployment_type': self.get_deployment_type(args)}
  55. playbook = "playbooks/{}/openshift-cluster/terminate.yml".format(args.provider)
  56. inventory = self.setup_provider(args.provider)
  57. return self.action(args, inventory, env, playbook)
  58. def list(self, args):
  59. """
  60. List VMs in cluster
  61. :param args: command line arguments provided by user
  62. :return: exit status from run command
  63. """
  64. env = {'cluster_id': args.cluster_id,
  65. 'deployment_type': self.get_deployment_type(args)}
  66. playbook = "playbooks/{}/openshift-cluster/list.yml".format(args.provider)
  67. inventory = self.setup_provider(args.provider)
  68. return self.action(args, inventory, env, playbook)
  69. def config(self, args):
  70. """
  71. Configure or reconfigure OpenShift across clustered VMs
  72. :param args: command line arguments provided by user
  73. :return: exit status from run command
  74. """
  75. env = {'cluster_id': args.cluster_id,
  76. 'deployment_type': self.get_deployment_type(args)}
  77. playbook = "playbooks/{}/openshift-cluster/config.yml".format(args.provider)
  78. inventory = self.setup_provider(args.provider)
  79. return self.action(args, inventory, env, playbook)
  80. def update(self, args):
  81. """
  82. Update to latest OpenShift across clustered VMs
  83. :param args: command line arguments provided by user
  84. :return: exit status from run command
  85. """
  86. env = {'cluster_id': args.cluster_id,
  87. 'deployment_type': self.get_deployment_type(args)}
  88. playbook = "playbooks/{}/openshift-cluster/update.yml".format(args.provider)
  89. inventory = self.setup_provider(args.provider)
  90. return self.action(args, inventory, env, playbook)
  91. def service(self, args):
  92. """
  93. Make the same service call across all nodes in the cluster
  94. :param args: command line arguments provided by user
  95. :return: exit status from run command
  96. """
  97. env = {'cluster_id': args.cluster_id,
  98. 'deployment_type': self.get_deployment_type(args),
  99. 'new_cluster_state': args.state}
  100. playbook = "playbooks/{}/openshift-cluster/service.yml".format(args.provider)
  101. inventory = self.setup_provider(args.provider)
  102. return self.action(args, inventory, env, playbook)
  103. def setup_provider(self, provider):
  104. """
  105. Setup ansible playbook environment
  106. :param provider: command line arguments provided by user
  107. :return: path to inventory for given provider
  108. """
  109. config = ConfigParser.ConfigParser()
  110. if 'gce' == provider:
  111. config.readfp(open('inventory/gce/hosts/gce.ini'))
  112. for key in config.options('gce'):
  113. os.environ[key] = config.get('gce', key)
  114. inventory = '-i inventory/gce/hosts'
  115. elif 'aws' == provider:
  116. config.readfp(open('inventory/aws/hosts/ec2.ini'))
  117. for key in config.options('ec2'):
  118. os.environ[key] = config.get('ec2', key)
  119. inventory = '-i inventory/aws/hosts'
  120. elif 'libvirt' == provider:
  121. inventory = '-i inventory/libvirt/hosts'
  122. elif 'openstack' == provider:
  123. inventory = '-i inventory/openstack/hosts'
  124. else:
  125. # this code should never be reached
  126. raise ValueError("invalid PROVIDER {}".format(provider))
  127. return inventory
  128. def action(self, args, inventory, env, playbook):
  129. """
  130. Build ansible-playbook command line and execute
  131. :param args: command line arguments provided by user
  132. :param inventory: derived provider library
  133. :param env: environment variables for kubernetes
  134. :param playbook: ansible playbook to execute
  135. :return: exit status from ansible-playbook command
  136. """
  137. verbose = ''
  138. if args.verbose > 0:
  139. verbose = '-{}'.format('v' * args.verbose)
  140. if args.option:
  141. for opt in args.option:
  142. k, v = opt.split('=', 1)
  143. env['opt_'+k] = v
  144. ansible_env = '-e \'{}\''.format(
  145. ' '.join(['%s=%s' % (key, value) for (key, value) in env.items()])
  146. )
  147. command = 'ansible-playbook {} {} {} {}'.format(
  148. verbose, inventory, ansible_env, playbook
  149. )
  150. if args.verbose > 1:
  151. command = 'time {}'.format(command)
  152. if args.verbose > 0:
  153. sys.stderr.write('RUN [{}]\n'.format(command))
  154. sys.stderr.flush()
  155. return os.system(command)
  156. if __name__ == '__main__':
  157. """
  158. User command to invoke ansible playbooks in a "known" environment
  159. Reads ~/.openshift-ansible for default configuration items
  160. [DEFAULT]
  161. validate_cluster_ids = False
  162. cluster_ids = marketing,sales
  163. providers = gce,aws,libvirt,openstack
  164. """
  165. environment = ConfigParser.SafeConfigParser({
  166. 'cluster_ids': 'marketing,sales',
  167. 'validate_cluster_ids': 'False',
  168. 'providers': 'gce,aws,libvirt,openstack',
  169. })
  170. path = os.path.expanduser("~/.openshift-ansible")
  171. if os.path.isfile(path):
  172. environment.read(path)
  173. cluster = Cluster()
  174. parser = argparse.ArgumentParser(
  175. description='Python wrapper to ensure proper environment for OpenShift ansible playbooks',
  176. )
  177. parser.add_argument('-v', '--verbose', action='count',
  178. help='Multiple -v options increase the verbosity')
  179. parser.add_argument('--version', action='version', version='%(prog)s 0.3')
  180. meta_parser = argparse.ArgumentParser(add_help=False)
  181. providers = environment.get('DEFAULT', 'providers').split(',')
  182. meta_parser.add_argument('provider', choices=providers, help='provider')
  183. if environment.get('DEFAULT', 'validate_cluster_ids').lower() in ("yes", "true", "1"):
  184. meta_parser.add_argument('cluster_id', choices=environment.get('DEFAULT', 'cluster_ids').split(','),
  185. help='prefix for cluster VM names')
  186. else:
  187. meta_parser.add_argument('cluster_id', help='prefix for cluster VM names')
  188. meta_parser.add_argument('-t', '--deployment-type',
  189. choices=['origin', 'online', 'enterprise'],
  190. help='Deployment type. (default: origin)')
  191. meta_parser.add_argument('-o', '--option', action='append',
  192. help='options')
  193. action_parser = parser.add_subparsers(dest='action', title='actions',
  194. description='Choose from valid actions')
  195. create_parser = action_parser.add_parser('create', help='Create a cluster',
  196. parents=[meta_parser])
  197. create_parser.add_argument('-m', '--masters', default=1, type=int,
  198. help='number of masters to create in cluster')
  199. create_parser.add_argument('-n', '--nodes', default=2, type=int,
  200. help='number of nodes to create in cluster')
  201. create_parser.set_defaults(func=cluster.create)
  202. config_parser = action_parser.add_parser('config',
  203. help='Configure or reconfigure a cluster',
  204. parents=[meta_parser])
  205. config_parser.set_defaults(func=cluster.config)
  206. terminate_parser = action_parser.add_parser('terminate',
  207. help='Destroy a cluster',
  208. parents=[meta_parser])
  209. terminate_parser.add_argument('-f', '--force', action='store_true',
  210. help='Destroy cluster without confirmation')
  211. terminate_parser.set_defaults(func=cluster.terminate)
  212. update_parser = action_parser.add_parser('update',
  213. help='Update OpenShift across cluster',
  214. parents=[meta_parser])
  215. update_parser.add_argument('-f', '--force', action='store_true',
  216. help='Update cluster without confirmation')
  217. update_parser.set_defaults(func=cluster.update)
  218. list_parser = action_parser.add_parser('list', help='List VMs in cluster',
  219. parents=[meta_parser])
  220. list_parser.set_defaults(func=cluster.list)
  221. service_parser = action_parser.add_parser('service', help='service for openshift across cluster',
  222. parents=[meta_parser])
  223. # choices are the only ones valid for the ansible service module: http://docs.ansible.com/service_module.html
  224. service_parser.add_argument('state', choices=['started', 'stopped', 'restarted', 'reloaded'],
  225. help='make service call across cluster')
  226. service_parser.set_defaults(func=cluster.service)
  227. args = parser.parse_args()
  228. if 'terminate' == args.action and not args.force:
  229. answer = raw_input("This will destroy the ENTIRE {} environment. Are you sure? [y/N] ".format(args.cluster_id))
  230. if answer not in ['y', 'Y']:
  231. sys.stderr.write('\nACTION [terminate] aborted by user!\n')
  232. exit(1)
  233. if 'update' == args.action and not args.force:
  234. answer = raw_input(
  235. "This is destructive and could corrupt {} environment. Continue? [y/N] ".format(args.cluster_id))
  236. if answer not in ['y', 'Y']:
  237. sys.stderr.write('\nACTION [update] aborted by user!\n')
  238. exit(1)
  239. status = args.func(args)
  240. if status != 0:
  241. sys.stderr.write("ACTION [{}] failed with exit status {}\n".format(args.action, status))
  242. exit(status)