From 39f8fc5bc105804c731aa2e26a983fd5d61d7669 Mon Sep 17 00:00:00 2001 From: Kazuhiro MUSASHI Date: Sun, 22 Nov 2020 16:00:00 +0900 Subject: [PATCH] Install from `deb` package. --- cookbooks/consul/default.rb | 2 - cookbooks/consul/dnsmasq.rb | 2 + cookbooks/consul/install.rb | 61 +++++-------------- cookbooks/consul/prerequisites.rb | 14 ----- cookbooks/consul/setup.rb | 20 ++---- .../etc/apt/sources.list.d/consul.list | 1 + 6 files changed, 21 insertions(+), 79 deletions(-) delete mode 100644 cookbooks/consul/prerequisites.rb create mode 100644 cookbooks/consul/templates/etc/apt/sources.list.d/consul.list diff --git a/cookbooks/consul/default.rb b/cookbooks/consul/default.rb index 21b3499..0320fb8 100644 --- a/cookbooks/consul/default.rb +++ b/cookbooks/consul/default.rb @@ -1,7 +1,5 @@ include_recipe './attributes.rb' -include_recipe './prerequisites.rb' - include_recipe './install.rb' include_recipe './setup.rb' diff --git a/cookbooks/consul/dnsmasq.rb b/cookbooks/consul/dnsmasq.rb index a0c2c3f..92286db 100644 --- a/cookbooks/consul/dnsmasq.rb +++ b/cookbooks/consul/dnsmasq.rb @@ -1,3 +1,5 @@ +package 'dnsmasq' + %w(dnsmasq resolvconf systemd-resolved).each do |s| service s do action :nothing diff --git a/cookbooks/consul/install.rb b/cookbooks/consul/install.rb index db1fffa..099e904 100644 --- a/cookbooks/consul/install.rb +++ b/cookbooks/consul/install.rb @@ -1,53 +1,20 @@ -# ------------------------------------------- -# Calculating the latest `consul` version: -# ------------------------------------------- -download_url = '' -tag_version = '' +# Install `Consul`: +KEYSRV = "https://apt.releases.hashicorp.com/gpg" +ID = "A3219F7B" -begin - require 'net/http' - - uri = URI.parse('https://www.consul.io/downloads.html') - - Timeout.timeout(3) do - response = Net::HTTP.get_response(uri) - - if response.body =~ /consul_(\d+\.\d+\.\d+)/ - tag_version = $1 - download_url = \ - "#{node['consul']['base_binary_url']}#{tag_version}/consul_#{tag_version}_linux_#{node['consul']['arch']}.zip" - end - end -rescue - # Abort the chef client process: - raise 'Cannot connect to https://www.consul.io/downloads.html' +execute "apt-key adv --keyserver #{KEYSRV} --recv-keys #{ID}" do + not_if "apt-key list | grep #{ID}" end -# ------------------------------------------- -# Main Part -# ------------------------------------------- +# Retrieve the Ubuntu code: +DIST = run_command('lsb_release -cs').stdout.chomp -# バージョン確認して、アップデート必要かどうか確認 -result = run_command("consul version | grep #{tag_version}", error: false) -if result.exit_status != 0 - # Download: - execute "wget #{download_url} -O #{node['consul']['tmp_path']}" - - # Unzip: - execute "unzip -qo #{node['consul']['tmp_path']}" do - cwd '/opt/consul/bin/' - end - - file '/opt/consul/bin/consul' do - owner 'root' - group 'root' - mode '755' - end - - # Create link: - link '/usr/local/bin/consul' do - user 'root' - to '/opt/consul/bin/consul' - end +# Deploy the `apt` sources: +template '/etc/apt/sources.list.d/consul.list' do + action :create + variables(distribution: DIST) end +execute 'apt update' + +package 'consul' diff --git a/cookbooks/consul/prerequisites.rb b/cookbooks/consul/prerequisites.rb deleted file mode 100644 index 87f6d3f..0000000 --- a/cookbooks/consul/prerequisites.rb +++ /dev/null @@ -1,14 +0,0 @@ -# Ensure that `unzip` and `dnsmasq` are available: -%w( unzip dnsmasq ).each do |p| - package p do - action :install - end -end - -%w(/etc/consul.d /var/opt/consul /opt/consul/bin).each do |d| - directory d do - owner 'root' - group 'root' - mode '755' - end -end diff --git a/cookbooks/consul/setup.rb b/cookbooks/consul/setup.rb index 94ed8af..37d8037 100644 --- a/cookbooks/consul/setup.rb +++ b/cookbooks/consul/setup.rb @@ -1,12 +1,6 @@ -remote_file '/etc/supervisor/conf.d/consul.conf' do - owner 'root' - group 'root' - mode '644' -end - template '/etc/consul.d/config.json' do - owner 'root' - group 'root' + owner 'consul' + group 'consul' mode '644' variables(manager: node['consul']['manager'], @@ -18,17 +12,11 @@ template '/etc/consul.d/config.json' do end remote_file '/etc/consul.d/service-consul.json' do - owner 'root' - group 'root' + owner 'consul' + group 'consul' mode '644' end -execute 'Reload supervisor' do - user 'root' - - command '/usr/bin/supervisorctl update' -end - # iptables settings here: %w( 8300/tcp 8301/tcp 8301/udp 8500/tcp ).each do |port| execute "ufw allow #{port}" do diff --git a/cookbooks/consul/templates/etc/apt/sources.list.d/consul.list b/cookbooks/consul/templates/etc/apt/sources.list.d/consul.list new file mode 100644 index 0000000..16f9f11 --- /dev/null +++ b/cookbooks/consul/templates/etc/apt/sources.list.d/consul.list @@ -0,0 +1 @@ +deb [arch=amd64] https://apt.releases.hashicorp.com <%= @distribution %> main