c12s-kubespray/nodes_to_inv.py

90 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# A simple dynamic replacemant of 'kargo prepare'
# Generates ansible inventory from a list of IPs in 'nodes' file.
import argparse
import json
2016-07-08 16:03:50 +00:00
import os
2016-07-08 15:45:02 +00:00
import yaml
def read_nodes_from_file(filename):
f = open(filename, 'r')
content = [x.strip('\n') for x in f.readlines()]
return content
2016-07-08 15:45:02 +00:00
def read_vars_from_file(src="/root/kargo/inventory/group_vars/all.yml"):
with open(src, 'r') as f:
content = yaml.load(f)
return content
2016-07-08 16:03:50 +00:00
def nodes_to_hash(nodes_list, masters, group_vars):
nodes = {
'all': {
'hosts': [],
2016-07-08 16:03:50 +00:00
'vars': group_vars
},
'etcd': {
'hosts': [],
},
'kube-master': {
'hosts': [],
},
'kube-node': {
'hosts': [],
},
'k8s-cluster': {
'children': ['kube-node', 'kube-master']
}
}
2016-07-08 15:22:52 +00:00
i = 1
for node_ip in nodes_list:
nodes['all']['hosts'].append("%s" % node_ip)
nodes['kube-node']['hosts'].append("%s" % node_ip)
if i <= masters:
nodes['kube-master']['hosts'].append("%s" % node_ip)
if i <= 3:
nodes['etcd']['hosts'].append("%s" % node_ip)
2016-07-08 15:22:52 +00:00
i += 1
return nodes
def main():
2016-07-08 16:03:50 +00:00
parser = argparse.ArgumentParser(description='Kargo inventory simulator')
parser.add_argument('--list', action='store_true')
parser.add_argument('--host', default=False)
args = parser.parse_args()
2016-07-08 16:03:50 +00:00
# Read params from ENV since ansible does not support passing args to dynamic inv scripts
if os.environ.get('K8S_NODES_FILE'):
nodes_file = os.environ['K8S_NODES_FILE']
else:
nodes_file = 'nodes'
if os.environ.get('K8S_MASTERS'):
masters = int(os.environ['K8S_MASTERS'])
else:
masters = 2
if os.environ.get('KARGO_GROUP_VARS'):
vars_file = os.environ['KARGO_GROUP_VARS']
else:
vars_file = "/root/kargo/inventory/group_vars/all.yml"
nodes_list = read_nodes_from_file(nodes_file)
if len(nodes_list) < 3:
print "Error: requires at least 3 nodes"
return
2016-07-08 16:03:50 +00:00
nodes = nodes_to_hash(nodes_list, masters, read_vars_from_file(vars_file))
if args.host:
print "{}"
else:
print json.dumps(nodes)
if __name__ == "__main__":
main()