forked from stringer-rss/stringer
/
Rakefile
210 lines (171 loc) · 6.2 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
require "sinatra/activerecord/rake"
require "rubygems"
require "bundler"
require "net/http"
Bundler.require
require "./app"
require_relative "./app/jobs/fetch_feed_job"
require_relative "./app/tasks/fetch_feeds"
require_relative "./app/tasks/change_password"
require_relative "./app/tasks/remove_old_stories.rb"
desc "Fetch all feeds."
task :fetch_feeds do
FetchFeeds.new(Feed.all).fetch_all
end
desc "Lazily fetch all feeds."
task :lazy_fetch do
if ENV['APP_URL']
uri = URI(ENV['APP_URL'])
Net::HTTP.get_response(uri)
end
FeedRepository.list.each do |feed|
Delayed::Job.enqueue FetchFeedJob.new(feed.id)
end
end
desc "Fetch single feed"
task :fetch_feed, :id do |t, args|
FetchFeed.new(Feed.find(args[:id])).fetch
end
desc "Clear the delayed_job queue."
task :clear_jobs do
Delayed::Job.delete_all
end
desc "Work the delayed_job queue."
task :work_jobs do
Delayed::Job.delete_all
3.times do
Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'],
:max_priority => ENV['MAX_PRIORITY']).start
end
end
desc "Change your password"
task :change_password do
ChangePassword.new.change_password
end
desc "Clean up old stories that are read and unstarred"
task :cleanup_old_stories, :number_of_days do |t, args|
args.with_defaults(:number_of_days => 30)
RemoveOldStories.remove!(args[:number_of_days].to_i)
end
desc "Start server and serve JavaScript test suite at /test"
task :test_js do
require_relative "./spec/javascript/test_controller"
Stringer.run!
end
begin
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:speedy_tests) do |t|
t.rspec_opts = "--tag ~speed:slow"
end
RSpec::Core::RakeTask.new(:spec)
task :default => [:speedy_tests]
rescue LoadError
# allow for bundle install --without development:test
end
desc "deploy stringer on Heroku"
task :deploy do
require 'excon'
require 'formatador'
require 'json'
require 'netrc'
require 'rendezvous'
require 'securerandom'
Formatador.display_line("[negative]<> deploying stringer to Heroku[/]")
# grab netrc credentials, set by toolbelt via `heroku login`
Formatador.display_line("[negative]<> reading your global Heroku credentials from ~/.netrc (set when you ran heroku login)...[/]")
_, password = Netrc.read['api.heroku.com']
# setup excon for API calls
heroku = Excon.new(
'https://api.heroku.com',
:headers => {
"Accept" => "application/vnd.heroku+json; version=3",
"Authorization" => "Basic #{[':' << password].pack('m').delete("\r\n")}",
"Content-Type" => "application/json"
}
)
#heroku create
Formatador.display_line("[negative]<> creating app[/]")
app_data = JSON.parse(heroku.post(:path => "/apps").body)
#git push heroku master
Formatador.display_line("[negative]<> pushing code to [underline]#{app_data['name']}[/]")
`git push git@heroku.com:#{app_data['name']}.git master`
heroku.reset # reset socket as git push may take long enough for timeout
#heroku config:set SECRET_TOKEN=`openssl rand -hex 20`
Formatador.display_line("[negative]<> setting SECRET_TOKEN on [underline]#{app_data['name']}[/]")
heroku.patch(
:body => { "SECRET_TOKEN" => SecureRandom.hex(20) }.to_json,
:path => "/apps/#{app_data['id']}/config-vars"
)
#heroku run rake db:migrate
Formatador.display_line("[negative]<> running `rake db:migrate` on [underline]#{app_data['name']}[/]")
run_data = JSON.parse(heroku.post(
:body => {
"attach" => true,
"command" => "rake db:migrate"
}.to_json,
:path => "/apps/#{app_data['id']}/dynos"
).body)
Rendezvous.start(
:url => run_data['attach_url']
)
heroku.reset # reset socket as db:migrate may take long enough for timeout
#heroku restart
Formatador.display_line("[negative]<> restarting [underline]#{app_data['name']}[/]")
heroku.delete(:path => "/apps/#{app_data['id']}/dynos")
#heroku addons:add scheduler
Formatador.display_line("[negative]<> adding scheduler:standard to [underline]#{app_data['name']}[/]")
heroku.post(
:body => { "plan" => { "name" => "scheduler:standard" } }.to_json,
:path => "/apps/#{app_data['id']}/addons"
)
#heroku addons:open scheduler
Formatador.display_lines([
"[negative]<> Add `[bold]rake fetch_feeds[/][negative]` hourly task at [underline]https://api.heroku.com/apps/#{app_data['id']}/addons/scheduler:standard[/]",
"[negative]<> Impatient? After adding feeds, immediately fetch the latest with `heroku run rake fetch_feeds -a #{app_data['name']}`",
"[negative]<> stringer available at [underline]#{app_data['web_url']}[/]"
])
end
desc "update stringer on heroku"
task :update, :app do |task, args|
require 'excon'
require 'formatador'
require 'json'
require 'netrc'
require 'rendezvous'
unless args.app
Formatador.display_line("[negative]! Error: App required, please run as `bundle exec rake update[app]`[/]")
exit
end
Formatador.display_line("[negative]<> updating Heroku stringer on [underline]#{args.app}[/]")
# grab netrc credentials, set by toolbelt via `heroku login`
Formatador.display_line("[negative]<> reading your global Heroku credentials from ~/.netrc (set when you ran heroku login)...[/]")
_, password = Netrc.read['api.heroku.com']
# setup excon for API calls
heroku = Excon.new(
'https://api.heroku.com',
:headers => {
"Accept" => "application/vnd.heroku+json; version=3",
"Authorization" => "Basic #{[':' << password].pack('m').delete("\r\n")}",
"Content-Type" => "application/json"
}
)
#git push heroku master
Formatador.display_line("[negative]<> pushing code to [underline]#{args.app}[/]")
`git push git@heroku.com:#{args.app}.git master`
#heroku run rake db:migrate
Formatador.display_line("[negative]<> running `rake db:migrate` on [underline]#{args.app}[/]")
run_data = JSON.parse(heroku.post(
:body => {
"attach" => true,
"command" => "rake db:migrate"
}.to_json,
:path => "/apps/#{args.app}/dynos"
).body)
Rendezvous.start(
:url => run_data['attach_url']
)
heroku.reset # reset socket as db:migrate may take long enough for timeout
#heroku restart
Formatador.display_line("[negative]<> restarting [underline]#{args.app}[/]")
heroku.delete(:path => "/apps/#{args.app}/dynos")
end