Class: Ey::Core::Cli::Ssh

Inherits:
Subcommand
  • Object
show all
Defined in:
lib/ey-core/cli/ssh.rb

Instance Method Summary collapse

Methods inherited from Subcommand

#handle_core_error, #run_handle, #setup

Methods included from Helpers::Core

#core_account, #core_accounts, #core_application_for, #core_applications, #core_client, #core_environment_for, #core_environment_variables, #core_environments, #core_operator_and_environment_for, #core_server_for, #core_url, #core_yaml, #eyrc_yaml, included, #longest_length_by_name, #operator, #unauthenticated_core_client, #write_core_yaml

Instance Method Details

#handleObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ey-core/cli/ssh.rb', line 78

def handle
  operator, environment = core_operator_and_environment_for(options)
  abort "Unable to find matching environment".red unless environment

  cmd      = option(:command)
  ssh_opts = []
  ssh_cmd  = ["ssh"]
  exits    = []
  user     = environment.username
  servers  = []

  if option(:command)
    if shell = option(:shell)
      cmd = Escape.shell_command([shell,'-lc',cmd])
    end

    if switch_active?(:tty)
      ssh_opts << "-t"
    elsif cmd.match(/sudo/)
      puts "sudo commands often need a tty to run correctly. Use -t option to spawn a tty.".yellow
    end

    servers += Ey::Core::Cli::Helpers::ServerSieve.filter(
      environment.servers,
      all: switch_active?(:all),
      app_servers: switch_active?(:app_servers),
      app_master: switch_active?(:app_master),
      db_servers: switch_active?(:db_servers),
      db_master: switch_active?(:db_master),
      utilities: option(:utilities)
    )
  else
    if option(:bind_address)
      ssh_opts += ["-L", option(:bind_address)]
    end

    if option(:server)
      servers += [core_server_for(server: option(:server), operator: environment)]
    else
      servers += Ey::Core::Cli::Helpers::ServerSieve.filter(
        environment.servers,
        all: switch_active?(:all),
        app_servers: switch_active?(:app_servers),
        app_master: switch_active?(:app_master),
        db_servers: switch_active?(:db_servers),
        db_master: switch_active?(:db_master),
        utilities: option(:utilities)
      )

      # Reinstate default behavior: If no command is passed and no
      # roles are passed, open a connection to the app master
      if servers.empty?
        servers += (environment.servers.all(role: "app_master") + environment.servers.all(role: "solo")).to_a
      end
    end
  end

  if servers.empty?
    abort "Unable to find any matching servers. Aborting.".red
  end

  servers.uniq!

  servers.each do |server|
    host = server.public_hostname
    name = server.name ? "#{server.role} (#{server.name})" : server.role
    puts "\nConnecting to #{name} #{host}".green
    sshcmd = Escape.shell_command((ssh_cmd + ["#{user}@#{host}"] + [cmd]).compact)
    puts "Running command: #{sshcmd}".green
    system sshcmd
    exits << $?.exitstatus
  end

  exit exits.detect {|status| status != 0 } || 0
end