self-hosting/scripts/scw_inventory.py

111 lines
3.4 KiB
Python
Raw Normal View History

2018-07-30 19:42:48 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2018-09-20 02:18:13 +02:00
"""
2018-07-30 19:42:48 +02:00
Generate an inventory of servers from scaleway which is
suitable for use as an ansible dynamic inventory
Right now, only the group 'cci-customer' is exported
2018-09-20 02:18:13 +02:00
"""
2018-07-30 19:42:48 +02:00
import configparser
2018-09-20 02:18:13 +02:00
import json
import os
from typing import Dict, Any
2018-07-30 19:42:48 +02:00
from scaleway.apis import ComputeAPI
2018-09-20 02:18:13 +02:00
2018-07-30 19:42:48 +02:00
class SCWInventory(object):
2018-09-20 02:18:13 +02:00
"""
2018-07-30 19:42:48 +02:00
The inventory class which calls out to scaleway and digests
2018-09-20 02:18:13 +02:00
The returned data, making it usable by ansible as an inventory
"""
response: Dict[str, Any]
2018-09-26 04:40:24 +02:00
2018-07-30 19:42:48 +02:00
def __init__(self):
self.inventory = None
self.auth_token = None
self.environment = None
self.response = {
'_meta': {
'hostvars': {
}
}
}
def parse_config(self, creds_file='scw.ini'):
2018-09-20 02:18:13 +02:00
"""
2018-07-30 19:42:48 +02:00
Parse the ini file to get the auth token
2018-09-20 02:18:13 +02:00
"""
2018-07-30 19:42:48 +02:00
config = configparser.ConfigParser()
config.read(creds_file)
with open(os.path.expanduser(config['credentials']['token_file']), 'r') as content_file:
self.auth_token = content_file.read().replace('\n', '')
self.environment = config['config']['environment']
def get_servers(self):
2018-09-20 02:18:13 +02:00
"""
Query scaleway api and pull down a list of servers
"""
2018-07-30 19:42:48 +02:00
self.parse_config()
api_par1 = ComputeAPI(auth_token=self.auth_token, region='par1')
api_ams1 = ComputeAPI(auth_token=self.auth_token, region='ams1')
result_par1 = api_par1.query().servers.get()
result_ams1 = api_ams1.query().servers.get()
self.inventory = [
2018-09-20 02:18:13 +02:00
[i['name'], i['public_ip'], i['tags'], i['private_ip']] for i in
result_par1['servers'] + result_ams1['servers']
2018-07-30 19:42:48 +02:00
]
for host, ip_info, tags, private_ip in self.inventory:
host_vars = {
'private_ip': private_ip,
'ansible_python_interpreter': '/usr/bin/python3'
}
if ip_info:
host_vars['ansible_host'] = ip_info['address']
host_vars['public_ip'] = ip_info['address']
else:
host_vars['ansible_host'] = private_ip
self.response['_meta']['hostvars'][host] = host_vars
if tags:
for tag in tags:
self._add_to_response(
tag,
host
)
2018-07-30 19:42:48 +02:00
for host, variables in self.response['_meta']['hostvars'].items():
if host != 'proxy1':
2018-09-20 02:18:13 +02:00
variables['ansible_ssh_common_args'] = '-o ProxyCommand="ssh -W %h:%p -q root@' + \
2018-09-26 04:40:24 +02:00
self.response['_meta']['hostvars']['proxy1'][
'public_ip'] + ' -o StrictHostKeyChecking=no"'
2018-07-30 19:42:48 +02:00
def _add_to_response(self, group, hostname):
2018-09-20 02:18:13 +02:00
"""
Add a host to a group within the response
"""
2018-07-30 19:42:48 +02:00
if group not in self.response:
self.response[group] = list()
if group in self.response:
self.response[group].append(hostname)
def print_inventory(self):
2018-09-20 02:18:13 +02:00
"""
Simply display the collected inventory
"""
2018-07-30 19:42:48 +02:00
print(json.dumps(self.response))
2018-09-20 02:18:13 +02:00
2018-07-30 19:42:48 +02:00
def main():
2018-09-20 02:18:13 +02:00
"""
Run the program starting here
"""
2018-07-30 19:42:48 +02:00
inventory = SCWInventory()
inventory.get_servers()
inventory.print_inventory()
2018-09-20 02:18:13 +02:00
2018-07-30 19:42:48 +02:00
if __name__ == '__main__':
main()