UNPKG

3.62 kBtext/coffeescriptView Raw
1url = require 'url'
2dockerHubApi = require '@octoblu/docker-hub-api'
3colors = require 'colors'
4debug = require('debug')('deploy-state-util:docker-hub-service')
5
6class DockerHubService
7 constructor: ({ config, dockerHubToken }) ->
8 throw new Error 'Missing config argument' unless config?
9 throw new Error 'Missing dockerHubToken argument' unless dockerHubToken?
10 dockerHubApi.setLoginToken(dockerHubToken)
11 @webhookUrl = url.format {
12 hostname: config['deploy-state'].hostname,
13 protocol: 'https',
14 slashes: true,
15 pathname: '/deployments/docker-hub'
16 auth: "#{config['deploy-state'].username}:#{config['deploy-state'].password}"
17 }
18 debug 'webhookUrl', @webhookUrl
19
20 configure: ({ @repo, @owner, @isPrivate }, callback) =>
21 debug 'setting up docker', { @repo, @owner, @isPrivate }
22 @_ensureRepository (error) =>
23 return callback error if error?
24 @_ensureWebhook callback
25
26 _ensureRepository: (callback) =>
27 @_repositoryExists (error, exists) =>
28 return callback error if error?
29 return callback null if exists
30 @_createRepository callback
31
32 _ensureWebhook: (callback) =>
33 @_createWebhook (error, webhookId) =>
34 return callback error if error?
35 return callback null unless webhookId?
36 @_createWebhookHook webhookId, callback
37
38 _createRepository: (callback) =>
39 debug 'repository does not exist, but I will make it exist'
40 details = {
41 active: true,
42 description: "docker registry for #{@owner}/#{@repo}"
43 build_tags: [
44 {
45 name: '{sourceref}',
46 source_name: '/v.*/',
47 source_type: 'Tag',
48 dockerfile_location: "/",
49 }
50 ],
51 is_private: @isPrivate,
52 provider: 'github',
53 vcs_repo_name: "#{@owner}/#{@repo}",
54 }
55 debug 'create respository build details', details
56 console.log colors.magenta('NOTICE'), colors.white('creating the repository hub.docker.com')
57 dockerHubApi.createAutomatedBuild @owner, @repo, details
58 .then (build) =>
59 debug 'created automated build', build
60 callback null
61 .catch (error) =>
62 debug 'create automated build failed', error
63 callback error
64
65 _repositoryExists: (callback) =>
66 debug 'checking if repository exists'
67 dockerHubApi.repository(@owner, @repo)
68 .then (repository) =>
69 debug 'got respository', repository
70 callback null, repository?
71 .catch (error) =>
72 return callback null, false if error.message == 'Object not found'
73 debug 'get registory error', error
74 callback error
75
76 _createWebhook: (callback) =>
77 debug 'creating webhook'
78 dockerHubApi.createWebhook @owner, @repo, 'Deploy State'
79 .then (webhook) =>
80 debug 'create webhook response', webhook
81 if webhook?.name[0].indexOf('already exists') > -1
82 debug 'webhook already exists'
83 return callback null
84 callback null, webhook.id
85 .catch (error) =>
86 debug 'create webhook failed', error
87 return callback null, false if error.message == 'Object not found'
88 callback error
89
90 _createWebhookHook: (webhookId, callback) =>
91 debug 'creating webhook hook'
92 console.log colors.magenta('NOTICE'), colors.white('creating the webhook docker build')
93 dockerHubApi.createWebhookHook @owner, @repo, webhookId, @webhookUrl
94 .then (hook) =>
95 debug 'created webhook hook', hook
96 callback null, hook
97 .catch (error) =>
98 debug 'create webhook hook failed', error
99 callback error
100
101module.exports = DockerHubService