Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from kickstarter/ec2-instance-connect
Browse files Browse the repository at this point in the history
EC2 instance connect init
  • Loading branch information
amancevice authored Jul 28, 2022
2 parents 94d8da3 + 4f4842c commit 9d8b587
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
2 changes: 2 additions & 0 deletions capistrano-hivequeen.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Gem::Specification.new do |s|

## List your runtime dependencies here. Runtime dependencies are those
## that are needed for an end user to actually USE your code.
s.add_dependency('aws-sdk-ec2', '~> 1.0')
s.add_dependency('aws-sdk-ec2instanceconnect', '~> 1.0')
s.add_dependency('capistrano', '>= 2.11.0')
s.add_dependency('activesupport', '>= 3.0.0')
s.add_dependency('json')
Expand Down
10 changes: 7 additions & 3 deletions lib/capistrano/hivequeen.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# HTTP Client for Hive Queen environment configuration
require 'base64'
require 'fileutils'
require 'json'

require 'active_support'
require 'active_support/core_ext'
require 'fileutils'
require 'aws-sdk-ec2'
require 'aws-sdk-ec2instanceconnect'
require 'excon'
require 'base64'

require 'capistrano/hivequeen/version'
require 'capistrano/hivequeen/multiio'
require 'capistrano/hivequeen/ec2_instance_connect'

# Special cases:
# - environment not found
Expand Down Expand Up @@ -135,4 +140,3 @@ def put_or_post(method, path, data)
end

require "capistrano/hivequeen/capistrano_configuration"

16 changes: 12 additions & 4 deletions lib/capistrano/hivequeen/capistrano_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,18 @@
command_format = "ssh -t -A -l %s %s"
env['roles'].keys.each do |role_name|
task role_name do
cmd = command_format % [user, roles[role_name.to_sym].servers.first]
server = roles[role_name.to_sym].servers.sample
HiveQueen.ec2_instance_connect(server.host)
cmd = command_format % [user, server]
puts "Executing #{cmd}"
exec cmd
end
end

task :default do
cmd = command_format % [user, roles.values.sample.servers.sample]
server = roles.values.sample.servers.sample
HiveQueen.ec2_instance_connect(server.host)
cmd = command_format % [user, server]
puts "Executing #{cmd}"
exec cmd
end
Expand All @@ -114,14 +118,18 @@
command_format = "ssh -t -A -l %s %s 'source /etc/profile; cd /apps/#{HiveQueen.project}/current && bundle exec rails console'"
env['roles'].keys.each do |role_name|
task role_name do
server = roles[role_name.to_sym].servers.sample
HiveQueen.ec2_instance_connect(server.host)
puts "Opening console"
exec command_format % [user, roles[role_name.to_sym].servers.first]
exec command_format % [user, server]
end
end

task :default do
server = roles.values.sample.servers.sample
HiveQueen.ec2_instance_connect(server.host)
puts "Opening console"
exec command_format % [user, roles.values.sample.servers.sample]
exec command_format % [user, server]
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions lib/capistrano/hivequeen/ec2_instance_connect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class HiveQueen
def self.ec2_client
@ec2_client ||= Aws::EC2::Client.new
end

def self.ec2_instance_connect_client
@ec2_instance_connect_client ||= Aws::EC2InstanceConnect::Client.new
end

def self.ec2_instance_connect(*private_dns)
# EC2 Instance Connect
ssh_public_key = File.read(File.expand_path('~/.ssh/ksr_ed25519.pub'))
ec2_params = {
filters: [{
name: 'network-interface.private-dns-name',
values: private_dns
}]
}
logger.trace("ec2:DescribeInstances #{ec2_params.to_json}")
instances = ec2_client.describe_instances(**ec2_params).reservations.map(&:instances).flatten
threads = instances.map do |instance|
Thread.new do
ec2ic_params = {
availability_zone: instance.placement.availability_zone,
instance_id: instance.instance_id,
instance_os_user: 'ksr',
ssh_public_key: ssh_public_key,
}
logger.trace("ec2-instance-connect:SendSSHPublicKey #{ec2ic_params.to_json}")
ec2_instance_connect_client.send_ssh_public_key(**ec2ic_params)
end
end

threads.each(&:join)
end
end
2 changes: 1 addition & 1 deletion lib/capistrano/hivequeen/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class HiveQueen
class Version
@@version = '7.6.0'
@@version = '7.7.0'

def self.to_s
@@version
Expand Down

0 comments on commit 9d8b587

Please sign in to comment.