Skip to content

Commit

Permalink
CPD-183: Added pre-check to plugin (#270)
Browse files Browse the repository at this point in the history
Change: minor
Purpose: feature
  • Loading branch information
AbhishekRana95 authored Nov 10, 2020
1 parent 40f27a0 commit d5d2960
Show file tree
Hide file tree
Showing 11 changed files with 422 additions and 273 deletions.
7 changes: 2 additions & 5 deletions docs/plugins/rotate_asg_instances.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ After all outdated instances are shutdown successfully, it terminates them and r
It allows gracefully shutting down each instance instead of terminating them and killing all the running processes.

## Configuration
The plugin uses the ssh username specified in the `MOONSHOT_SSH_USER` or the `LOGNAME` environment variable for logging into the ASG instances to shutdown. The value should be the username with which you have the access to the instances. For example:
```ruby
export MOONSHOT_SSH_USER=abhishek.rana
```
The plugin uses config.ssh_config.ssh_user value for logging into the ASG instances to shutdown. The value should be the username with which you have the access to the instances.

The plugin needs no additional configuration parameters:
The plugin accepts additional configuration from config.ssh_config.ssh_options for ssh.

## Example
```ruby
Expand Down
1 change: 1 addition & 0 deletions lib/moonshot/ssh_command_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(ssh_config, instance_id)

def build(command = nil)
cmd = ['ssh', '-t']
cmd << @config.ssh_options if @config.ssh_options
cmd << "-i #{@config.ssh_identity_file}" if @config.ssh_identity_file
cmd << "-l #{@config.ssh_user}" if @config.ssh_user
cmd << instance_ip
Expand Down
2 changes: 2 additions & 0 deletions lib/moonshot/ssh_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ module Moonshot
class SSHConfig
attr_accessor :ssh_identity_file
attr_accessor :ssh_user
attr_accessor :ssh_options

def initialize
@ssh_identity_file = ENV['MOONSHOT_SSH_KEY_FILE']
@ssh_user = ENV['MOONSHOT_SSH_USER']
@ssh_options = ENV['MOONSHOT_SSH_OPTIONS']
end
end
end
10 changes: 5 additions & 5 deletions lib/moonshot/ssh_fork_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
module Moonshot
# Run an SSH command via fork/exec.
class SSHForkExecutor
Result = Struct.new(:output, :exitstatus)
Result = Struct.new(:output, :error, :exitstatus)

def run(cmd)
output = StringIO.new

output = error = StringIO.new
exit_status = nil
Open3.popen3(cmd) do |_, stdout, _, wt|
Open3.popen3(cmd) do |_, stdout, stderr, wt|
error << stderr.read until stderr.eof?
output << stdout.read until stdout.eof?
exit_status = wt.value.exitstatus
end

Result.new(output.string.chomp, exit_status)
Result.new(output.string.chomp, error.string.chomp, exit_status)
end
end
end
31 changes: 27 additions & 4 deletions lib/plugins/rotate_asg_instances.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
# frozen_string_literal: true

require 'aws-sdk'
require_relative 'rotate_asg_instances/asg'

module Moonshot
module Plugins
# Rotate ASG instances after update.
class RotateAsgInstances
include DoctorHelper

def doctor(resources)
@resources = resources
run_all_checks
end

def pre_update(resources)
@resources = resources
asg.verify_ssh
end

def post_update(resources)
asg = ASG.new(resources)
asg.rotate_asg_instances
asg.teardown_outdated_instances
@resources = resources
asg.perform_rotation
end

private

def asg
Moonshot::RotateAsgInstances::ASG.new(@resources)
end

def doctor_check_ssh
asg.verify_ssh
success('Successfully opened SSH connection to an instance.')
rescue Moonshot::RotateAsgInstances::SSHValidationError
critical('SSH connection test failed, check your SSH settings')
end
end
end
Expand Down
Loading

0 comments on commit d5d2960

Please sign in to comment.