cluster 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. cluster = {'cluster_id': args.cluster_id,
  52. 'deployment_type': self.get_deployment_type(args)}
  53. playbook = "playbooks/{0}/openshift-cluster/launch.yml".format(args.provider)
  54. inventory = self.setup_provider(args.provider)
  55. cluster['num_masters'] = args.masters
  56. cluster['num_nodes'] = args.nodes
  57. cluster['num_infra'] = args.infra
  58. cluster['num_etcd'] = args.etcd
  59. cluster['cluster_env'] = args.env
  60. self.action(args, inventory, cluster, playbook)
  61. def addNodes(self, args):
  62. """
  63. Add nodes to an existing cluster for given provider
  64. :param args: command line arguments provided by user
  65. """
  66. cluster = {'cluster_id': args.cluster_id,
  67. 'deployment_type': self.get_deployment_type(args),
  68. }
  69. playbook = "playbooks/{0}/openshift-cluster/addNodes.yml".format(args.provider)
  70. inventory = self.setup_provider(args.provider)
  71. cluster['num_nodes'] = args.nodes
  72. cluster['num_infra'] = args.infra
  73. cluster['cluster_env'] = args.env
  74. self.action(args, inventory, cluster, playbook)
  75. def terminate(self, args):
  76. """
  77. Destroy OpenShift cluster
  78. :param args: command line arguments provided by user
  79. """
  80. cluster = {'cluster_id': args.cluster_id,
  81. 'deployment_type': self.get_deployment_type(args),
  82. 'cluster_env': args.env,
  83. }
  84. playbook = "playbooks/{0}/openshift-cluster/terminate.yml".format(args.provider)
  85. inventory = self.setup_provider(args.provider)
  86. self.action(args, inventory, cluster, playbook)
  87. def list(self, args):
  88. """
  89. List VMs in cluster
  90. :param args: command line arguments provided by user
  91. """
  92. cluster = {'cluster_id': args.cluster_id,
  93. 'deployment_type': self.get_deployment_type(args),
  94. 'cluster_env': args.env,
  95. }
  96. playbook = "playbooks/{0}/openshift-cluster/list.yml".format(args.provider)
  97. inventory = self.setup_provider(args.provider)
  98. self.action(args, inventory, cluster, playbook)
  99. def config(self, args):
  100. """
  101. Configure or reconfigure OpenShift across clustered VMs
  102. :param args: command line arguments provided by user
  103. """
  104. cluster = {'cluster_id': args.cluster_id,
  105. 'deployment_type': self.get_deployment_type(args),
  106. 'cluster_env': args.env,
  107. }
  108. playbook = "playbooks/{0}/openshift-cluster/config.yml".format(args.provider)
  109. inventory = self.setup_provider(args.provider)
  110. self.action(args, inventory, cluster, playbook)
  111. def update(self, args):
  112. """
  113. Update to latest OpenShift across clustered VMs
  114. :param args: command line arguments provided by user
  115. """
  116. cluster = {'cluster_id': args.cluster_id,
  117. 'deployment_type': self.get_deployment_type(args),
  118. 'cluster_env': args.env,
  119. }
  120. playbook = "playbooks/{0}/openshift-cluster/update.yml".format(args.provider)
  121. inventory = self.setup_provider(args.provider)
  122. self.action(args, inventory, cluster, playbook)
  123. def service(self, args):
  124. """
  125. Make the same service call across all nodes in the cluster
  126. :param args: command line arguments provided by user
  127. """
  128. cluster = {'cluster_id': args.cluster_id,
  129. 'deployment_type': self.get_deployment_type(args),
  130. 'new_cluster_state': args.state,
  131. 'cluster_env': args.env,
  132. }
  133. playbook = "playbooks/{0}/openshift-cluster/service.yml".format(args.provider)
  134. inventory = self.setup_provider(args.provider)
  135. self.action(args, inventory, cluster, playbook)
  136. def setup_provider(self, provider):
  137. """
  138. Setup ansible playbook environment
  139. :param provider: command line arguments provided by user
  140. :return: path to inventory for given provider
  141. """
  142. config = ConfigParser.ConfigParser()
  143. if 'gce' == provider:
  144. gce_ini_default_path = os.path.join('inventory/gce/hosts/gce.ini')
  145. gce_ini_path = os.environ.get('GCE_INI_PATH', gce_ini_default_path)
  146. if os.path.exists(gce_ini_path):
  147. config.readfp(open(gce_ini_path))
  148. for key in config.options('gce'):
  149. os.environ[key] = config.get('gce', key)
  150. inventory = '-i inventory/gce/hosts'
  151. elif 'aws' == provider:
  152. config.readfp(open('inventory/aws/hosts/ec2.ini'))
  153. for key in config.options('ec2'):
  154. os.environ[key] = config.get('ec2', key)
  155. inventory = '-i inventory/aws/hosts'
  156. key_vars = ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY']
  157. key_missing = [key for key in key_vars if key not in os.environ]
  158. boto_conf_files = ['~/.aws/credentials', '~/.boto']
  159. conf_exists = lambda conf: os.path.isfile(os.path.expanduser(conf))
  160. boto_configs = [conf for conf in boto_conf_files if conf_exists(conf)]
  161. if len(key_missing) > 0 and len(boto_configs) == 0:
  162. raise ValueError("PROVIDER aws requires {0} environment variable(s). See README_AWS.md".format(key_missing))
  163. elif 'libvirt' == provider:
  164. inventory = '-i inventory/libvirt/hosts'
  165. elif 'openstack' == provider:
  166. inventory = '-i inventory/openstack/hosts'
  167. else:
  168. # this code should never be reached
  169. raise ValueError("invalid PROVIDER {0}".format(provider))
  170. return inventory
  171. def action(self, args, inventory, cluster, playbook):
  172. """
  173. Build ansible-playbook command line and execute
  174. :param args: command line arguments provided by user
  175. :param inventory: derived provider library
  176. :param cluster: cluster variables for kubernetes
  177. :param playbook: ansible playbook to execute
  178. """
  179. verbose = ''
  180. if args.verbose > 0:
  181. verbose = '-{0}'.format('v' * args.verbose)
  182. if args.option:
  183. for opt in args.option:
  184. k, v = opt.split('=', 1)
  185. cluster['cli_' + k] = v
  186. ansible_extra_vars = '-e \'{0}\''.format(
  187. ' '.join(['%s=%s' % (key, value) for (key, value) in cluster.items()])
  188. )
  189. command = 'ansible-playbook {0} {1} {2} {3}'.format(
  190. verbose, inventory, ansible_extra_vars, playbook
  191. )
  192. if args.profile:
  193. command = 'ANSIBLE_CALLBACK_PLUGINS=ansible-profile/callback_plugins ' + command
  194. if args.verbose > 1:
  195. command = 'time {0}'.format(command)
  196. if args.verbose > 0:
  197. sys.stderr.write('RUN [{0}]\n'.format(command))
  198. sys.stderr.flush()
  199. try:
  200. subprocess.check_call(command, shell=True)
  201. except subprocess.CalledProcessError as exc:
  202. raise ActionFailed("ACTION [{0}] failed: {1}"
  203. .format(args.action, exc))
  204. class ActionFailed(Exception):
  205. """
  206. Raised when action failed.
  207. """
  208. pass
  209. if __name__ == '__main__':
  210. """
  211. User command to invoke ansible playbooks in a "known" configuration
  212. Reads ~/.openshift-ansible for default configuration items
  213. [DEFAULT]
  214. validate_cluster_ids = False
  215. cluster_ids = marketing,sales
  216. providers = gce,aws,libvirt,openstack
  217. """
  218. cluster_config = ConfigParser.SafeConfigParser({
  219. 'cluster_ids': 'marketing,sales',
  220. 'validate_cluster_ids': 'False',
  221. 'providers': 'gce,aws,libvirt,openstack',
  222. })
  223. path = os.path.expanduser("~/.openshift-ansible")
  224. if os.path.isfile(path):
  225. cluster_config.read(path)
  226. cluster = Cluster()
  227. parser = argparse.ArgumentParser(
  228. description='Python wrapper to ensure proper configuration for OpenShift ansible playbooks',
  229. )
  230. parser.add_argument('-v', '--verbose', action='count',
  231. help='Multiple -v options increase the verbosity')
  232. parser.add_argument('--version', action='version', version='%(prog)s 0.3')
  233. meta_parser = argparse.ArgumentParser(add_help=False)
  234. providers = cluster_config.get('DEFAULT', 'providers').split(',')
  235. meta_parser.add_argument('provider', choices=providers, help='provider')
  236. if cluster_config.get('DEFAULT', 'validate_cluster_ids').lower() in ("yes", "true", "1"):
  237. meta_parser.add_argument('cluster_id', choices=cluster_config.get('DEFAULT', 'cluster_ids').split(','),
  238. help='prefix for cluster VM names')
  239. else:
  240. meta_parser.add_argument('cluster_id', help='prefix for cluster VM names')
  241. meta_parser.add_argument('-t', '--deployment-type',
  242. choices=['origin', 'online', 'enterprise'],
  243. help='Deployment type. (default: origin)')
  244. meta_parser.add_argument('-T', '--product-type',
  245. choices=['openshift', 'atomic-enterprise'],
  246. help='Product type. (default: openshift)')
  247. meta_parser.add_argument('-o', '--option', action='append',
  248. help='options')
  249. meta_parser.add_argument('--env', default='dev', type=str,
  250. help='environment for the cluster. Defaults to \'dev\'.')
  251. meta_parser.add_argument('-p', '--profile', action='store_true',
  252. help='Enable playbook profiling')
  253. action_parser = parser.add_subparsers(dest='action', title='actions',
  254. description='Choose from valid actions')
  255. create_parser = action_parser.add_parser('create', help='Create a cluster',
  256. parents=[meta_parser])
  257. create_parser.add_argument('-m', '--masters', default=1, type=int,
  258. help='number of masters to create in cluster')
  259. create_parser.add_argument('-n', '--nodes', default=2, type=int,
  260. help='number of nodes to create in cluster')
  261. create_parser.add_argument('-i', '--infra', default=1, type=int,
  262. help='number of infra nodes to create in cluster')
  263. create_parser.add_argument('-e', '--etcd', default=0, type=int,
  264. help='number of external etcd hosts to create in cluster')
  265. create_parser.set_defaults(func=cluster.create)
  266. create_parser = action_parser.add_parser('addNodes', help='Add nodes to a cluster',
  267. parents=[meta_parser])
  268. create_parser.add_argument('-n', '--nodes', default=1, type=int,
  269. help='number of nodes to add to the cluster')
  270. create_parser.add_argument('-i', '--infra', default=1, type=int,
  271. help='number of infra nodes to add to the cluster')
  272. create_parser.set_defaults(func=cluster.addNodes)
  273. config_parser = action_parser.add_parser('config',
  274. help='Configure or reconfigure a cluster',
  275. parents=[meta_parser])
  276. config_parser.set_defaults(func=cluster.config)
  277. terminate_parser = action_parser.add_parser('terminate',
  278. help='Destroy a cluster',
  279. parents=[meta_parser])
  280. terminate_parser.add_argument('-f', '--force', action='store_true',
  281. help='Destroy cluster without confirmation')
  282. terminate_parser.set_defaults(func=cluster.terminate)
  283. update_parser = action_parser.add_parser('update',
  284. help='Update OpenShift across cluster',
  285. parents=[meta_parser])
  286. update_parser.add_argument('-f', '--force', action='store_true',
  287. help='Update cluster without confirmation')
  288. update_parser.set_defaults(func=cluster.update)
  289. list_parser = action_parser.add_parser('list', help='List VMs in cluster',
  290. parents=[meta_parser])
  291. list_parser.set_defaults(func=cluster.list)
  292. service_parser = action_parser.add_parser('service', help='service for openshift across cluster',
  293. parents=[meta_parser])
  294. # choices are the only ones valid for the ansible service module: http://docs.ansible.com/service_module.html
  295. service_parser.add_argument('state', choices=['started', 'stopped', 'restarted', 'reloaded'],
  296. help='make service call across cluster')
  297. service_parser.set_defaults(func=cluster.service)
  298. args = parser.parse_args()
  299. if 'terminate' == args.action and not args.force:
  300. answer = raw_input("This will destroy the ENTIRE {0} cluster. Are you sure? [y/N] ".format(args.cluster_id))
  301. if answer not in ['y', 'Y']:
  302. sys.stderr.write('\nACTION [terminate] aborted by user!\n')
  303. exit(1)
  304. if 'update' == args.action and not args.force:
  305. answer = raw_input(
  306. "This is destructive and could corrupt {0} cluster. Continue? [y/N] ".format(args.cluster_id))
  307. if answer not in ['y', 'Y']:
  308. sys.stderr.write('\nACTION [update] aborted by user!\n')
  309. exit(1)
  310. try:
  311. args.func(args)
  312. except Exception as exc:
  313. if args.verbose:
  314. traceback.print_exc(file=sys.stderr)
  315. else:
  316. print >>sys.stderr, exc
  317. exit(1)