forked from ruby-china/homeland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup
executable file
·112 lines (95 loc) · 2.45 KB
/
setup
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
#!/usr/bin/env ruby
require 'pathname'
# path to your application root.
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
ROW_SIZE = 80
class String
COLORS = {
red: 31,
green: 32,
yellow: 33,
blue: 34
}
def colorize(color)
"\033[#{COLORS[color]}m#{self}\033[0m"
end
end
def puts_section(info, &block)
puts info
puts '-' * ROW_SIZE
yield block
puts '-' * ROW_SIZE
puts ''
end
def puts_line(info, &block)
print info
rsize = ROW_SIZE - info.length
success = yield block
if success == false
puts '[Failed]'.rjust(rsize).colorize(:red)
else
puts '[Done]'.rjust(rsize).colorize(:green)
end
end
def puts_line_with_yn(info, &block)
print info
rsize = ROW_SIZE - info.length
success = yield block
if success == false
puts '[No]'.rjust(rsize).colorize(:red)
else
puts '[Yes]'.rjust(rsize).colorize(:green)
end
end
def replace_file(file_name, from, to)
File.open(file_name, 'r+') do |f|
out = ''
f.each do |line|
out << line.gsub(from, to)
end
f.pos = 0
f.print out
f.truncate(f.pos)
end
end
Dir.chdir APP_ROOT do
# This script is a starting point to setup your application.
# Add necessary setup steps to this file.
puts_section 'Checking Package Dependencies...' do
pkg_exist = true
[['psql', 'PostgreSQL 9.4+'], ['redis-server', 'Redis 2.0+'], ['memcached', 'Memcached 1.4+'], ['convert', 'ImageMagick 6.5+']].each do |item|
puts_line_with_yn item[1] do
if `which #{item[0]}` == ''
pkg_exist = false
false
else
true
end
end
end
exit(0) if pkg_exist == false
end
puts_section 'Installing dependencies' do
system 'gem install bundler --conservative'
system 'bundle check || bundle install'
end
# Config files
puts_section 'Configure' do
%w(database config redis secrets elasticsearch).each do |fname|
system "cp config/#{fname}.yml.default config/#{fname}.yml"
end
print 'Your Redis host (default: 127.0.0.1:6379):'
host = gets.strip
host = '127.0.0.1:6379' if host == ''
replace_file('config/redis.yml', '127.0.0.1', host.split(':')[0])
replace_file('config/redis.yml', '6379', host.split(':')[1])
end
puts_line 'Seed default data...' do
`bundle exec rake db:setup`
end
puts "\n== Removing old logs and tempfiles =="
system 'rm -f log/*'
system 'rm -rf tmp/cache'
puts ''
puts 'Ruby China Successfully Installed.'
end