Module: Clusterlb

Defined in:
lib/clusterlb.rb,
lib/clusterlb/version.rb

Constant Summary collapse

VERSION =
"0.1.12"

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.config_dirObject

Returns the value of attribute config_dir.



15
16
17
# File 'lib/clusterlb.rb', line 15

def config_dir
  @config_dir
end

.config_fileObject

Returns the value of attribute config_file.



16
17
18
# File 'lib/clusterlb.rb', line 16

def config_file
  @config_file
end

Instance Method Details

#aws_configObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/clusterlb.rb', line 64

def aws_config
  aws_config_file="#{ENV["HOME"]}/.aws/credentials"
  if !File.exists? aws_config_file
    puts "Please configure your AWS credentials in #{ENV["HOME"]}/.aws/credentials, and setup profile with the name of #{config["aws"]["profile"]}"
    exit 1
  end
  awsconfig = IniFile.load(aws_config_file)[config["aws"]["profile"]]
  if awsconfig.empty?
    puts "It looks like you're  #{ENV["HOME"]}/.aws/credentials file has no section configured for #{config["aws"]["profile"]} profile. Please Fix this."
    exit 1
  end
  Aws.config.update({
    region: config["aws"]["region"],
    credentials: Aws::Credentials.new(awsconfig['aws_access_key_id'],  awsconfig['aws_secret_access_key'])
  })
end

#cmd_nginx(cmd, node) ⇒ Object

test | reload | restart | stop | start , lb01 or “all”



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/clusterlb.rb', line 40

def cmd_nginx(cmd,node) # test | reload | restart | stop | start ,  lb01 or "all"
  allowed_commands=["test","reload","restart","stop","start"]
  hosts=[]
  if !allowed_commands.include? cmd
    puts "command not in the allowed list: #{allowed_commands.join(',')}".colorize(:red)
    exit 1
  end
  cmd= "configtest" if cmd == "test"

  if node == "all"
    list_lbs.each do |h|
      hosts.push "#{h}.#{config["clusterlb"]["lb_nodes_dns_postfix"]}"
    end
  else
    hosts.push "#{node}.#{config["clusterlb"]["lb_nodes_dns_postfix"]}"
  end

  hosts.each do |h|
    puts "#{h}:".colorize(:light_blue)
    go_ssh(h,"sudo /etc/init.d/nginx #{cmd}",ENV['USER'])
    puts "--\n".colorize(:light_blue)
  end
end

#configObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/clusterlb.rb', line 27

def config
  envCheck
  writeSampleConfig(config_file) if !File.exists? config_file
  begin
    config = JSON.parse(File.read config_file)
  rescue
    puts "ERROR: Unable to load config file: #{ENV["CLUSTERLB_HOME"]}/configs/clusterlb.json "
    exit 1
  end
end

#disable_site(site, node) ⇒ Object



174
175
176
177
178
179
# File 'lib/clusterlb.rb', line 174

def disable_site(site,node)
  Dir.chdir ENV["CLUSTERLB_HOME"]
  link_dir="#{config["clusterlb"]["sites_enabled"]}/#{node}"
  FileUtils.rm("#{link_dir}/#{site}")
  puts "Disableing Site #{site} on #{node}"
end

#enable_site(site, node) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/clusterlb.rb', line 156

def enable_site(site,node)
  Dir.chdir ENV["CLUSTERLB_HOME"]
  link_to_dir="#{config["clusterlb"]["sites_enabled"]}/#{node}"

  if !config["clusterlb"]["lb_nodes"].include? node
    puts "#{node} is not listed as an active loadballancer, check your configs".colorize(:yellow)
    exit 1
  end

  if !File.directory?(link_to_dir)
    puts "#{link_to_dir} does not exist, check your configs".colorize(:yellow)
    exit 1
  end

  FileUtils.ln_s "../../#{config["clusterlb"]["sites"]}/#{site}", "#{link_to_dir}/#{site}", force: true
  puts "Enableing Site #{site} on #{node}"
end

#get_s3_cert(fqdn) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/clusterlb.rb', line 118

