2017-05-19 05:52:44 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2018-10-17 14:53:39 +08:00
|
|
|
from __future__ import print_function
|
2017-05-19 05:52:44 +08:00
|
|
|
import boto3
|
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
|
|
|
|
class SearchEC2Tags(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.parse_args()
|
|
|
|
if self.args.list:
|
|
|
|
self.search_tags()
|
|
|
|
if self.args.host:
|
|
|
|
data = {}
|
2018-10-17 14:53:39 +08:00
|
|
|
print(json.dumps(data, indent=2))
|
2017-05-19 05:52:44 +08:00
|
|
|
|
|
|
|
def parse_args(self):
|
|
|
|
|
|
|
|
##Check if VPC_VISIBILITY is set, if not default to private
|
|
|
|
if "VPC_VISIBILITY" in os.environ:
|
|
|
|
self.vpc_visibility = os.environ['VPC_VISIBILITY']
|
|
|
|
else:
|
|
|
|
self.vpc_visibility = "private"
|
|
|
|
|
|
|
|
##Support --list and --host flags. We largely ignore the host one.
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--list', action='store_true', default=False, help='List instances')
|
|
|
|
parser.add_argument('--host', action='store_true', help='Get all the variables about a specific instance')
|
|
|
|
self.args = parser.parse_args()
|
|
|
|
|
|
|
|
def search_tags(self):
|
|
|
|
hosts = {}
|
|
|
|
hosts['_meta'] = { 'hostvars': {} }
|
|
|
|
|
2017-06-17 01:25:46 +08:00
|
|
|
##Search ec2 three times to find nodes of each group type. Relies on kubespray-role key/value.
|
2017-05-19 05:52:44 +08:00
|
|
|
for group in ["kube-master", "kube-node", "etcd"]:
|
|
|
|
hosts[group] = []
|
2017-06-17 01:25:46 +08:00
|
|
|
tag_key = "kubespray-role"
|
2017-05-19 05:52:44 +08:00
|
|
|
tag_value = ["*"+group+"*"]
|
|
|
|
region = os.environ['REGION']
|
|
|
|
|
|
|
|
ec2 = boto3.resource('ec2', region)
|
|
|
|
|
|
|
|
instances = ec2.instances.filter(Filters=[{'Name': 'tag:'+tag_key, 'Values': tag_value}, {'Name': 'instance-state-name', 'Values': ['running']}])
|
|
|
|
for instance in instances:
|
2018-11-14 16:21:56 +08:00
|
|
|
node_labels_tag = list(filter(lambda t: t['Key'] == 'kubespray-node-labels', instance.tags))
|
|
|
|
node_labels = ''
|
|
|
|
if node_labels_tag:
|
|
|
|
node_labels = dict([ label.strip().split('=') for label in node_labels_tag[0]['Value'].split(',') ])
|
|
|
|
|
2017-05-19 05:52:44 +08:00
|
|
|
if self.vpc_visibility == "public":
|
|
|
|
hosts[group].append(instance.public_dns_name)
|
|
|
|
hosts['_meta']['hostvars'][instance.public_dns_name] = {
|
2018-11-14 16:21:56 +08:00
|
|
|
'ansible_ssh_host': instance.public_ip_address,
|
|
|
|
'node_labels': node_labels
|
2017-05-19 05:52:44 +08:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
hosts[group].append(instance.private_dns_name)
|
|
|
|
hosts['_meta']['hostvars'][instance.private_dns_name] = {
|
2018-11-14 16:21:56 +08:00
|
|
|
'ansible_ssh_host': instance.private_ip_address,
|
|
|
|
'node_labels': node_labels
|
2017-05-19 05:52:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hosts['k8s-cluster'] = {'children':['kube-master', 'kube-node']}
|
2018-10-17 14:53:39 +08:00
|
|
|
print(json.dumps(hosts, sort_keys=True, indent=2))
|
2017-05-19 05:52:44 +08:00
|
|
|
|
|
|
|
SearchEC2Tags()
|