class Omc::StackCommand

Public Class Methods

new(aws_account, user, stack_name, app: nil, layer: nil, forward_agent: false, remote_forward: nil) click to toggle source
# File lib/omc/stack_command.rb, line 6
def initialize aws_account, user, stack_name, app: nil, layer: nil, forward_agent: false, remote_forward: nil
  @aws_account = aws_account
  @user = user
  @stack_name = stack_name
  @app_name = app
  @layer_name = layer
  @forward_agent = forward_agent
  @remote_forward = remote_forward
end

Public Instance Methods

console() click to toggle source
# File lib/omc/stack_command.rb, line 20
def console
  ssh_and_execute_as_deploy "cd /srv/www/#{app[:shortname]}/current && RAILS_ENV=#{app[:attributes]['RailsEnv']} bundle exec rails c"
end
db() click to toggle source
# File lib/omc/stack_command.rb, line 24
def db
  ssh_and_execute_as_deploy "cd /srv/www/#{app[:shortname]}/current && RAILS_ENV=#{app[:attributes]['RailsEnv']} bundle exec rails db -p"
end
ssh() click to toggle source
# File lib/omc/stack_command.rb, line 16
def ssh
  exec "ssh", *([ssh_host] + default_ssh_args)
end
ssh_and_execute(command) click to toggle source
# File lib/omc/stack_command.rb, line 52
def ssh_and_execute(command)
  puts "Executing '#{command}'"
  args = default_ssh_args + ['-t', ssh_host, command]

  exec 'ssh', *args
end
status(thor) click to toggle source
# File lib/omc/stack_command.rb, line 37
def status(thor)
  details = stack.instances.map do |i|
    [
      i[:hostname],
      i[:instance_type],
      i[:status],
      i[:availability_zone],
      i[:ec2_instance_id],
      i[:public_ip],
      i[:private_ip],
    ]
  end
  thor.print_table(details)
end
unicorn(action) click to toggle source
# File lib/omc/stack_command.rb, line 28
def unicorn(action)
  case action
  when "restart"
    stack.execute_recipes(app, recipes: ["deploy::rails-restart"], name: "restart")
  else
    abort("Unicorn action should be one of [restart]")
  end
end

Private Instance Methods

account() click to toggle source
# File lib/omc/stack_command.rb, line 90
def account
  @account ||= Omc::Account.new(@user.credentials)
end
app() click to toggle source
# File lib/omc/stack_command.rb, line 64
def app
  if @app_name
    get_by_name(stack.apps, @app_name)
  else
    stack.apps.first
  end
end
bastion() click to toggle source
# File lib/omc/stack_command.rb, line 98
def bastion
  @aws_account.bastions.detect { |y| y.name == @stack_name }
end
default_ssh_args() click to toggle source
# File lib/omc/stack_command.rb, line 102
def default_ssh_args
  [].tap do |args|
    if (bastion)
      proxy_command = "ProxyCommand ssh -W %h:%p #{bastion.host}"
      proxy_command += " -R #{@remote_forward}" if @remote_forward
      args.push("-o", proxy_command)
    end
    args.push("-A") if @forward_agent
    args.push("-R #{@remote_forward}") if @remote_forward
  end
end
get_by_name(collection, name, key: :name) click to toggle source
# File lib/omc/stack_command.rb, line 114
def get_by_name collection, name, key: :name
  collection.detect do |x|
    x[key] == name
  end || abort("Can't find #{name.inspect} among #{collection.map{|x| x[key] }.inspect}")
end
instance() click to toggle source
# File lib/omc/stack_command.rb, line 78
def instance
  instances = layer ? layer.instances : stack.instances
  instances.detect(&:online?) || abort("No running instances")
end
layer() click to toggle source
# File lib/omc/stack_command.rb, line 72
def layer
  if @layer_name
    get_by_name(stack.layers, @layer_name, key: :shortname)
  end
end
ssh_and_execute_as_deploy(command) click to toggle source
# File lib/omc/stack_command.rb, line 60
def ssh_and_execute_as_deploy(command)
  ssh_and_execute "sudo su deploy -c '#{command}'"
end
ssh_host() click to toggle source
# File lib/omc/stack_command.rb, line 83
def ssh_host
  ip_address = bastion ? instance[:private_ip] : instance[:public_ip]
  host = "#{@user.name}@#{ip_address}"
  puts "Connecting to #{host}"
  host
end
stack() click to toggle source
# File lib/omc/stack_command.rb, line 94
def stack
  @stack ||= get_by_name(account.stacks, @stack_name)
end