Class: ComposeHook::WebHook

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/compose_hook/webhook.rb

Defined Under Namespace

Classes: RequestError, ServerError

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ WebHook

Returns a new instance of WebHook.



9
10
11
12
13
14
15
# File 'lib/compose_hook/webhook.rb', line 9

def initialize(app=nil)
  super

  raise "WEBHOOK_JWT_SECRET is not set" if secret.to_s.empty?
  raise "CONFIG_PATH is not set" if ENV["CONFIG_PATH"].to_s.empty?
  raise "File #{ENV['CONFIG_PATH']} not found" unless File.exist?(ENV["CONFIG_PATH"])
end

Instance Method Details

#answer(response_status, message) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/compose_hook/webhook.rb', line 105

def answer(response_status, message)
  status response_status

  {
    message: message
  }.to_json
end

#configObject



25
26
27
28
29
30
# File 'lib/compose_hook/webhook.rb', line 25

def config
  config = YAML.load_file(ENV["CONFIG_PATH"])
  raise "The config file is empty or non-existent" if config.empty?

  config
end

#decoderObject



21
22
23
# File 'lib/compose_hook/webhook.rb', line 21

def decoder
  ComposeHook::Payload.new(secret: secret)
end

#find_service(service, path) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/compose_hook/webhook.rb', line 39

def find_service(service, path)
  puts "find_service: #{path}"

  Dir[File.join(path, "*.{yml,yaml}")].each do |file|
    begin
      return file unless YAML.load_file(file)["services"][service].empty?
    rescue StandardError => e
      warn "Error while parsing deployment files:", e
    end
  end
  raise RequestError.new("service #{service} not found")
end

#secretObject



17
18
19
# File 'lib/compose_hook/webhook.rb', line 17

def secret
  ENV["WEBHOOK_JWT_SECRET"]
end

#update_service(path, service, image) ⇒ Object



32
33
34
35
36
37
# File 'lib/compose_hook/webhook.rb', line 32

def update_service(path, service, image)
  file = YAML.load_file(path)

  file["services"][service]["image"] = image
  File.write(path, file.to_yaml)
end