Skip to content

Commit

Permalink
#207 update swarm setup and rollback to support multiple containers o…
Browse files Browse the repository at this point in the history
…n same host
  • Loading branch information
maany committed May 14, 2020
1 parent d800860 commit 3411e7c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 95 deletions.
79 changes: 48 additions & 31 deletions lib/puppet/functions/simple_grid/generate_dns_file_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,63 @@
param 'String', :subnet
param 'String', :meta_info_prefix
param 'String', :dns_key
optional_param 'Boolean', :generate_unique_hosts
end
def generate_dns_file_content(augmented_site_level_config_file, subnet, meta_info_prefix, dns_key)
def generate_dns_file_content(augmented_site_level_config_file, subnet, meta_info_prefix, dns_key, generate_unique_hosts=false)
dns_content = Array.new
final_dns_content = Array.new
final_fqdn_content = Array.new
net = IPAddr.new subnet
ip_range = net.to_range.to_a
ip_offset = 1
ip_index = 10
data = YAML.load_file(augmented_site_level_config_file)
if data.key?(dns_key)
return {
"dns_pre_exists" => true,
"string" => data[dns_key],
"hash" => YAML.load(data[dns_key].to_yaml)
}
dns_pre_exists = data.key?(dns_key)
if dns_pre_exists
dns_content = YAML.load(data[dns_key].to_yaml)
# return {
# "dns_pre_exists" => true,
# "string" => data[dns_key],
# "hash" => YAML.load(data[dns_key].to_yaml)
# }
else
lightweight_components = data['lightweight_components']
lightweight_components.each do |lightweight_component|
name = lightweight_component['name']
meta_info_parent = generate_meta_info_parent_name(meta_info_prefix,name)
meta_info = data[meta_info_parent]
container_type = meta_info['type']
host_fqdn = lightweight_component['deploy']['node']
host_ip = get_host_ip(data['site_infrastructure'],host_fqdn)
level_2_configurator = simple_get_level_2_configurator(augmented_site_level_config_file, lightweight_component)
ip_address = ip_range[ip_index]
ip_index=ip_index+1
execution_id = lightweight_component['execution_id']
fqdn = host_fqdn.split('.')
hostname = fqdn[0]
domain = fqdn[1, fqdn.length].join('.')
if meta_info['level_2_configurators']["#{level_2_configurator}"]['docker_run_parameters'].key?("hostname")
container_fqdn = meta_info['level_2_configurators']["#{level_2_configurator}"]['docker_run_parameters']['hostname']
else
container_fqdn = [name, hostname, execution_id].join("_") + ".#{domain}"
end
dns_entry = {"container_fqdn" => "#{container_fqdn}", "host_fqdn" => host_fqdn, "host_ip" => host_ip,'container_ip' => ip_address.to_s, 'type' => container_type, 'execution_id' => execution_id}
dns_content << dns_entry
end
end
lightweight_components = data['lightweight_components']
lightweight_components.each do |lightweight_component|
name = lightweight_component['name']
meta_info_parent = generate_meta_info_parent_name(meta_info_prefix,name)
meta_info = data[meta_info_parent]
container_type = meta_info['type']
host_fqdn = lightweight_component['deploy']['node']
host_ip = get_host_ip(data['site_infrastructure'],host_fqdn)
level_2_configurator = simple_get_level_2_configurator(augmented_site_level_config_file, lightweight_component)
ip_address = ip_range[ip_index]
ip_index=ip_index+1
execution_id = lightweight_component['execution_id']
fqdn = host_fqdn.split('.')
hostname = fqdn[0]
domain = fqdn[1, fqdn.length].join('.')
if meta_info['level_2_configurators']["#{level_2_configurator}"]['docker_run_parameters'].key?("hostname")
container_fqdn = meta_info['level_2_configurators']["#{level_2_configurator}"]['docker_run_parameters']['hostname']
else
container_fqdn = [name, hostname, execution_id].join("_") + ".#{domain}"
end
dns_content << {"container_fqdn" => "#{container_fqdn}", "host_fqdn" => host_fqdn, "host_ip" => host_ip,'container_ip' => ip_address.to_s, 'type' => container_type, 'execution_id' => execution_id}
if generate_unique_hosts
dns_content.each do |dns|
if not final_fqdn_content.include? dns['host_fqdn']
final_dns_content << dns
final_fqdn_content << dns['host_fqdn']
end
end
else
final_dns_content = dns_content
end
{ "string" => dns_content.to_yaml.lines.to_a[1..-1].join,
"hash" => dns_content,
"dns_pre_exists" => false
{ "string" => final_dns_content.to_yaml.lines.to_a[1..-1].join,
"hash" => final_dns_content,
"dns_pre_exists" => dns_pre_exists
}
end

Expand Down
17 changes: 9 additions & 8 deletions manifests/ccm_function/config_orchestrator.pp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@
$manager_token = $swarm_status["tokens"]["manager"]
$worker_token = $swarm_status["tokens"]["worker"]
class{'simple_grid::components::swarm::configure::firewall':}
if $fqdn in $managers {
class {'simple_grid::components::swarm::join':
token => $manager_token,
main_manager => $main_manager,
role => 'manager'
}
} elsif $fqdn == $main_manager{
notify{'Not executing docker swarm join command as the node is the main swarm manager':}
if $fqdn == $main_manager {
notify{'Not executing docker swarm join command as the node is the main swarm manager':}
}
elsif $fqdn in $managers {
class {'simple_grid::components::swarm::join':
token => $manager_token,
main_manager => $main_manager,
role => 'manager'
}
}else {
class {'simple_grid::components::swarm::join':
token => $worker_token,
Expand Down
4 changes: 2 additions & 2 deletions manifests/components/swarm.pp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
$dns_parent_name = lookup('simple_grid::components::site_level_config_file::objects:dns_parent'),
$swarm_status_file = lookup('simple_grid::components::swarm::status_file')
){
$dns_file_content = simple_grid::generate_dns_file_content($augmented_site_level_config_file, $subnet, $meta_info_prefix, $dns_parent_name)
$dns_file_content = simple_grid::generate_dns_file_content($augmented_site_level_config_file, $subnet, $meta_info_prefix, $dns_parent_name, true)
$dns_file_content['hash'].each |Hash $dns|{
if $dns['host_fqdn'] == $main_manager {
$ip_addr = $dns['host_ip']
Expand Down Expand Up @@ -94,7 +94,7 @@
$retry_wrapper = lookup('simple_grid::scripts::wrapper::retry'),
$wrapper_dir = lookup('simple_grid::scripts::wrapper_dir'),
){
$dns_file_content = simple_grid::generate_dns_file_content($augmented_site_level_config_file, $subnet, $meta_info_prefix, $dns_parent_name)
$dns_file_content = simple_grid::generate_dns_file_content($augmented_site_level_config_file, $subnet, $meta_info_prefix, $dns_parent_name, true)
$dns_file_content['hash'].each |Hash $dns|{
if $dns['host_fqdn'] == $fqdn {
$ip_addr = $dns['host_ip']
Expand Down
66 changes: 12 additions & 54 deletions tasks/rollback_swarm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,8 @@
require_relative "../../ruby_task_helper/files/task_helper.rb"

class Deploy < TaskHelper
# Get all ip addresses
def get_element_ip(site_level_config_file_path, element)
ip = Array.new
data = YAML::load_file(site_level_config_file_path)
site_infrastructure = data["site_infrastructure"]
lightweight_components = data["lightweight_components"]
lightweight_components.each do |lc|
if lc["type"] == element then
site_infrastructure.each do |node|
if node['fqdn'] == lc['deploy']['node']
ip << node["ip_address"]
end
end
end
end
return ip
end
def swarm_leave_managers(main_manager, network, ingress_network_name, modulepath)
puts "Removing main manager on #{main_manager}"
get_cmd = "bolt task run simple_grid::swarm_leave force=true --targets #{main_manager} --modulepath #{modulepath}"
stdout, stderr, status = Open3.capture3(get_cmd)
puts get_cmd
if !status.success?
raise "Failed to leave swarm manager: #{stderr}"
end
# NOTE: Deprecated since #101 as docker swarm leave --force removes ingress network automatically
# puts "Removing substitute ingress network on #{main_manager}"
# rm_cmd = "bolt command run 'docker network rm #{ingress_network_name}' --targets #{main_manager} --modulepath #{modulepath}"
# stdout, stderr, status = Open3.capture3(rm_cmd)
# puts rm_cmd
# if !status.success?
# raise "Failed to remove #{ingress_network_name} network on #{main_manager}: #{stderr}"
# end
end
# Generate, extract token and join docker swarm
def swarm_leave_workers(wn_ip, modulepath)
wn_ip.each do |wip|
def swarm_leave(member_ips, modulepath)
member_ips.each do |wip|
puts "***"
leave_cmd = "bolt task run simple_grid::swarm_leave force=true --targets #{wip} --modulepath #{modulepath}"
puts leave_cmd
Expand All @@ -54,37 +19,30 @@ def swarm_leave_workers(wn_ip, modulepath)
end
end
end
def get_managers_and_workers(augmented_site_level_config_file)
def get_swarm_members(augmented_site_level_config_file)
data = YAML::load_file(augmented_site_level_config_file)
lightweight_components = data['lightweight_components']
site_infrastructure = data['site_infrastructure']
swarm_manager_ip = String.new
swarm_worker_ip_array = Array.new
swarm_member_ip_array = Array.new
lightweight_components.each do |lightweight_component|
node_fqdn = lightweight_component['deploy']['node']
site_infrastructure.each do |infrastructure|
if infrastructure['fqdn'] == node_fqdn
if lightweight_component['execution_id'] == 0
swarm_manager_ip = infrastructure['ip_address']
else
unless swarm_worker_ip_array.include? infrastructure['ip_address']
swarm_worker_ip_array << infrastructure['ip_address']
end
end
unless swarm_member_ip_array.include? infrastructure['ip_address']
swarm_member_ip_array << infrastructure['ip_address']
end
end
end
end
return swarm_manager_ip, swarm_worker_ip_array
return swarm_member_ip_array
end
def task(augmented_site_level_config_file:nil, network:nil, subnet:nil, ingress_network_name:nil, modulepath:nil, **kwargs)
swarm_manager_ip, swarm_worker_ip_array = get_managers_and_workers(augmented_site_level_config_file)
swarm_leave_workers(swarm_worker_ip_array, modulepath)
swarm_leave_managers(swarm_manager_ip, network, ingress_network_name, modulepath)
swarm_members = get_swarm_members(augmented_site_level_config_file)
swarm_leave(swarm_members, modulepath)

puts "#########################"
puts "Removed swarm manager on #{swarm_manager_ip}"
puts "Removed swarm workers on the following nodes:"
puts swarm_worker_ip_array
puts "Removed docker swarm on the following nodes:"
puts swarm_members
puts "#########################"
{status: 'success'}
end
Expand Down

0 comments on commit 3411e7c

Please sign in to comment.