You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

404 lines
16 KiB

  1. #!/usr/bin/env python3
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  11. # implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # Usage: inventory.py ip1 [ip2 ...]
  16. # Examples: inventory.py 10.10.1.3 10.10.1.4 10.10.1.5
  17. #
  18. # Advanced usage:
  19. # Add another host after initial creation: inventory.py 10.10.1.5
  20. # Add range of hosts: inventory.py 10.10.1.3-10.10.1.5
  21. # Add hosts with different ip and access ip:
  22. # inventory.py 10.0.0.1,192.168.10.1 10.0.0.2,192.168.10.2 10.0.0.3,192.168.1.3
  23. # Delete a host: inventory.py -10.10.1.3
  24. # Delete a host by id: inventory.py -node1
  25. #
  26. # Load a YAML or JSON file with inventory data: inventory.py load hosts.yaml
  27. # YAML file should be in the following format:
  28. # group1:
  29. # host1:
  30. # ip: X.X.X.X
  31. # var: val
  32. # group2:
  33. # host2:
  34. # ip: X.X.X.X
  35. from collections import OrderedDict
  36. from ipaddress import ip_address
  37. from ruamel.yaml import YAML
  38. import os
  39. import re
  40. import sys
  41. ROLES = ['all', 'kube-master', 'kube-node', 'etcd', 'k8s-cluster',
  42. 'calico-rr']
  43. PROTECTED_NAMES = ROLES
  44. AVAILABLE_COMMANDS = ['help', 'print_cfg', 'print_ips', 'print_hostnames', 'load']
  45. _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True,
  46. '0': False, 'no': False, 'false': False, 'off': False}
  47. yaml = YAML()
  48. yaml.Representer.add_representer(OrderedDict, yaml.Representer.represent_dict)
  49. def get_var_as_bool(name, default):
  50. value = os.environ.get(name, '')
  51. return _boolean_states.get(value.lower(), default)
  52. # Configurable as shell vars start
  53. CONFIG_FILE = os.environ.get("CONFIG_FILE", "./inventory/sample/hosts.yaml")
  54. KUBE_MASTERS = int(os.environ.get("KUBE_MASTERS_MASTERS", 2))
  55. # Reconfigures cluster distribution at scale
  56. SCALE_THRESHOLD = int(os.environ.get("SCALE_THRESHOLD", 50))
  57. MASSIVE_SCALE_THRESHOLD = int(os.environ.get("SCALE_THRESHOLD", 200))
  58. DEBUG = get_var_as_bool("DEBUG", True)
  59. HOST_PREFIX = os.environ.get("HOST_PREFIX", "node")
  60. # Configurable as shell vars end
  61. class KubesprayInventory(object):
  62. def __init__(self, changed_hosts=None, config_file=None):
  63. self.config_file = config_file
  64. self.yaml_config = {}
  65. if self.config_file:
  66. try:
  67. self.hosts_file = open(config_file, 'r')
  68. self.yaml_config = yaml.load(self.hosts_file)
  69. except FileNotFoundError:
  70. pass
  71. if changed_hosts and changed_hosts[0] in AVAILABLE_COMMANDS:
  72. self.parse_command(changed_hosts[0], changed_hosts[1:])
  73. sys.exit(0)
  74. self.ensure_required_groups(ROLES)
  75. if changed_hosts:
  76. changed_hosts = self.range2ips(changed_hosts)
  77. self.hosts = self.build_hostnames(changed_hosts)
  78. self.purge_invalid_hosts(self.hosts.keys(), PROTECTED_NAMES)
  79. self.set_all(self.hosts)
  80. self.set_k8s_cluster()
  81. etcd_hosts_count = 3 if len(self.hosts.keys()) >= 3 else 1
  82. self.set_etcd(list(self.hosts.keys())[:etcd_hosts_count])
  83. if len(self.hosts) >= SCALE_THRESHOLD:
  84. self.set_kube_master(list(self.hosts.keys())[
  85. etcd_hosts_count:(etcd_hosts_count + KUBE_MASTERS)])
  86. else:
  87. self.set_kube_master(list(self.hosts.keys())[:KUBE_MASTERS])
  88. self.set_kube_node(self.hosts.keys())
  89. if len(self.hosts) >= SCALE_THRESHOLD:
  90. self.set_calico_rr(list(self.hosts.keys())[:etcd_hosts_count])
  91. else: # Show help if no options
  92. self.show_help()
  93. sys.exit(0)
  94. self.write_config(self.config_file)
  95. def write_config(self, config_file):
  96. if config_file:
  97. with open(self.config_file, 'w') as f:
  98. yaml.dump(self.yaml_config, f)
  99. else:
  100. print("WARNING: Unable to save config. Make sure you set "
  101. "CONFIG_FILE env var.")
  102. def debug(self, msg):
  103. if DEBUG:
  104. print("DEBUG: {0}".format(msg))
  105. def get_ip_from_opts(self, optstring):
  106. if 'ip' in optstring:
  107. return optstring['ip']
  108. else:
  109. raise ValueError("IP parameter not found in options")
  110. def ensure_required_groups(self, groups):
  111. for group in groups:
  112. if group == 'all':
  113. self.debug("Adding group {0}".format(group))
  114. if group not in self.yaml_config:
  115. all_dict = OrderedDict([('hosts', OrderedDict({})),
  116. ('children', OrderedDict({}))])
  117. self.yaml_config = {'all': all_dict}
  118. else:
  119. self.debug("Adding group {0}".format(group))
  120. if group not in self.yaml_config['all']['children']:
  121. self.yaml_config['all']['children'][group] = {'hosts': {}}
  122. def get_host_id(self, host):
  123. '''Returns integer host ID (without padding) from a given hostname.'''
  124. try:
  125. short_hostname = host.split('.')[0]
  126. return int(re.findall("\\d+$", short_hostname)[-1])
  127. except IndexError:
  128. raise ValueError("Host name must end in an integer")
  129. def build_hostnames(self, changed_hosts):
  130. existing_hosts = OrderedDict()
  131. highest_host_id = 0
  132. try:
  133. for host in self.yaml_config['all']['hosts']:
  134. existing_hosts[host] = self.yaml_config['all']['hosts'][host]
  135. host_id = self.get_host_id(host)
  136. if host_id > highest_host_id:
  137. highest_host_id = host_id
  138. except Exception:
  139. pass
  140. # FIXME(mattymo): Fix condition where delete then add reuses highest id
  141. next_host_id = highest_host_id + 1
  142. all_hosts = existing_hosts.copy()
  143. for host in changed_hosts:
  144. if host[0] == "-":
  145. realhost = host[1:]
  146. if self.exists_hostname(all_hosts, realhost):
  147. self.debug("Marked {0} for deletion.".format(realhost))
  148. all_hosts.pop(realhost)
  149. elif self.exists_ip(all_hosts, realhost):
  150. self.debug("Marked {0} for deletion.".format(realhost))
  151. self.delete_host_by_ip(all_hosts, realhost)
  152. elif host[0].isdigit():
  153. if ',' in host:
  154. ip, access_ip = host.split(',')
  155. else:
  156. ip = host
  157. access_ip = host
  158. if self.exists_hostname(all_hosts, host):
  159. self.debug("Skipping existing host {0}.".format(host))
  160. continue
  161. elif self.exists_ip(all_hosts, ip):
  162. self.debug("Skipping existing host {0}.".format(ip))
  163. continue
  164. next_host = "{0}{1}".format(HOST_PREFIX, next_host_id)
  165. next_host_id += 1
  166. all_hosts[next_host] = {'ansible_host': access_ip,
  167. 'ip': ip,
  168. 'access_ip': access_ip}
  169. elif host[0].isalpha():
  170. raise Exception("Adding hosts by hostname is not supported.")
  171. return all_hosts
  172. def range2ips(self, hosts):
  173. reworked_hosts = []
  174. def ips(start_address, end_address):
  175. try:
  176. # Python 3.x
  177. start = int(ip_address(start_address))
  178. end = int(ip_address(end_address))
  179. except:
  180. # Python 2.7
  181. start = int(ip_address(unicode(start_address)))
  182. end = int(ip_address(unicode(end_address)))
  183. return [ip_address(ip).exploded for ip in range(start, end + 1)]
  184. for host in hosts:
  185. if '-' in host and not host.startswith('-'):
  186. start, end = host.strip().split('-')
  187. try:
  188. reworked_hosts.extend(ips(start, end))
  189. except ValueError:
  190. raise Exception("Range of ip_addresses isn't valid")
  191. else:
  192. reworked_hosts.append(host)
  193. return reworked_hosts
  194. def exists_hostname(self, existing_hosts, hostname):
  195. return hostname in existing_hosts.keys()
  196. def exists_ip(self, existing_hosts, ip):
  197. for host_opts in existing_hosts.values():
  198. if ip == self.get_ip_from_opts(host_opts):
  199. return True
  200. return False
  201. def delete_host_by_ip(self, existing_hosts, ip):
  202. for hostname, host_opts in existing_hosts.items():
  203. if ip == self.get_ip_from_opts(host_opts):
  204. del existing_hosts[hostname]
  205. return
  206. raise ValueError("Unable to find host by IP: {0}".format(ip))
  207. def purge_invalid_hosts(self, hostnames, protected_names=[]):
  208. for role in self.yaml_config['all']['children']:
  209. if role != 'k8s-cluster' and self.yaml_config['all']['children'][role]['hosts']: # noqa
  210. all_hosts = self.yaml_config['all']['children'][role]['hosts'].copy() # noqa
  211. for host in all_hosts.keys():
  212. if host not in hostnames and host not in protected_names:
  213. self.debug(
  214. "Host {0} removed from role {1}".format(host, role)) # noqa
  215. del self.yaml_config['all']['children'][role]['hosts'][host] # noqa
  216. # purge from all
  217. if self.yaml_config['all']['hosts']:
  218. all_hosts = self.yaml_config['all']['hosts'].copy()
  219. for host in all_hosts.keys():
  220. if host not in hostnames and host not in protected_names:
  221. self.debug("Host {0} removed from role all".format(host))
  222. del self.yaml_config['all']['hosts'][host]
  223. def add_host_to_group(self, group, host, opts=""):
  224. self.debug("adding host {0} to group {1}".format(host, group))
  225. if group == 'all':
  226. if self.yaml_config['all']['hosts'] is None:
  227. self.yaml_config['all']['hosts'] = {host: None}
  228. self.yaml_config['all']['hosts'][host] = opts
  229. elif group != 'k8s-cluster:children':
  230. if self.yaml_config['all']['children'][group]['hosts'] is None:
  231. self.yaml_config['all']['children'][group]['hosts'] = {
  232. host: None}
  233. else:
  234. self.yaml_config['all']['children'][group]['hosts'][host] = None # noqa
  235. def set_kube_master(self, hosts):
  236. for host in hosts:
  237. self.add_host_to_group('kube-master', host)
  238. def set_all(self, hosts):
  239. for host, opts in hosts.items():
  240. self.add_host_to_group('all', host, opts)
  241. def set_k8s_cluster(self):
  242. k8s_cluster = {'children': {'kube-master': None, 'kube-node': None}}
  243. self.yaml_config['all']['children']['k8s-cluster'] = k8s_cluster
  244. def set_calico_rr(self, hosts):
  245. for host in hosts:
  246. if host in self.yaml_config['all']['children']['kube-master']:
  247. self.debug("Not adding {0} to calico-rr group because it "
  248. "conflicts with kube-master group".format(host))
  249. continue
  250. if host in self.yaml_config['all']['children']['kube-node']:
  251. self.debug("Not adding {0} to calico-rr group because it "
  252. "conflicts with kube-node group".format(host))
  253. continue
  254. self.add_host_to_group('calico-rr', host)
  255. def set_kube_node(self, hosts):
  256. for host in hosts:
  257. if len(self.yaml_config['all']['hosts']) >= SCALE_THRESHOLD:
  258. if host in self.yaml_config['all']['children']['etcd']['hosts']: # noqa
  259. self.debug("Not adding {0} to kube-node group because of "
  260. "scale deployment and host is in etcd "
  261. "group.".format(host))
  262. continue
  263. if len(self.yaml_config['all']['hosts']) >= MASSIVE_SCALE_THRESHOLD: # noqa
  264. if host in self.yaml_config['all']['children']['kube-master']['hosts']: # noqa
  265. self.debug("Not adding {0} to kube-node group because of "
  266. "scale deployment and host is in kube-master "
  267. "group.".format(host))
  268. continue
  269. self.add_host_to_group('kube-node', host)
  270. def set_etcd(self, hosts):
  271. for host in hosts:
  272. self.add_host_to_group('etcd', host)
  273. def load_file(self, files=None):
  274. '''Directly loads JSON to inventory.'''
  275. if not files:
  276. raise Exception("No input file specified.")
  277. import json
  278. for filename in list(files):
  279. # Try JSON
  280. try:
  281. with open(filename, 'r') as f:
  282. data = json.load(f)
  283. except ValueError:
  284. raise Exception("Cannot read %s as JSON, or CSV", filename)
  285. self.ensure_required_groups(ROLES)
  286. self.set_k8s_cluster()
  287. for group, hosts in data.items():
  288. self.ensure_required_groups([group])
  289. for host, opts in hosts.items():
  290. optstring = {'ansible_host': opts['ip'],
  291. 'ip': opts['ip'],
  292. 'access_ip': opts['ip']}
  293. self.add_host_to_group('all', host, optstring)
  294. self.add_host_to_group(group, host)
  295. self.write_config(self.config_file)
  296. def parse_command(self, command, args=None):
  297. if command == 'help':
  298. self.show_help()
  299. elif command == 'print_cfg':
  300. self.print_config()
  301. elif command == 'print_ips':
  302. self.print_ips()
  303. elif command == 'print_hostnames':
  304. self.print_hostnames()
  305. elif command == 'load':
  306. self.load_file(args)
  307. else:
  308. raise Exception("Invalid command specified.")
  309. def show_help(self):
  310. help_text = '''Usage: inventory.py ip1 [ip2 ...]
  311. Examples: inventory.py 10.10.1.3 10.10.1.4 10.10.1.5
  312. Available commands:
  313. help - Display this message
  314. print_cfg - Write inventory file to stdout
  315. print_ips - Write a space-delimited list of IPs from "all" group
  316. print_hostnames - Write a space-delimited list of Hostnames from "all" group
  317. Advanced usage:
  318. Add another host after initial creation: inventory.py 10.10.1.5
  319. Add range of hosts: inventory.py 10.10.1.3-10.10.1.5
  320. Add hosts with different ip and access ip: inventory.py 10.0.0.1,192.168.10.1 10.0.0.2,192.168.10.2 10.0.0.3,192.168.10.3
  321. Delete a host: inventory.py -10.10.1.3
  322. Delete a host by id: inventory.py -node1
  323. Configurable env vars:
  324. DEBUG Enable debug printing. Default: True
  325. CONFIG_FILE File to write config to Default: ./inventory/sample/hosts.yaml
  326. HOST_PREFIX Host prefix for generated hosts. Default: node
  327. SCALE_THRESHOLD Separate ETCD role if # of nodes >= 50
  328. MASSIVE_SCALE_THRESHOLD Separate K8s master and ETCD if # of nodes >= 200
  329. ''' # noqa
  330. print(help_text)
  331. def print_config(self):
  332. yaml.dump(self.yaml_config, sys.stdout)
  333. def print_hostnames(self):
  334. print(' '.join(self.yaml_config['all']['hosts'].keys()))
  335. def print_ips(self):
  336. ips = []
  337. for host, opts in self.yaml_config['all']['hosts'].items():
  338. ips.append(self.get_ip_from_opts(opts))
  339. print(' '.join(ips))
  340. def main(argv=None):
  341. if not argv:
  342. argv = sys.argv[1:]
  343. KubesprayInventory(argv, CONFIG_FILE)
  344. if __name__ == "__main__":
  345. sys.exit(main())