2014-10-14 11:29:21 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
fleetctl base external inventory script. Automatically finds the IPs of the booted coreos instances and
|
|
|
|
returns it under the host group 'coreos'
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Copyright (C) 2014 Andrew Rothstein <andrew.rothstein at gmail.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#
|
|
|
|
# Thanks to the vagrant.py inventory script for giving me the basic structure
|
|
|
|
# of this.
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import re
|
|
|
|
import string
|
|
|
|
from optparse import OptionParser
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except:
|
|
|
|
import simplejson as json
|
|
|
|
|
|
|
|
# Options
|
2017-05-09 21:38:08 +00:00
|
|
|
# ------------------------------
|
2014-10-14 11:29:21 +00:00
|
|
|
|
|
|
|
parser = OptionParser(usage="%prog [options] --list | --host <machine>")
|
|
|
|
parser.add_option('--list', default=False, dest="list", action="store_true",
|
2014-11-28 05:00:35 +00:00
|
|
|
help="Produce a JSON consumable grouping of servers in your fleet")
|
2014-10-14 11:29:21 +00:00
|
|
|
parser.add_option('--host', default=None, dest="host",
|
|
|
|
help="Generate additional host specific details for given host for Ansible")
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
#
|
|
|
|
# helper functions
|
|
|
|
#
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-11-28 05:00:35 +00:00
|
|
|
def get_ssh_config():
|
2014-10-14 11:29:21 +00:00
|
|
|
configs = []
|
2014-11-28 05:00:35 +00:00
|
|
|
for box in list_running_boxes():
|
2014-10-14 11:29:21 +00:00
|
|
|
config = get_a_ssh_config(box)
|
|
|
|
configs.append(config)
|
|
|
|
return configs
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
|
|
|
# list all the running instances in the fleet
|
2014-10-14 11:29:21 +00:00
|
|
|
def list_running_boxes():
|
|
|
|
boxes = []
|
2014-11-28 05:00:35 +00:00
|
|
|
for line in subprocess.check_output(["fleetctl", "list-machines"]).split('\n'):
|
2017-11-21 03:08:30 +00:00
|
|
|
matcher = re.search(r"[^\s]+[\s]+([^\s]+).+", line)
|
2014-10-14 11:29:21 +00:00
|
|
|
if matcher and matcher.group(1) != "IP":
|
|
|
|
boxes.append(matcher.group(1))
|
|
|
|
|
|
|
|
return boxes
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-11-28 05:00:35 +00:00
|
|
|
def get_a_ssh_config(box_name):
|
2014-10-14 11:29:21 +00:00
|
|
|
config = {}
|
|
|
|
config['Host'] = box_name
|
|
|
|
config['ansible_ssh_user'] = 'core'
|
|
|
|
config['ansible_python_interpreter'] = '/opt/bin/python'
|
|
|
|
return config
|
|
|
|
|
2017-05-09 21:38:08 +00:00
|
|
|
|
2014-10-14 11:29:21 +00:00
|
|
|
# List out servers that vagrant has running
|
2017-05-09 21:38:08 +00:00
|
|
|
# ------------------------------
|
2014-10-14 11:29:21 +00:00
|
|
|
if options.list:
|
|
|
|
ssh_config = get_ssh_config()
|
2017-05-09 21:38:08 +00:00
|
|
|
hosts = {'coreos': []}
|
2017-01-27 23:45:23 +00:00
|
|
|
|
2014-11-28 05:00:35 +00:00
|
|
|
for data in ssh_config:
|
2014-10-14 11:29:21 +00:00
|
|
|
hosts['coreos'].append(data['Host'])
|
|
|
|
|
2015-08-28 06:18:13 +00:00
|
|
|
print(json.dumps(hosts))
|
2014-10-14 11:29:21 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Get out the host details
|
2017-05-09 21:38:08 +00:00
|
|
|
# ------------------------------
|
2014-10-14 11:29:21 +00:00
|
|
|
elif options.host:
|
|
|
|
result = {}
|
|
|
|
ssh_config = get_ssh_config()
|
|
|
|
|
|
|
|
details = filter(lambda x: (x['Host'] == options.host), ssh_config)
|
|
|
|
if len(details) > 0:
|
2017-05-09 21:38:08 +00:00
|
|
|
# pass through the port, in case it's non standard.
|
2014-10-14 11:29:21 +00:00
|
|
|
result = details[0]
|
|
|
|
|
2015-08-28 06:18:13 +00:00
|
|
|
print(json.dumps(result))
|
2014-10-14 11:29:21 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
# Print out help
|
2017-05-09 21:38:08 +00:00
|
|
|
# ------------------------------
|
2014-10-14 11:29:21 +00:00
|
|
|
else:
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(1)
|