Module: ORS::Helpers

Includes:
CommandHelpers, ParseHelpers, PrepareHelpers
Included in:
Commands::Base
Defined in:
lib/ors/helpers.rb

Defined Under Namespace

Modules: CommandHelpers, ParseHelpers, PrepareHelpers

Instance Method Summary collapse

Methods included from CommandHelpers

#bundle_install, #restart_server, #run_migrations, #setup_repo, #setup_ruby, #start_server, #stop_server, #update_code

Methods included from PrepareHelpers

#prepare_environment, #prepare_environment_with_rvm, #prepare_initial_environment

Methods included from ParseHelpers

#parse_remote_and_or_branch

Instance Method Details

#build_command(server, *commands_and_maybe_options) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ors/helpers.rb', line 146

def build_command server, *commands_and_maybe_options
  return "" if commands_and_maybe_options.empty?

  if commands_and_maybe_options.last.is_a?(Hash)
    options = commands_and_maybe_options.pop
    command_array = commands_and_maybe_options
  else
    command_array = commands_and_maybe_options
    options = {}
  end

  commands = command_array.join " && "
  psuedo_tty = options[:exec] ? '-t ' : ''
  quiet_ssh = options[:quiet_ssh] ? '-q ' : ''

  if options[:local]
    commands
  else
    if ORS.config[:use_gateway]
      %(ssh #{quiet_ssh}#{psuedo_tty}#{ORS.config[:gateway]} 'ssh #{quiet_ssh}#{psuedo_tty}#{ORS.config[:user]}@#{server} "#{commands}"')
    else
      %(ssh #{quiet_ssh}#{psuedo_tty}#{ORS.config[:user]}@#{server} "#{commands}")
    end
  end
end

#execute_command(server, *command_array) ⇒ Object

options = => ?, :capture => ?, :quiet_ssh => ?



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ors/helpers.rb', line 121

def execute_command(server, *command_array)
  options = {:exec => false, :capture => false, :quiet_ssh => false}
  options.merge!(command_array.pop) if command_array.last.is_a?(Hash)
  options[:local] = true if server.to_s == "localhost"

  command = build_command(server, command_array, options)

  if ORS.config[:pretending]
    info("[#{server}] #{command}")
  else
    if options[:exec]
      exec command
    else
      results = `#{command}`
      if options[:capture]
        return results
      else
        results.split("\n").each do |result|
          info "[#{server}] #{result.chomp}\n"
        end
      end
    end
  end
end

#execute_in_parallel(servers) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/ors/helpers.rb', line 172

def execute_in_parallel servers
  servers.map do |server|
    Thread.new(server) do |server|
      yield server
    end
  end.map {|thread| thread.join }
end

#fatal(message) ⇒ Object



184
185
186
187
# File 'lib/ors/helpers.rb', line 184

def fatal message
  info message
  exit 1
end

#info(message) ⇒ Object



180
181
182
# File 'lib/ors/helpers.rb', line 180

def info message
  STDOUT.puts message
end