Skip to content

Commit 4fda2ae

Browse files
authored
Allow cortex to run in multiple services (#7)
* Allow cortex to run in multiple services Signed-off-by: Julien Pivotto <[email protected]>
1 parent c236d70 commit 4fda2ae

File tree

12 files changed

+321
-10
lines changed

12 files changed

+321
-10
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ All variables which can be overridden are stored in [defaults/main.yml](defaults
1919

2020
| Name | Default Value | Description |
2121
| -------------- | ------------- | -----------------------------------|
22+
| `cortex_all_in_one` | true | Setup Cortex in all-in-one binary mode. |
23+
| `cortex_services` | `{}` | Cortex services configurations. |
2224
| `cortex_web_listen_address` | "0.0.0.0:9009" | Address on which cortex will listen |
2325
| `cortex_binary_local_dir` | "" | Allows to use local packages instead of ones distributed on github. As parameter it takes a directory where `cortex` binaries are stored on host on which ansible is ran. This overrides `cortex_version` parameter |
2426
| `cortex_interface` | "{{ ansible_default_ipv4.interface }}" | Network adapter that cortex will be using |
@@ -66,6 +68,34 @@ Use it in a playbook as follows:
6668
roles:
6769
- cloudalchemy.cortex
6870
```
71+
72+
### Services mode
73+
74+
You can run the different Cortex modules as separate services by setting
75+
`cortex_services` as a module:config map and `cortex_all_in_one` to `false`.
76+
```yaml
77+
- hosts: all
78+
roles:
79+
- cloudalchemy.cortex
80+
vars:
81+
cortex_all_in_one: false
82+
cortex_services:
83+
ingester:
84+
server:
85+
http_listen_port: 9009
86+
ingester:
87+
lifecycler:
88+
join_after: 0
89+
min_ready_duration: 0s
90+
final_sleep: 0s
91+
num_tokens: 512
92+
93+
ring:
94+
kvstore:
95+
store: inmemory
96+
replication_factor: 1
97+
```
98+
6999
## Local Testing
70100

71101
The preferred way of locally testing the role is to use Docker and [molecule](https://github.com/metacloud/molecule) (v2.x). You will have to install Docker on your system. See "Get started" for a Docker package suitable to for your system.

defaults/main.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
---
2+
cortex_version: 1.4.0
3+
cortex_all_in_one: true
4+
25
cortex_web_listen_address: "0.0.0.0:9009"
36
cortex_binary_local_dir: ''
47
cortex_skip_install: false
58

9+
cortex_services: {}
10+
611
cortex_interface: "{{ ansible_default_ipv4.interface }}"
712

813
cortex_config_file: 'cortex.yml.j2'
@@ -12,8 +17,6 @@ cortex_db_dir: /var/lib/cortex
1217
cortex_system_user: "cortex"
1318
cortex_system_group: "cortex"
1419

15-
cortex_version: 1.4.0
16-
1720
cortex_auth_enabled: false
1821

1922
cortex_server:

handlers/main.yml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
---
2-
- name: restart cortex
2+
- name: restart cortex all-in-one
33
become: true
44
systemd:
55
daemon_reload: true
66
name: cortex
77
state: restarted
8-
- name: reload cortex
8+
listen: restart cortex
9+
when: cortex_all_in_one
10+
- name: restart cortex all-in-one
11+
become: true
12+
systemd:
13+
daemon_reload: true
14+
name: cortex@{{ item.key }}
15+
state: restarted
16+
listen: restart cortex
17+
loop: "{{ cortex_services | dict2items }}"
18+
- name: restart cortex all-in-one
19+
set_fact:
20+
_cortex_service_status: started
21+
listen: restart cortex
22+
- name: reload cortex all-in-one
923
become: true
1024
systemd:
1125
name: cortex
12-
state: reloaded
26+
state: "{{ _cortex_service_status | default('reloaded') }}"
27+
listen: reload cortex
28+
when: cortex_all_in_one
29+
- name: reload cortex services
30+
become: true
31+
systemd:
32+
name: cortex@{{ item.key }}
33+
state: "{{ _cortex_service_status | default('reloaded') }}"
34+
listen: reload cortex
35+
loop: "{{ cortex_services | dict2items }}"

molecule/latest/tests/test_latest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import pytest
21
import os
32
import testinfra.utils.ansible_runner
43

molecule/services/molecule.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
dependency:
3+
name: galaxy
4+
driver:
5+
name: docker
6+
lint:
7+
name: yamllint
8+
platforms:
9+
- name: bionic
10+
image: quay.io/paulfantom/molecule-systemd:ubuntu-18.04
11+
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
12+
privileged: true
13+
volumes:
14+
- /sys/fs/cgroup:/sys/fs/cgroup:ro
15+
- name: xenial
16+
image: quay.io/paulfantom/molecule-systemd:ubuntu-16.04
17+
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
18+
privileged: true
19+
volumes:
20+
- /sys/fs/cgroup:/sys/fs/cgroup:ro
21+
- name: stretch
22+
image: quay.io/paulfantom/molecule-systemd:debian-9
23+
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
24+
privileged: true
25+
volumes:
26+
- /sys/fs/cgroup:/sys/fs/cgroup:ro
27+
- name: buster
28+
image: quay.io/paulfantom/molecule-systemd:debian-10
29+
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
30+
privileged: true
31+
volumes:
32+
- /sys/fs/cgroup:/sys/fs/cgroup:ro
33+
- name: centos7
34+
image: quay.io/paulfantom/molecule-systemd:centos-7
35+
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
36+
privileged: true
37+
volumes:
38+
- /sys/fs/cgroup:/sys/fs/cgroup:ro
39+
- name: centos8
40+
image: quay.io/paulfantom/molecule-systemd:centos-8
41+
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
42+
privileged: true
43+
volumes:
44+
- /sys/fs/cgroup:/sys/fs/cgroup:ro
45+
groups:
46+
- python3
47+
- name: fedora
48+
image: quay.io/paulfantom/molecule-systemd:fedora-30
49+
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
50+
privileged: true
51+
volumes:
52+
- /sys/fs/cgroup:/sys/fs/cgroup:ro
53+
groups:
54+
- python3
55+
provisioner:
56+
name: ansible
57+
lint:
58+
name: ansible-lint
59+
playbooks:
60+
create: ../default/create.yml
61+
prepare: prepare.yml
62+
converge: playbook.yml
63+
destroy: ../default/destroy.yml
64+
inventory:
65+
group_vars:
66+
python3:
67+
ansible_python_interpreter: /usr/bin/python3
68+
scenario:
69+
name: services
70+
verifier:
71+
name: testinfra
72+
lint:
73+
name: flake8
74+
enabled: true

molecule/services/playbook.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
- name: Run role
3+
hosts: all
4+
any_errors_fatal: true
5+
roles:
6+
- ansible-cortex
7+
vars:
8+
cortex_config_dir: /cortex
9+
cortex_db_dir: /cortexdb
10+
cortex_system_user: "vortex"
11+
cortex_system_group: "vortex"
12+
cortex_all_in_one: false
13+
cortex_services:
14+
ingester:
15+
server:
16+
http_listen_port: 9009
17+
ingester:
18+
lifecycler:
19+
join_after: 0
20+
min_ready_duration: 0s
21+
final_sleep: 0s
22+
num_tokens: 512
23+
24+
ring:
25+
kvstore:
26+
store: inmemory
27+
replication_factor: 1

molecule/services/prepare.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- name: Prepare
3+
hosts: all
4+
gather_facts: false
5+
tasks: []
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pytest
2+
import os
3+
import yaml
4+
import testinfra.utils.ansible_runner
5+
6+
7+
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
8+
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
9+
10+
11+
@pytest.fixture()
12+
def AnsibleDefaults():
13+
with open("../../defaults/main.yml", 'r') as stream:
14+
return yaml.load(stream)
15+
16+
17+
@pytest.mark.parametrize("dirs", [
18+
"/cortex",
19+
"/cortex/rules",
20+
"/cortexdb"
21+
])
22+
def test_directories(host, dirs):
23+
d = host.file(dirs)
24+
assert d.is_directory
25+
assert d.exists
26+
27+
28+
@pytest.mark.parametrize("files", [
29+
"/cortex/cortex-ingester.yml",
30+
"/etc/systemd/system/[email protected]",
31+
"/usr/local/bin/cortex-linux-amd64",
32+
])
33+
def test_files(host, files):
34+
f = host.file(files)
35+
assert f.exists
36+
assert f.is_file
37+
38+
39+
def test_user(host):
40+
assert host.group("vortex").exists
41+
assert host.user("vortex").exists
42+
43+
44+
def test_service(host):
45+
s = host.service("cortex@ingester")
46+
# assert s.is_enabled
47+
assert s.is_running
48+
49+
50+
def test_socket(host):
51+
s = host.socket("tcp://0.0.0.0:9009")
52+
assert s.is_listening

tasks/configure.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
---
22
- name: Copy the cortex systemd service file
33
template:
4-
src: cortex.service.j2
5-
dest: /etc/systemd/system/cortex.service
4+
src: "{{ item }}.service.j2"
5+
dest: /etc/systemd/system/{{ item }}.service
66
owner: root
77
group: root
88
mode: 0644
99
notify: restart cortex
10+
loop: [cortex, cortex@]
1011

1112
- name: configure cortex
1213
template:
@@ -18,3 +19,16 @@
1819
mode: 0640
1920
notify:
2021
- reload cortex
22+
when: cortex_all_in_one
23+
24+
- name: configure cortex targets
25+
copy:
26+
content: "{{ item.value | to_nice_yaml(indent=2) }}"
27+
dest: "{{ cortex_config_dir }}/cortex-{{ item.key }}.yml"
28+
force: true
29+
owner: root
30+
group: "{{ cortex_system_user }}"
31+
mode: 0640
32+
notify:
33+
- reload cortex
34+
loop: "{{ cortex_services | dict2items }}"

tasks/main.yml

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,66 @@
2626
tags:
2727
- cortex_configure
2828

29-
- name: Ensure cortex is enabled on boot
29+
- name: Ensure cortex all-in-one is enabled on boot
3030
become: true
3131
systemd:
3232
daemon_reload: true
3333
name: cortex
3434
enabled: true
3535
tags:
3636
- cortex_run
37+
when: cortex_all_in_one
3738

38-
- name: ensure cortex service is started and enabled
39+
- name: ensure cortex all-in-one service is started and enabled
3940
become: true
4041
systemd:
4142
daemon_reload: true
4243
name: cortex
44+
state: "{% if cortex_all_in_one %}started{% else %}stopped{%endif%}"
45+
enabled: "{{ cortex_all_in_one }}"
46+
tags:
47+
- cortex_run
48+
49+
- name: Ensure cortex services are disabled on boot
50+
become: true
51+
systemd:
52+
daemon_reload: true
53+
name: cortex@{{ item }}
54+
enabled: false
55+
tags:
56+
- cortex_run
57+
when: "item not in cortex_services"
58+
loop: "{{ existing_cortex_units }}"
59+
60+
- name: Ensure cortex services are stopped
61+
become: true
62+
systemd:
63+
daemon_reload: true
64+
name: cortex@{{ item }}
65+
state: "stopped"
66+
enabled: false
67+
tags:
68+
- cortex_run
69+
when: "item not in cortex_services"
70+
loop: "{{ existing_cortex_units }}"
71+
72+
- name: Ensure cortex services are enabled on boot
73+
become: true
74+
systemd:
75+
daemon_reload: true
76+
name: cortex@{{ item.key }}
77+
enabled: true
78+
tags:
79+
- cortex_run
80+
loop: "{{ cortex_services | dict2items }}"
81+
82+
- name: ensure cortex services are started and enabled
83+
become: true
84+
systemd:
85+
daemon_reload: true
86+
name: cortex@{{ item.key }}
4387
state: started
4488
enabled: true
4589
tags:
4690
- cortex_run
91+
loop: "{{ cortex_services | dict2items }}"

0 commit comments

Comments
 (0)