Skip to content

Sourcery refactored main branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions ansible/plugins/inventory/nodejs_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ def merge(a, b, path=None):
merge(a[key], b[key], path + [str(key)])
elif isinstance(a[key], list) and isinstance(b[key], list):
a[key] = sorted(set(a[key]).union(b[key]))
elif a[key] == b[key]:
pass # same leaf value
else:
raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
elif a[key] != b[key]:
raise Exception(f"Conflict at {'.'.join(path + [str(key)])}")
Comment on lines -107 to +108
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function merge refactored with the following changes:

This removes the following comments ( why? ):

# same leaf value

else:
a[key] = b[key]
return a
Expand All @@ -127,7 +125,10 @@ def check_decrypt_tool():
except OSError as e:
print(e, file=sys.stderr)

print("WARNING: cannot find or use %s executable" % DECRYPT_TOOL, file=sys.stderr)
print(
f"WARNING: cannot find or use {DECRYPT_TOOL} executable",
file=sys.stderr,
)
Comment on lines -130 to +131
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function check_decrypt_tool refactored with the following changes:

return False


Expand All @@ -139,10 +140,9 @@ def get_secrets_path():
path = os.path.realpath(path)
if os.path.isdir(path):
return path
else:
print("WARNING: NODE_BUILD_SECRETS defined but not a directory", file=sys.stderr)
print("It must be the path to a local checkout of https://github.com/nodejs-private/secrets", file=sys.stderr)
return None
print("WARNING: NODE_BUILD_SECRETS defined but not a directory", file=sys.stderr)
print("It must be the path to a local checkout of https://github.com/nodejs-private/secrets", file=sys.stderr)
return None
Comment on lines -142 to +145
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_secrets_path refactored with the following changes:


