cluster 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #!/usr/bin/env python2
  2. # vim: expandtab:tabstop=4:shiftwidth=4
  3. import argparse
  4. import ConfigParser
  5. import os
  6. import sys
  7. import subprocess
  8. import traceback
  9. class Cluster(object):
  10. """
  11. Provide Command, Control and Configuration (c3) Interface for OpenShift Clusters
  12. """
  13. def __init__(self):
  14. # setup ansible ssh environment
  15. if 'ANSIBLE_SSH_ARGS' not in os.environ:
  16. os.environ['ANSIBLE_SSH_ARGS'] = (
  17. '-o ForwardAgent=yes '
  18. '-o StrictHostKeyChecking=no '
  19. '-o UserKnownHostsFile=/dev/null '
  20. '-o ControlMaster=auto '
  21. '-o ControlPersist=600s '
  22. )
  23. # Because of `UserKnownHostsFile=/dev/null`
  24. # our `.ssh/known_hosts` file most probably misses the ssh host public keys
  25. # of our servers.
  26. # In that case, ansible serializes the execution of ansible modules
  27. # because we might be interactively prompted to accept the ssh host public keys.
  28. # Because of `StrictHostKeyChecking=no` we know that we won't be prompted
  29. # So, we don't want our modules execution to be serialized.
  30. os.environ['ANSIBLE_HOST_KEY_CHECKING'] = 'False'
  31. # TODO: A more secure way to proceed would consist in dynamically
  32. # retrieving the ssh host public keys from the IaaS interface
  33. def get_deployment_type(self, args):
  34. """
  35. Get the deployment_type based on the environment variables and the
  36. command line arguments
  37. :param args: command line arguments provided by the user
  38. :return: string representing the deployment type
  39. """
  40. deployment_type = 'origin'
  41. if args.deployment_type:
  42. deployment_type = args.deployment_type
  43. elif 'OS_DEPLOYMENT_TYPE' in os.environ:
  44. deployment_type = os.environ['OS_DEPLOYMENT_TYPE']
  45. return deployment_type
  46. def create(self, args):
  47. """
  48. Create an OpenShift cluster for given provider
  49. :param args: command line arguments provided by user
  50. """
  51. env = {'cluster_id': args.cluster_id,
  52. 'deployment_type': self.get_deployment_type(args)}
  53. playbook = "playbooks/{}/openshift-cluster/launch.yml".format(args.provider)
  54. inventory = self.setup_provider(args.provider)
  55. env['num_masters'] = args.masters
  56. env['num_nodes'] = args.nodes
  57. env['num_infra'] = args.infra
  58. env['num_etcd'] = args.etcd
  59. self.action(args, inventory, env, playbook)
  60. def terminate(self, args):
  61. """
  62. Destroy OpenShift cluster
  63. :param args: command line arguments provided by user
  64. """
  65. env = {'cluster_id': args.cluster_id,
  66. 'deployment_type': self.get_deployment_type(args)}
  67. playbook = "playbooks/{}/openshift-cluster/terminate.yml".format(args.provider)
  68. inventory = self.setup_provider(args.provider)
  69. self.action(args, inventory, env, playbook)
  70. def list(self, args):
  71. """
  72. List VMs in cluster
  73. :param args: command line arguments provided by user
  74. """
  75. env = {'cluster_id': args.cluster_id,
  76. 'deployment_type': self.get_deployment_type(args)}
  77. playbook = "playbooks/{}/openshift-cluster/list.yml".format(args.provider)
  78. inventory = self.setup_provider(args.provider)
  79. self.action(args, inventory, env, playbook)
  80. def config(self, args):
  81. """
  82. Configure or reconfigure OpenShift across clustered VMs
  83. :param args: command line arguments provided by user
  84. """
  85. env = {'cluster_id': args.cluster_id,
  86. 'deployment_type': self.get_deployment_type(args)}
  87. playbook = "playbooks/{}/openshift-cluster/config.yml".format(args.provider)
  88. inventory = self.setup_provider(args.provider)
  89. self.action(args, inventory, env, playbook)
  90. def update(self, args):
  91. """
  92. Update to latest OpenShift across clustered VMs
  93. :param args: command line arguments provided by user
  94. """
  95. env = {'cluster_id': args.cluster_id,
  96. 'deployment_type': self.get_deployment_type(args)}
  97. playbook = "playbooks/{}/openshift-cluster/update.yml".format(args.provider)
  98. inventory = self.setup_provider(args.provider)
  99. self.action(args, inventory, env, playbook)
  100. def service(self, args):
  101. """
  102. Make the same service call across all nodes in the cluster
  103. :param args: command line arguments provided by user
  104. """
  105. env = {'cluster_id': args.cluster_id,
  106. 'deployment_type': self.get_deployment_type(args),
  107. 'new_cluster_state': args.state}
  108. playbook = "playbooks/{}/openshift-cluster/service.yml".format(args.provider)
  109. inventory = self.setup_provider(args.provider)
  110. self.action(args, inventory, env, playbook)
  111. def setup_provider(self, provider):
  112. """
  113. Setup ansible playbook environment
  114. :param provider: command line arguments provided by user
  115. :return: path to inventory for given provider
  116. """
  117. config = ConfigParser.ConfigParser()
  118. if 'gce' == provider:
  119. config.readfp(open('inventory/gce/hosts/gce.ini'))
  120. for key in config.options('gce'):
  121. os.environ[key] = config.get('gce', key)
  122. inventory = '-i inventory/gce/hosts'
  123. elif 'aws' == provider:
  124. config.readfp(open('inventory/aws/hosts/ec2.ini'))
  125. for key in config.options('ec2'):
  126. os.environ[key] = config.get('ec2', key)
  127. inventory = '-i inventory/aws/hosts'
  128. key_vars = ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY']
  129. key_missing = [key for key in key_vars if key not in os.environ]
  130. boto_conf_files = ['~/.aws/credentials', '~/.boto']
  131. conf_exists = lambda conf: os.path.isfile(os.path.expanduser(conf))
  132. boto_configs = [conf for conf in boto_conf_files if conf_exists(conf)]
  133. if len(key_missing) > 0 and len(boto_configs) == 0:
  134. raise ValueError("PROVIDER aws requires {} environment variable(s). See README_AWS.md".format(key_missing))
  135. elif 'libvirt' == provider:
  136. inventory = '-i inventory/libvirt/hosts'
  137. elif 'openstack' == provider:
  138. inventory = '-i inventory/openstack/hosts'
  139. else:
  140. # this code should never be reached
  141. raise ValueError("invalid PROVIDER {}".format(provider))
  142. return inventory
  143. def action(self, args, inventory, env, playbook):
  144. """
  145. Build ansible-playbook command line and execute
  146. :param args: command line arguments provided by user
  147. :param inventory: derived provider library
  148. :param env: environment variables for kubernetes
  149. :param playbook: ansible playbook to execute
  150. """
  151. verbose = ''
  152. if args.verbose > 0:
  153. verbose = '-{}'.format('v' * args.verbose)
  154. if args.option:
  155. for opt in args.option:
  156. k, v = opt.split('=', 1)
  157. env['cli_' + k] = v
  158. ansible_env = '-e \'{}\''.format(
  159. ' '.join(['%s=%s' % (key, value) for (key, value) in env.items()])
  160. )
  161. command = 'ansible-playbook {} {} {} {}'.format(
  162. verbose, inventory, ansible_env, playbook
  163. )
  164. if args.profile:
  165. command = 'ANSIBLE_CALLBACK_PLUGINS=ansible-profile/callback_plugins ' + command
  166. if args.verbose > 1:
  167. command = 'time {}'.format(command)
  168. if args.verbose > 0:
  169. sys.stderr.write('RUN [{}]\n'.format(command))
  170. sys.stderr.flush()
  171. try:
  172. subprocess.check_call(command, shell=True)
  173. except subprocess.CalledProcessError as exc:
  174. raise ActionFailed("ACTION [{}] failed: {}"
  175. .format(args.action, exc))
  176. class ActionFailed(Exception):
  177. """
  178. Raised when action failed.
  179. """
  180. pass
  181. if __name__ == '__main__':
  182. """
  183. User command to invoke ansible playbooks in a "known" environment
  184. Reads ~/.openshift-ansible for default configuration items
  185. [DEFAULT]
  186. validate_cluster_ids = False
  187. cluster_ids = marketing,sales
  188. providers = gce,aws,libvirt,openstack
  189. """
  190. environment = ConfigParser.SafeConfigParser({
  191. 'cluster_ids': 'marketing,sales',
  192. 'validate_cluster_ids': 'False',
  193. 'providers': 'gce,aws,libvirt,openstack',
  194. })
  195. path = os.path.expanduser("~/.openshift-ansible")
  196. if os.path.isfile(path):
  197. environment.read(path)
  198. cluster = Cluster()
  199. parser = argparse.ArgumentParser(
  200. description='Python wrapper to ensure proper environment for OpenShift ansible playbooks',
  201. )
  202. parser.add_argument('-v', '--verbose', action='count',
  203. help='Multiple -v options increase the verbosity')
  204. parser.add_argument('--version', action='version', version='%(prog)s 0.3')
  205. meta_parser = argparse.ArgumentParser(add_help=False)
  206. providers = environment.get('DEFAULT', 'providers').split(',')
  207. meta_parser.add_argument('provider', choices=providers, help='provider')
  208. if environment.get('DEFAULT', 'validate_cluster_ids').lower() in ("yes", "true", "1"):
  209. meta_parser.add_argument('cluster_id', choices=environment.get('DEFAULT', 'cluster_ids').split(','),
  210. help='prefix for cluster VM names')
  211. else:
  212. meta_parser.add_argument('cluster_id', help='prefix for cluster VM names')
  213. meta_parser.add_argument('-t', '--deployment-type',
  214. choices=['origin', 'online', 'enterprise'],
  215. help='Deployment type. (default: origin)')
  216. meta_parser.add_argument('-T', '--product-type',
  217. choices=['openshift', 'atomic-enterprise'],
  218. help='Product type. (default: openshift)')
  219. meta_parser.add_argument('-o', '--option', action='append',
  220. help='options')
  221. meta_parser.add_argument('-p', '--profile', action='store_true',
  222. help='Enable playbook profiling')
  223. action_parser = parser.add_subparsers(dest='action', title='actions',
  224. description='Choose from valid actions')
  225. create_parser = action_parser.add_parser('create', help='Create a cluster',
  226. parents=[meta_parser])
  227. create_parser.add_argument('-m', '--masters', default=1, type=int,
  228. help='number of masters to create in cluster')
  229. create_parser.add_argument('-n', '--nodes', default=2, type=int,
  230. help='number of nodes to create in cluster')
  231. create_parser.add_argument('-i', '--infra', default=1, type=int,
  232. help='number of infra nodes to create in cluster')
  233. create_parser.add_argument('-e', '--etcd', default=0, type=int,
  234. help='number of external etcd hosts to create in cluster')
  235. create_parser.set_defaults(func=cluster.create)
  236. config_parser = action_parser.add_parser('config',
  237. help='Configure or reconfigure a cluster',
  238. parents=[meta_parser])
  239. config_parser.set_defaults(func=cluster.config)
  240. terminate_parser = action_parser.add_parser('terminate',
  241. help='Destroy a cluster',
  242. parents=[meta_parser])
  243. terminate_parser.add_argument('-f', '--force', action='store_true',
  244. help='Destroy cluster without confirmation')
  245. terminate_parser.set_defaults(func=cluster.terminate)
  246. update_parser = action_parser.add_parser('update',
  247. help='Update OpenShift across cluster',
  248. parents=[meta_parser])
  249. update_parser.add_argument('-f', '--force', action='store_true',
  250. help='Update cluster without confirmation')
  251. update_parser.set_defaults(func=cluster.update)
  252. list_parser = action_parser.add_parser('list', help='List VMs in cluster',
  253. parents=[meta_parser])
  254. list_parser.set_defaults(func=cluster.list)
  255. service_parser = action_parser.add_parser('service', help='service for openshift across cluster',
  256. parents=[meta_parser])
  257. # choices are the only ones valid for the ansible service module: http://docs.ansible.com/service_module.html
  258. service_parser.add_argument('state', choices=['started', 'stopped', 'restarted', 'reloaded'],
  259. help='make service call across cluster')
  260. service_parser.set_defaults(func=cluster.service)
  261. args = parser.parse_args()
  262. if 'terminate' == args.action and not args.force:
  263. answer = raw_input("This will destroy the ENTIRE {} environment. Are you sure? [y/N] ".format(args.cluster_id))
  264. if answer not in ['y', 'Y']:
  265. sys.stderr.write('\nACTION [terminate] aborted by user!\n')
  266. exit(1)
  267. if 'update' == args.action and not args.force:
  268. answer = raw_input(
  269. "This is destructive and could corrupt {} environment. Continue? [y/N] ".format(args.cluster_id))
  270. if answer not in ['y', 'Y']:
  271. sys.stderr.write('\nACTION [update] aborted by user!\n')
  272. exit(1)
  273. try:
  274. args.func(args)
  275. except Exception as exc:
  276. if args.verbose:
  277. traceback.print_exc(file=sys.stderr)
  278. else:
  279. print >>sys.stderr, exc
  280. exit(1)