Module: UffizziCore::DockerHubService

Defined in:
app/services/uffizzi_core/docker_hub_service.rb

Constant Summary collapse

HOOK_NAME =
'Uffizzi OpenSource Deploy Webhook'
HOOK_URL =
"#{Settings.app.host}api/cli/v1/webhooks/docker_hub"
REGISTRY =
'registry-1.docker.io'

Class Method Summary collapse

Class Method Details

.accounts(credential) ⇒ Object



42
43
44
45
46
47
48
49
# File 'app/services/uffizzi_core/docker_hub_service.rb', line 42

def accounts(credential)
  client = user_client(credential)
  response = client.accounts
  Rails.logger.info("DockerHubService accounts response=#{response.inspect} credential_id=#{credential.id}")

  accounts_response = response.result
  accounts_response.nil? ? [] : accounts_response.namespaces
end

.create_webhook(credential, image) ⇒ Object



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
# File 'app/services/uffizzi_core/docker_hub_service.rb', line 9

def create_webhook(credential, image)
  client = user_client(credential)

  webhooks_response = client.get_webhooks(slug: image, registry: REGISTRY)
  Rails.logger.info("DockerHubService create_webhook get_webhooks_response=#{webhooks_response.inspect}")

  return false if webhooks_response.status != 200

  webhooks = webhooks_response.result['results']

  webhook = webhooks.detect { |hook| hook['name'] == HOOK_NAME }

  return true if !webhook.nil?

  params = {
    slug: image,
    name: HOOK_NAME,
    expect_final_callback: false,
    webhooks: [{ name: HOOK_NAME, hook_url: HOOK_URL, registry: REGISTRY }],
  }

  response = client.create_webhook(params)

  Rails.logger.info("DockerHubService create_webhook create_webhook_response=#{response.inspect} params=#{params.inspect}")

  response.status == 201
end

.digest(credential, image, tag) ⇒ Object



64
65
66
67
68
69
# File 'app/services/uffizzi_core/docker_hub_service.rb', line 64

def digest(credential, image, tag)
  docker_hub_client = UffizziCore::DockerHubClient.new(credential)
  token = docker_hub_client.get_token(image).result.token
  response = docker_hub_client.digest(image: image, tag: tag, token: token)
  response.headers['docker-content-digest']
end

.send_webhook_answer(callback_url) ⇒ Object



37
38
39
40
# File 'app/services/uffizzi_core/docker_hub_service.rb', line 37

def send_webhook_answer(callback_url)
  params = { state: 'success', description: 'Successfully deployed to Uffizzi' }
  public_docker_hub_client.send_webhook_answer(callback_url, params)
end

.user_client(credential) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/uffizzi_core/docker_hub_service.rb', line 51

def user_client(credential)
  if @client.nil?
    @client = UffizziCore::DockerHubClient.new(credential)

    unless @client.authentificated?
      Rails.logger.warn("broken credentials, DockerHubService credential_id=#{credential.id}")
      credential.unauthorize! unless credential.unauthorized?
    end
  end

  @client
end