From 2eed57822c02576f4fd1107e19ee941ddf3ff6f7 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Sat, 1 May 2021 18:13:25 +0900 Subject: [PATCH] Deploy `consul-template`. --- cookbooks/consul-template/attributes.rb | 13 +++++++ cookbooks/consul-template/default.rb | 7 ++++ .../files/etc/default/consul-template | 1 + .../etc/rsyslog.d/30-consul-template.conf | 7 ++++ .../systemd/system/consul-template.service | 13 +++++++ cookbooks/consul-template/install.rb | 34 ++++++++++++++++ cookbooks/consul-template/setup.rb | 39 +++++++++++++++++++ 7 files changed, 114 insertions(+) create mode 100644 cookbooks/consul-template/attributes.rb create mode 100644 cookbooks/consul-template/default.rb create mode 100644 cookbooks/consul-template/files/etc/default/consul-template create mode 100644 cookbooks/consul-template/files/etc/rsyslog.d/30-consul-template.conf create mode 100644 cookbooks/consul-template/files/etc/systemd/system/consul-template.service create mode 100644 cookbooks/consul-template/install.rb create mode 100644 cookbooks/consul-template/setup.rb diff --git a/cookbooks/consul-template/attributes.rb b/cookbooks/consul-template/attributes.rb new file mode 100644 index 0000000..cb6f58c --- /dev/null +++ b/cookbooks/consul-template/attributes.rb @@ -0,0 +1,13 @@ +# ------------------------------------------- +# Specifying the default settings: +# ------------------------------------------- +node.reverse_merge!({ + 'consulTemplate' => { + 'baseUrl' => 'https://releases.hashicorp.com/consul-template/', + 'version' => '0.25.2', + 'zipPrefix' => 'consul-template_', + 'zipPostfix' => '_linux_amd64.zip', + 'storage' => '/opt/consul-template/consul-template', + 'location' => '/usr/local/bin/consul-template' + }, +}) diff --git a/cookbooks/consul-template/default.rb b/cookbooks/consul-template/default.rb new file mode 100644 index 0000000..f0eb864 --- /dev/null +++ b/cookbooks/consul-template/default.rb @@ -0,0 +1,7 @@ +# Loading the attributes: +include_recipe './attributes.rb' + +# Install loki here: +include_recipe './install.rb' +include_recipe './setup.rb' + diff --git a/cookbooks/consul-template/files/etc/default/consul-template b/cookbooks/consul-template/files/etc/default/consul-template new file mode 100644 index 0000000..769569a --- /dev/null +++ b/cookbooks/consul-template/files/etc/default/consul-template @@ -0,0 +1 @@ +OPTIONS="-syslog -syslog-name=consul-template" diff --git a/cookbooks/consul-template/files/etc/rsyslog.d/30-consul-template.conf b/cookbooks/consul-template/files/etc/rsyslog.d/30-consul-template.conf new file mode 100644 index 0000000..1a4c6cc --- /dev/null +++ b/cookbooks/consul-template/files/etc/rsyslog.d/30-consul-template.conf @@ -0,0 +1,7 @@ +# Log kernel generated promtail log messages to file +:syslogtag,contains,"consul-template" /var/log/consul-template.log + +# Uncomment the following to stop logging anything that matches the last rule. +# Doing this will stop logging kernel generated UFW log messages to the file +# normally containing kern.* messages (eg, /var/log/kern.log) +& stop diff --git a/cookbooks/consul-template/files/etc/systemd/system/consul-template.service b/cookbooks/consul-template/files/etc/systemd/system/consul-template.service new file mode 100644 index 0000000..1acdc36 --- /dev/null +++ b/cookbooks/consul-template/files/etc/systemd/system/consul-template.service @@ -0,0 +1,13 @@ +[Unit] +Description=consul-template +Requires=network-online.target +After=network-online.target consul.service + +[Service] +EnvironmentFile=-/etc/default/consul-template +Restart=on-failure +ExecStart=/usr/local/bin/consul-template $OPTIONS -config=/etc/consul-template.d/conf/ +KillSignal=SIGINT + +[Install] +WantedBy=multi-user.target diff --git a/cookbooks/consul-template/install.rb b/cookbooks/consul-template/install.rb new file mode 100644 index 0000000..7c0b01a --- /dev/null +++ b/cookbooks/consul-template/install.rb @@ -0,0 +1,34 @@ +consulTemplate_zip = "#{node['consulTemplate']['zipPrefix']}#{node['consulTemplate']['version']}#{node['consulTemplate']['zipPostfix']}" +consulTemplate_url = "#{node['consulTemplate']['baseUrl']}#{node['consulTemplate']['version']}/#{consulTemplate_zip}" + +# バージョン確認して、アップデート必要かどうか確認 +result = run_command('which consul-template', error: false) +if result.exit_status != 0 + + # Download: + TMP = "/tmp/#{consulTemplate_zip}" + + execute "wget #{consulTemplate_url} -O #{TMP}" + + directory '/opt/consul-template' do + owner 'root' + group 'root' + mode '0755' + end + + execute "unzip #{TMP} -d /opt/consul-template/" do + not_if 'test -e /opt/consul-template/consul-template' + end + + # Change Owner and Permissions: + file "#{node['consulTemplate']['storage']}" do + owner 'root' + group 'root' + mode '755' + end + + # Create Link + link "#{node['consulTemplate']['location']}" do + to "#{node['consulTemplate']['storage']}" + end +end diff --git a/cookbooks/consul-template/setup.rb b/cookbooks/consul-template/setup.rb new file mode 100644 index 0000000..25d3460 --- /dev/null +++ b/cookbooks/consul-template/setup.rb @@ -0,0 +1,39 @@ +# Create `/var/log/vector`: +%w( /etc/consul-template.d/conf /etc/consul-template.d/templates ).each do |d| + directory d do + owner 'root' + group 'root' + mode '0755' + end +end + +%w( /etc/systemd/system/consul-template.service /etc/default/consul-template).each do |conf| + remote_file conf do + owner 'root' + group 'root' + mode '0644' + + notifies :run, 'execute[systemctl daemon-reload]', :immediately + end +end + +execute 'systemctl daemon-reload' do + action :nothing +end + +service 'consul-template' do + action [:enable, :restart] +end + +remote_file '/etc/rsyslog.d/30-consul-template.conf' do + owner 'root' + group 'root' + mode '0644' + + notifies :restart, 'service[rsyslog]', :immediately +end + +service 'rsyslog' do + action [ :nothing ] +end +