-
Notifications
You must be signed in to change notification settings - Fork 11
/
Rakefile
218 lines (166 loc) · 7.01 KB
/
Rakefile
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Checkout copy of webpage from git and run a build. Then push the build to
# www.sproutcore.com and symlink pages.
require 'extlib'
# Config Settings
LANGUAGES = [:en, :es, :de, :ru, :uk, :no, :fr, :ro]
# Autodetect some environment variables
SC_BUILD = '../abbot/bin/sc-build'
WORKING = File.dirname(__FILE__)
# discover the targets
TARGETS = Dir.glob(WORKING/'pages'/'*').map { |x| File.basename(x) }.uniq
# store global config options
OPTS = {}
# Make it so we can load Abbot as a library. Once Abbot is installed as a
# gem this
$:
desc "builds the pages to prepare for deployment"
task :build do
Dir.chdir WORKING
puts `#{SC_BUILD} #{TARGETS * ' '} -rv --languages=#{LANGUAGES.join(',')}`
end
desc "cleans the build output"
task :clean do
path = WORKING / 'tmp' / 'build' / 'production' / 'static'
puts "removing #{path}"
puts `rm -r #{path}`
end
desc "installs gems needed for this Rakefile to run"
task :install_gems do
puts "sudo gem install highline net-ssh net-scp sproutit-sproutcore"
puts `sudo gem install highline net-ssh net-scp sproutit-sproutcore`
end
desc "collects the login password from the operator"
task :collect_password do
begin
require 'highline/import'
rescue LoadError => e
puts "\n ~ FATAL: sproutcore gem is required.\n Try: rake install_gems"
exit(1)
end
puts "Enter login password for www.sproutcore.com to complete this task"
OPTS[:password] = ask("Password: ") { |q| q.echo = '*' }
end
desc "finds all targets in the system and computes their build numbers"
task :prepare_targets do
begin
require 'sproutcore'
rescue LoadError => e
puts "\n ~ FATAL: sproutcore gem is required.\n Try: rake install_gems"
exit(1)
end
puts "discovering all installed targets"
SC.build_mode = :production
project = SC.load_project(WORKING)
# get all targets and prepare them so that build numbers work
targets = TARGETS.map do |name|
target = project.target_for(name)
[target] + target.expand_required_targets
end
targets = targets.flatten.compact.uniq
puts "preparing build numbers"
targets.each { |t| t.prepare!.compute_build_number }
OPTS[:targets] = targets
OPTS[:project] = project
end
desc "copies the built files onto the www.sproutcore.com server"
task :deploy_assets => [:collect_password, :build, :prepare_targets] do
begin
require 'net/ssh'
require 'net/scp'
rescue LoadError => e
puts "\n ~ FATAL: net-scp gem is required.\n Try: rake install_gems"
exit(1)
end
password = OPTS[:password]
targets = OPTS[:targets]
installed = {}
puts "building directory structure"
Net::SSH.start('www.sproutcore.com', 'root', :password => password) do |ssh|
targets.each do |target|
LANGUAGES.each do |lang|
remote_path = "/var/www/static#{target.index_root}/#{lang}"
puts ssh.exec!(%[mkdir -p "#{remote_path}"]) || "%: mkdir -p #{remote_path}"
# see if this guy is already installed
remote_path = "#{remote_path}/#{target.build_number}"
installed[remote_path] = !(ssh.exec!("ls #{remote_path}") =~ /No such file or directory/)
end
end
end
puts "copying static resources onto remote server"
Net::SCP.start('www.sproutcore.com', 'root', :password => password) do |scp|
targets.each do |target|
LANGUAGES.each do |lang|
local_path = target.build_root / lang / target.build_number
remote_path = "/var/www/static#{target.index_root}/#{lang}"
short_path = local_path.gsub /^#{Regexp.escape(target.build_root)}/,''
if installed["#{remote_path}/#{target.build_number}"]
puts " ~ #{target.target_name}#{short_path} already installed"
elsif File.directory?(local_path)
puts " ~ uploading #{target.target_name}#{short_path}"
scp.upload! local_path, remote_path, :recursive => true
else
puts "\n\n ~ WARN: cannot install #{target.target_name}:#{lang} - local path #{local_path} does not exist\n\n"
end
end # LANGUAGES.each
end # targets.each
end # Net::SCP.start
end
desc "creates symlinks to the latest versions of all pages and apps. Make sure you deploy your assets also!"
task :link_current => [:collect_password, :prepare_targets] do
# don't require unless this task runs to avoid dependency problems
begin
require 'net/ssh'
rescue LoadError => e
puts "\n ~ FATAL: net-ssh gem is required.\n Try: rake install_gems"
exit(1)
end
# now filter out only app targets living in the current project
targets = OPTS[:targets]
project = OPTS[:project]
targets = targets.select { |t| t.target_type == :app }
targets = targets.select do |t|
t.source_root =~ /^#{Regexp.escape(project.project_root)}/
end
puts "linking targets:\n #{targets.map {|t| t.target_name} * "\n " }"
# SSH in and do the symlink
password = OPTS[:password]
Net::SSH.start('www.sproutcore.com', 'root', :password => password) do |ssh|
targets.each do |target|
# find the local build number
build_number = target.prepare!.compute_build_number
puts "Installing #{target.target_name}..."
# first, link index.html
from_path = "/var/www/static#{target.index_root}/en/#{build_number}/index.html"
to_path = "/var/www#{target.target_name}"
puts ssh.exec!("mkdir -p #{to_path}") || "% mkdir -p #{to_path}"
to_path = "#{to_path}/index.html"
unless ssh.exec!("ls #{to_path}").empty? # check for existance
puts ssh.exec!("rm #{to_path}") || " ~ Removed link at #{to_path}"
end
puts ssh.exec!("ln -s #{from_path} #{to_path}") || " ~ Linked #{from_path} => #{to_path}"
# link each language
LANGUAGES.each do |lang|
from_path = "/var/www/static#{target.index_root}/#{lang}/#{build_number}"
to_path = "/var/www#{target.target_name}/#{lang}"
puts " ~ installing language: #{lang}"
unless ssh.exec!("ls #{to_path}").empty? # check for existance
puts ssh.exec!("rm #{to_path}") || " ~ Removed link at #{to_path}"
end
puts ssh.exec!("ln -s #{from_path} #{to_path}") || " ~ Linked #{from_path} => #{to_path}"
end
# Also - this is for home only
if target.target_name.to_s == '/home'
to_path = '/var/www/index.html'
from_path = "/var/www/static#{target.index_root}/en/#{build_number}/index.html"
unless ssh.exec!("ls #{to_path}").empty? # check for existance
puts ssh.exec!("rm #{to_path}") || " ~ Removed link at #{to_path}"
end
puts ssh.exec!("ln -s #{from_path} #{to_path}") || " ~ Linked #{from_path} => #{to_path}"
end
end
end
end
desc "builds and then deploys the files to the server. This will not clean the build first, which will be faster. If you have trouble try deploy_clean"
task :deploy => [:collect_password, :build, :deploy_assets, :link_current]
desc "first cleans, then deploys the files"
task :deploy_clean => [:clean, :deploy]