Cleanup Scaleway inventory script

This commit is contained in:
Paul-Henri Froidmont 2018-09-20 02:18:13 +02:00
parent 7e1e5f9c91
commit 5acc7652a9

View file

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