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.

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