ddsl 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative './command/docker/build'
4
4
  require_relative './command/docker/push'
5
+ require_relative './command/docker/pull'
5
6
  require_relative './command/docker/run'
6
7
  require_relative './command/docker/login'
7
8
  require_relative './command/docker_compose/run'
@@ -14,6 +14,8 @@ module DDSL
14
14
  end
15
15
 
16
16
  def run(spec)
17
+ exec_before_block(spec)
18
+
17
19
  argv = [
18
20
  executable,
19
21
  executable_options.call(spec),
@@ -27,6 +29,10 @@ module DDSL
27
29
  exec_after_block(spec)
28
30
  end
29
31
 
32
+ def exec_before_block(spec)
33
+ instance_exec(spec, &before_block) unless before_block.nil?
34
+ end
35
+
30
36
  def exec_after_block(spec)
31
37
  instance_exec(spec, &after_block) unless after_block.nil?
32
38
  end
@@ -28,6 +28,12 @@ module DDSL
28
28
  accept_keys(['context'])
29
29
  end
30
30
 
31
+ before do |spec|
32
+ if spec['pull'] && spec['cache_from'].count.positive?
33
+ spec['cache_from'].each { |t| Pull.new(shell).run('image' => t) }
34
+ end
35
+ end
36
+
31
37
  after do |spec|
32
38
  spec['tags'].each { |t| Push.new(shell).run('image' => t) } if spec['push'] && spec['tags'].count.positive?
33
39
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../base'
4
+
5
+ module DDSL
6
+ module Command
7
+ module Docker
8
+ class Pull < Base
9
+ executable 'docker'
10
+ command 'pull'
11
+
12
+ arguments do
13
+ accept_keys(['image'])
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -39,6 +39,12 @@ module DDSL
39
39
  end
40
40
  end
41
41
 
42
+ def before(&block)
43
+ singleton_class.class_eval do
44
+ @before_block = block
45
+ end
46
+ end
47
+
42
48
  def after(&block)
43
49
  singleton_class.class_eval do
44
50
  @after_block = block
@@ -62,6 +68,10 @@ module DDSL
62
68
  search_ancestor_tree_variable(:@arguments) || DEFAULT_ARGUMENTS_TRANSFORMER
63
69
  end
64
70
 
71
+ def before_block
72
+ search_ancestor_tree_variable(:@before_block)
73
+ end
74
+
65
75
  def after_block
66
76
  search_ancestor_tree_variable(:@after_block)
67
77
  end
@@ -1,176 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Metrics/ModuleLength
4
3
  module DDSL
5
- SCHEMA_VERSION = :draft4
6
- SCHEMA = {
7
- type: :object,
8
- required: [:version],
9
- additionalProperties: false,
10
- properties: {
11
- version: {
12
- type: :integer,
13
- enum: [1]
14
- },
15
- registries: {
16
- type: :array,
17
- items: { '$ref': '#/definitions/registry' },
18
- default: []
19
- },
20
- builds: {
21
- type: :array,
22
- items: { '$ref': '#/definitions/build' }
23
- },
24
- runs: {
25
- type: :array,
26
- items: { '$ref': '#/definitions/run' }
27
- }
28
- },
29
- definitions: {
30
- registry: {
31
- type: :object,
32
- required: %i[url username password],
33
- properties: {
34
- url: { type: :string },
35
- username: { type: :string },
36
- password: { type: :string },
37
- use_cache: { type: :boolean }
38
- }
39
- },
40
- build: {
41
- type: :object,
42
- required: [:name],
43
- oneOf: [
44
- { "$ref": '#/definitions/build_docker_options' },
45
- { "$ref": '#/definitions/build_docker_compose_options' }
46
- ],
47
- properties: {
48
- name: {
49
- type: :string
50
- }
51
- }
52
- },
53
- run: {
54
- type: :object,
55
- oneOf: [
56
- { "$ref": '#/definitions/run_docker_options' },
57
- { "$ref": '#/definitions/run_docker_compose_options' }
58
- ]
59
- },
60
- build_docker_options: {
61
- type: :object,
62
- required: %i[type context file],
63
- properties: {
64
- type: {
65
- type: :string,
66
- enum: [:docker]
67
- },
68
- context: { type: :string },
69
- file: { type: :string },
70
- build_args: {
71
- type: :object,
72
- additionalProperties: {
73
- type: :string
74
- }
75
- },
76
- tags: {
77
- type: :array,
78
- items: { type: :string }
79
- },
80
- labels: {
81
- type: :array,
82
- items: { type: :string }
83
- },
84
- cache_from: {
85
- type: :array,
86
- items: { type: :string }
87
- },
88
- push: { type: :boolean }
89
- }
90
- },
91
- build_docker_compose_options: {
92
- type: :object,
93
- required: %i[type],
94
- properties: {
95
- type: {
96
- type: :string,
97
- enum: [:'docker-compose']
98
- },
99
- file: { type: :string },
100
- service: { type: :string },
101
- no_cache: { type: :boolean },
102
- pull: { type: :boolean },
103
- force_rm: { type: :boolean },
104
- compress: { type: :boolean },
105
- parallel: { type: :boolean },
106
- memory: { type: :string },
107
- build_args: {
108
- type: :object,
109
- additionalProperties: {
110
- type: :string
111
- }
112
- }
113
- }
114
- },
115
- run_docker_options: {
116
- type: :object,
117
- required: %i[type image],
118
- properties: {
119
- type: {
120
- type: :string,
121
- enum: [:docker]
122
- },
123
- image: { type: :string },
124
- cmd: { type: :string },
125
- user: { type: :string },
126
- workdir: { type: :string },
127
- rm: { type: :boolean },
128
- env: {
129
- type: :object,
130
- additionalProperties: {
131
- type: :string
132
- }
133
- },
134
- volumes: {
135
- type: :object,
136
- additionalProperties: {
137
- type: :string
138
- }
139
- }
140
- }
141
- },
142
- run_docker_compose_options: {
143
- type: :object,
144
- required: %i[type service],
145
- properties: {
146
- type: {
147
- type: :string,
148
- enum: [:'docker-compose']
149
- },
150
- service: { type: :string },
151
- file: { type: :string },
152
- cmd: { type: :string },
153
- rm: { type: :boolean },
154
- user: { type: :string },
155
- workdir: { type: :string },
156
- service_ports: { type: :string },
157
- detach: { type: :boolean },
158
- no_deps: { type: :boolean },
159
- env: {
160
- type: :object,
161
- additionalProperties: {
162
- type: :string
163
- }
164
- },
165
- volumes: {
166
- type: :object,
167
- additionalProperties: {
168
- type: :string
169
- }
170
- }
171
- }
172
- }
173
- }
174
- }.freeze
4
+ SCHEMA_VERSION = :draft4
5
+ SCHEMA_DIR = File.join(File.dirname(__FILE__), '../../schemas')
6
+ SCHEMA_PATH = Pathname.new(File.join(SCHEMA_DIR, '/ddsl.schema.json'))
7
+ SCHEMA_RESOLVER = lambda { |uri|
8
+ file_name = uri.path.split('/').last
9
+ file_path = File.join(SCHEMA_DIR, file_name)
10
+
11
+ JSON.parse(File.read(file_path))
12
+ }
175
13
  end
176
- # rubocop:enable Metrics/ModuleLength
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json-schema'
3
+ require 'json_schemer'
4
4
  require_relative './schema'
5
5
 
6
6
  module DDSL
@@ -17,13 +17,18 @@ module DDSL
17
17
  # @return [Hash] data with defaults if appropiate
18
18
  #
19
19
  def validate!(data)
20
- errors = JSON::Validator.fully_validate(DDSL::SCHEMA, data,
21
- version: DDSL::SCHEMA_VERSION,
22
- insert_defaults: true)
23
-
24
- raise InvalidError, errors.join('\n') if errors.count.positive?
20
+ errors = json_schema_validator.validate(data).to_a
21
+ raise InvalidError, 'Invalid schema' if errors&.count&.positive?
25
22
 
26
23
  data
27
24
  end
25
+
26
+ private def json_schema_validator
27
+ @json_schema_validator ||= JSONSchemer.schema(
28
+ DDSL::SCHEMA_PATH,
29
+ format: true,
30
+ ref_resolver: DDSL::SCHEMA_RESOLVER
31
+ )
32
+ end
28
33
  end
29
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DDSL
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
@@ -0,0 +1,141 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-06/schema#",
3
+ "$id": "https://raw.githubusercontent.com/bilby91/ddsl/master/docs/build.schema.json",
4
+ "title": "Build",
5
+ "description": "Build task to perform using either docker or docker-compose",
6
+ "type": "object",
7
+ "required": [
8
+ "name"
9
+ ],
10
+ "oneOf": [
11
+ {
12
+ "$ref": "#/definitions/build_docker_options"
13
+ },
14
+ {
15
+ "$ref": "#/definitions/build_docker_compose_options"
16
+ }
17
+ ],
18
+ "properties": {
19
+ "name": {
20
+ "type": "string",
21
+ "description": "Unique name to identify the build task."
22
+ }
23
+ },
24
+ "definitions": {
25
+ "build_docker_options": {
26
+ "type": "object",
27
+ "required": [
28
+ "type",
29
+ "context",
30
+ "file"
31
+ ],
32
+ "properties": {
33
+ "type": {
34
+ "type": "string",
35
+ "enum": [
36
+ "docker"
37
+ ],
38
+ "description": "Type of builder to use"
39
+ },
40
+ "context": {
41
+ "type": "string",
42
+ "description": "Path for the `docker build` context"
43
+ },
44
+ "file": {
45
+ "type": "string",
46
+ "description": "Path to the Dockerfile to build"
47
+ },
48
+ "build_args": {
49
+ "type": "object",
50
+ "additionalProperties": {
51
+ "type": "string"
52
+ },
53
+ "description": "Set build-time variables"
54
+ },
55
+ "tags": {
56
+ "type": "array",
57
+ "items": {
58
+ "type": "string"
59
+ },
60
+ "description": "Name and optionally a tag in the ‘name:tag’ format"
61
+ },
62
+ "labels": {
63
+ "type": "array",
64
+ "items": {
65
+ "type": "string"
66
+ },
67
+ "description": "Set metadata for an image"
68
+ },
69
+ "cache_from": {
70
+ "type": "array",
71
+ "items": {
72
+ "type": "string"
73
+ },
74
+ "description": "Images to consider as cache sources"
75
+ },
76
+ "push": {
77
+ "type": "boolean",
78
+ "description": "If true, push any configured tag to the appropiate registry"
79
+ },
80
+ "pull": {
81
+ "type": "boolean",
82
+ "description": "If true, all images specified in `cache_from` will be pulled before building"
83
+ }
84
+ }
85
+ },
86
+ "build_docker_compose_options": {
87
+ "type": "object",
88
+ "required": [
89
+ "type"
90
+ ],
91
+ "properties": {
92
+ "type": {
93
+ "type": "string",
94
+ "enum": [
95
+ "docker-compose"
96
+ ],
97
+ "description": "Type of builder to use"
98
+ },
99
+ "file": {
100
+ "type": "string",
101
+ "description": "Path to the docker-compose file"
102
+ },
103
+ "service": {
104
+ "type": "string",
105
+ "description": "Name of the docker-compose service to build"
106
+ },
107
+ "no_cache": {
108
+ "type": "boolean",
109
+ "description": "Do not use cache when building the image"
110
+ },
111
+ "pull": {
112
+ "type": "boolean",
113
+ "description": "Always attempt to pull a newer version of the image"
114
+ },
115
+ "force_rm": {
116
+ "type": "boolean",
117
+ "description": "Always remove intermediate containers"
118
+ },
119
+ "compress": {
120
+ "type": "boolean",
121
+ "description": "Compress the build context using gzip"
122
+ },
123
+ "parallel": {
124
+ "type": "boolean",
125
+ "description": "Build images in parallel"
126
+ },
127
+ "memory": {
128
+ "type": "string",
129
+ "description": "Sets memory limit for the build container."
130
+ },
131
+ "build_args": {
132
+ "type": "object",
133
+ "additionalProperties": {
134
+ "type": "string"
135
+ },
136
+ "description": "Set build-time variables for services"
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }