Permalink
Browse files

refactors elastic_ips and meal structure

  • Loading branch information...
slagyr committed Sep 25, 2012
1 parent 3baa208 commit 066537997b905c1c3c20bb3f9ca0ea9a80b24240
View
@@ -60,13 +60,9 @@ def self.update_recipes(server)
end
end
- def self.cook_meal(server, meal)
- Boucher::Nagios.remove_host(server)
-
+ def self.cook_meal(server, meal_name)
update_recipes(server)
- ssh server, "cd infrastructure && git pull && sudo BUTCHER_ENV=#{Boucher::Config[:env]} BRANCH=#{Boucher::Config[:branch]} chef-solo -c config/solo.rb -j config/#{meal}.json"
-
- Boucher::Nagios.add_host(server)
+ ssh server, "cd infrastructure && git pull && sudo BUTCHER_ENV=#{Boucher::Config[:env]} BRANCH=#{Boucher::Config[:branch]} chef-solo -c config/solo.rb -j config/#{meal_name}.json"
end
def self.ssh_open?(server)
View
@@ -17,7 +17,7 @@ def self.real!
end
def self.verbose(*args)
- if ENV["VERBOSE"] != "false"
+ if ENV["VERBOSE"]
puts *args
end
end
View
@@ -16,12 +16,18 @@ def self.meals
Dir.glob(File.join("config", "*.json")).each do |file|
spec = json_to_meal(::IO.read(file))
meal_name = File.basename(file)[0...-5].to_sym
- @meals[meal_name] = spec
+ @meals[meal_name] = spec.merge(:name => meal_name)
end
end
@meals
end
+ def self.meal(meal_name)
+ the_meal = meals[meal_name.to_sym]
+ raise "Missing meal: #{mean_name}" unless the_meal
+ return the_meal
+ end
+
def self.setup_meal(server, meal)
server.image_id = meal[:base_image_id] || Boucher::Config[:base_image_id]
server.flavor_id = meal[:flavor_id] || Boucher::Config[:default_instance_flavor_id]
View
@@ -1,50 +0,0 @@
-require 'boucher/servers'
-require 'boucher/compute'
-
-module Boucher
- module Nagios
- def self.remove_host(host)
- return if host.tags["Meal"] == "nagios_server"
-
- monitors_for(host.tags["Env"]).each do |monitor|
- commands = [
- "cd /home/#{Boucher::Config[:username]}/infrastructure",
- "sudo rake nagios:remove_host[#{host.id}]",
- "sudo /etc/init.d/nagios3 restart"
- ]
- Boucher.ssh(monitor, commands.join(" && "))
- end
- end
-
- def self.add_host(host)
- return if checks_for(host.tags["Meal"]).empty?
-
- monitors_for(host.tags["Env"]).each do |monitor|
- commands = [
- "cd /home/#{Boucher::Config[:username]}/infrastructure",
- "sudo rake nagios:add_host[#{host.id},#{host.public_ip_address},#{host.tags["Meal"]}]",
- "sudo /etc/init.d/nagios3 restart"
- ]
-
- Boucher.ssh(monitor, commands.join(" && "))
- end
- end
-
- def self.checks_for(meal)
- config_path = File.expand_path("../../../config/#{meal}.json", __FILE__)
- return [] unless File.exists? config_path
- server_configuration = JSON.parse(File.read config_path)
- server_configuration["nagios"] || []
- end
-
- private
-
- def self.monitors_for(env)
- Boucher::Servers.all.select do |monitor|
- monitor.tags["Meal"] == "nagios_server" &&
- monitor.tags["Env"] == env &&
- monitor.dns_name
- end
- end
- end
-end
View
@@ -2,7 +2,6 @@
require 'boucher/io'
require 'boucher/servers'
require 'boucher/volumes'
-require 'boucher/nagios'
require 'retryable'
module Boucher
@@ -30,47 +29,44 @@ def self.establish_all_servers
end
end
- def self.establish_server(server, meal)
+ def self.establish_server(server, meal_name)
+ meal = Boucher.meal(meal_name)
if server.nil?
- Boucher.provision(meal, Boucher.meals[meal.to_sym])
+ Boucher.provision(meal)
elsif server.state == "stopped"
Boucher::Servers.start(server.id)
server.reload
- Boucher.cook_meals_on_server(meal, Boucher.meals[meal.to_sym], server)
+ Boucher.cook_meal_on_server(meal, server)
else
- Boucher.cook_meals_on_server(meal, Boucher.meals[meal.to_sym], server)
+ Boucher.cook_meal_on_server(meal, server)
end
end
- def self.provision(meal, meal_map)
- puts "Provisioning new #{meal} server..."
- server = create_meal_server(meal_map)
+ def self.provision(meal)
+ puts "Provisioning new #{meal[:name]} server..."
+ server = create_meal_server(meal)
wait_for_server_to_boot(server)
wait_for_server_to_accept_ssh(server)
- volumes = create_volumes(meal_map, server)
+ volumes = create_volumes(meal, server)
attach_volumes(volumes, server)
- cook_meals_on_server(meal, meal_map, server)
- puts "\nThe new #{meal} server has been provisioned! id: #{server.id}"
+ cook_meal_on_server(meal, server)
+ puts "\nThe new #{meal[:name]} server has been provisioned! id: #{server.id}"
end
def self.attach_elastic_ips(meal, server)
puts "Attaching elastic IPs..."
- return unless Boucher::Config[:elastic_ips] && Boucher::Config[:elastic_ips][meal]
+ ips = meal[:elastic_ips] || []
- puts "Associating #{server.id} with #{Boucher::Config[:elastic_ips][meal]}"
- compute.associate_address(server.id, Boucher::Config[:elastic_ips][meal])
+ ips.each do |ip|
+ puts "Associating #{server.id} with #{ip}"
+ compute.associate_address(server.id, ip)
+ end
end
private
- def self.cook_meals_on_server(meal, meal_map, server)
- return unless meal_map[:meals]
-
- meal_map[:meals].each do |meal|
- meal = meal.call if meal.is_a?(Proc)
- Boucher.cook_meal(server, meal)
- end
-
+ def self.cook_meal_on_server(meal, server)
+ Boucher.cook_meal(server, meal[:name])
attach_elastic_ips(meal, server)
end
@@ -87,16 +83,16 @@ def self.wait_for_server_to_boot(server)
Boucher.print_servers([server])
end
- def self.create_meal_server(meal_map)
+ def self.create_meal_server(meal)
server = compute.servers.new(:tags => {})
- Boucher.setup_meal(server, meal_map)
+ Boucher.setup_meal(server, meal)
server.save
Boucher.print_servers([server])
server
end
- def self.create_volumes(meal_map, server)
- Array(meal_map[:volumes]).map do |volume_name|
+ def self.create_volumes(meal, server)
+ Array(meal[:volumes]).map do |volume_name|
attributes = Boucher.volume_configs[volume_name]
snapshot = snapshots.get(attributes[:snapshot])
puts "Creating volume from snapshot #{snapshot.id}..."
View
@@ -68,7 +68,6 @@ def self.stop(server_id)
end
def self.terminate(server)
- Boucher::Nagios.remove_host(server)
volumes = server.volumes
volumes_to_destroy = volumes.select {|v| !v.delete_on_termination}
@@ -5,7 +5,6 @@ require 'boucher/meals'
require 'boucher/provision'
require 'boucher/servers'
require 'boucher/volumes'
-require 'boucher/nagios'
desc "Starts a console with the Boucher modules loaded"
task :console do
View
@@ -4,13 +4,13 @@ require 'retryable'
namespace :env do
- desc "Attaches elastic IPs"
- task :elastic_ips, [:env] do |t, args|
- Boucher.force_env!(args.env)
- Boucher.each_required_server do |server, meal|
- Boucher.attach_elastic_ips(meal, server)
- end
- end
+ #desc "Attaches elastic IPs"
+ #task :elastic_ips, [:env] do |t, args|
+ # Boucher.force_env!(args.env)
+ # Boucher.each_required_server do |server, meal|
+ # Boucher.attach_elastic_ips(meal, server)
+ # end
+ #end
desc "Starts and deploys all the servers for the specified environment"
task :start, [:env] do |t, args|
@@ -1,74 +0,0 @@
-require 'json'
-require 'fileutils'
-
-def make_config_path(host_name)
- "/etc/nagios3/conf.d/#{host_name}.cfg"
-end
-
-def host_entry(host_name, ip)
-<<-HOST
-define host {
- host_name #{host_name}
- alias #{host_name}
- address #{ip}
- check_command check_ssh
- notification_interval 0
- notification_period 24x7
- max_check_attempts 10
- notification_options d,u,r
- notifications_enabled 1
-}
-HOST
-end
-
-def service_entry(host_name, ip, plugin)
-<<-SERVICE
-define command {
- command_name #{host_name}-#{plugin}
- command_line /usr/lib/nagios/plugins/check_nrpe -H #{ip} -c "#{plugin}"
-}
-
-define service {
- use generic-service
- host_name #{host_name}
- check_command #{host_name}-#{plugin}
- service_description #{plugin}
- normal_check_interval 5
- check_period 24x7
- notifications_enabled 1
-}
-SERVICE
-end
-
-namespace :nagios do
- desc "Remove a nagios host from this machine"
- task :remove_host, [:name] do |t, args|
- FileUtils.rm_f make_config_path(args.name)
- end
-
- desc "Adds a nagios host to be monitored by this machine"
- task :add_host, [:name, :ip, :meal] do |t, args|
- checks = Boucher::Nagios.checks_for(args.meal)
- return if checks.empty?
-
- File.open(make_config_path(args.name), "w") do |file|
- host_name = "#{args.meal}-#{args.name}"
-
- file.puts host_entry(host_name, args.ip)
-
- checks.each do |plugin, command|
- file.puts
- file.puts service_entry(host_name, args.ip, plugin)
- end
- end
- end
-
- desc "Opens the nagios web console"
- task :open do
- server = Boucher::Servers["nagios_server"]
- url = "http://#{server.public_ip_address}/nagios3"
- puts "Nagios lives at #{url}"
- puts "Login using nagiosadmin / 'we are many lonely souls'"
- `open #{url}`
- end
-end
@@ -5,7 +5,6 @@ require 'boucher/meals'
require 'boucher/provision'
require 'boucher/servers'
require 'boucher/volumes'
-require 'boucher/nagios'
require 'retryable'
def meals
@@ -67,15 +66,13 @@ web console and click Instance Actions -> Change Termination Protection -> Yes."
desc "Stops the specified server"
task :stop, [:server_id] do |t, args|
server = Boucher.compute.servers.get(args.server_id)
- Boucher::Nagios.remove_host(server)
Boucher::Servers.stop(args.server_id)
end
desc "Starts the specified server"
task :start, [:server_id] do |t, args|
Boucher::Servers.start(args.server_id)
server = Boucher.compute.servers.get(args.server_id)
- Boucher::Nagios.add_host(server)
end
desc "Open an SSH session with the specified server"
@@ -103,8 +100,8 @@ web console and click Instance Actions -> Change Termination Protection -> Yes."
desc "Provision new server [#{Boucher.meals.keys.sort.join(', ')}]"
task :provision, [:meal] do |t, args|
- meal_map = Boucher.meals[args.meal.to_sym]
- Boucher.provision(args.meal, meal_map)
+ meal = Boucher.meal(args.meal)
+ Boucher.provision(meal)
end
desc "Provision new, or chef existing server of the specified meal"
@@ -1,6 +1,5 @@
require_relative "../spec_helper"
require 'boucher/compute'
-require 'boucher/nagios'
require 'ostruct'
describe "Boucher Cloud" do
@@ -64,9 +63,7 @@
server = OpenStruct.new(:id => "test_id")
Boucher.should_receive(:update_recipes).with(server)
- Boucher.should_receive(:ssh).with(server, "cd infrastructure && sudo BUTCHER_ENV=env_name BRANCH=branch_name chef-solo -c config/solo.rb -j config/meal_name.json")
- Boucher::Nagios.should_receive(:remove_host).with(server)
- Boucher::Nagios.should_receive(:add_host).with(server)
+ Boucher.should_receive(:ssh).with(server, "cd infrastructure && git pull && sudo BUTCHER_ENV=env_name BRANCH=branch_name chef-solo -c config/solo.rb -j config/meal_name.json")
Boucher::Config[:branch] = "branch_name"
Boucher::Config[:env] = "env_name"
Oops, something went wrong.

0 comments on commit 0665379

Please sign in to comment.