diff --git a/states/etcd/config.sls b/states/etcd/config.sls new file mode 100644 index 0000000..ef1086c --- /dev/null +++ b/states/etcd/config.sls @@ -0,0 +1,13 @@ +--- +{%- from "etcd/map.jinja" import etcd with context %} +etcd-config-dir: + file.directory: + - name: /etc/etcd + +etcd-config: + file.managed: + - name: /etc/etcd/etcd.yml + - source: salt://etcd/templates/etcd.yml.j2 + - template: jinja + - watch_in: + - service: etcd-service diff --git a/states/etcd/defaults.yaml b/states/etcd/defaults.yaml new file mode 100644 index 0000000..c6d6c44 --- /dev/null +++ b/states/etcd/defaults.yaml @@ -0,0 +1,20 @@ +--- +etcd: + enabled: true + install_dir: /usr/local/apps + release_dir: /usr/local/apps/releases + mirror: https://github.com/etcd-io/etcd/releases/download + version: 3.5.9 + os: linux + arch: amd64 + runuser: etcd + user: user + password: password + verify: false + config: + name: etcd + data-dir: /var/lib/etcd + initial-advertise-peer-urls: http://sys99.paulbsd.com:2380 + advertise-client-urls: http://sys99.paulbsd.com:2379 + listen-peer-urls: http://[::]:2380 + listen-client-urls: http://[::]:2379 diff --git a/states/etcd/init.sls b/states/etcd/init.sls new file mode 100644 index 0000000..383c46b --- /dev/null +++ b/states/etcd/init.sls @@ -0,0 +1,5 @@ +--- +include: + - .install + - .config + - .service diff --git a/states/etcd/install.sls b/states/etcd/install.sls new file mode 100644 index 0000000..9bfb62a --- /dev/null +++ b/states/etcd/install.sls @@ -0,0 +1,41 @@ +--- +{%- from "etcd/map.jinja" import etcd with context %} +etcd-user: + user.present: + - name: {{ etcd.runuser }} + - shell: /bin/nologin + +etcd-archive-extract: + archive.extracted: + - name: {{ etcd.release_dir }}/etcd-{{ etcd.version }} + - source: {{ etcd.mirror }}/v{{ etcd.version }}/etcd-v{{ etcd.version }}-{{ etcd.os }}-{{ etcd.arch }}.tar.gz + - user: {{ etcd.runuser }} + - enforce_toplevel: false + - skip_verify: true + - archive_format: tar + - options: --strip 1 + - if_missing: {{ etcd.release_dir }}/etcd-{{ etcd.version }} + - watch_in: + - service: etcd-service + +etcd-bin-symlink: + file.symlink: + - name: {{ etcd.install_dir }}/etcd + - target: {{ etcd.release_dir }}/etcd-{{ etcd.version }} + - user: {{ etcd.runuser }} + - force: true + - watch_in: + - service: etcd-service + +etcd-data-dir: + file.directory: + - name: {{ etcd.config.get('data-dir') }} + - user: {{ etcd.runuser }} + - watch_in: + - service: etcd-service + +etcd-cleanup: + software.cleanup: + - name: etcd + - path: {{ etcd.release_dir }} + - version: "{{ etcd.version }}" diff --git a/states/etcd/kernelmap.yaml b/states/etcd/kernelmap.yaml new file mode 100644 index 0000000..40943f2 --- /dev/null +++ b/states/etcd/kernelmap.yaml @@ -0,0 +1,3 @@ +--- +Linux: + os: "linux" diff --git a/states/etcd/map.jinja b/states/etcd/map.jinja new file mode 100644 index 0000000..0f201c7 --- /dev/null +++ b/states/etcd/map.jinja @@ -0,0 +1,14 @@ +{%- import_yaml "etcd/defaults.yaml" as default_settings -%} + +{%- import_yaml "etcd/kernelmap.yaml" as kernelmap %} +{%- import_yaml "etcd/osarchmap.yaml" as osarchmap %} + +{%- set defaults = salt['grains.filter_by'](default_settings, + default='etcd', + merge=salt['grains.filter_by'](osarchmap, grain='osarch', + merge=salt['grains.filter_by'](kernelmap, grain='kernel') + ) + ) +-%} + +{%- set etcd = salt['pillar.get']('etcd', default=defaults, merge=True) -%} diff --git a/states/etcd/osarchmap.yaml b/states/etcd/osarchmap.yaml new file mode 100644 index 0000000..132c992 --- /dev/null +++ b/states/etcd/osarchmap.yaml @@ -0,0 +1,21 @@ +--- +amd64: + arch: "amd64" + +x86_64: + arch: "amd64" + +386: + arch: "386" + +arm64: + arch: "arm64" + +aarch64: + arch: "arm64" + +armv7l: + arch: "armv7" + +armhf: + arch: "armv7" diff --git a/states/etcd/service.sls b/states/etcd/service.sls new file mode 100644 index 0000000..73f42e0 --- /dev/null +++ b/states/etcd/service.sls @@ -0,0 +1,16 @@ +--- +{%- from "etcd/map.jinja" import etcd with context %} +etcd-service-file: + file.managed: + - name: /etc/systemd/system/etcd.service + - source: salt://etcd/templates/etcd.service.j2 + - user: root + - group: root + - template: jinja + - watch_in: + - service: etcd-service + +etcd-service: + service.running: + - name: etcd + - enable: true diff --git a/states/etcd/templates/etcd.service.j2 b/states/etcd/templates/etcd.service.j2 new file mode 100644 index 0000000..b275e58 --- /dev/null +++ b/states/etcd/templates/etcd.service.j2 @@ -0,0 +1,17 @@ +{%- from "etcd/map.jinja" import etcd with context -%} +## {{ salt['pillar.get']('salt_managed', default='Salt Managed') }} +[Unit] +Description=Distributed reliable key-value store for the most critical data of a distributed system +Documentation=https://etcd.io/ +After=network.target + +[Service] +ExecStart={{ etcd.install_dir }}/etcd/etcd --config-file /etc/etcd/etcd.yml +ExecReload=/bin/kill -HUP $MAINPID +KillMode=control-group +Restart=on-failure +RestartForceExitStatus=SIGPIPE +WorkingDirectory=/var/lib/etcd + +[Install] +WantedBy=multi-user.target diff --git a/states/etcd/templates/etcd.yml.j2 b/states/etcd/templates/etcd.yml.j2 new file mode 100644 index 0000000..c67755e --- /dev/null +++ b/states/etcd/templates/etcd.yml.j2 @@ -0,0 +1,4 @@ +## {{ salt['pillar.get']('salt_managed', default='Salt Managed') }} +--- +{%- from "etcd/map.jinja" import etcd with context %} +{{ etcd.config|yaml(False) }}