/
test_app.rb
190 lines (152 loc) · 3.77 KB
/
test_app.rb
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
95
96
97
98
99
100
101
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
185
186
187
188
189
190
require "English"
require "fileutils"
require "pathname"
module TestApp
extend self
def install
install_test_app_with(default_config)
end
def default_config
<<-CONFIG
set :deploy_to, '#{deploy_to}'
set :repo_url, 'git://github.com/capistrano/capistrano.git'
set :branch, 'master'
set :ssh_options, { keys: "\#{ENV['HOME']}/.vagrant.d/insecure_private_key", auth_methods: ['publickey'] }
server 'vagrant@localhost:2220', roles: %w{web app}
set :linked_files, #{linked_files}
set :linked_dirs, #{linked_dirs}
set :format_options, log_file: nil
set :local_user, #{current_user.inspect}
CONFIG
end
def linked_files
%w{config/database.yml}
end
def linked_file
shared_path.join(linked_files.first)
end
def linked_dirs
%w{bin log public/system}
end
def create_test_app
FileUtils.rm_rf(test_app_path)
FileUtils.mkdir(test_app_path)
File.open(gemfile, "w+") do |file|
file.write "gem 'capistrano', path: '#{path_to_cap}'"
end
Dir.chdir(test_app_path) do
`bundle`
end
end
def install_test_app_with(config)
create_test_app
Dir.chdir(test_app_path) do
`bundle exec cap install STAGES=#{stage}`
end
write_local_deploy_file(config)
end
def write_local_deploy_file(config)
File.open(test_stage_path, "w") do |file|
file.write config
end
end
def write_local_stage_file(filename, config=nil)
File.open(test_app_path.join("config/deploy/#{filename}"), "w") do |file|
file.write(config) if config
end
end
def append_to_deploy_file(config)
File.open(test_stage_path, "a") do |file|
file.write config + "\n"
end
end
def prepend_to_capfile(config)
current_capfile = File.read(capfile)
File.open(capfile, "w") do |file|
file.write config
file.write current_capfile
end
end
def create_shared_directory(path)
FileUtils.mkdir_p(shared_path.join(path))
end
def create_shared_file(path)
File.open(shared_path.join(path), "w")
end
def cap(task, subdirectory=nil)
run "bundle exec cap #{stage} #{task}", subdirectory
end
def run(command, subdirectory=nil)
output = nil
dir = subdirectory ? test_app_path.join(subdirectory) : test_app_path
Dir.chdir(dir) do
output = `#{command}`
end
[$CHILD_STATUS.success?, output]
end
def stage
"test"
end
def test_stage_path
test_app_path.join("config/deploy/test.rb")
end
def test_app_path
Pathname.new("/tmp/test_app")
end
def deploy_to
Pathname.new("/home/vagrant/var/www/deploy")
end
def shared_path
deploy_to.join("shared")
end
def current_path
deploy_to.join("current")
end
def releases_path
deploy_to.join("releases")
end
def release_path
releases_path.join(timestamp)
end
def timestamp
Time.now.utc.strftime("%Y%m%d%H%M%S")
end
def repo_path
deploy_to.join("repo")
end
def path_to_cap
File.expand_path(".")
end
def gemfile
test_app_path.join("Gemfile")
end
def capfile
test_app_path.join("Capfile")
end
def current_user
"(GitHub Web Flow) via ShipIt"
end
def task_dir
test_app_path.join("lib/capistrano/tasks")
end
def copy_task_to_test_app(source)
FileUtils.cp(source, task_dir)
end
def config_path
test_app_path.join("config")
end
def move_configuration_to_custom_location(location)
prepend_to_capfile(
<<-CONFIG
set :stage_config_path, "app/config/deploy"
set :deploy_config_path, "app/config/deploy.rb"
CONFIG
)
location = test_app_path.join(location)
FileUtils.mkdir_p(location)
FileUtils.mv(config_path, location)
end
def git_wrapper_path
"/tmp/git-ssh-my_app_name-#{stage}-#{current_user}.sh"
end
end