-
Notifications
You must be signed in to change notification settings - Fork 6
/
Rakefile
290 lines (261 loc) · 9.46 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
require 'rubygems'
task :default => ["db:seed", :spec, "analyzer:all"]
task :environment do
require File.join(File.dirname(__FILE__), 'app', 'init')
end
namespace :db do
desc "Seed the database"
task :seed => [:environment] do
require 'seed/seed'
end
desc "Create indexes"
task :create_indexes => [:environment] do
files = Dir.glob(File.dirname(__FILE__) + '/lib/service/model/*.rb')
files.each do |file|
classname = File.basename(file).gsub('.rb','').camelize
clazz = Kernel.const_get(classname)
if clazz.respond_to?(:create_indexes)
puts "Creating indexes for #{classname}"
clazz.create_indexes
end
end
end
desc "Truncate categories - USE WITH CAUTION"
task :truncate_categories => [:environment] do
Category.collection.drop
end
desc "Truncate stacks - USE WITH CAUTION"
task :truncate_stacks => [:environment] do
Stack.collection.drop
Template.collection.drop
end
desc "Truncate clouds - USE WITH CAUTION"
task :truncate_clouds => [:environment] do
Cloud.collection.drop
end
desc "Truncate cloud mappings"
task :truncate_mappings => [:environment] do
Cloud.all.each do |c|
c.cloud_mappings = []
end
end
desc "Truncate cloud prices"
task :truncate_prices => [:environment] do
Cloud.all.each do |c|
c.prices = []
end
end
desc "Truncate projects - USE WITH CAUTION"
task :truncate_projects => [:environment] do
Project.collection.drop
end
end
begin
require 'rspec/core/rake_task'
# Put spec opts in a file named .rspec in root
desc "Run all specs"
RSpec::Core::RakeTask.new(:spec)
desc "Run only core specs"
RSpec::Core::RakeTask.new("spec:core") do |t|
t.pattern = "./spec/core/**/*_spec.rb"
end
desc "Run only cfdoc specs"
RSpec::Core::RakeTask.new("spec:cfdoc") do |t|
t.pattern = "./spec/cfdoc/**/*_spec.rb"
end
desc "Run only service specs"
RSpec::Core::RakeTask.new("spec:service") do |t|
t.pattern = "./spec/service/**/*_spec.rb"
end
desc "Run only Sinatra specs"
RSpec::Core::RakeTask.new("spec:app") do |t|
t.pattern = "./spec/app/**/*_spec.rb"
end
rescue LoadError
# not in dev/test env - skip installing these tasks
end
namespace :admin do
desc "Clear all stacks and templates. CAUTION!"
task :clear_stacks => [:environment] do
Mongoid.database['stacks'].remove
Mongoid.database['templates'].remove
end
desc "Clear all categories. CAUTION!"
task :clear_categories => [:environment] do
Mongoid.database['categories'].remove
end
end
namespace :update do
require File.join(File.dirname(__FILE__), 'lib', 'service')
Mongoid.load!('app/config/mongoid.yml')
desc "Move all CURRENT project versions to 0.1.0"
task :move_current_versions do
Project.find(:all).each do |project|
initial_version = project.versions.find(:all, :conditions=>{:number=>"0.1.0"}).first
if initial_version.nil?
new_version = Version.new
new_version.number = "0.1.0"
new_version.description = "Initial version"
new_version.versionable = project
new_version.save!
project_version = project.current_version
unless project_version.nil?
project_version.version = "0.1.0"
project_version.save!
end
end
end
end
desc "Update cloud account and add cloud credentials model"
task :cloud_account_models => [:environment] do
Mongoid.load!('app/config/mongoid.yml')
require 'mongo'
include Mongo
if ENV['RACK_ENV'] == "development"
cli = MongoClient.new("localhost", 27017)
db = cli.db("stack_place_development")
else
cli = MongoClient.new
db = cli.db("transcend_db")
end
Org.find(:all).each do |org|
Cloud.find(:all).each do |cloud|
cloud_account = CloudAccount.where(:cloud_id => cloud.id, :org_id => org.id).first
if cloud_account.nil?
cloud_account = CloudAccount.new(:name => cloud.name)
cloud_account.org = org
cloud_account.cloud = cloud
db_cloud = db["clouds"].find("_id" => cloud.id).to_a[0]
cloud_services = db_cloud["cloud_services"]
unless cloud_services.nil?
cloud_services.each do |service|
new_service = CloudService.new
new_service.service_type = service["type"]
new_service.path = service["path"]
new_service.host = service["host"]
new_service.port = service["port"]
new_service.enabled = service["enabled"]
cloud_account.cloud_services << new_service
end
end
cloud_mappings = db_cloud["cloud_mappings"]
unless cloud_mappings.nil?
cloud_mappings.each do |mapping|
new_mapping = CloudMapping.new
new_mapping.name = mapping["name"]
new_mapping.mapping_type = mapping["mapping_type"]
new_mapping.properties = mapping["properties"]
new_mapping.mapping_entries = mapping["mapping_entries"]
cloud_account.cloud_mappings << new_mapping
end
end
prices = db_cloud["prices"]
unless prices.nil?
prices.each do |price|
new_price = Price.new
new_price.name = price["name"]
new_price.type = price["type"]
new_price.effective_price = price["effective_price"]
new_price.effective_date = price["effective_date"]
new_price.properties = price["properties"]
new_price.entries = price["entries"]
cloud_account.prices << new_price
end
end
puts "Creating cloud account for org: #{org.name}, cloud: #{cloud.name}"
cloud_account.save
end
org.accounts.each do |account|
db_account = db["accounts"].find("_id" => account.id).to_a[0]
cloud_accounts = db_account["cloud_accounts"]
unless cloud_accounts.nil?
cloud_accounts.each do |ca|
if account.cloud_credentials.where(:name => ca["name"]).first.nil?
if ca["cloud_id"] == cloud.id
new_credentials = CloudCredential.new
new_credentials.name = ca["name"]
new_credentials.description = ca["description"]
new_credentials.access_key = ca["access_key"]
new_credentials.secret_key = ca["secret_key"]
new_credentials.cloud_attributes = ca["cloud_attributes"]
new_credentials.stack_preferences = ca["stack_preferences"]
new_credentials.topstack_configured = ca["topstack_configured"]
account.add_cloud_credential!(cloud_account.id, new_credentials)
end
end
end
end
end
end
end
cli.close
end
end
namespace :update do
desc "Give admin permissions to project owners"
task :admin_permissions_to_owners do
Project.find(:all).each do |project|
project.members.select { |s| s.role.to_s == "owner" }.each do |member|
if member.permissions.nil? || member.permissions.length == 0
member.permissions = []
environments = [Environment::DEV, Environment::TEST, Environment::STAGE, Environment::PROD]
environments.each do |e|
member.permissions << Permission.new(:name => Permission::VIEW, :environment => e)
member.permissions << Permission.new(:name => Permission::EDIT, :environment => e)
member.permissions << Permission.new(:name => Permission::PUBLISH, :environment => e)
member.permissions << Permission.new(:name => Permission::PROMOTE, :environment => e)
member.permissions << Permission.new(:name => Permission::CREATE_STACK, :environment => e)
member.permissions << Permission.new(:name => Permission::UPDATE_STACK, :environment => e)
member.permissions << Permission.new(:name => Permission::DELETE_STACK, :environment => e)
member.permissions << Permission.new(:name => Permission::MONITOR, :environment => e)
end
end
end
end
end
end
begin
namespace :analyzer do
desc "run all code analyzing tools (flog)"
task :all => ["flog:total", "flay:flay"]
namespace :flog do
require 'flog_cli'
desc "Analyze total code complexity with flog"
task :total do
threshold = 20
flog = FlogCLI.new
flog.flog %w(app lib)
average = flog.average.round(1)
total_score = flog.total_score
puts "Average complexity: #{flog.average.round(1)}"
puts "Total complexity: #{flog.total_score.round(1)}"
flog.report
fail "Average code complexity has exceeded max! (#{average} > #{threshold})" if average > threshold
end
end
namespace :flay do
require 'flay_task'
#desc "Analyze code duplication with flay"
FlayTask.new() do |t|
t.verbose = true
t.threshold = 22000
end
end
end
rescue LoadError
# not in dev/test env - skip installing these tasks
end
namespace :ci do
desc "Update continuous integration status for configuration managers"
task :update_status do
require "lib/cloudmux/chef"
config_managers_to_update = ConfigManager.where(:continuous_integration_server_ids.nin => [nil, []])
config_managers_to_update.each do |cm|
puts "Updating Continuous Integration Status for Config Manager: #{cm.name}"
manager = CloudMux::Chef::Manager.new(cm)
manager.setup_manageable_objects
manager.generate_all_jobs
manager.update_status
end
end
end