-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
148 lines (131 loc) · 3.84 KB
/
server.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
require 'rubygems'
require 'bundler'
Bundler.require
require 'sinatra/base'
require 'sinatra/json'
require 'webrick'
require 'webrick/https'
require 'openssl'
CERT_PATH = '.'
webrick_options = {
:Port => 8443,
:Logger => WEBrick::Log::new($stderr, WEBrick::Log::INFO),
:DocumentRoot => '.',
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLCertificate => OpenSSL::X509::Certificate.new(File.open(File.join(CERT_PATH, "server.crt")).read),
:SSLPrivateKey => OpenSSL::PKey::RSA.new(File.open(File.join(CERT_PATH, "server.key")).read),
:SSLCertName => [ [ "CN",WEBrick::Utils::getservername ] ]
}
class MyServer < Sinatra::Base
helpers Sinatra::JSON
get '/' do
puts params
json({
'text' => '배포 테스트 ~!'
})
end
post '/v2/reload' do
begin
puts params
if params['token'] != ENV['API_TOKEN']
json({
'text' => '잘못된 요청'
})
else
puts "RELOAD START"
env = [
"SECRET_KEY_BASE=#{ENV['SECRET_KEY_BASE']}",
"MANDRILL_USERNAME=#{ENV['MANDRILL_USERNAME']}",
"MANDRILL_APIKEY=#{ENV['MANDRILL_APIKEY']}",
"RORLA_HOST=#{ENV['RORLA_HOST']}",
"RORLA_LOGENTRIES_TOKEN=#{ENV['RORLA_LOGENTRIES_TOKEN']}"
]
result = `#{env.join(' ')} /bin/bash reload.sh`
puts "RELOAD END"
json({
'text' => result
})
end
rescue Exception => e
json({
'text' => "에러 발생. #{e}"
})
end
end
post '/reload' do
begin
puts params
if params['token'] != ENV['API_TOKEN']
json({
'text' => '잘못된 요청'
})
else
# pull latest image
Docker::Image.create('fromImage' => 'rorla/rorla', 'tag' => 'latest')
# stop exist container
begin
exist_container = Docker::Container.get('rorla-latest')
exist_container.stop
exist_container.delete
rescue Docker::Error::NotFoundError => e
puts '기존에 생성된 컨테이너 없음'
end
image_name = 'rorla/rorla'
env = [
"SECRET_KEY_BASE=#{ENV['SECRET_KEY_BASE']}",
"MANDRILL_USERNAME=#{ENV['MANDRILL_USERNAME']}",
"MANDRILL_APIKEY=#{ENV['MANDRILL_APIKEY']}",
"RORLA_HOST=#{ENV['RORLA_HOST']}",
"RORLA_LOGENTRIES_TOKEN=#{ENV['RORLA_LOGENTRIES_TOKEN']}"
]
links = ['mysql:mysql']
volumes_from = ['rorla_uploads']
# create migration container
migration_container = Docker::Container.create(
'name' => 'rorla-latest-migration',
'Image' => image_name,
'Env' => env,
'Cmd' => ['bin/rake', 'db:migrate']
)
# run migration container
migration_container.start(
'Links' => links,
'VolumesFrom' => volumes_from
)
migration_container.stop
migration_container.delete
# create new container
new_container = Docker::Container.create(
'name' => 'rorla-latest',
'Image' => image_name,
'Env' => env
)
# run new container
new_container.start(
'Links' => links,
'VolumesFrom' => volumes_from,
'PortBindings' => {
'80/tcp' => [{
'HostIp' => '0.0.0.0',
'HostPort' => '80'
}]
}
)
container_info = new_container.json
json({
'text' => "#{container_info['Name']} container launch. #{container_info['State']}"
})
end
rescue Exception => e
json({
'text' => "에러 발생. #{e}"
})
end
end
end
server = ::Rack::Handler::WEBrick
trap(:INT) do
server.shutdown
end
server.run(MyServer, webrick_options)