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\-\.]+)[\'\"]/
DIR_PATTERN =
/set :deploy_to,\s+[\'\"](?<path>(.+)\/([^\/]+))[\'\"]/

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Runner



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

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

Instance Method Details

#runObject



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
50
51
52
53
54
55
56
57
# File 'lib/capx/runner.rb', line 13

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

      if match = DIR_PATTERN.match(line)
        @deploy_to = File.join(match[:path], 'current')
      end
    end

    if @server.nil? || @user.nil?
      puts 'capistrano server/user not found'
    else
      ssh_cmd = "ssh #{@user}@#{@server}"

      if @switch == 'ssh'
        # call ssh
        ssh_cmd = "ssh -t #{@user}@#{@server} \"cd #{@deploy_to}; exec /bin/bash -l\"" unless @deploy_to.nil?

        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