Last active
April 17, 2016 11:05
-
-
Save inokappa/99093155f1f95c82eceb9a55e6ee4613 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk' | |
ec2 = Aws::EC2::Client.new( | |
region: "ap-northeast-1" | |
) | |
namespace :ecs do | |
namespace :instance do | |
desc "Launch ECS Container Instances" | |
task :launch do | |
sh "ecs-cli up --keypair keyname \ | |
--capability-iam \ | |
--size 1 \ | |
--vpc vpc-xxxxxxxx \ | |
--instance-type t2.small \ | |
--subnets subnet-xxxxxxxx,subnet-xxxxxxxx \ | |
--azs ap-northeast-1a,ap-northeast-1c" | |
end | |
desc "Terminate ECS Container Instances" | |
task :down do | |
sh "ecs-cli down --force" | |
end | |
end | |
end | |
namespace :ssh do | |
namespace :config do | |
desc "Generate ssh config file" | |
task :generate do | |
instances = ec2.describe_instances( | |
filters:[ | |
{ name: "tag:Name", values: ["ECS Instance - *"] }, | |
{ name: "instance-state-name", values: ["running"] }, | |
] | |
) | |
num = 0 | |
instances.reservations.each do |r| | |
r.instances.each do |i| | |
name_tag = i.tags.select {|tag| tag[:key] == "Name"} | |
puts "Host #{name_tag[0][:value].gsub(/ECS Instance - /,'')}_#{"%03d" % num}\n Hostname #{i.public_dns_name}" | |
end.join("\n\n") | |
num += 1 | |
end | |
puts <<-"CONF" | |
Host * | |
Port 22 | |
IdentityFile path/to/keyname.pem | |
User ec2-user | |
CONF | |
end | |
end | |
namespace :login do | |
targets = [] | |
begin | |
File.open("config") do |f| | |
while line = f.gets | |
if line.include?("Host ") && ! line.include?("*") | |
targets << line.gsub(/Host /,'').chomp | |
end | |
end | |
end | |
rescue SystemCallError => e | |
puts e.message | |
end | |
targets.each do |target| | |
desc "Access to #{target}" | |
task target.to_sym do | |
sh "ssh -F config #{target}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment