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/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/libraries/minio_helpers.rb b/resources/libraries/minio_helpers.rb index 8055250..0cd3c97 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,40 @@ 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/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' diff --git a/resources/providers/config.rb b/resources/providers/config.rb index dcbc522..2c28999 100644 --- a/resources/providers/config.rb +++ b/resources/providers/config.rb @@ -1,13 +1,18 @@ +# 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 +20,6 @@ dnf_package 'minio' do action :upgrade - flush_cache [:before] end execute 'create_user' do @@ -38,7 +42,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 +54,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( @@ -71,6 +75,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 +102,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 +111,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 +120,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 +135,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 + not_if { ::File.exist?('/root/.mcli/share/downloads.json') } + end + + file '/root/.mcli/share/uploads.json' do + action :touch + not_if { ::File.exist?('/root/.mcli/share/uploads.json') } + end + template '/root/.mcli/config.json' do source 'mcli_config.json.erb' cookbook 'minio' @@ -128,6 +163,21 @@ 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'