forked from ari/jobsworth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.rb
executable file
·300 lines (237 loc) · 6.67 KB
/
setup.rb
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
#!/usr/bin/env ruby
puts "*******************************************************************************************"
puts "This setup script will overwrite any configuration files you've already created in config/*"
puts "If you don't want this to happen, please press <Ctrl-c> to abort."
puts "*******************************************************************************************"
puts
print "Enter MySQL database name for ClockingIT [cit]: "
db = gets
db = "cit" if db == "\n"
print "Enter username for ClockingIT MySQL account [cit]: "
dbuser = gets
dbuser = "cit" if dbuser == "\n"
print "Enter password for ClockingIT MySQL account [cit]: "
dbpw = gets
dbpw = "cit" if dbpw == "\n"
print "Enter host for ClockingIT MySQL account [localhost]: "
dbhost = gets
dbhost = "localhost" if dbhost == "\n"
db.strip!
dbuser.strip!
dbpw.strip!
dbhost.strip!
puts
puts "Using '#{dbuser}' / '#{dbpw}' to access the '#{db}' database on '#{dbhost}'."
puts
puts "Please create the database and user for ClockingIT by running something like this: "
puts " echo \"CREATE DATABASE #{db} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; GRANT ALL ON #{db}.* TO '#{dbuser}'@'localhost' IDENTIFIED BY '#{dbpw}'; FLUSH PRIVILEGES;\" | mysql -u root -p "
puts
puts "Press <Return> once you have done this."
gets
puts
domain = "\n"
while domain == "\n" || domain.split('.').size < 3
puts
print "Enter hostname for the ClockingIT service (for example projects.mycompany.com): "
domain = gets
end
domain.strip!
subdomain = domain.split('.').first
domain = domain.split('.')[1..-1].join('.')
puts
puts "Using '#{subdomain}.#{domain}' to access ClockingIT."
puts
company = "\n"
while company == "\n"
print "Enter name of initial company: "
company = gets
end
name = "\n"
while name == "\n"
print "Enter name of initial user: "
name = gets
end
username = "\n"
while username == "\n"
print "Enter username for initial user: "
username = gets
end
password = "\n"
password2 = "\n"
while password != password2 || password == "\n"
password = "\n"
password2 = "\n"
while password == "\n"
print "Enter password for initial user: "
password = gets
end
while password2 == "\n"
print "Enter password (again) for initial user: "
password2 = gets
end
password.strip!
password2.strip!
end
email = "\n"
while email == "\n"
print "Enter email address of initial user: "
email = gets
end
company.strip!
name.strip!
username.strip!
password.strip!
email.strip!
puts
puts "Will create '#{username}' with password '#{password}' for '#{company}' as initial administrator account."
puts
jug_port = "\n"
print "Enter port for push server [443]: "
jug_port = gets
jug_port = "443" if jug_port == "\n"
jug_port.strip!
puts "Creating config files..."
puts " Creating config/database.yml"
socket = %x[mysql_config --socket]
socket.strip!
db_config = []
File.open("config/database.yml-example") do |file|
while line = file.gets
db_config << line
end
end
db_config = db_config.join
db_config.gsub!(/DATABASE/, db)
db_config.gsub!(/USERNAME/, dbuser)
db_config.gsub!(/PASSWORD/, dbpw)
db_config.gsub!(/HOST/, dbhost)
db_config.gsub!(/SOCKET/, (socket.include?('/') ? "socket: #{socket}" : "") )
File.open("config/database.yml", "w") do |file|
file.puts db_config
end
puts " Creating config/environment.local.rb"
env = []
File.open("config/environment.local.example") do |file|
while line = file.gets
env << line
end
end
env = env.join
env.gsub!(/clockingit\.com/, domain)
File.open("config/environment.local.rb", "w") do |file|
file.puts env
end
puts " Creating config/juggernaut_config.yml"
jug = []
File.open("config/juggernaut_config.yml-example") do |file|
while line = file.gets
jug << line
end
end
jug = jug.join
jug.gsub!(/clockingit\.com/, domain)
jug.gsub!(/www\./, subdomain + ".")
jug.gsub!(/443/, jug_port)
File.open("config/juggernaut_config.yml", "w") do |file|
file.puts jug
end
puts "Creating directories..."
puts " log..."
Dir.mkdir("log") rescue nil
puts " index..."
Dir.mkdir("index") rescue nil
puts " store..."
Dir.mkdir("store") rescue nil
puts " store/avatars..."
Dir.mkdir("store/avatars") rescue nil
puts " store/logos..."
Dir.mkdir("store/logos") rescue nil
puts " tmp..."
Dir.mkdir("tmp") rescue nil
puts " tmp/cache..."
Dir.mkdir("tmp/cache") rescue nil
puts
print "Initialize database schema [n]: "
init_db = gets
init_db = "n" if init_db == "\n"
if init_db.include?('y') || init_db.include?('Y')
puts "Initializing database schema"
system("rake db:schema:load RAILS_ENV=production")
system("rake db:migrate RAILS_ENV=production")
end
puts
puts "Loading Rails to create account..."
begin
require "config/environment"
rescue
puts "** Unable to load Rails, please try:"
puts " ./script/console"
puts "and look at the error reported."
exit
end
@user = User.new
@company = Company.new
@user.name = name
@user.username = username
@user.password = password
@user.email = email
@user.time_zone = "Europe/Oslo"
@user.locale = "en_US"
@user.option_externalclients = 1
@user.option_tracktime = 1
@user.option_tooltips = 1
@user.date_format = "%d/%m/%Y"
@user.time_format = "%H:%M"
@user.admin = 1
puts " Creating initial company..."
@company.name = company
@company.contact_email = email
@company.contact_name = name
@company.subdomain = subdomain.downcase
if @company.save
@customer = Customer.new
@customer.name = @company.name
@company.customers << @customer
puts " Creating initial user..."
@company.users << @user
else
c = Company.find_by_subdomain(subdomain)
if c
puts "** Unable to create initial company, #{subdomain} already registered.. **"
del = "\n"
print "Delete existing company '#{c.name}' with subdomain '#{subdomain}' and try again? [y]: "
del = gets
del = "y" if del == "\n"
del.strip!
if del.downcase.include?('y')
c.destroy
if @company.save
@customer = Customer.new
@customer.name = @company.name
@company.customers << @customer
puts " Creating initial user..."
@company.users << @user
else
puts " Still unable to create initial company. Check database settings..."
exit
end
end
else
exit
end
end
puts "Creating merged CSS and JavaScript files..."
system("rake asset:packager:build_all")
puts "Done"
puts "Running any pending migrations..."
system("rake db:migrate RAILS_ENV=production")
puts "Done"
puts
puts "All done!"
puts "---------"
puts
puts "Please start the required services by entering the following in a console:"
puts " nohup ./script/push_server &"
puts "Make sure passenger and apache httpd are properly set up and a virtual host defined."
puts
puts "Access your installation from http://#{subdomain}.#{domain}:3000"