Permalink
Browse files

Allow specifying the username for ssh commands

This is useful since the AMIs are already customizable and the username
will vary based on it.
  • Loading branch information...
1 parent 6a11f0d commit 8c6d31fe2275486a235a251c9f2cf5dbe99f02f7 @trptcolin trptcolin committed Apr 10, 2013
Showing with 23 additions and 5 deletions.
  1. +1 −0 README.md
  2. +8 −1 lib/boucher/compute.rb
  3. +14 −4 spec/boucher/compute_spec.rb
View
@@ -121,6 +121,7 @@ allows you to add extra configuration information under the "Boucher": key. For
"base_image_id": "ami-abcd1234", // overides :default_image_id config
"flavor_id": "t1.micro", // overides :default_flavor_id config
"groups": ["SSH"], // overides :default_groups config
+ "username": "ec2-user", // overides :username config
"key_name": ["some_key"], // overides :aws_key_filename config
"elastic_ips": ["1.2.3.4"], // a list of elastic IPs that'll be attached to the server. Elastic IP's acquired via AWS management console.
"volumes": {"/dev/sda2": <volume spec>} // See Volume Specs below
View
@@ -28,7 +28,14 @@ def self.ssh(server, command=nil)
command_arg = "\"#{command}\""
end
- command = "#{ssh_command} #{Boucher::Config[:username]}@#{server.dns_name} #{command_arg}"
+ meal = {}
+ meal_name = server.tags["Meal"]
+ if meal_name
+ meal = Boucher.meal(meal_name)
+ end
+ username = meal[:username] || Boucher::Config[:username]
+
+ command = "#{ssh_command} #{username}@#{server.dns_name} #{command_arg}"
verbose command
Kernel.system command
raise "command failed with code #{$?.exitstatus}" unless $?.success?
@@ -16,7 +16,7 @@
end
it "ssh's a command" do
- server = OpenStruct.new(:dns_name => "test_dns")
+ server = OpenStruct.new(:dns_name => "test_dns", :tags => {})
system "echo > /dev/null" # to populate $?
Kernel.should_receive(:system).with("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i #{Boucher::Config[:aws_key_filename]}.pem #{Boucher::Config[:username]}@test_dns \"some command\"")
@@ -25,23 +25,33 @@
end
it "opens an ssh shell" do
- server = OpenStruct.new(:dns_name => "test_dns")
+ server = OpenStruct.new(:dns_name => "test_dns", :tags => {})
system "echo > /dev/null" # to populate $?
Kernel.should_receive(:system).with("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i #{Boucher::Config[:aws_key_filename]}.pem #{Boucher::Config[:username]}@test_dns ")
Boucher.ssh(server)
end
+ it "prefers the meal's username when it's available" do
+ Boucher.stub(:meal).with("test_meal").and_return({:username => "json_specified_username"})
+ server = OpenStruct.new(:dns_name => "test_dns", :tags => {"Meal" => "test_meal"})
+
+ system "echo > /dev/null" # to populate $?
+ Kernel.should_receive(:system).with("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i #{Boucher::Config[:aws_key_filename]}.pem json_specified_username@test_dns ")
+
+ Boucher.ssh(server)
+ end
+
it "updates recipes" do
- server = OpenStruct.new(:id => "test_id")
+ server = OpenStruct.new(:id => "test_id", :tags => {})
Boucher.should_receive(:ssh).with(server, "cd infrastructure && git checkout . && git clean -d -f && git pull && bundle")
Boucher.update_recipes(server)
end
it "cooks a meal" do
- server = OpenStruct.new(:id => "test_id")
+ server = OpenStruct.new(:id => "test_id", :tags => {})
Boucher.should_receive(:update_recipes).with(server)
Boucher.should_receive(:ssh).with(server, "cd infrastructure && sudo BENV=env_name BRANCH=branch_name chef-solo -c config/solo.rb -j config/meal_name.json")

0 comments on commit 8c6d31f

Please sign in to comment.