forked from intrigueio/intrigue-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
216 lines (179 loc) · 6.14 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
require 'rspec/core'
require 'rspec/core/rake_task'
require 'json'
require 'yaml'
require 'fileutils'
# Config files
$intrigue_basedir = File.dirname(__FILE__)
$intrigue_environment = "development"
# Configuration and scripts
puma_config_file = "#{$intrigue_basedir}/config/puma.rb"
system_config_file = "#{$intrigue_basedir}/config/config.json"
database_config_file = "#{$intrigue_basedir}/config/database.yml"
sidekiq_config_file = "#{$intrigue_basedir}/config/sidekiq.yml"
redis_config_file = "#{$intrigue_basedir}/config/redis.yml"
control_script = "#{$intrigue_basedir}/util/control.sh"
all_config_files = [
puma_config_file,
system_config_file,
database_config_file,
sidekiq_config_file,
redis_config_file
]
desc "Clean"
task :clean do
puts "[+] Cleaning up!"
all_config_files.each do |c|
FileUtils.mv c, "#{c}.backup"
end
end
desc "System Setup"
task :setup do
puts "[+] Setup initiated!"
## Copy puma config into place
puts "[+] Copying puma config...."
if File.exist? puma_config_file
puts "[ ] File already exists, skipping: #{puma_config_file}"
else
puts "[+] Creating.... #{puma_config_file}"
FileUtils.cp "#{puma_config_file}.default", puma_config_file
end
## Copy system config into place
puts "[+] Copying system config...."
if File.exist? system_config_file
puts "[ ] File already exists, skipping: #{system_config_file}"
config = JSON.parse(File.read(system_config_file))
system_password = config["credentials"]["password"]
else
puts "[+] Creating system config: #{system_config_file}"
FileUtils.cp "#{system_config_file}.default", system_config_file
# Set up our password
config = JSON.parse(File.read(system_config_file))
if config["credentials"]["password"] == "AUTOGENERATED"
# generate a new password
system_password = "#{(0...11).map { ('a'..'z').to_a[rand(26)] }.join}"
puts "[+] ==================================="
puts "[+] GENERATING NEW SYSTEM PASSWORD!"
# Save it
config["credentials"]["password"] = system_password
File.open(system_config_file,"w").puts JSON.pretty_generate(config)
else
system_password = config["credentials"]["password"]
end
end
# Print it
puts "[+] SYSTEM USERNAME: intrigue"
puts "[+] SYSTEM PASSWORD: #{system_password}"
puts "[+] ==================================="
## Copy database config into place
puts "[+] Copying database config."
if File.exist? database_config_file
puts "[ ] File already exists, skipping: #{database_config_file}"
else
puts "[+] Creating.... #{database_config_file}"
FileUtils.cp "#{database_config_file}.default", database_config_file
# Set up our password
config = YAML.load_file(database_config_file)
config["development"]["password"] = system_password
config["production"]["password"] = system_password
config["docker"]["password"] = system_password
File.open(database_config_file,"w").puts YAML.dump config
end
## Copy redis config into place
puts "[+] Copying redis config."
if File.exist? redis_config_file
puts "[ ] File already exists, skipping: #{redis_config_file}"
else
puts "[+] Creating.... #{redis_config_file}"
FileUtils.cp "#{redis_config_file}.default", redis_config_file
end
## Place sidekiq task worker configs into place
puts "[+] Setting up sidekiq config...."
worker_configs = [
sidekiq_config_file
]
worker_configs.each do |wc|
unless File.exist?(wc)
puts "[+] Copying: #{wc}.default"
FileUtils.cp "#{wc}.default", wc
else
puts "[+] #{wc} already exists!"
end
end
# end worker config placement
puts "[+] Downloading latest data files..."
Dir.chdir("#{$intrigue_basedir}/data/"){ puts %x["./get_latest.sh"] }
puts "[+] Complete!"
end
desc "Reset Workers"
task :reset_workers do
puts "[ ] Resetting workers"
require './core'
Intrigue::Model::Project.all.each do |p|
puts "[+] Clean state for #{p.name}"
p.graph_generation_in_progress=false
p.save
end
end
#desc "Run Specs"
#task :spec do
#end
#desc "Run Integration Specs (requires API running)"
#task :integration do
# t.rspec_opts = "--pattern spec/integration/*_spec.rb"
#end
# database set up
def setup_database
require "sequel"
options = {
:max_connections => 16,
:pool_timeout => 240
}
database_config = YAML.load_file("#{$intrigue_basedir}/config/database.yml")
database_host = database_config[$intrigue_environment]["host"] || "localhost"
database_port = database_config[$intrigue_environment]["port"] || 5432
database_user = database_config[$intrigue_environment]["user"]
database_pass = database_config[$intrigue_environment]["password"]
database_name = database_config[$intrigue_environment]["database"]
database_debug = database_config[$intrigue_environment]["debug"]
if database_pass
$db = Sequel.connect("postgres://#{database_user}:#{database_pass}@#{database_host}:#{database_port}/#{database_name}", options)
else
$db = Sequel.connect("postgres://#{database_user}@#{database_host}:#{database_port}/#{database_name}", options)
end
$db.loggers << Logger.new($stdout) if database_debug
# Allow datasets to be paginated
$db.extension :pagination
Sequel.extension :pg_json_ops
Sequel.extension :migration
end
namespace :db do
desc "Prints current schema version"
task :version do
setup_database
version = if $db.tables.include?(:schema_info)
$db[:schema_info].first[:version]
end || 0
puts "[+] Schema Version: #{version}"
end
desc "Perform migration up to latest migration available"
task :migrate do
setup_database
Sequel::Migrator.run($db, "db")
Rake::Task['db:version'].execute
end
desc "Perform rollback to specified target or full rollback as default"
task :rollback, :target do |t, args|
setup_database
args.with_defaults(:target => 0)
Sequel::Migrator.run($db, "db", :target => args[:target].to_i)
Rake::Task['db:version'].execute
end
desc "Perform migration reset (full rollback and migration)"
task :reset do
setup_database
Sequel::Migrator.run($db, "db", :target => 0)
Sequel::Migrator.run($db, "db")
Rake::Task['db:version'].execute
end
end