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
44
45
46
47
48
49
# 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
      ssh_cmd = "ssh #{@user}@#{@server}"
      if @switch == 'ssh'
        # call ssh

        cmd = ssh_cmd
        execute_cmd(cmd)
      elsif @switch == 'disk'
        # call remote df -H

        cmd = "#{ssh_cmd} 'df -H'"
        execute_cmd(cmd)
      elsif @switch == 'info'
        # call remote cat /etc/*-release

        cmd = "#{ssh_cmd} 'cat /etc/*-release'"
        execute_cmd(cmd)
      elsif @switch == 'redis'
        # call remote redis-cli INFO keyspace

        cmd = "#{ssh_cmd} 'redis-cli INFO keyspace'"
        execute_cmd(cmd)
      else
        puts "#{@user}@#{@server}"
      end
    end
  else
    puts "File #{file} not found"
  end
end