path = os.path.realpath('../secrets/build/')
if os.path.isdir(path):
Expand Down Expand Up @@ -197,7 +197,7 @@ def load_yaml_secrets(file_name):
p = subprocess.Popen([DECRYPT_TOOL, "-q", "--decrypt", file_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, _) = p.communicate()
if p.returncode != 0:
print("WARNING: cannot load %s" % file_name, file=sys.stderr)
print(f"WARNING: cannot load {file_name}", file=sys.stderr)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function load_yaml_secrets refactored with the following changes:

return None

return yaml.safe_load(stdout)
Expand All @@ -210,10 +210,8 @@ def parse_yaml(hosts, config):

for host_types in hosts['hosts']:
for host_type, providers in host_types.items():
export[host_type] = {}
export[host_type]['hosts'] = []

key = '~/.ssh/nodejs_build_%s' % host_type
export[host_type] = {'hosts': []}
key = f'~/.ssh/nodejs_build_{host_type}'
Comment on lines -213 to +214
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_yaml refactored with the following changes:

export[host_type]['vars'] = {
'ansible_ssh_private_key_file': key
}
Expand All @@ -225,8 +223,7 @@ def parse_yaml(hosts, config):
# some hosts have metadata appended to provider
# which requires underscore
delimiter = "_" if host.count('-') == 3 else "-"
hostname = '{}-{}{}{}'.format(host_type, provider_name,
delimiter, host)
hostname = f'{host_type}-{provider_name}{delimiter}{host}'

export[host_type]['hosts'].append(hostname)

Expand All @@ -235,28 +232,28 @@ def parse_yaml(hosts, config):
try:
parsed_host = parse_host(hostname)
for k, v in parsed_host.items():
hostvars.update({k: v[0] if type(v) is dict else v})
hostvars[k] = v[0] if type(v) is dict else v
except Exception as e:
raise Exception('Failed to parse host: %s' % e)
raise Exception(f'Failed to parse host: {e}')

if 'ip' in metadata:
hostvars.update({'ansible_host': metadata['ip']})
hostvars['ansible_host'] = metadata['ip']
del metadata['ip']

if 'port' in metadata:
hostvars.update({'ansible_port': str(metadata['port'])})
hostvars['ansible_port'] = str(metadata['port'])
del metadata['port']

if 'user' in metadata:
hostvars.update({'ansible_user': metadata['user']})
hostvars.update({'ansible_become': True})
hostvars['ansible_user'] = metadata['user']
hostvars['ansible_become'] = True
del metadata['user']

if 'password' in metadata:
hostvars.update({'ansible_password': str(metadata['password'])})
hostvars['ansible_password'] = str(metadata['password'])
del metadata['password']

hostvars.update(metadata)
hostvars |= metadata

# add specific options from config
for option in filter(lambda s: s.startswith('hosts:'),
Expand All @@ -265,7 +262,7 @@ def parse_yaml(hosts, config):
if option[6:] in hostname:
for o in config.items(option):
# configparser returns tuples of key, value
hostvars.update({o[0]: o[1]})
hostvars[o[0]] = o[1]

export['_meta']['hostvars'][hostname] = {}
export['_meta']['hostvars'][hostname].update(hostvars)
Expand All @@ -278,20 +275,17 @@ def parse_yaml(hosts, config):
def parse_host(host):
"""Parses a host and validates it against our naming conventions"""

hostinfo = dict()
info = host.split('-')

expected = ['type', 'provider', 'os', 'arch', 'uid']

if len(info) != 5:
raise Exception('Host format is invalid: %s,' % host)

for key, item in enumerate(expected):
hostinfo[item] = has_metadata(info[key])
raise Exception(f'Host format is invalid: {host},')

hostinfo = {item: has_metadata(info[key]) for key, item in enumerate(expected)}
for item in ['type', 'provider', 'arch']:
if hostinfo[item] not in valid[item]:
raise Exception('Invalid %s: %s' % (item, hostinfo[item]))
raise Exception(f'Invalid {item}: {hostinfo[item]}')
Comment on lines -281 to +288
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_host refactored with the following changes:


return hostinfo

Expand Down
4 changes: 2 additions & 2 deletions ansible/plugins/library/remmina_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def main():

password = remmina_encrypt(remmina_secret, metadata['ansible_password'])
rendered = render_template(hostname, metadata, password)
filename = os.path.join(target_dir, '%s.remmina' % hostname)
filename = os.path.join(target_dir, f'{hostname}.remmina')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:


try:
with open(filename, 'w+') as f:
Expand All @@ -145,7 +145,7 @@ def main():

os.chmod(filename, 0o600)

module.exit_json(changed=True, meta=('Updated %s successfully' % target_dir))
module.exit_json(changed=True, meta=f'Updated {target_dir} successfully')


if __name__ == '__main__':
Expand Down
8 changes: 3 additions & 5 deletions ansible/plugins/library/ssh_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,8 @@ def main():
if not is_templatable(path, contents):
module.fail_json(msg='Your ssh config lacks template stubs. Check README.md for instructions.')

rendered = '{}{}{}'.format(
pre_match,
render_template(module.params['hostinfo']),
post_match
rendered = (
f"{pre_match}{render_template(module.params['hostinfo'])}{post_match}"
Comment on lines -106 to +107
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

)

try:
Expand All @@ -116,7 +114,7 @@ def main():
except IOError:
module.fail_json(msg='Couldn\'t write to ssh config. Check permissions')

module.exit_json(changed=True, meta='Updated %s successfully' % path)
module.exit_json(changed=True, meta=f'Updated {path} successfully')


if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions ansible/www-standalone/tools/metrics/country-lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import geoip2.database
import os

reader = geoip2.database.Reader(os.path.dirname(os.path.realpath(__file__)) + '/GeoLite2-City.mmdb')
reader = geoip2.database.Reader(
f'{os.path.dirname(os.path.realpath(__file__))}/GeoLite2-City.mmdb')
Comment on lines -8 to +9
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 8-26 refactored with the following changes:


logFileWriter = csv.writer(sys.stdout, quoting=csv.QUOTE_MINIMAL)
logFileReader = csv.reader(sys.stdin)
Expand All @@ -22,8 +23,7 @@
region = ""

try:
georec = reader.city(row.pop(0))
if georec:
if georec := reader.city(row.pop(0)):
if georec.country.iso_code:
country = georec.country.iso_code
if georec.subdivisions.most_specific.iso_code:
Expand Down