Rakefile
4.25 KB
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
require 'yaml'
begin
load 'local.rake'
rescue LoadError
# nothing
end
$SPB_ENV = ENV.fetch('SPB_ENV', 'local')
ssh_config_file = "config/#{$SPB_ENV}/ssh_config"
ips_file = "config/#{$SPB_ENV}/ips.yaml"
config_file = "config/#{$SPB_ENV}/config.yaml"
iptables_file = "config/#{$SPB_ENV}/iptables-filter-rules"
ENV['CHAKE_SSH_CONFIG'] = ssh_config_file
if $SPB_ENV == 'lxc'
system("mkdir -p config/lxc; sudo lxc-ls -f -F name,ipv4 | sed -e '/^softwarepublico/ !d; s/softwarepublico_//; s/_[0-9_]*/:/ ' > #{ips_file}.new")
begin
ips = YAML.load_file("#{ips_file}.new")
raise ArgumentError unless ips.is_a?(Hash)
FileUtils.mv ips_file + '.new', ips_file
rescue Exception => ex
puts ex.message
puts
puts "Q: did you boot the containers first?"
exit
end
config = YAML.load_file('config/local/config.yaml')
config['external_ip'] = ips['reverseproxy']
config['relay_ip'] = ips['email']
File.open(config_file, 'w') do |f|
f.puts(YAML.dump(config))
end
File.open('config/lxc/iptables-filter-rules', 'w') do |f|
lxc_host_bridge_ip = '192.168.122.1' # FIXME don't hardcode
f.puts "-A INPUT -s #{lxc_host_bridge_ip} -p tcp -m state --state NEW --dport 22 -j ACCEPT"
end
end
require 'chake'
if Chake::VERSION < '0.4.3'
fail "Please upgrade to chake 0.4.3+"
end
ips ||= YAML.load_file(ips_file)
config ||= YAML.load_file(config_file)
firewall ||= File.open(iptables_file).read
$nodes.each do |node|
node.data['config'] = config
node.data['peers'] = ips
node.data['firewall'] = firewall
end
task :console do
require 'pry'
binding.pry
end
task :test do
sh "SPB_ENV=#{$SPB_ENV} ./test/run_all"
end
file 'ssh_config.erb'
if ['local', 'lxc'].include?($SPB_ENV)
file ssh_config_file => ['nodes.yaml', ips_file, 'ssh_config.erb', 'Rakefile'] do |t|
require 'erb'
template = ERB.new(File.read('ssh_config.erb'))
File.open(t.name, 'w') do |f|
f.write(template.result(binding))
end
puts 'ERB %s' % t.name
end
end
task :backup => ssh_config_file do
# setup
sh 'ssh', '-F', ssh_config_file, 'integration', 'sudo', 'rm -rf /tmp/backups'
sh 'ssh', '-F', ssh_config_file, 'social', 'sudo', 'rm -rf /tmp/backups'
sh 'mkdir', '-p', 'backups'
# integration
sh 'scp', '-F', ssh_config_file, 'utils/migration/backup_integration.sh', 'integration:/tmp'
sh 'ssh', '-F', ssh_config_file, 'integration', 'sudo', '/tmp/backup_integration.sh'
sh 'scp', '-F', ssh_config_file, 'integration:/tmp/backups/*', 'backups/'
# social
sh 'scp', '-F', ssh_config_file, 'utils/migration/backup_social.sh', 'social:/tmp'
sh 'ssh', '-F', ssh_config_file, 'social', 'sudo', '/tmp/backup_social.sh'
sh 'scp', '-F', ssh_config_file, 'social:/tmp/backups/*', 'backups/'
end
task :restore => ssh_config_file do
#integration
sh 'scp', '-r', '-F', ssh_config_file, 'backups', 'integration:/tmp'
sh 'scp', '-F', ssh_config_file, 'utils/migration/restore_integration.sh', 'integration:/tmp'
sh 'ssh', '-F', ssh_config_file, 'integration', 'sudo', '/tmp/restore_integration.sh'
#social
sh 'scp', '-r', '-F', ssh_config_file, 'backups', 'social:/tmp'
sh 'scp', '-F', ssh_config_file, 'utils/migration/restore_social.sh', 'social:/tmp'
sh 'ssh', '-F', ssh_config_file, 'social', 'sudo', '/tmp/restore_social.sh'
end
task :bootstrap_common => 'config/local/ssh_config'
unless ENV['nodeps']
task 'converge:integration' => 'converge:database'
task 'converge:social' => 'converge:database'
end
$ALT_SSH_PORT = config.fetch('alt_ssh_port', 2222)
$nodes.find { |n| n.hostname == 'reverseproxy' }.data['ssh_port'] = $ALT_SSH_PORT
desc 'Makes configurations needed before the bootstrap phase'
task :preconfig => ssh_config_file do
sh 'mkdir', '-p', 'tmp/'
preconfig_file = "tmp/preconfig.#{$SPB_ENV}.stamp"
if File.exist?(preconfig_file)
puts "I: preconfig already done."
puts "I: delete #{preconfig_file} to force running again"
else
sh 'scp', '-F', ssh_config_file, 'utils/reverseproxy_ssh_setup', 'reverseproxy.unconfigured:/tmp'
sh 'ssh', '-F', ssh_config_file, 'reverseproxy.unconfigured', 'sudo', '/tmp/reverseproxy_ssh_setup', $ALT_SSH_PORT.to_s, ips['reverseproxy'], ips['integration']
File.open(preconfig_file, 'w') do |f|
f.puts($ALT_SSH_PORT)
end
end
end
Dir.glob('tasks/*.rake').each { |f| load f }