Commit 79ddcc9e authored by ale's avatar ale

Make code compatible with both Python 2 and 3

Remove deprecated idioms such as dict.iteritems(), replacing it
with dict.items(), which is not very efficient on Python 2 but is
compatible with Python 3.
parent 40765a98
......@@ -30,7 +30,7 @@ def _host_addrs_by_domain(host_vars):
# Return a {domain: [addrs]} map of all addresses of this host by
# domain.
by_domain = {}
for key, value in host_vars.iteritems():
for key, value in host_vars.items():
if key == 'ip' or key == 'ip6':
by_domain.setdefault('public', []).append(value)
elif key.startswith('ip_'):
......@@ -92,7 +92,7 @@ def _service_addrs(hostname, service, service_hosts, host_vars, host_addrs_map):
def _host_addrs(hostname, host_addrs_map):
dns = []
for domain, addrs in host_addrs_map[hostname].iteritems():
for domain, addrs in host_addrs_map[hostname].items():
if domain == 'public':
# <hostname>.<domain> points at the public IP addresses.
dns.append((hostname, addrs))
......@@ -128,7 +128,7 @@ class ActionModule(ActionBase):
entries.extend(_host_addrs(host, host_addrs_map))
# Service-related entries, from the perspective of the current host.
for service, service_hosts in assignments.iteritems():
for service, service_hosts in assignments.items():
entries.extend(_service_addrs(
hostname, service, service_hosts, host_vars, host_addrs_map))
......
......@@ -48,19 +48,9 @@ from ansible.errors import AnsibleParserError
from ansible.plugins.inventory import BaseFileInventoryPlugin
from ansible.module_utils._text import to_native
# The basic working schema for this inventory type is:
#
# Point the inventory at a simple YAML file, containing two
# attributes:
#
# * services_file: path to services.yml, contains service metadata
# * hosts_file: path to hosts.yml, contains hosts and scheduling
# groups
#
def _merge_dict(src, dst):
for k, v in dst.iteritems():
for k, v in dst.items():
if k in src and isinstance(v, dict):
_merge_dict(src[k], v)
else:
......@@ -152,7 +142,7 @@ def _read_config(loader, cf_path):
def _make_services_list(services_dict):
out = []
for name, service in services_dict.iteritems():
for name, service in services_dict.items():
service['name'] = name
out.append(service)
return out
......@@ -160,7 +150,7 @@ def _make_services_list(services_dict):
def _make_groups(hosts):
groups = {'all': []}
for name, h in hosts.iteritems():
for name, h in hosts.items():
groups['all'].append(name)
for g in h.get('groups', []):
groups.setdefault(g, []).append(name)
......@@ -174,8 +164,8 @@ def _make_dynamic_groups(hosts):
# overlays defined.
dgroups = {}
overlay_hosts = set()
for name, h in hosts.iteritems():
for k, v in h.iteritems():
for name, h in hosts.items():
for k, v in h.items():
# net-overlay group autodetection.
if k.startswith('ip_'):
group_name = 'overlay-%s' % k[3:]
......@@ -189,7 +179,7 @@ def _make_dynamic_hostvars(hostvars):
# Create dynamic per-host variables (based on other host variables
# defined in the inventory).
dynamic_vars = {}
for k, v in hostvars.iteritems():
for k, v in hostvars.items():
if k.startswith('ip_'):
overlay_name = k[3:]
dynamic_vars.setdefault('host_net_overlays', []).append(overlay_name)
......@@ -198,7 +188,7 @@ def _make_dynamic_hostvars(hostvars):
def _is_attribute_set(services, attr):
# Return True if at least one of the services has 'attr' set.
for s in services.itervalues():
for s in services.values():
if s.get(attr):
return True
return False
......@@ -246,7 +236,7 @@ class InventoryModule(BaseFileInventoryPlugin):
hv.update(_make_dynamic_hostvars(conf['hosts'][h]))
host_vars[h] = hv
# Set variables etc. for assignments.
for service_name, hosts in assignments.iteritems():
for service_name, hosts in assignments.items():
service_name_var = service_name.replace('-', '_')
service_tag = 'enable_' + service_name_var
for h in all_hosts:
......@@ -292,23 +282,23 @@ class InventoryModule(BaseFileInventoryPlugin):
def _populate_inventory(self, inventory, assignments, host_vars, groups, group_vars):
# We have all the data now, let's tell Ansible.
# Set the host vars.
for h, hv in host_vars.iteritems():
for h, hv in host_vars.items():
inventory.add_host(h)
for k, v in hv.iteritems():
for k, v in hv.items():
inventory.set_variable(h, k, v)
# Create and populate the hostsfile-driven groups.
for group_name, hosts in groups.iteritems():
for group_name, hosts in groups.items():
inventory.add_group(group_name)
for h in hosts:
inventory.add_child(group_name, h)
# Create and populate the per-service dynamic groups.
for service_name, hosts in assignments.iteritems():
for service_name, hosts in assignments.items():
inventory.add_group(service_name)
for h in hosts:
inventory.add_child(service_name, h)
# Set group vars from the hostfile.
for g, gv in group_vars.iteritems():
for k, v in gv.iteritems():
for g, gv in group_vars.items():
for k, v in gv.items():
inventory.set_variable(g, k, v)
def _assign_services(self, services, groups):
......@@ -352,7 +342,7 @@ def predictable_random(*args):
invocation, as long as the arguments are identical.
"""
# Uses crc32(x) as seed.
return random.Random(crc32(','.join(args)))
return random.Random(crc32(','.join(args).encode()))
def binpack(hosts, occupation_map, n):
......
{%- for service_name, service in services.iteritems() %}
{%- for service_name, service in services.items() %}
{%- for ep in service.get('public_endpoints', []) %}
{%- for c in service.get('containers', []) %}
{%- for d in domain_public %}
......
......@@ -22,7 +22,7 @@
mx{{ loop.index }}: {{ hostvars[h]['ip'] }}
{% endfor %}
{% endif %}
{% for service_name, s in services.iteritems() %}
{% for service_name, s in services.items() %}
{% for pe in s.get('public_endpoints', []) %}
{{ pe.name }}: CNAME www
{% if pe.get('sharded') %}
......
HOSTNAME="{{ ansible_hostname }}"
PUBLIC_ADDR="{{ ansible_hostname }}"
USER="{{ container_user }}"
{% for k, v in container.get('env', {}).iteritems() %}
{% for k, v in container.get('env', {}).items() %}
{{ k }}="{{ v }}"
{% endfor %}
......@@ -8,7 +8,7 @@ TimeoutStartSec=0
EnvironmentFile=-/etc/default/{{ service.name }}-{{ container.name }}
ExecStartPre=/usr/bin/docker pull {{ container.image }}
# Minimum set: --cgroups name=systemd
ExecStart=/usr/bin/systemd-docker --env run --rm --name {{ service.name }}-{{ container.name }} {% if container.resources is defined %}{% if container.resources.ram is defined %}--memory={{ container.resources.ram }}{% endif %} {% if container.resources.cpu is defined %}--cpus={{ container.resources.cpu }}{% endif %}{% endif %} --network host {% if container.get('ports', []) %} {% for port in container.get('ports',[]) %}--expose {{ port }} {% endfor %} {% else %}--expose {{ container.port }}{% endif %} --mount type=bind,source=/dev/log,destination=/dev/log {% for creds in service.get('service_credentials', []) %} --mount type=bind,source=/etc/credentials/x509/{{ creds.name }},destination=/etc/credentials/x509/{{ creds.name }}{% endfor %} {% for mount in container.get('volumes', []) %} --mount type=bind{% for k, v in mount.iteritems() %},src={{ k }},dst={{ v }}{% endfor %}{% endfor %} {% if not container.get('root') %}--user {{ container_user_task.uid }}:{{ container_group_task.gid }}{% if service.get('credentials') %} --group-add {{ service.name }}-credentials{% endif %}{% endif %} {{ container.get('docker_options', '') }} {{ container.image }} {{ container.get('args', '') }}
ExecStart=/usr/bin/systemd-docker --env run --rm --name {{ service.name }}-{{ container.name }} {% if container.resources is defined %}{% if container.resources.ram is defined %}--memory={{ container.resources.ram }}{% endif %} {% if container.resources.cpu is defined %}--cpus={{ container.resources.cpu }}{% endif %}{% endif %} --network host {% if container.get('ports', []) %} {% for port in container.get('ports',[]) %}--expose {{ port }} {% endfor %} {% else %}--expose {{ container.port }}{% endif %} --mount type=bind,source=/dev/log,destination=/dev/log {% for creds in service.get('service_credentials', []) %} --mount type=bind,source=/etc/credentials/x509/{{ creds.name }},destination=/etc/credentials/x509/{{ creds.name }}{% endfor %} {% for mount in container.get('volumes', []) %} --mount type=bind{% for k, v in mount.items() %},src={{ k }},dst={{ v }}{% endfor %}{% endfor %} {% if not container.get('root') %}--user {{ container_user_task.uid }}:{{ container_group_task.gid }}{% if service.get('credentials') %} --group-add {{ service.name }}-credentials{% endif %}{% endif %} {{ container.get('docker_options', '') }} {{ container.image }} {{ container.get('args', '') }}
Restart=always
RestartSec=10s
Type=notify
......
......@@ -19,7 +19,7 @@ defaults
timeout client 1440m
timeout server 1440m
{% for service_name, service in services.iteritems() %}
{% for service_name, service in services.items() %}
{% for ep in service.get('public_tcp_endpoints', []) %}
{% if ep.get('ports', []) %}
......
......@@ -34,7 +34,7 @@ userPassword: {crypt}{{ '%s' | format(ldap_replica_password) | password_hash('sh
simplicity.
#}
{% for service in services.itervalues() %}
{% for service in services.values() %}
{% for ldap_creds in service.get('ldap_credentials', []) %}
dn: cn={{ ldap_creds.name }},ou=Operators,dc=investici,dc=org,o=Anarchy
objectClass: top
......
......@@ -71,7 +71,7 @@ scrape_configs:
{% endmacro %}
{# Iterate over monitoring endpoints of all services. #}
{% for service_name, service in services.iteritems() %}
{% for service_name, service in services.items() %}
{%- for target_config in service.get('monitoring_endpoints', []) %}
{{ static_target(service_name, target_config) }}
{%- endfor %}
......
......@@ -4,6 +4,8 @@
#
# See also ansible sources: lib/ansible/parsing/yaml/constructor.py
from __future__ import print_function
import base64
import sys
import optparse
......@@ -74,7 +76,7 @@ def main():
parser.error('Not enough arguments')
if not os.getenv('ANSIBLE_VAULT_PASSWORD_FILE'):
print >>sys.stderr, "You need to set ANSIBLE_VAULT_PASSWORD_FILE"
print("You need to set ANSIBLE_VAULT_PASSWORD_FILE", file=sys.stderr)
sys.exit(2)
password_file = args[0]
......@@ -90,7 +92,7 @@ def main():
name = entry['name']
if name not in passwords:
print >>sys.stderr, "Generating password for '%s'" % name
print("Generating password for '%s'" % name, file=sys.stderr)
passwords[name] = generate_password(entry)
changed = True
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment