This repository has been archived by the owner on May 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Capfile
94 lines (80 loc) · 2.5 KB
/
Capfile
1
2
3
4
5
6
7
8
9
10
11
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require "grit"
set :application, "brcm-accounts-api"
set :deploy_directory, "/opt/brcm-accounts-api"
set :current_jar, "current.jar"
set :remote_user, "brcm"
set :remote_group, "brcm"
set :timestamp, Time.now.strftime("%Y%m%d%H%M%S")
set :head, Grit::Repo.new(".").commits.first.sha[0..5]
set :jar_file, "#{application}-#{timestamp}-#{head}.jar"
def check_for_roles
if roles.empty?
logger.important("No environment specified. Use `cap staging CMD` or `cap production CMD`")
exit(-1)
end
end
task :build do
output = %x{ mvn clean package }
if $?.exitstatus != 0
STDERR.puts output
STDERR.puts
logger.important("Unable to build JAR. Try again when `mvn clean package` works.")
exit(-1)
end
%x{ mv target/#{application}-*.jar target/#{jar_file} }
end
task :stage do
temp_dir = "/tmp/#{application}.#{Array.new(6).map { rand(10) }.join("")}"
run("mkdir -p #{temp_dir}")
uploaded_jar_filename = "#{temp_dir}/#{jar_file}"
local_jar_filename = "target/#{jar_file}"
max_size = File.size(local_jar_filename).to_f
last_percent = Hash.new { |h, k| h[k] = 0 }
upload(local_jar_filename, uploaded_jar_filename, :via => :sftp) do |event, options, *details|
if event == :put
this_percent = ((details[1] / max_size) * 100).round
if this_percent >= last_percent[options[:host]] + 5
logger.debug("#{options[:host]}: #{this_percent}%")
last_percent[options[:host]] = this_percent
end
end
end
sudo("mv #{uploaded_jar_filename} #{deploy_directory}/")
sudo("chown #{remote_user}:#{remote_group} #{deploy_directory}/#{jar_file}")
end
task :install do
sudo("ln -sf #{deploy_directory}/#{jar_file} #{deploy_directory}/#{current_jar}")
end
desc "Restart the application"
task :restart, :max_hosts => 1 do
check_for_roles
sudo("/etc/init.d/brcm-accounts-api restart")
end
desc "Scope all actions to the staging servers"
task :staging do
role :app, "brcm1.stage"
end
desc "Scope all actions to the production servers"
task :production do
role :app, "brcm1.prod", "brcm2.prod"
end
desc "Build and deploy the latest version of the application"
task :deploy do
check_for_roles
build
stage
install
restart
end
desc "Remove all but the last five installed versions"
task :clean do
files = []
run("ls -A1 #{deploy_directory}") do |channel, stream, data|
files += data.split("\n")
end
files.uniq!
files -= [current_jar]
files.sort!
files -= files[-5..-1]
sudo("rm -f #{files.map { |f| "#{deploy_directory}/#{f}" }.join(" ")}")
end