compose-hook 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/VERSION +1 -1
- data/config/docker-compose.yml +6 -0
- data/lib/compose_hook/webhook.rb +30 -17
- metadata +2 -2
- data/config/deployments.yml +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6af1586d6748a6aa330311bd40f8b7814c2b75de17b7cd1d8a41ebcab5c5f70
|
4
|
+
data.tar.gz: d090d9aa9581fb85f0aa3d0471ed9fe581afac047fb14aa40276c84b53a59ac3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88738ed3b89d1153c023504b662681197cf7c29f47acba4146216df4cedcd1101bf64b02883d9e5179b89c4d2deeaca8c6341b3bbdf60b59fe40be9b0161932e
|
7
|
+
data.tar.gz: 130429b7385c4e9c8e9bcd9545d366ccbcf07e61e9c2ccccf42745fc18ade73b06bd63e0fdbe89bed435bde69727bd1f0f0451a43ced243db923853bcac2f1fd
|
data/Gemfile.lock
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/compose_hook/webhook.rb
CHANGED
@@ -4,22 +4,29 @@ class ComposeHook::WebHook < Sinatra::Base
|
|
4
4
|
class RequestError < StandardError; end
|
5
5
|
class ServerError < StandardError; end
|
6
6
|
|
7
|
-
attr_accessor :config, :secret, :decoder
|
8
|
-
|
9
7
|
set :show_exceptions, false
|
10
8
|
|
11
9
|
def initialize(app=nil)
|
12
10
|
super
|
13
11
|
|
14
|
-
@secret = ENV["WEBHOOK_JWT_SECRET"]
|
15
12
|
raise "WEBHOOK_JWT_SECRET is not set" if secret.to_s.empty?
|
16
13
|
raise "CONFIG_PATH is not set" if ENV["CONFIG_PATH"].to_s.empty?
|
17
14
|
raise "File #{ENV['CONFIG_PATH']} not found" unless File.exist?(ENV["CONFIG_PATH"])
|
15
|
+
end
|
16
|
+
|
17
|
+
def secret
|
18
|
+
ENV["WEBHOOK_JWT_SECRET"]
|
19
|
+
end
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
+
def decoder
|
22
|
+
ComposeHook::Payload.new(secret: secret)
|
23
|
+
end
|
24
|
+
|
25
|
+
def config
|
26
|
+
config = YAML.load_file(ENV["CONFIG_PATH"])
|
27
|
+
raise "The config file is empty or non-existent" if config.empty?
|
21
28
|
|
22
|
-
|
29
|
+
config
|
23
30
|
end
|
24
31
|
|
25
32
|
def update_service(path, service, image)
|
@@ -30,17 +37,16 @@ class ComposeHook::WebHook < Sinatra::Base
|
|
30
37
|
end
|
31
38
|
|
32
39
|
def find_service(service, path)
|
33
|
-
|
40
|
+
puts "find_service: #{path}"
|
34
41
|
|
35
42
|
Dir[File.join(path, "*.yml")].each do |file|
|
36
43
|
begin
|
37
|
-
|
44
|
+
return file unless YAML.load_file(file)["services"][service].empty?
|
38
45
|
rescue StandardError => e
|
39
|
-
|
46
|
+
warn "Error while parsing deployment files:", e
|
40
47
|
end
|
41
48
|
end
|
42
|
-
|
43
|
-
res
|
49
|
+
raise RequestError.new("service #{service} not found")
|
44
50
|
end
|
45
51
|
|
46
52
|
before do
|
@@ -53,31 +59,35 @@ class ComposeHook::WebHook < Sinatra::Base
|
|
53
59
|
|
54
60
|
get "/deploy/:token" do |token|
|
55
61
|
begin
|
56
|
-
decoded =
|
62
|
+
decoded = decoder.safe_decode(token)
|
57
63
|
return answer(400, "invalid token") unless decoded
|
58
64
|
|
59
65
|
service = decoded["service"]
|
60
66
|
image = decoded["image"]
|
61
67
|
hostname = request.host
|
68
|
+
return answer(500, "configuration must be an array of hash") unless config.is_a?(Array)
|
69
|
+
|
62
70
|
deployment = config.find {|d| d["domain"] == hostname }
|
63
|
-
|
71
|
+
return answer(400, "unknown domain #{hostname}") unless deployment
|
72
|
+
return answer(400, "root missing for #{hostname}") unless deployment["root"]
|
64
73
|
|
74
|
+
service_file = find_service(service, File.join(deployment["root"], deployment["subpath"].to_s))
|
65
75
|
return answer(400, "service is not specified") unless service
|
66
76
|
return answer(400, "image is not specified") unless image
|
67
77
|
return answer(404, "invalid domain") unless deployment
|
68
78
|
return answer(404, "invalid service") unless service_file
|
69
|
-
return answer(400, "invalid image") if (%r(^(([-_\w\.]){,20}(\/|:))+([-\w\.]{,20})$) =~ image).nil?
|
79
|
+
return answer(400, "invalid image format") if (%r(^(([-_\w\.]){,20}(\/|:))+([-\w\.]{,20})$) =~ image).nil?
|
70
80
|
|
71
|
-
system "docker image pull #{image}"
|
81
|
+
Kernel.system "docker image pull #{image}"
|
72
82
|
|
73
83
|
unless $CHILD_STATUS.success?
|
74
|
-
system("docker image inspect #{image} > /dev/null")
|
84
|
+
Kernel.system("docker image inspect #{image} > /dev/null")
|
75
85
|
return answer(404, "invalid image") unless $CHILD_STATUS.success?
|
76
86
|
end
|
77
87
|
|
78
88
|
Dir.chdir(deployment["root"]) do
|
79
89
|
update_service(service_file, service, image)
|
80
|
-
system "docker-compose up -Vd #{service}"
|
90
|
+
Kernel.system "docker-compose up -Vd #{service}"
|
81
91
|
raise ServerError.new("could not recreate the container") unless $CHILD_STATUS.success?
|
82
92
|
end
|
83
93
|
|
@@ -86,6 +96,9 @@ class ComposeHook::WebHook < Sinatra::Base
|
|
86
96
|
return answer(400, e.to_s)
|
87
97
|
rescue ServerError => e
|
88
98
|
return answer(500, e.to_s)
|
99
|
+
rescue StandardError => e
|
100
|
+
warn "Error: #{e}:\n#{e.backtrace.join("\n")}"
|
101
|
+
return answer(500, "Internal server error")
|
89
102
|
end
|
90
103
|
end
|
91
104
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: compose-hook
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danylo P.
|
@@ -246,7 +246,7 @@ files:
|
|
246
246
|
- bin/install_webhook
|
247
247
|
- compose-hook.gemspec
|
248
248
|
- config.ru
|
249
|
-
- config/
|
249
|
+
- config/docker-compose.yml
|
250
250
|
- lib/compose-hook.rb
|
251
251
|
- lib/compose_hook/payload.rb
|
252
252
|
- lib/compose_hook/webhook.rb
|
data/config/deployments.yml
DELETED