Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minio site replication with mcli tool #29

Merged
merged 10 commits into from
Nov 13, 2024
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Style/SpecialGlobalVars:
Enabled: false
Enabled: false
AllCops:
Exclude:
- 'packaging/rpm/*.spec'
45 changes: 40 additions & 5 deletions resources/libraries/minio_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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 = $?

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(':')
Expand All @@ -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")
Expand All @@ -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
80 changes: 65 additions & 15 deletions resources/providers/config.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
# 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
end

dnf_package 'minio' do
action :upgrade
flush_cache [:before]
end

execute 'create_user' do
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -71,26 +75,42 @@
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)
end
end

action :add_s3_conf_nginx do
s3_hosts = new_resource.s3_hosts

service 'nginx' do
service_name 'nginx'
ignore_failure true
supports status: true, reload: true, restart: true, enable: true
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'
Expand All @@ -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

Expand All @@ -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'
Expand All @@ -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'
Expand Down
Loading