Skip to content

Commit

Permalink
fix: drop python 2 support, update libs, fix some linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
PopoSensei committed May 17, 2021
1 parent 8378443 commit 87fc0d0
Show file tree
Hide file tree
Showing 17 changed files with 441 additions and 339 deletions.
21 changes: 18 additions & 3 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
[flake8]
ignore = D100, # missing docstring in public module
D104,
D105,
D200, # one line docstring should fit on one line with quotes
D202,
D203, # 1 blank line required before class docstring
D205, # blank line required after description (in docstrings)
D400,
D401,
D403,
# ignore line length problems; those are fixed by black
E501
# via black
E203
H101,
H238,
H301,
H304,
H306,
H404,
H405,
E114 # Ignore line length for comments,
F403 # Import rule, exception for ansible boilerplate,
F405 # Import rule, exception for ansible boilerplate
E114, # Ignore line length for comments,
F403, # Import rule, exception for ansible boilerplate,
F405, # Import rule, exception for ansible boilerplate
W503

exclude =
# No need to traverse our git directory
.git,
Expand All @@ -30,3 +42,6 @@ per-file-ignores =
modules/upcloud_firewall.py: E501, E402
modules/upcloud_tag.py: E501, E402
modules/upcloud.py: E501, E402
test/*: D101,D102,D103,F841,S101,S106,B011
# ignore "imported but not used" in any __init__.pys
*/__init__.py: F401
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ ENV/
.tox
*.pyc
.pytest_cache
.vscode/
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.13
3.6.10
103 changes: 66 additions & 37 deletions inventory/upcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@
import os
import sys
import argparse
try:
import configparser
except ImportError:
from six.moves import configparser
from six.moves import configparser


try:
Expand All @@ -94,15 +91,15 @@ def get_hostname_or_ip(server, get_ip_address, get_non_fqdn_name, addr_family):
if get_ip_address:
# prevent API request during get_public_ip, as IPs were matched manually
# bypass server.__setattr__ as setting populated is not normallow allowed by the class
object.__setattr__(server, 'populated', True)
object.__setattr__(server, "populated", True)
public_ip_addresses = [server.get_public_ip(addr_family=addr_family)]
if len(public_ip_addresses) == 0:
public_ip_addresses = [server.get_public_ip()]
return public_ip_addresses

hostname = server.hostname.split('.')[0]
hostname = server.hostname.split(".")[0]
if get_non_fqdn_name and hostname != server.hostname:
return [server.hostname, server.hostname.split('.')[0]]
return [server.hostname, server.hostname.split(".")[0]]
return [server.hostname]


Expand All @@ -120,7 +117,7 @@ def assign_ips_to_servers(manager, servers):
servermap[server.uuid] = server

# bypass server.__setattr__ as it does not normally allow assigning ip_addresses manually
object.__setattr__(server, 'ip_addresses', [])
object.__setattr__(server, "ip_addresses", [])

