Class: Capx::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/capx/runner.rb

Constant Summary collapse

PATTERN =
/server:?\s+[\'\"]([\w\-\.]+)[\'\"],\s+user:?\s+[\'\"]([\w\-\.]+)[\'\"]/

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Runner

Returns a new instance of Runner.



5
6
7
8
9
10
# File 'lib/capx/runner.rb', line 5

def initialize(options)
  @stage  = options[:stage]
  @switch = options[:switch]
  @user   = options[:user]
  @server = nil
end

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/capx/runner.rb', line 12

def run
  file = "config/deploy/#{@stage}.rb"
  if File.exist?(file)
    File.open(file).each do |line|
      if match = PATTERN.match(line)
        @server = match.captures[0]
        @user = match.captures[1] if @user.nil?
      end  
    end

    if @server.nil? || @user.nil?
      puts "capistrano server/user not found"
    else
      if @switch == 'ssh'
        # call ssh  
        cmd = "ssh #{@user}@#{@server}"
        execute_cmd(cmd)
      elsif @switch == 'disk'
        # call remote df -H
        cmd = "ssh #{@user}@#{@server} 'df -H'" 
        execute_cmd(cmd)
      elsif @switch == 'info'
        cmd = "ssh #{@user}@#{@server} 'cat /etc/*-release'"  
        execute_cmd(cmd)
      else    
        puts "#{@user}@#{@server}"
      end 
    end
  else
    puts "File #{file} not found"
  end  
end