def get_s3_cert(fqdn)

  ensure_dir_exitsts
  aws_config
  certs_dir="#{ENV["CLUSTERLB_HOME"]}/#{config["clusterlb"]["certificates_dir"]}"
  Dir.chdir certs_dir

  s3 = Aws::S3::Client.new
  resp = s3.list_objects_v2({
    bucket: config["aws"]["ssl_cert_bucket"],
    prefix: fqdn
  })
  date_list=[]

  if resp["contents"].length <= 0
    puts "Sorry, i found no Certificates with that prefix #{fqdn}"
    exit 1
  end

  resp["contents"].each do |k|
    date_list.push(DateTime.strptime(k["key"].match('\d{1,2}-\d{1,2}-\d{4}').to_s, "%m-%d-%Y"))
  end

  file_extensions=["full.pem","key","pem","ca.pem"]
  newest_cert_date_string = date_list.sort.reverse.first.strftime("%-m-%-d-%Y")

  file_extensions.each do |ext|
    filename="#{fqdn}.#{newest_cert_date_string}.#{ext}"
    puts "Getting: #{filename} "
    sourceObj = Aws::S3::Object.new(config["aws"]["ssl_cert_bucket"], "#{filename}")
    sourceObj.get(response_target: "#{certs_dir}/#{filename}") if !File.exists? "#{certs_dir}/#{filename}"
    FileUtils.ln_s filename, "#{fqdn}.#{ext}", force: true
    FileUtils.chown(nil,config["clusterlb"]["nginx_unix_group"],filename)

  end
end

#letsEncrypt(fqdn) ⇒ Object

:LetsEncrypt =>

:sites_enabled => [],
:challange_dir => "LetsEncrypt/challage",
:certificates_dir => "LetsEncrypt/certs",
:acme_home_dir => "LetsEncrypt/.acme.sh",
:acme_bin => "/srv/lb-config/lets-encrypt/.acme.sh"



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/clusterlb.rb', line 88

def letsEncrypt(fqdn) # fqdn | all
  if fqdn == "all" && config["LetsEncrypt"]["sites_enabled"].count > 0
    config["LetsEncrypt"]["sites_enabled"].each do |site|
      letsEncrypt_getCert(site)
    end
    cmd_nginx("reload","all")
  else
    letsEncrypt_getCert(fqdn)
    cmd_nginx("reload","all")
  end

end

#letsEncrypt_getCert(fqdn) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/clusterlb.rb', line 102

def letsEncrypt_getCert(fqdn)
  le_env = {
    "le_challange_dir" => "#{ENV["CLUSTERLB_HOME"]}/#{config["LetsEncrypt"]["challange_dir"]}",
    "le_cert_dir"      => config["LetsEncrypt"]["certificates_dir"],
    "acme"             => config["LetsEncrypt"]["acme_bin"],
    "le_home"          => "#{ENV["CLUSTERLB_HOME"]}/#{config["LetsEncrypt"]["acme_home_dir"]}",
  }
  puts "Trying to renew Certificate: #{site}".colorize(:light_blue)
  cmd = "sudo ${acme} --cron --home \"${le_home}\" --issue  -d ${1} -w ${le_challange_dir} \
          --cert-file ${le_cert_dir}/#{site}.pem \
          --key-file  ${le_cert_dir}/#{site}.key \
          --fullchain-file ${le_cert_dir}/#{site}.full.pem"
  system(le_env, cmd)
  puts "--\n".colorize(:light_blue)
end

#list_lbsObject



22
23
24
25
# File 'lib/clusterlb.rb', line 22

def list_lbs
  ensure_dir_exitsts
  config["clusterlb"]["lb_nodes"]
end

#matrix_listObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/clusterlb.rb', line 181

def matrix_list
  table_config = [{:key=>:site, :size=>35, :title=>"Site"}]
  config["clusterlb"]["lb_nodes"].each do |node|
    table_config.push( {:key=>node.to_sym, :size=>9, :title=>node, :justify=>:center})
  end
  ConsoleTable.define(table_config) do |table|
      Dir.glob("#{ENV["CLUSTERLB_HOME"]}/#{config["clusterlb"]["sites"]}/*").each do |full_path_file|
        filename=full_path_file.split('/')[-1]
        result={:site=>filename}
        config["clusterlb"]["lb_nodes"].each do |node|
          result[node.to_sym]="✓".colorize(:green) if File.exists?("#{ENV["CLUSTERLB_HOME"]}/#{config["clusterlb"]["sites_enabled"]}/#{node}/#{filename}")
        end
        table << result

      end
    end

end