Page MenuHomeDevCentral

No OneTemporary

diff --git a/_modules/paas_docker.py b/_modules/paas_docker.py
index afe4c6a..8e4fb59 100644
--- a/_modules/paas_docker.py
+++ b/_modules/paas_docker.py
@@ -1,134 +1,148 @@
# -*- coding: utf-8 -*-
# -------------------------------------------------------------
# Salt — PaaS Docker execution module
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Created: 2018-10-07
# Description: Functions related to data format conversions
# License: BSD-2-Clause
# -------------------------------------------------------------
def get_image(default_image, args):
"""
A function to output a pillar key in JSON.
State Example::
{% image = salt['paas_docker.get_image']("nasqueron/mysql", container) %}
"""
image = default_image
if "image" in args:
image = args["image"]
if "version" in args:
image += ":" + str(args["version"])
return image
def list_images():
"""
A function to get the list of images used on a Docker engine.
Example:
salt docker-002 paas_docker.list_images
"""
images = __pillar__.get("docker_images", [])
# Workaround for a merge issue for lists:
# Salt Tower concatenates them, a set will dedup them.
return set(images)
def get_subnets():
"""
A function to get the Docker subnets list.
CLI Example:
salt * paas_docker.get_subnets
"""
try:
networks = __pillar__["docker_networks"]
except KeyError:
networks = {}
# Defined Docker subnet
subnets = [network["subnet"] for network in networks.values()]
# Default Docker subnet
subnets.append("172.17.0.0/16")
return subnets
def _get_containers():
try:
return __pillar__["docker_containers"]
except KeyError:
return {}
def list_containers():
"""
A function to list all the containers provisionned on a Docker engine.
This function uses the pillar docker_containers as authoritative source,
so it documents the expected configuration, not the actual containers
running. That allows to compare both states.
CLI Example:
salt * paas_docker.list_containers
"""
return [
key
for service, service_containers in _get_containers().items()
for key in service_containers.keys()
]
+# -------------------------------------------------------------
+# Nginx
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+def resolve_vhost_config_file(service, dir="roles/paas-docker/nginx/files/vhosts"):
+ candidate = f"{dir}/{service}.conf"
+
+ if __salt__["slsutil.file_exists"](candidate):
+ return candidate
+
+ return f"{dir}/_default.conf"
+
+
# -------------------------------------------------------------
# Monitoring
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def _get_health_check_url(check_type, container, url):
if check_type[-6:] == "_proxy":
return f"https://{container['host']}{url}"
return f"http://localhost:{container['app_port']}{url}"
def get_health_checks():
"""
A function to get a dictionary with health checks
for known containers to use with our monitoring.
CLI Example:
salt * paas_docker.get_health_checks
"""
containers = _get_containers()
monitoring = __pillar__["docker_containers_monitoring"]
return {
check_type: {
instance: _get_health_check_url(check_type, container, url)
for service, url in monitoring[check_type].items()
for instance, container in containers.get(service, {}).items()
}
for check_type in monitoring.keys()
}
# -------------------------------------------------------------
# Format
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def format_env_list(values, separator=",", assign_op="-"):
return separator.join([f"{k}{assign_op}{v}" for k, v in values.items()])
diff --git a/_tests/data/empty.conf b/_tests/data/empty.conf
new file mode 100644
index 0000000..e69de29
diff --git a/_tests/modules/test_paas_docker.py b/_tests/modules/test_paas_docker.py
index b96387c..963e06f 100755
--- a/_tests/modules/test_paas_docker.py
+++ b/_tests/modules/test_paas_docker.py
@@ -1,72 +1,86 @@
#!/usr/bin/env python3
from importlib.machinery import SourceFileLoader
+import os
import unittest
salt_test_case = SourceFileLoader("salt_test_case", "salt_test_case.py").load_module()
docker = SourceFileLoader("docker", "../_modules/paas_docker.py").load_module()
class Testinstance(unittest.TestCase, salt_test_case.SaltTestCase):
def setUp(self):
self.initialize_mocks()
self.instance = docker
self.mock_pillar("data/paas_docker.yaml")
self.mock_grains()
self.grains["id"] = "egladil"
+ self.mock_salt()
+ self.salt["slsutil.file_exists"] = lambda file: os.path.exists(file)
+
def test_get_image(self):
container = {"image": "foo", "version": "42"}
self.assertEqual("foo:42", docker.get_image("not_foo", container))
def test_list_images(self):
expected = {"foo", "bar", "quux"}
self.assertEqual(expected, docker.list_images())
def test_get_image_without_version(self):
container = {
"image": "foo",
}
self.assertEqual("foo", docker.get_image("not_foo", container))
def test_get_image_without_image(self):
container = {"version": "42"}
self.assertEqual("not_foo:42", docker.get_image("not_foo", container))
def test_get_image_without_anything(self):
self.assertEqual("not_foo", docker.get_image("not_foo", {}))
def test_get_image_with_numeric_version(self):
container = {"image": "foo", "version": 2.5}
self.assertEqual("foo:2.5", docker.get_image("not_foo", container))
+ def test_resolve_vhost_config_file(self):
+ config_file = docker.resolve_vhost_config_file("empty", dir="data")
+
+ self.assertEqual("data/empty.conf", config_file)
+
+ def test_resolve_vhost_config_file_when_not_existing(self):
+ config_file = docker.resolve_vhost_config_file("foo", dir="notexisting")
+
+ self.assertEqual("notexisting/_default.conf", config_file)
+
def test_get_subnets(self):
expected = ["172.18.1.0/24", "172.18.2.0/24", "172.17.0.0/16"]
self.assertEqual(expected, docker.get_subnets())
def test_get_subnets_when_none_are_defined(self):
# Only the default Docker one
expected = ["172.17.0.0/16"]
self.grains["id"] = "voidserver"
self.assertEqual(expected, docker.get_subnets())
def test_format_env_list(self):
expression = {"foo": "bar", "quux": 42}
expected = "foo: bar; quux: 42"
self.assertEqual(
expected, docker.format_env_list(expression, assign_op=": ", separator="; ")
)
if __name__ == "__main__":
unittest.main()
diff --git a/roles/paas-docker/nginx/config.sls b/roles/paas-docker/nginx/config.sls
index 194ec3b..ca687d2 100644
--- a/roles/paas-docker/nginx/config.sls
+++ b/roles/paas-docker/nginx/config.sls
@@ -1,85 +1,87 @@
# -------------------------------------------------------------
# Salt — Provision Docker engine
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# Created: 2018-03-16
# License: Trivial work, not eligible to copyright
# -------------------------------------------------------------
{% from "map.jinja" import dirs with context %}
{% set containers = pillar.get('docker_containers', {}) %}
# -------------------------------------------------------------
# Base folder
#
# :: general configuration
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{{ dirs.etc }}/nginx/nginx.conf:
file.managed:
- source: salt://roles/paas-docker/nginx/files/nginx.conf
nginx_dhparams:
cmd.run:
- name: openssl dhparam -out {{ dirs.etc }}/nginx/dhparams.pem 2048
- creates: {{ dirs.etc }}/nginx/dhparams.pem
# -------------------------------------------------------------
# includes folder
#
# :: general configuration
# :: application-specific code
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{{ dirs.etc }}/nginx/includes:
file.recurse:
- source: salt://roles/paas-docker/nginx/files/includes
- dir_mode: 755
- file_mode: 644
# -------------------------------------------------------------
# vhosts folder
#
# :: fallback when a domain isn't found
# :: server cover page
# :: containers
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{{ dirs.etc }}/nginx/vhosts:
file.directory:
- dir_mode: 755
{{ dirs.etc }}/nginx/vhosts/000-fallback.conf:
file.managed:
- source: salt://roles/paas-docker/nginx/files/vhosts/base/fallback.conf
{{ dirs.etc }}/nginx/vhosts/001-server.conf:
file.managed:
- source: salt://roles/paas-docker/nginx/files/vhosts/base/server.conf
- template: jinja
- context:
fqdn: {{ grains['fqdn'] }}
ipv4: {{ grains['ipv4'] | join(" ") }}
ipv6: "{{ salt['node.get_ipv6_list']() }}"
{% for service, instances in containers.items() %}
{% for instance, container in instances.items() %}
{% if 'host' in container %}
+{% set vhost_config = salt["paas_docker.resolve_vhost_config_file"](service) %}
+
{{ dirs.etc }}/nginx/vhosts/{{ service }}/{{ instance }}.conf:
file.managed:
- - source: salt://roles/paas-docker/nginx/files/vhosts/{{ service }}.conf
+ - source: salt://{{ vhost_config }}
- mode: 644
- makedirs: True
- template: jinja
- context:
fqdn: {{ container['host'] }}
app_port: {{ container['app_port'] }}
aliases: {{ container['aliases'] | default('', true) | join(" ") }}
# If the nginx configuration needs more key,
# pass directly the container dictionary.
args: {{ container }}
{% endif %}
{% endfor %}
{% endfor %}
diff --git a/roles/paas-docker/nginx/files/vhosts/hound.conf b/roles/paas-docker/nginx/files/vhosts/hound.conf
deleted file mode 100644
index 60f08b5..0000000
--- a/roles/paas-docker/nginx/files/vhosts/hound.conf
+++ /dev/null
@@ -1 +0,0 @@
-{% include 'roles/paas-docker/nginx/files/vhosts/_default.conf' %}

File Metadata

Mime Type
text/x-diff
Expires
Tue, May 5, 11:50 (6 h, 35 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3684477
Default Alt Text
(10 KB)

Event Timeline