-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
425 lines (345 loc) · 10.7 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
require 'rubygems'
require 'bundler'
require 'yaml'
BASE_PATH = File.dirname(File.absolute_path(__FILE__))
begin
Bundler.setup(:default, :development, :test)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
require 'rake'
require 'rspec/core'
require 'rspec/core/rake_task'
require "sequel"
def expand_env_string(env)
return "development" if env == "dev"
return "production" if env == "prod"
return env
end
namespace :spec do
desc "watch files and run tests automatically"
task :onchange do
system "guard -g specs"
end
desc "run tests now"
RSpec::Core::RakeTask.new(:now) do |spec|
# do not run integration tests, doesn't work on TravisCI
spec.pattern = FileList['spec/api/*_spec.rb', 'spec/models/*_spec.rb',
'spec/lib/*_spec.rb']
end
desc "generate nice html view"
task :html do
system "rspec --format html --out docs/rspec.html"
end
end
task :default do
exec "rake -T"
end
desc "Start|Stop production API (as deamon)"
task :daemon, :action do |cmd, args|
def pid_exist?(pid)
begin
return Process.getpgid(pid)
rescue
return false
end
end
def check_pid_file
if File.exist?("api.pid")
# pid file exists: api running?
pid = IO.read("api.pid").to_i
if(pid_exist?(pid))
puts "api server is running (PID: #{pid})"
exit 0
end
# pid file exists, but process crashed maybe
# anyway, delete pid file
File.delete("api.pid")
end
end
if args[:action] == "start"
check_pid_file
# no process is running ... start a new one
system "rackup -E production -D -P api.pid > log/daemon_production.log"
sleep 0.5
check_pid_file
# if you reached this, api was not started
puts "api server not running"
exit 50
elsif args[:action] == "stop"
if File.exist?("api.pid")
pid = IO.read("api.pid").to_i
if(pid_exist?(pid))
system "kill $(cat api.pid)"
File.delete("api.pid")
exit 0
end
end
exit 0
elsif args[:action] == "status"
check_pid_file
# if you reached this, api was not started
puts "api server not running"
exit 50
end
end
desc "Start API with environment (prod|dev)"
task :start, :env do |cmd, args|
env = expand_env_string(args[:env]) || "production"
if env == "production"
puts "Starting in production mode ..."
exec "rackup -E production"
elsif env == "development"
puts "Starting in development mode ..."
exec "bundle exec guard -g development"
else
puts "Please specify environment."
end
end
task :environment, [:env] do |cmd, args|
ENV["RACK_ENV"] = expand_env_string(args[:env]) || "development"
require "./config/environment"
end
namespace :db do
desc "Create super admin"
task :create_super_admin, :env, :username do |cmd, args|
env = expand_env_string(args[:env]) || "development"
Rake::Task['environment'].invoke(env)
require "digest"
api_access_key = Piecemaker::Helper::API_Access_Key::generate
time_now = Time.now.to_i
name = args[:username] || "Super Admin"
email = args[:username] || "super-admin-#{time_now}@example.com"
password = args[:username] || "super-admin-#{time_now}"
DB[:users].insert(
:name => name,
:email => email,
:password => Digest::SHA1.hexdigest(password),
:api_access_key => api_access_key,
:user_role_id => 'super_admin')
puts ""
puts "Email : #{email}"
puts "Password : #{password}"
puts "Role : super_admin"
puts ""
puts "A fresh API Access Key has been generated '#{api_access_key}'."
end
desc "Run database migrations (:version is optional and defaults to latest)"
task :migrate, :env, :version do |cmd, args|
env = expand_env_string(args[:env]) || "development"
Rake::Task['environment'].invoke(env)
require 'sequel/extensions/migration'
if(args[:version])
Sequel::Migrator.apply(DB, "db/migrations", args[:version].to_i)
else
Sequel::Migrator.apply(DB, "db/migrations")
end
end
desc "Rollback the database"
task :rollback, :env do |cmd, args|
env = expand_env_string(args[:env]) || "development"
Rake::Task['environment'].invoke(env)
require 'sequel/extensions/migration'
version = (row = DB[:schema_info].first) ? row[:version] : nil
Sequel::Migrator.apply(DB, "db/migrations", version - 1)
end
desc "Nuke the database (drop all tables)"
task :nuke, :env do |cmd, args|
env = expand_env_string(args[:env]) || "development"
Rake::Task['environment'].invoke(env)
DB.tables.each do |table|
DB.drop_table(table, :cascade=>true)
end
end
desc "Reset the database (nuke & migrate & import_from_file)"
task :reset, :env do |cmd, args|
env = expand_env_string(args[:env])
Rake::Task['db:nuke'].invoke(env)
Rake::Task['db:migrate'].invoke(env)
Rake::Task['db:import_from_file'].invoke(env, 'user_roles')
Rake::Task['db:import_from_file'].reenable # @todo use execute instead?
Rake::Task['db:import_from_file'].invoke(env, 'role_permissions')
end
desc "Export table into file"
task :export_into_file, :env, :table do |cmd, args|
unless args[:table]
puts "Usage: rake db:export_into_file[env,'table']"
exit 1
end
env = expand_env_string(args[:env])
Rake::Task['environment'].invoke(env)
DB.copy_into( args[:table].to_sym , {
:data => File.read( BASE_PATH + "/db/init/#{args[:table]}.sql" ),
:format => :csv,
:options => "HEADER TRUE"
})
end
desc "Import table into database"
task :import_from_file, :env, :table do |cmd, args|
unless args[:table]
puts "Usage: rake db:import_from_file[env,'table']"
exit 1
end
env = expand_env_string(args[:env])
Rake::Task['environment'].invoke(env)
DB.copy_into( args[:table].to_sym , {
:data => File.read( BASE_PATH + "/db/init/#{args[:table]}.sql" ),
:format => :csv,
:options => "HEADER TRUE"
})
end
end
def scan_entities(verbose)
return_value = []
entities = []
Dir[BASE_PATH + "/api/*"].each do |file|
line_no = 0
line_stack = []
IO.foreach(file) do |line|
line_no += 1
line_stack << line
authorize = line.scan(/authorize! .*/)
if authorize[0]
desc = []
url = []
line_stack.reverse.each do |lstack|
if desc.size == 0 || url.size == 0
desc = lstack.scan(/desc "(.*)"/) if desc.size == 0
url = lstack.scan(/((get|put|post|delete) .*)/) if url.size == 0
else
line_stack = []
end
end
line_stack = []
if verbose
return_value << [
"__#{authorize[0]}__",
url[0][0].gsub(/do */, "") + " in api/#{File.basename(file)}:#{line_no}",
desc,
""
]
else
_action = authorize[0].scan(/:(.*), /).flatten[0]
entities << _action if _action
end
end
end
end
unless verbose
entities.uniq!
entities.sort!
return entities
else
return return_value
end
end
namespace :roles do
desc "Scan files for permission entities"
task :scan_entities, :verbose do |cmd, args|
puts scan_entities(args[:verbose])
end
desc "Update config/permissions.yml with all available permissions"
task :update_permissions_file do
entities = scan_entities(false)
content = "# this file is auto-generated with 'rake roles:update_permissions_file'" + "\n" + YAML.dump(entities)
IO.write('config/permissions.yml', content)
puts "config/permission.yml updated"
end
desc "Generate roles and permissions matrix from database (format:html|json)"
task :output, :env, :format do |cmd, args|
env = expand_env_string(args[:env]) || "development"
Rake::Task['environment'].invoke(env)
entities = scan_entities(false)
# build user roles array
def get_user_roles_ordered_by_inheritance(id, user_roles_ordered)
root_user_roles = UserRole.where(:inherit_from_id => id).all
if root_user_roles
root_user_roles.each do |user_role|
user_roles_ordered << user_role
get_user_roles_ordered_by_inheritance(
user_role.id, user_roles_ordered)
end
end
end
@user_roles_ordered = []
get_user_roles_ordered_by_inheritance(nil, @user_roles_ordered)
@user_roles_ordered.reverse!
# build role permissions array
@distinct_entities = RolePermission.distinct(:action).select(:action).order(:action).all
# build matrix
matrix = []
@distinct_entities.each do |action|
action = action.action
permissions = {}
@user_roles_ordered.each do |user_role|
permission = Piecemaker::Helper::Auth::get_permission_recursively(user_role, action)
if permission
if permission.allowed
permissions[user_role.id] = "Yes"
else
permissions[user_role.id] = "No"
end
else
permissions[user_role.id] = "No"
end
end
deleted_action = entities.delete(action)
matrix << {
:action => action + (deleted_action ? "" : " (not used)"),
:permissions => permissions
}
end
entities.each do |action|
permissions = {}
@user_roles_ordered.each do |user_role|
permissions[user_role.id] = "Def"
end
matrix << {
:action => action,
:permissions => permissions
}
end
output = {
:headers => @user_roles_ordered.map{|user_role| user_role.id },
:data => matrix
}
# all done ... do something with the data
if args[:format] == "html"
puts "<html>"
puts "<head>"
puts "<style type='text/css'>"
puts IO.read(BASE_PATH + "/docs/roles_matrix.css")
puts "</style>"
puts "</head>"
puts "<body>"
puts "<table>"
# header
puts "<thead>"
puts "<tr>"
puts "<th>Action</th>"
output[:headers].each do |h|
puts "<th>#{h}</th>"
end
puts "<tr>"
puts "</thead>"
# data
puts "<tbody>"
output[:data].each do |e|
puts "<tr>"
puts "<td class='action'><input type='checkbox'> #{e[:action]}</td>"
output[:headers].each do |p|
puts "<td class='permission #{e[:permissions][p]}'>#{e[:permissions][p]}</td>"
end
puts "</tr>"
end
puts "</tbody>"
puts "</table>"
puts "</body>"
puts "</html>"
else # json
puts output
end
end
end