Module: AppDeploy

Defined in:
lib/app-deploy.rb,
lib/app-deploy/version.rb

Constant Summary collapse

VERSION =
'0.5.0'

Class Method Summary collapse

Class Method Details

.clone(opts) ⇒ Object

about git



72
73
74
75
76
77
78
79
80
81
# File 'lib/app-deploy.rb', line 72

def clone opts
  user, proj, path = opts[:github_user], opts[:github_project], opts[:git_path]

  if File.exist?(path)
    puts "Skip #{proj} because #{path} exists"
  else
    sh "git clone git://github.com/#{user}/#{proj}.git #{path}"
    sh "git --git-dir #{path}/.git gc"
  end
end

.dependency(opts = {}) ⇒ Object



9
10
11
12
13
# File 'lib/app-deploy.rb', line 9

def dependency opts = {}
  opts = opts.dup
  opts[:git_path] ||= opts[:github_project]
  github << opts.freeze
end

.dependency_gem(opts = {}, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/app-deploy.rb', line 16

def dependency_gem opts = {}, &block
  opts = opts.dup

  if opts[:github_project]
    opts[:git_path] ||= opts[:github_project]
    opts[:task_gem] = block if block_given?
    github << opts.freeze
  end

  gem << opts.freeze
end

.each(*with) ⇒ Object



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
# File 'lib/app-deploy.rb', line 28

def each *with
  with = [:github, :gem] if with.empty?
  cwd = Dir.pwd

  # github's gem would be in @github and @gem,
  # so call uniq to ensure it wouldn't get called twice.
  with.map{ |kind| AppDeploy.send(kind) }.flatten.uniq.each{ |opts|
    puts

    begin
      if opts[:github_project]
        if File.directory?(opts[:git_path])
          Dir.chdir(opts[:git_path])
        else
          puts "Skip #{opts[:github_project]}, because it was not found"
        end
      end

      yield(opts)

    rescue RuntimeError => e
      puts e

    ensure
      Dir.chdir(cwd)

    end
  }
end

.extract_config(config) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/app-deploy.rb', line 58

def extract_config config
  require 'yaml'
  YAML.load(File.read(config)).inject([]){ |result, opt_value|
    opt, value = opt_value
    if block_given?
      result << yield(opt, value)
    else
      result << "--#{opt} #{value}"
    end
    result
  }.compact.join(' ')
end

.gemObject



15
# File 'lib/app-deploy.rb', line 15

def gem; @gem ||= []; end

.githubObject



8
# File 'lib/app-deploy.rb', line 8

def github; @github ||= []; end

.hup(pid_path, name = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/app-deploy.rb', line 145

def hup pid_path, name = nil
  if pid = AppDeploy.read_pid(pid_path)
    puts "Sending HUP to #{name}(#{pid})..."
    Process.kill('HUP', pid)
  end
rescue Errno::ESRCH
  puts "WARN: No such pid: #{pid}, removing #{pid_path}..."
  File.delete(pid_path)
end

.install_gem(opts) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/app-deploy.rb', line 88

def install_gem opts
  gem_name = opts[:gem] || opts[:github_project]

  if AppDeploy.installed_gem?(gem_name)
    puts "Skip #{gem_name} because it was installed. Uninstall first if you want to reinstall"

  else
    if opts[:gem]
      AppDeploy.install_gem_remote(opts[:gem], opts[:source])
    else
      AppDeploy.install_gem_local(opts[:github_project], opts[:task_gem])
    end

  end
end

.install_gem_local(proj, task) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/app-deploy.rb', line 117

def install_gem_local proj, task
  case task
    when 'bones'
      sh 'rake clobber'
      sh 'rake gem:package'
      sh "gem install --local pkg/#{proj}-*.gem --no-ri --no-rdoc"

    when 'hoe'
      sh 'rake gem'
      sh "gem install --local pkg/#{proj}-*.gem --no-ri --no-rdoc"

    when Proc
      task.call
  end
end

.install_gem_remote(gem_name, source = nil) ⇒ Object



113
114
115
# File 'lib/app-deploy.rb', line 113

def install_gem_remote gem_name, source = nil
  sh "gem install #{gem_name} --no-ri --no-rdoc#{source ? ' --source' + source : ''}"
end

.installed_gem?(gem_name) ⇒ Boolean

about gem

Returns:

  • (Boolean)


84
85
86
# File 'lib/app-deploy.rb', line 84

def installed_gem? gem_name
  `gem list '^#{gem_name}$'` =~ /^#{gem_name}/
end

.read_pid(pid_path) ⇒ Object

about sending signal



134
135
136
137
138
139
140
141
142
143
# File 'lib/app-deploy.rb', line 134

def read_pid pid_path
  if File.exist?(pid_path)
    File.read(pid_path).strip.to_i

  else
    puts "WARN: No pid file found in #{pid_path}"
    nil

  end
end

.term(pid_path, name = nil, limit = 5) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/app-deploy.rb', line 155

def term pid_path, name = nil, limit = 5
  if pid = AppDeploy.read_pid(pid_path)
    puts "Sending TERM to #{name}(#{pid})..."

  else
    return

  end

  require 'timeout'
  begin
    timeout(limit){
      while true
        Process.kill('TERM', pid)
        sleep(0.1)
      end
    }
  rescue Errno::ESRCH
    if File.exist?(pid_path)
      puts "WARN: No such pid: #{pid}, removing #{pid_path}..."
      File.delete(pid_path)
    else
      puts "Killed #{name}(#{pid})"
    end

  rescue Timeout::Error
    puts "Timeout(#{limit}) killing #{name}(#{pid})"

  end
end

.uninstall_gem(opts) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/app-deploy.rb', line 104

def uninstall_gem opts
  gem_name = opts[:gem] || opts[:github_project]
  if AppDeploy.installed_gem?(gem_name)
    sh "gem uninstall #{gem_name}"
  else
    puts "Skip #{gem_name} because it was not installed"
  end
end