1 {%- from "influxdb/map.jinja" import client with context %}
3 {%- if client.get('enabled') %}
5 {%- set curl_command = 'curl' %}
6 {%- if grains.get('noservices') %}
7 {%- set curl_command = 'true ' + curl_command %}
10 {%- set noauth_url = "{}://{}:{}/query".format(client.server.protocol, client.server.host, client.server.port) %}
11 {%- set auth_url = "{}?u={}&p={}".format(noauth_url, client.server.user, client.server.password) %}
13 {# Create the admin user (this is only required on the first run) #}
14 {% set create_admin_query = "--data-urlencode \"q=CREATE USER {} WITH PASSWORD '{}' WITH ALL PRIVILEGES\"".format(client.server.user, client.server.password) %}
15 influxdb_create_admin:
17 - name: {{ curl_command }} -f -S -POST "{{ noauth_url }}" {{ create_admin_query }} || {{ curl_command }} -f -S -POST "{{ auth_url }}" {{ create_admin_query }}
19 {# Create the regular users #}
20 {%- for user_name, user in client.get('user', {}).iteritems() %}
21 {%- if user.get('enabled', False) %}
22 {%- if user.get('admin', False) %}
23 {% set create_user_query = "--data-urlencode \"q=CREATE USER {} WITH PASSWORD '{}' WITH ALL PRIVILEGES\"".format(user.name, user.password) %}
25 {% set create_user_query = "--data-urlencode \"q=CREATE USER {} WITH PASSWORD '{}'\"".format(user.name, user.password) %}
27 influxdb_create_user_{{user.name}}:
29 - name: {{ curl_command }} -f -S -POST "{{ auth_url }}" {{ create_user_query }}
31 - cmd: influxdb_create_admin
35 {# Create the databases #}
36 {%- for db_name, db in client.get('database', {}).iteritems() %}
37 {%- if db.get('enabled', False) %}
38 {% set create_db_query = "--data-urlencode \"q=CREATE DATABASE {}\"".format(db.name) %}
39 influxdb_create_db_{{ db.name }}:
41 - name: {{ curl_command }} -f -S -POST "{{ auth_url }}" {{ create_db_query }}
43 - cmd: influxdb_create_admin
45 {% for rp in db.get('retention_policy', []) %}
46 {% set rp_name = rp.get('name', 'autogen') %}
47 {% if rp.get('is_default') %}
48 {% set is_default = 'DEFAULT' %}
50 {% set is_default = '' %}
52 {% set duration = rp.get('duration', 'INF') %}
53 {% set replication = rp.get('replication', '1') %}
54 {% if rp.get('shard_duration') %}
55 {% set shard_duration = 'SHARD DURATION {}'.format(rp.shard_duration) %}
57 {% set shard_duration = '' %}
59 {% set query_retention_policy = 'RETENTION POLICY {} ON {} DURATION {} REPLICATION {} {} {}'.format(
60 rp_name, db.name, duration, replication, shard_duration, is_default)
62 influxdb_retention_policy_{{db.name}}_{{ rp_name }}:
64 - name: {{ curl_command }} -s -S -POST "{{ auth_url }}" --data-urlencode "q=CREATE {{ query_retention_policy }}"|grep -v "policy already exists" || {{ curl_command }} -s -S -POST "{{ auth_url }}" --data-urlencode "q=ALTER {{ query_retention_policy }}"
66 - cmd: influxdb_create_db_{{db.name}}
71 {%- for grant_name, grant in client.get('grant', {}).iteritems() %}
72 {%- if grant.get('enabled', False) %}
73 {% set query_grant_user_access = "--data-urlencode \"q=GRANT {} ON {} TO {}\"".format(grant.privilege, grant.database, grant.user) %}
74 influxdb_grant_{{ grant_name }}:
76 - name: {{ curl_command }} -f -S -POST "{{ auth_url }}" {{ query_grant_user_access }}
78 - cmd: influxdb_create_db_{{ grant.database }}
79 - cmd: influxdb_create_user_{{ grant.user }}
84 {# CONTINUOUS QUERIES #}
85 {# as of influxdb 1.4 CQ can't be altered #}
86 {%- for db_name, db in client.get('database', {}).iteritems() %}
87 {%- set db_name = db.get('name', db_name) %}
88 {%- for cq_name, cq in db.get('continuous_query', {}).iteritems() %}
89 {%- set cq_name_on= 'CONTINUOUS QUERY {} ON {}'.format(cq_name, db_name) %}
90 {%- set query_continuous_query = '{} BEGIN {} END'.format(cq_name_on, cq) %}
91 influxdb_continuous_query_{{db_name}}_{{ cq_name }}:
94 - name: fail_on() { echo "${@:2}"; if [[ "${@:2}" == *"$1"* ]]; then return 1; return 0; fi; }; fail_on "already exist" $({{ curl_command }} -s -S -POST "{{ auth_url }}" --data-urlencode "q=CREATE {{ query_continuous_query }}") || fail_on error $({{ curl_command }} -s -S -POST "{{ auth_url }}" --data-urlencode $'q=DROP {{ cq_name_on }};\nCREATE {{ query_continuous_query }}')
95 - onlyif: {{ curl_command }} -s -S -POST "{{ auth_url }}" --data-urlencode "q=SHOW DATABASES" | grep {{ db_name }}