# assign IPs to their corresponding server
ips = manager.get_ips(ignore_ips_without_server=True)
Expand All @@ -138,18 +135,20 @@ def list_servers(manager, get_ip_address, return_non_fqdn_names, default_ipv_ver
groups = dict()
groups["uc_all"] = []
for server in servers:
if server.state == 'started':
for hostname_or_ip in get_hostname_or_ip(server, get_ip_address, return_non_fqdn_names, default_ipv_version):
if server.state == "started":
for hostname_or_ip in get_hostname_or_ip(
server, get_ip_address, return_non_fqdn_names, default_ipv_version
):
groups["uc_all"].append(hostname_or_ip)

# group by tags
# group by tags
for tag in server.tags:
if tag not in groups:
groups[tag] = []
groups[tag].append(hostname_or_ip)

# group by zones
formatted_zone = server.zone.replace('-', '_')
# group by zones
formatted_zone = server.zone.replace("-", "_")
if formatted_zone not in groups:
groups[formatted_zone] = []
groups[formatted_zone].append(hostname_or_ip)
Expand All @@ -170,7 +169,7 @@ def namespace_fields(server):
"""Generate a JSON response to a --host call"""
namespaced_server_dict = {}
for key, value in server.to_dict().items():
namespaced_server_dict['uc_' + key] = value
namespaced_server_dict["uc_" + key] = value
return namespaced_server_dict

if with_ip_addresses:
Expand All @@ -184,8 +183,12 @@ def namespace_fields(server):

servers = manager.get_servers()
for server in servers:
server_name = server.hostname.split('.')[0]
if (return_non_fqdn_names and search_item == server_name) or server.hostname == search_item or server.uuid == search_item:
server_name = server.hostname.split(".")[0]
if (
(return_non_fqdn_names and search_item == server_name)
or server.hostname == search_item
or server.uuid == search_item
):
server.populate()
server_dict = namespace_fields(server)
print(json.dumps(server_dict))
Expand All @@ -197,11 +200,25 @@ def namespace_fields(server):

def read_cli_args():
"""Handle command line arguments"""
parser = argparse.ArgumentParser(description="Produce an Ansible Inventory from UpCloud's API")

parser.add_argument('--list', action='store_true', help='List all active servers as Ansible inventory (default: True)')
parser.add_argument('--host', action='store', help='Get all Ansible inventory variables about a specific server')
parser.add_argument('--return-ip-addresses', action='store_true', help='Return IP-addresses instead of hostnames with --list. Also configurable in upcloud.ini')
parser = argparse.ArgumentParser(
description="Produce an Ansible Inventory from UpCloud's API"
)

parser.add_argument(
"--list",
action="store_true",
help="List all active servers as Ansible inventory (default: True)",
)
parser.add_argument(
"--host",
action="store",
help="Get all Ansible inventory variables about a specific server",
)
parser.add_argument(
"--return-ip-addresses",
action="store_true",
help="Return IP-addresses instead of hostnames with --list. Also configurable in upcloud.ini",
)

args = parser.parse_args()

Expand All @@ -220,9 +237,11 @@ def read_api_credentials(config):
password = os.getenv("UPCLOUD_API_PASSWD")

if not (username and password):
if config.has_option('upcloud', 'UPCLOUD_API_USER') and config.has_option('upcloud', 'UPCLOUD_API_PASSWD'):
username = config.get('upcloud', 'UPCLOUD_API_USER')
password = config.get('upcloud', 'UPCLOUD_API_PASSWD')
if config.has_option("upcloud", "UPCLOUD_API_USER") and config.has_option(
"upcloud", "UPCLOUD_API_PASSWD"
):
username = config.get("upcloud", "UPCLOUD_API_USER")
password = config.get("upcloud", "UPCLOUD_API_PASSWD")

if not (username and password):
err_msg = "Please set UPCLOUD_API_USER and UPCLOUD_API_PASSWD as environment variables or at upcloud.ini"
Expand All @@ -233,7 +252,9 @@ def read_api_credentials(config):


def return_error_msg_due_to_faulty_ini_file(missing_variable):
err_msg = "Could not find {} variable in the ini file. Please check if the ini is configured correctly.".format(missing_variable)
err_msg = "Could not find {} variable in the ini file. Please check if the ini is configured correctly.".format(
missing_variable
)
sys.stderr.write(err_msg)
sys.exit(-1)

Expand All @@ -244,39 +265,47 @@ def return_error_msg_due_to_faulty_ini_file(missing_variable):
args = read_cli_args()

config = configparser.ConfigParser()
config.read(os.path.dirname(os.path.realpath(__file__)) + '/upcloud.ini')
config.read(os.path.dirname(os.path.realpath(__file__)) + "/upcloud.ini")

# setup API connection
username, password = read_api_credentials(config)
default_timeout = os.getenv('UPCLOUD_API_TIMEOUT') or config.get('upcloud', 'default_timeout')
default_timeout = os.getenv("UPCLOUD_API_TIMEOUT") or config.get(
"upcloud", "default_timeout"
)
if not default_timeout:
default_timeout = None
else:
default_timeout = float(default_timeout)
manager = upcloud_api.CloudManager(username, password, default_timeout)

# decide whether to return hostnames or ip_addresses
if config.has_option('upcloud', 'return_ip_addresses'):
with_ip_addresses = str(config.get('upcloud', 'return_ip_addresses')).lower() == "true"
if config.has_option("upcloud", "return_ip_addresses"):
with_ip_addresses = (
str(config.get("upcloud", "return_ip_addresses")).lower() == "true"
)
else:
return_error_msg_due_to_faulty_ini_file('return_ip_addresses')
return_error_msg_due_to_faulty_ini_file("return_ip_addresses")

if config.has_option('upcloud', 'return_non_fqdn_names'):
return_non_fqdn_names = str(config.get('upcloud', 'return_non_fqdn_names')).lower() == "true"
if config.has_option("upcloud", "return_non_fqdn_names"):
return_non_fqdn_names = (
str(config.get("upcloud", "return_non_fqdn_names")).lower() == "true"
)
else:
return_error_msg_due_to_faulty_ini_file('return_non_fqdn_names')
return_error_msg_due_to_faulty_ini_file("return_non_fqdn_names")

if config.has_option('upcloud', 'default_ipv_version'):
default_ipv_version = config.get('upcloud', 'default_ipv_version')
if config.has_option("upcloud", "default_ipv_version"):
default_ipv_version = config.get("upcloud", "default_ipv_version")
else:
return_error_msg_due_to_faulty_ini_file('default_ipv_version')
return_error_msg_due_to_faulty_ini_file("default_ipv_version")

if args.return_ip_addresses:
with_ip_addresses = True

# choose correct action
if args.list:
list_servers(manager, with_ip_addresses, return_non_fqdn_names, default_ipv_version)
list_servers(
manager, with_ip_addresses, return_non_fqdn_names, default_ipv_version
)

elif args.host:
get_server(manager, args.host, with_ip_addresses, return_non_fqdn_names)
Loading

0 comments on commit 87fc0d0

Please sign in to comment.