Skip to content
Browse files

refactors elastic_ips and meal structure

  • Loading branch information...
1 parent 3baa208 commit 066537997b905c1c3c20bb3f9ca0ea9a80b24240 @slagyr slagyr committed
View
8 lib/boucher/compute.rb
@@ -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
2 lib/boucher/io.rb
@@ -17,7 +17,7 @@ def self.real!
end
def self.verbose(*args)
- if ENV["VERBOSE"] != "false"
+ if ENV["VERBOSE"]
puts *args
end
end
View
8 lib/boucher/meals.rb
@@ -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
50 lib/boucher/nagios.rb
@@ -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
48 lib/boucher/provision.rb
@@ -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
1 lib/boucher/servers.rb
@@ -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}
View
1 lib/boucher/tasks/console.rake
@@ -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
14 lib/boucher/tasks/env.rake
@@ -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|
View
74 lib/boucher/tasks/nagios.rake
@@ -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
View
7 lib/boucher/tasks/servers.rake
@@ -5,7 +5,6 @@ require 'boucher/meals'
require 'boucher/provision'
require 'boucher/servers'
require 'boucher/volumes'
-require 'boucher/nagios'
require 'retryable'
def meals
@@ -67,7 +66,6 @@ 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
@@ -75,7 +73,6 @@ web console and click Instance Actions -> Change Termination Protection -> Yes."
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"
View
5 spec/boucher/compute_spec.rb
@@ -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"
View
42 spec/boucher/provision_spec.rb
@@ -13,38 +13,40 @@
describe "establish server" do
it "provisions a server if one does not exist" do
- Boucher.stub(:meals).and_return({:class_of_server => "server details"})
- Boucher.should_receive(:provision).with("class_of_server", "server details")
+ Boucher.should_receive(:meal).with("some_meal").and_return(:meal)
+ Boucher.should_receive(:provision).with(:meal)
- Boucher.establish_server nil, "class_of_server"
+ Boucher.establish_server nil, "some_meal"
end
it "starts a server if it is stopped" do
server = mock(:id => "the id", :state => "stopped")
- Boucher.stub(:meals).and_return({:class_of_server => "server details"})
+ meal = {:name => "some_meal"}
+ Boucher.stub(:meal).and_return(meal)
Boucher.should_receive(:change_server_state).with("the id", :start, "running")
server.should_receive(:reload)
- Boucher.should_receive(:cook_meals_on_server).with("class_of_server", "server details", server)
+ Boucher.should_receive(:cook_meal_on_server).with(meal, server)
- Boucher.establish_server server, "class_of_server"
+ Boucher.establish_server server, "some_meal"
end
it "attaches elastic IPs if the server was stopped" do
- Boucher::Config[:elastic_ips] = { "class_of_server" => "1.2.3.4" }
server = mock(:id => "the id", :state => "stopped", :reload => nil)
- Boucher.stub(:meals).and_return({:class_of_server => {:meals => []}})
+ Boucher.stub(:meal).and_return({:name => "some_meal", :elastic_ips => %w(1.2.3.4)})
Boucher.stub(:change_server_state)
+ Boucher.stub(:cook_meal)
Boucher.compute.should_receive(:associate_address).with(anything, "1.2.3.4")
- Boucher.establish_server server, "class_of_server"
+ Boucher.establish_server server, "meal_name"
end
it "cooks meals on server if it is up and running" do
running_server = mock(:id => "the id", :state => "running")
- Boucher.stub(:meals).and_return({:class_of_server => "server details"})
- Boucher.should_receive(:cook_meals_on_server).with("class_of_server", "server details", running_server)
+ meal = {:name => "some_meal"}
+ Boucher.stub(:meal).and_return(meal)
+ Boucher.should_receive(:cook_meal_on_server).with(meal, running_server)
- Boucher.establish_server running_server, "class_of_server"
+ Boucher.establish_server running_server, "some_meal"
end
end
@@ -52,26 +54,18 @@
it "provisions a server" do
Boucher.stub!(:ssh)
Boucher.should_receive(:setup_meal)
- Boucher.should_receive(:cook_meal).with(anything, "foo")
+ Boucher.should_receive(:cook_meal).with(anything, "some_meal")
- Boucher.provision "foo", {:meals => ["foo"]}
- end
-
- it "provisions a server with Procs as meals" do
- Boucher.stub!(:ssh)
- Boucher.should_receive(:setup_meal)
- Boucher.should_receive(:cook_meal).with(anything, "foo")
-
- Boucher.provision "foo", {:meals => [lambda{"foo"}]}
+ Boucher.provision :name => "some_meal"
end
it "provisions a server with elastic IP" do
Boucher.stub!(:ssh)
- Boucher::Config[:elastic_ips] = { "foo" => "1.2.3.4" }
Boucher.should_receive(:setup_meal)
+ Boucher.stub(:cook_meal)
Boucher.compute.should_receive(:associate_address).with(anything, "1.2.3.4")
- Boucher.provision "foo", {:meals => []}
+ Boucher.provision :name => "some_meal", :elastic_ips => %w(1.2.3.4)
end
end
end
2 template
@@ -1 +1 @@
-Subproject commit fec8edfd3b3cf90c7b42c42e1bbdfd62197b82ca
+Subproject commit 1e78a0516fac088983d36733dcc79d94d7282b63

0 comments on commit 0665379

Please sign in to comment.
Something went wrong with that request. Please try again.