Class: Capistrano::Former03::DeployCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/former03/deploy_command.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DeployCommand

Returns a new instance of DeployCommand.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/capistrano/former03/deploy_command.rb', line 5

def initialize(opts={})

  # test for required flags
  required_flags = [:command, :deploy_flag ,:test_cmd, :required]
  required_flags.each do |flag|
    if not opts.has_key?(flag)
      raise "Required arg '#{flag}' not given"
    end
  end

  # copy opts as instance vars
  opts.each do |k,v|
    instance_variable_set("@#{k}", v)
  end
end

Instance Method Details

#add_env_pathObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/capistrano/former03/deploy_command.rb', line 58

def add_env_path
  #TODO do host local env setting
  env = SSHKit.config.default_env
  bin_path = fetch(:remote_bin_path).to_s
  if env.has_key? (:path)
    split = env[:path].split(':')
    if not split.include?(bin_path)
     split = [bin_path] + split
    end
    env[:path] = split.join(':')
  else
    env[:path] = "#{bin_path}:$PATH"
  end
end

#deploy(ssh) ⇒ Object



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
# File 'lib/capistrano/former03/deploy_command.rb', line 102

def deploy(ssh)

  # receive ssh handle
  @ssh = ssh
  @deploy = fetch(@deploy_flag)
  # deploy states
  if @deploy == true
    return deploy_run
  elsif @deploy.nil?
    if test_run_path
      return false
    else
      return deploy_run
    end
  else
    if @required
      if test_run_path
        return false
      else
        fail_missing
      end
    else
      return false
    end
  end

end

#deploy_runObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/capistrano/former03/deploy_command.rb', line 80

def deploy_run
  # Deploy own binary if it doesn't already exist
  return deploy_succeed if test_run_absolute

  # Upload x64 binary
  upload(dest_src('x64'))
  return deploy_succeed if test_run_absolute

  # Upload x32 binary
  upload(dest_src('x32'))
  return deploy_succeed if test_run_absolute

  # Fail if arrive here
  fail_missing

end

#deploy_succeedObject



74
75
76
77
78
# File 'lib/capistrano/former03/deploy_command.rb', line 74

def deploy_succeed
  add_env_path
  @ssh.host.properties.set "deploy_#{@command.to_sym}_path".to_sym, dest_path
  return true
end

#dest_pathObject



21
22
23
# File 'lib/capistrano/former03/deploy_command.rb', line 21

def dest_path
  File.join(fetch(:remote_bin_path), @command)
end

#dest_src(arch = 'x64') ⇒ Object



25
26
27
28
29
30
# File 'lib/capistrano/former03/deploy_command.rb', line 25

def dest_src(arch='x64')
  File.expand_path(File.join(
    File.expand_path(File.dirname(__FILE__)),
    "../../../share/#{@command}/#{@command}_#{arch}"
  ))
end

#fail_missingObject



97
98
99
# File 'lib/capistrano/former03/deploy_command.rb', line 97

def fail_missing
  fail "No usable '#{@command}' command found"
end

#test_run_absoluteObject



36
37
38
# File 'lib/capistrano/former03/deploy_command.rb', line 36

def test_run_absolute
  @ssh.test(dest_path, *@test_cmd)
end

#test_run_pathObject



32
33
34
# File 'lib/capistrano/former03/deploy_command.rb', line 32

def test_run_path
  @ssh.test(@command, *@test_cmd)
end

#upload(src) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/capistrano/former03/deploy_command.rb', line 40

def upload(src)
  @ssh.upload! src, dest_path
  @ssh.execute :chmod, '+x', dest_path
  if test_run_absolute
    if not @install_cmd.nil?
      if @ssh.test(dest_path, *@install_cmd)
        return true
      else
        return false
      end
    else
      return true
    end
  else
    return false
  end
end