From c8231635f1b84fc2eb2bca8bfe105458f40eafcf Mon Sep 17 00:00:00 2001 From: David Vanhoucke Date: Thu, 24 Oct 2024 15:37:45 +0100 Subject: [PATCH 1/5] minio site replication with mcli tool --- resources/libraries/minio_helpers.rb | 46 ++++++++++++++-- resources/providers/config.rb | 82 +++++++++++++++++++++++----- 2 files changed, 108 insertions(+), 20 deletions(-) diff --git a/resources/libraries/minio_helpers.rb b/resources/libraries/minio_helpers.rb index 8055250..d2f4469 100644 --- a/resources/libraries/minio_helpers.rb +++ b/resources/libraries/minio_helpers.rb @@ -1,6 +1,6 @@ module Minio module Helpers - def self.check_remote_port(host, port) + def check_remote_port(host, port) `nc -zv #{host} #{port} 2>&1` process_status = $? @@ -8,14 +8,14 @@ def self.check_remote_port(host, port) process_status.exitstatus == 0 end - def self.generate_random_key(len) + def generate_random_key(len) chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' key = '' len.times { key << chars[rand(chars.size)] } key end - def self.check_remote_hosts(hosts) + def check_remote_hosts(hosts) all_alive = true hosts.each do |host| host, port = host.split(':') @@ -24,11 +24,11 @@ def self.check_remote_hosts(hosts) all_alive end - def self.exists_minio_conf? + def exists_minio_conf? File.exist?('/etc/default/minio') end - def self.s3_ready? + def s3_ready? command_output = `serf members list` nodes = command_output.split("\n") @@ -40,5 +40,41 @@ def self.s3_ready? false end end + + def s3_running? + system('systemctl is-active minio --quiet') + end + + def mcli?(node_name) + File.exist?('/usr/local/bin/mcli') && system("/usr/local/bin/mcli alias ls | grep -q ^#{node_name}") + end + + def replication_started? + !system("/usr/local/bin/mcli admin replicate info local | grep -i 'SiteReplication is not enabled'") + end + + def follower? + !Dir.exist?('/var/minio/data/bucket') + end + + def member_of_replication_cluster?(node_name) + system("/usr/local/bin/mcli admin replicate info local | grep #{node_name}") + end + + def add_to_minio_replication(s3_hosts, node_name) + s3_nodes = s3_hosts.dup + s3_nodes.delete(node_name) + s3_nodes = s3_nodes.join(" ") + " " + node_name + system("/usr/local/bin/mcli admin replicate add #{s3_nodes}") + end + + def remove_from_minio_replication(node_name) + system("/usr/local/bin/mcli admin replicate rm #{node_name} #{node_name} --force") + end + + def remove_data_from_disk + system('rm -rf /var/minio/data') + end + end end diff --git a/resources/providers/config.rb b/resources/providers/config.rb index dcbc522..927bbb0 100644 --- a/resources/providers/config.rb +++ b/resources/providers/config.rb @@ -1,13 +1,19 @@ +# Cookbook:: minio +# Provider:: config + +include Minio::Helpers + action :add do begin user = new_resource.user s3_bucket = new_resource.s3_bucket s3_endpoint = new_resource.s3_endpoint + managers_with_minio = new_resource.managers_with_minio - if !Minio::Helpers.s3_ready? - s3_user = Minio::Helpers.generate_random_key(20) - s3_password = Minio::Helpers.generate_random_key(40) + if !s3_ready? + s3_user = generate_random_key(20) + s3_password = generate_random_key(40) else s3_user = new_resource.access_key_id s3_password = new_resource.secret_key_id @@ -15,7 +21,6 @@ dnf_package 'minio' do action :upgrade - flush_cache [:before] end execute 'create_user' do @@ -38,7 +43,7 @@ ignore_failure true supports status: true, reload: true, restart: true, enable: true action [:enable, :start] - only_if { Minio::Helpers.exists_minio_conf? } + only_if { exists_minio_conf? } end template '/etc/default/minio' do @@ -50,7 +55,7 @@ notifies :restart, 'service[minio]', :delayed end - unless Minio::Helpers.s3_ready? + unless s3_ready? template '/etc/redborder/s3_init_conf.yml' do source 's3_init_conf.yml.erb' variables( @@ -60,7 +65,7 @@ s3_endpoint: s3_endpoint ) end - + template '/root/.s3cfg_initial' do source 's3cfg_initial.erb' variables( @@ -71,6 +76,26 @@ end end + ruby_block 'check_minio_replication' do + block do + if managers_with_minio.count > 1 && mcli?(node['name']) + if replication_started? + if !member_of_replication_cluster?(node['name']) && follower? + add_to_minio_replication(managers_with_minio, node['name']) + Chef::Log.info("Added node to Minio replication : #{node['name']}") + end + elsif follower? + add_to_minio_replication(managers_with_minio, node['name']) + Chef::Log.info("Minio replication started on #{managers_with_minio}") + end + else + Chef::Log.info('no Minio replication on 1 node minio cluster') + end + end + action :run + only_if { s3_running? } + end + Chef::Log.info('Minio cookbook has been processed') rescue => e Chef::Log.error(e.message) @@ -78,6 +103,8 @@ end action :add_s3_conf_nginx do + s3_hosts = new_resource.s3_hosts + service 'nginx' do service_name 'nginx' ignore_failure true @@ -85,12 +112,6 @@ action [:nothing] end - execute 'rb_sync_minio_cluster' do - command '/usr/lib/redborder/bin/rb_sync_minio_cluster.sh' - action :nothing - end - - s3_hosts = new_resource.s3_hosts template '/etc/nginx/conf.d/s3.conf' do ignore_failure true source 's3.conf.erb' @@ -100,8 +121,6 @@ cookbook 'nginx' variables(s3_hosts: s3_hosts) notifies :restart, 'service[nginx]', :delayed - notifies :run, 'execute[rb_sync_minio_cluster]', :delayed - only_if { Minio::Helpers.check_remote_hosts(s3_hosts) } end end @@ -117,6 +136,23 @@ action :create end + directory '/root/.mcli/share' do + owner 'root' + group 'root' + mode '0755' + action :create + end + + file '/root/.mcli/share/downloads.json' do + action :touch + only_if { !::File.exist?('/root/.mcli/share/downloads.json') } + end + + file '/root/.mcli/share/uploads.json' do + action :touch + only_if { !::File.exist?('/root/.mcli/share/uploads.json') } + end + template '/root/.mcli/config.json' do source 'mcli_config.json.erb' cookbook 'minio' @@ -129,6 +165,22 @@ action :remove do begin + ruby_block 'check_minio_replication' do + block do + if replication_started? && mcli?(node['name']) + if member_of_replication_cluster?(node['name']) + remove_from_minio_replication(node['name']) + remove_data_from_disk + Chef::Log.info("removed node from Minio replication : #{node['name']}") + end + else + Chef::Log.info('no Minio replication started') + end + end + action :run + only_if { s3_running? } + end + service 'minio' do service_name 'minio' ignore_failure true From 6cacccbd376cd38d751887c40b124cb5ec05f053 Mon Sep 17 00:00:00 2001 From: Juan Soto Date: Tue, 12 Nov 2024 11:48:01 +0000 Subject: [PATCH 2/5] Fix lint problems --- .rubocop.yml | 5 ++++- resources/libraries/minio_helpers.rb | 3 +-- resources/metadata.rb | 2 ++ resources/providers/config.rb | 27 +++++++++++++-------------- resources/resources/config.rb | 2 ++ 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index f126235..3a7ce30 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,2 +1,5 @@ Style/SpecialGlobalVars: - Enabled: false \ No newline at end of file + Enabled: false +AllCops: + Exclude: + - 'packaging/rpm/*.spec' diff --git a/resources/libraries/minio_helpers.rb b/resources/libraries/minio_helpers.rb index d2f4469..0cd3c97 100644 --- a/resources/libraries/minio_helpers.rb +++ b/resources/libraries/minio_helpers.rb @@ -64,7 +64,7 @@ def member_of_replication_cluster?(node_name) def add_to_minio_replication(s3_hosts, node_name) s3_nodes = s3_hosts.dup s3_nodes.delete(node_name) - s3_nodes = s3_nodes.join(" ") + " " + node_name + s3_nodes = s3_nodes.join(' ') + ' ' + node_name system("/usr/local/bin/mcli admin replicate add #{s3_nodes}") end @@ -75,6 +75,5 @@ def remove_from_minio_replication(node_name) def remove_data_from_disk system('rm -rf /var/minio/data') end - end end diff --git a/resources/metadata.rb b/resources/metadata.rb index e4542c0..a64c879 100644 --- a/resources/metadata.rb +++ b/resources/metadata.rb @@ -1,3 +1,5 @@ +unified_mode true + name 'minio' maintainer 'Eneo TecnologĂ­a S.L.' maintainer_email 'git@redborder.com' diff --git a/resources/providers/config.rb b/resources/providers/config.rb index 927bbb0..cd4db87 100644 --- a/resources/providers/config.rb +++ b/resources/providers/config.rb @@ -1,11 +1,11 @@ # Cookbook:: minio -# Provider:: config +# Provider:: config include Minio::Helpers +unified_mode true action :add do begin - user = new_resource.user s3_bucket = new_resource.s3_bucket s3_endpoint = new_resource.s3_endpoint @@ -65,7 +65,7 @@ s3_endpoint: s3_endpoint ) end - + template '/root/.s3cfg_initial' do source 's3cfg_initial.erb' variables( @@ -78,7 +78,7 @@ ruby_block 'check_minio_replication' do block do - if managers_with_minio.count > 1 && mcli?(node['name']) + if managers_with_minio.count > 1 && mcli?(node['name']) if replication_started? if !member_of_replication_cluster?(node['name']) && follower? add_to_minio_replication(managers_with_minio, node['name']) @@ -89,7 +89,7 @@ Chef::Log.info("Minio replication started on #{managers_with_minio}") end else - Chef::Log.info('no Minio replication on 1 node minio cluster') + Chef::Log.info('no Minio replication on 1 node minio cluster') end end action :run @@ -145,12 +145,12 @@ file '/root/.mcli/share/downloads.json' do action :touch - only_if { !::File.exist?('/root/.mcli/share/downloads.json') } + not_if { ::File.exist?('/root/.mcli/share/downloads.json') } end file '/root/.mcli/share/uploads.json' do action :touch - only_if { !::File.exist?('/root/.mcli/share/uploads.json') } + not_if { ::File.exist?('/root/.mcli/share/uploads.json') } end template '/root/.mcli/config.json' do @@ -164,17 +164,16 @@ action :remove do begin - ruby_block 'check_minio_replication' do block do if replication_started? && mcli?(node['name']) if member_of_replication_cluster?(node['name']) - remove_from_minio_replication(node['name']) - remove_data_from_disk - Chef::Log.info("removed node from Minio replication : #{node['name']}") + remove_from_minio_replication(node['name']) + remove_data_from_disk + Chef::Log.info("removed node from Minio replication : #{node['name']}") end else - Chef::Log.info('no Minio replication started') + Chef::Log.info('no Minio replication started') end end action :run @@ -214,7 +213,7 @@ action :nothing end.run_action(:run) - node.normal['minio']['registered'] = true + node.override['minio']['registered'] = true Chef::Log.info('Minio service has been registered on consul') end @@ -232,7 +231,7 @@ action :nothing end.run_action(:run) - node.normal['minio']['registered'] = false + node.override['minio']['registered'] = false Chef::Log.info('Minio service has been deregistered from consul') end diff --git a/resources/resources/config.rb b/resources/resources/config.rb index ae1d74a..deed313 100644 --- a/resources/resources/config.rb +++ b/resources/resources/config.rb @@ -1,3 +1,5 @@ +unified_mode true + actions :add, :remove, :register, :deregister, :add_s3_conf_nginx, :add_mcli default_action :add From 34a8f22fdde0c3276bfecdee38c304b24de2954b Mon Sep 17 00:00:00 2001 From: Juan Soto Date: Wed, 13 Nov 2024 14:12:05 +0000 Subject: [PATCH 3/5] Delete unified_mode --- resources/libraries/minio_helpers.rb | 2 +- resources/metadata.rb | 2 -- resources/providers/config.rb | 5 ++--- resources/resources/config.rb | 2 -- 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/resources/libraries/minio_helpers.rb b/resources/libraries/minio_helpers.rb index 0cd3c97..c49998a 100644 --- a/resources/libraries/minio_helpers.rb +++ b/resources/libraries/minio_helpers.rb @@ -64,7 +64,7 @@ def member_of_replication_cluster?(node_name) def add_to_minio_replication(s3_hosts, node_name) s3_nodes = s3_hosts.dup s3_nodes.delete(node_name) - s3_nodes = s3_nodes.join(' ') + ' ' + node_name + s3_nodes = s3_nodes.join(" ") + " " + node_name system("/usr/local/bin/mcli admin replicate add #{s3_nodes}") end diff --git a/resources/metadata.rb b/resources/metadata.rb index a64c879..e4542c0 100644 --- a/resources/metadata.rb +++ b/resources/metadata.rb @@ -1,5 +1,3 @@ -unified_mode true - name 'minio' maintainer 'Eneo TecnologĂ­a S.L.' maintainer_email 'git@redborder.com' diff --git a/resources/providers/config.rb b/resources/providers/config.rb index cd4db87..2c28999 100644 --- a/resources/providers/config.rb +++ b/resources/providers/config.rb @@ -2,7 +2,6 @@ # Provider:: config include Minio::Helpers -unified_mode true action :add do begin @@ -213,7 +212,7 @@ action :nothing end.run_action(:run) - node.override['minio']['registered'] = true + node.normal['minio']['registered'] = true Chef::Log.info('Minio service has been registered on consul') end @@ -231,7 +230,7 @@ action :nothing end.run_action(:run) - node.override['minio']['registered'] = false + node.normal['minio']['registered'] = false Chef::Log.info('Minio service has been deregistered from consul') end diff --git a/resources/resources/config.rb b/resources/resources/config.rb index deed313..ae1d74a 100644 --- a/resources/resources/config.rb +++ b/resources/resources/config.rb @@ -1,5 +1,3 @@ -unified_mode true - actions :add, :remove, :register, :deregister, :add_s3_conf_nginx, :add_mcli default_action :add From 71574d51252a2c25cb4fe31f64e85a92b869d2f6 Mon Sep 17 00:00:00 2001 From: Juan Soto Date: Wed, 13 Nov 2024 14:14:29 +0000 Subject: [PATCH 4/5] Use single quote --- resources/libraries/minio_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/libraries/minio_helpers.rb b/resources/libraries/minio_helpers.rb index c49998a..0cd3c97 100644 --- a/resources/libraries/minio_helpers.rb +++ b/resources/libraries/minio_helpers.rb @@ -64,7 +64,7 @@ def member_of_replication_cluster?(node_name) def add_to_minio_replication(s3_hosts, node_name) s3_nodes = s3_hosts.dup s3_nodes.delete(node_name) - s3_nodes = s3_nodes.join(" ") + " " + node_name + s3_nodes = s3_nodes.join(' ') + ' ' + node_name system("/usr/local/bin/mcli admin replicate add #{s3_nodes}") end From 2768e1809b6c4e04f4afbfc5efdfeef666ab174e Mon Sep 17 00:00:00 2001 From: Juan Soto Date: Wed, 13 Nov 2024 14:30:35 +0000 Subject: [PATCH 5/5] Release 1.3.0 --- CHANGELOG.md | 5 +++++ resources/metadata.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5b687..7c893dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ cookbook-minio CHANGELOG =============== +## 1.3.0 + + - David Vanhoucke + - [c823163] minio site replication with mcli tool + ## 1.2.0 - manegron diff --git a/resources/metadata.rb b/resources/metadata.rb index e4542c0..90c8b32 100644 --- a/resources/metadata.rb +++ b/resources/metadata.rb @@ -3,4 +3,4 @@ maintainer_email 'git@redborder.com' license 'AGPL-3.0' description 'Installs/Configures Minio' -version '1.2.0' +version '1.3.0'