diff --git a/states/clickhouse/config.sls b/states/clickhouse/config.sls
new file mode 100644
index 0000000..75edb1b
--- /dev/null
+++ b/states/clickhouse/config.sls
@@ -0,0 +1,46 @@
+{%- from "clickhouse/map.jinja" import clickhouse with context -%}
+---
+clickhouse-config-file-settings:
+ file.managed:
+ - name: /etc/clickhouse-server/config.d/settings.xml
+ - source: salt://clickhouse/templates/settings.xml.j2
+ - user: clickhouse
+ - group: clickhouse
+ - mode: 600
+ - template: jinja
+
+clickhouse-config-file-macros:
+ file.managed:
+ - name: /etc/clickhouse-server/config.d/macros.xml
+ - source: salt://clickhouse/templates/macros.xml.j2
+ - user: clickhouse
+ - group: clickhouse
+ - mode: 600
+ - template: jinja
+
+clickhouse-config-file-replica:
+ file.managed:
+ - name: /etc/clickhouse-server/config.d/replica.xml
+ - source: salt://clickhouse/templates/replica.xml.j2
+ - user: clickhouse
+ - group: clickhouse
+ - mode: 600
+ - template: jinja
+
+clickhouse-config-file-keeper:
+ file.managed:
+ - name: /etc/clickhouse-server/config.d/keeper.xml
+ - source: salt://clickhouse/templates/keeper.xml.j2
+ - user: clickhouse
+ - group: clickhouse
+ - mode: 600
+ - template: jinja
+
+clickhouse-config-file-tls:
+ file.managed:
+ - name: /etc/clickhouse-server/config.d/tls.xml
+ - source: salt://clickhouse/templates/tls.xml.j2
+ - user: clickhouse
+ - group: clickhouse
+ - mode: 600
+ - template: jinja
diff --git a/states/clickhouse/defaults.yaml b/states/clickhouse/defaults.yaml
new file mode 100644
index 0000000..991f875
--- /dev/null
+++ b/states/clickhouse/defaults.yaml
@@ -0,0 +1,12 @@
+---
+clickhouse:
+ enabled: true
+ pkgs:
+ - clickhouse-server
+ - clickhouse-client
+ databases: {}
+ users: {}
+ cluster: paulbsd
+ clusters:
+ paulbsd:
+ nodes: {}
diff --git a/states/clickhouse/init.sls b/states/clickhouse/init.sls
new file mode 100644
index 0000000..6a847b8
--- /dev/null
+++ b/states/clickhouse/init.sls
@@ -0,0 +1,6 @@
+---
+include:
+ - .install
+ - .config
+ - .service
+ - .users
diff --git a/states/clickhouse/install.sls b/states/clickhouse/install.sls
new file mode 100644
index 0000000..caa610e
--- /dev/null
+++ b/states/clickhouse/install.sls
@@ -0,0 +1,6 @@
+---
+{%- from "clickhouse/map.jinja" import clickhouse with context %}
+clickhouse-install:
+ pkg.installed:
+ - name: clickhouse-install
+ - pkgs: {{ clickhouse.pkgs }}
diff --git a/states/clickhouse/map.jinja b/states/clickhouse/map.jinja
new file mode 100644
index 0000000..6c953ee
--- /dev/null
+++ b/states/clickhouse/map.jinja
@@ -0,0 +1,5 @@
+{%- import_yaml "clickhouse/defaults.yaml" as default_settings -%}
+
+{%- set defaults = salt['grains.filter_by'](default_settings, default='clickhouse') -%}
+
+{%- set clickhouse = salt['pillar.get']('clickhouse', default=defaults, merge=True) -%}
diff --git a/states/clickhouse/service.sls b/states/clickhouse/service.sls
new file mode 100644
index 0000000..ba04a45
--- /dev/null
+++ b/states/clickhouse/service.sls
@@ -0,0 +1,6 @@
+---
+{%- from "clickhouse/map.jinja" import clickhouse with context %}
+clickhouse-service:
+ service.running:
+ - name: clickhouse-server
+ - enable: true
diff --git a/states/clickhouse/templates/keeper.xml.j2 b/states/clickhouse/templates/keeper.xml.j2
new file mode 100644
index 0000000..35cbdbc
--- /dev/null
+++ b/states/clickhouse/templates/keeper.xml.j2
@@ -0,0 +1,35 @@
+{%- from "clickhouse/map.jinja" import clickhouse with context -%}
+
+
+{%- for nodename, settings in clickhouse.clusters[clickhouse.cluster].nodes.items() %}
+
+ {{ nodename }}
+ {{ settings.port|default(9281) }}
+ 1
+
+{%- endfor %}
+
+
+ {{ settings.port|default(9281) }}
+ {{ clickhouse.clusters[clickhouse.cluster].nodes[salt['grains.get']('fqdn')].id }}
+ /var/lib/clickhouse/coordination/log
+ /var/lib/clickhouse/coordination/snapshots
+
+
+ 10000
+ 30000
+ fatal
+
+
+
+ true
+{%- for nodename, settings in clickhouse.clusters[clickhouse.cluster].nodes.items() %}
+
+ {{ settings.id }}
+ {{ nodename }}
+ {{ settings.port|default(9444) }}
+
+{%- endfor %}
+
+
+
diff --git a/states/clickhouse/templates/macros.xml.j2 b/states/clickhouse/templates/macros.xml.j2
new file mode 100644
index 0000000..b8086fc
--- /dev/null
+++ b/states/clickhouse/templates/macros.xml.j2
@@ -0,0 +1,8 @@
+{%- from "clickhouse/map.jinja" import clickhouse with context %}
+
+
+ {{ clickhouse.cluster }}
+ {{ clickhouse.clusters[clickhouse.cluster].nodes[salt['grains.get']('fqdn')].shard }}
+ {{ salt['grains.get']('fqdn') }}
+
+
diff --git a/states/clickhouse/templates/replica.xml.j2 b/states/clickhouse/templates/replica.xml.j2
new file mode 100644
index 0000000..56bc2c9
--- /dev/null
+++ b/states/clickhouse/templates/replica.xml.j2
@@ -0,0 +1,19 @@
+{%- from "clickhouse/map.jinja" import clickhouse with context %}
+
+
+ <{{ clickhouse.cluster }}>
+
+ false
+{% for nodename, settings in clickhouse.clusters[clickhouse.cluster].nodes.items() %}
+
+ {{ nodename }}
+ 9440
+ default
+
+ 1
+
+{% endfor %}
+
+ {{ clickhouse.cluster }}>
+
+
diff --git a/states/clickhouse/templates/settings.xml.j2 b/states/clickhouse/templates/settings.xml.j2
new file mode 100644
index 0000000..942dfa7
--- /dev/null
+++ b/states/clickhouse/templates/settings.xml.j2
@@ -0,0 +1,14 @@
+{%- from "clickhouse/map.jinja" import clickhouse with context %}
+
+ ::
+ {replica}
+ /clickhouse/{cluster}/tables/{uuid}/{database}/{table}-{shard}
+
+ fatal
+
+ 0.5
+ 9440
+ 8443
+
+ 9010
+
diff --git a/states/clickhouse/templates/tls.xml.j2 b/states/clickhouse/templates/tls.xml.j2
new file mode 100644
index 0000000..eb26fc6
--- /dev/null
+++ b/states/clickhouse/templates/tls.xml.j2
@@ -0,0 +1,25 @@
+{%- from "clickhouse/map.jinja" import clickhouse with context %}
+
+
+
+ /etc/clickhouse-server/certs/{{ salt['grains.get']('fqdn') }}.crt
+ /etc/clickhouse-server/certs/{{ salt['grains.get']('fqdn') }}.key
+ relaxed
+ /etc/clickhouse-server/certs/{{ clickhouse.cluster }}_ca.crt
+ true
+ sslv2,sslv3
+ true
+
+
+ false
+ /etc/clickhouse-server/certs/{{ clickhouse.cluster }}_ca.crt
+ true
+ sslv2,sslv3
+ true
+ relaxed
+
+ RejectCertificateHandler
+
+
+
+
diff --git a/states/clickhouse/templates/user.xml.j2 b/states/clickhouse/templates/user.xml.j2
new file mode 100644
index 0000000..cd43562
--- /dev/null
+++ b/states/clickhouse/templates/user.xml.j2
@@ -0,0 +1,24 @@
+{%- from "clickhouse/map.jinja" import clickhouse with context -%}
+
+
+ <{{ name }}>
+
+ {%- for network in params.networks|default(["127.0.0.1/8"]) %}
+ {{ network }}
+ {%- endfor %}
+
+ {{ params.profile|default("default") }}
+ {{ params.quota|default("default") }}
+ {{ params.default_database|default("default") }}
+ {{ params.access_management|default(0)}}
+ {%- if params.databases|default([])|length > 0 %}
+
+ {%- for database in params.databases|default([]) %}
+ {{ database }}
+ {%- endfor %}
+
+ {%- endif %}
+ {{ params.password|default("") }}
+ {{ name }}>
+
+
diff --git a/states/clickhouse/users.sls b/states/clickhouse/users.sls
new file mode 100644
index 0000000..472be8c
--- /dev/null
+++ b/states/clickhouse/users.sls
@@ -0,0 +1,15 @@
+---
+{%- from "clickhouse/map.jinja" import clickhouse with context %}
+{% for user, params in clickhouse.users.items() %}
+clickhouse-user-{{ user }}:
+ file.managed:
+ - name: /etc/clickhouse-server/users.d/{{ user }}.xml
+ - source: salt://clickhouse/templates/user.xml.j2
+ - user: clickhouse
+ - group: clickhouse
+ - mode: 600
+ - template: jinja
+ - context:
+ name: {{ user }}
+ params: {{ params }}
+{% endfor %}