From db461a97bcf17eae463e932f9cf4d6f3cc1bb81f Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Mon, 21 Sep 2020 19:18:53 +0000 Subject: [PATCH] Switch to installing prometheus exporters from a custom git repository --- cookbooks/apache/recipes/default.rb | 2 - cookbooks/memcached/recipes/default.rb | 1 - cookbooks/prometheus/metadata.rb | 1 + cookbooks/prometheus/recipes/default.rb | 14 +++++- cookbooks/prometheus/resources/exporter.rb | 51 +++++----------------- 5 files changed, 26 insertions(+), 43 deletions(-) diff --git a/cookbooks/apache/recipes/default.rb b/cookbooks/apache/recipes/default.rb index 039b55faf..d443f1e40 100644 --- a/cookbooks/apache/recipes/default.rb +++ b/cookbooks/apache/recipes/default.rb @@ -97,8 +97,6 @@ munin_plugin "apache_processes" munin_plugin "apache_volume" prometheus_exporter "apache" do - github_owner "Lusitaniae" - version "0.8.0" port 9117 listen_switch "telemetry.address" options "--scrape_uri=http://localhost/server-status?auto" diff --git a/cookbooks/memcached/recipes/default.rb b/cookbooks/memcached/recipes/default.rb index bc24aef65..56916ec9e 100644 --- a/cookbooks/memcached/recipes/default.rb +++ b/cookbooks/memcached/recipes/default.rb @@ -46,7 +46,6 @@ end end prometheus_exporter "memcached" do - version "0.7.0" port 9150 options "--memcached.address=#{node[:memcached][:ip_address]}:#{node[:memcached][:tcp_port]} --memcached.pid-file=/run/memcached/memcached.pid" end diff --git a/cookbooks/prometheus/metadata.rb b/cookbooks/prometheus/metadata.rb index d3167f888..e6173a037 100644 --- a/cookbooks/prometheus/metadata.rb +++ b/cookbooks/prometheus/metadata.rb @@ -8,4 +8,5 @@ version "1.0.0" supports "ubuntu" depends "apache" depends "apt" +depends "git" depends "networking" diff --git a/cookbooks/prometheus/recipes/default.rb b/cookbooks/prometheus/recipes/default.rb index 7cfd70b6a..820d46b2c 100644 --- a/cookbooks/prometheus/recipes/default.rb +++ b/cookbooks/prometheus/recipes/default.rb @@ -17,6 +17,7 @@ # limitations under the License. # +include_recipe "git" include_recipe "networking" if node.internal_ipaddress @@ -39,13 +40,24 @@ else end directory "/opt/prometheus" do + action :delete owner "root" group "root" mode "755" + recursive true + not_if { ::Dir.exist?("/opt/prometheus/.git") } +end + +git "/opt/prometheus" do + action :sync + repository "https://github.com/openstreetmap/prometheus-exporters.git" + revision "main" + depth 1 + user "root" + group "root" end prometheus_exporter "node" do - version "1.0.1" port 9100 options "--collector.ntp --collector.processes --collector.interrupts" end diff --git a/cookbooks/prometheus/resources/exporter.rb b/cookbooks/prometheus/resources/exporter.rb index fd6e85467..ba214b5d6 100644 --- a/cookbooks/prometheus/resources/exporter.rb +++ b/cookbooks/prometheus/resources/exporter.rb @@ -20,36 +20,11 @@ default_action :create property :exporter, :kind_of => String, :name_property => true -property :github_owner, :kind_of => String, :default => "prometheus" -property :github_project, :kind_of => String -property :version, :kind_of => String, :required => [:create] property :port, :kind_of => Integer, :required => [:create] property :listen_switch, :kind_of => String, :default => "web.listen-address" property :options, :kind_of => [String, Array] action :create do - package "prometheus-#{new_resource.exporter}-exporter" do - action :purge - end - - remote_file archive_file do - action :create_if_missing - source archive_url - owner "root" - group "root" - mode "644" - backup false - end - - execute archive_file do - action :nothing - command "tar -xf #{archive_file}" - cwd "/opt/prometheus" - user "root" - group "root" - subscribes :run, "remote_file[#{archive_file}]" - end - systemd_service service_name do description "Prometheus #{new_resource.exporter} exporter" type "simple" @@ -83,30 +58,24 @@ action :delete do action [:disable, :stop] end - package package_name do - action :purge + systemd_service service_name do + action :delete end end -action_class do - def github_project - new_resource.github_project || "#{new_resource.exporter}_exporter" - end - - def archive_url - "https://github.com/#{new_resource.github_owner}/#{github_project}/releases/download/v#{new_resource.version}/#{github_project}-#{new_resource.version}.linux-amd64.tar.gz" - end - - def archive_file - "#{Chef::Config[:file_cache_path]}/prometheus-#{new_resource.exporter}-exporter-#{new_resource.version}.tar.gz" +action :restart do + service service_name do + action :restart end +end +action_class do def service_name "prometheus-#{new_resource.exporter}-exporter" end def executable_path - "/opt/prometheus/#{github_project}-#{new_resource.version}.linux-amd64/#{github_project}" + "/opt/prometheus/exporters/#{new_resource.exporter}/#{new_resource.exporter}_exporter" end def executable_options @@ -121,3 +90,7 @@ action_class do end end end + +def after_created + subscribes :restart, "git[/opt/prometheus]" +end -- 2.45.1