From 8c6e2bada222745c9994da42f21bad2ab43b9351 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Sat, 14 Jan 2017 14:39:53 -0500 Subject: Add support for docker image configuration in .gitlab-ci.yml --- lib/gitlab/ci/config/entry/docker_image.rb | 31 ++++++++++++++++++++++++++ lib/gitlab/ci/config/entry/image.rb | 9 +++++++- lib/gitlab/ci/config/entry/service.rb | 35 ++++++++++++++++++++++++++++++ lib/gitlab/ci/config/entry/services.rb | 25 ++++++++++++++++++++- lib/gitlab/ci/config/entry/validators.rb | 8 +++++++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 lib/gitlab/ci/config/entry/docker_image.rb create mode 100644 lib/gitlab/ci/config/entry/service.rb (limited to 'lib') diff --git a/lib/gitlab/ci/config/entry/docker_image.rb b/lib/gitlab/ci/config/entry/docker_image.rb new file mode 100644 index 00000000000..1170e129bc0 --- /dev/null +++ b/lib/gitlab/ci/config/entry/docker_image.rb @@ -0,0 +1,31 @@ +module Gitlab + module Ci + class Config + module Entry + module DockerImage + def hash? + @config.is_a?(Hash) + end + + def string? + @config.is_a?(String) + end + + def name + value[:name] + end + + def entrypoint + value[:entrypoint] + end + + def value + return { name: @config } if string? + return @config if hash? + {} + end + end + end + end + end +end diff --git a/lib/gitlab/ci/config/entry/image.rb b/lib/gitlab/ci/config/entry/image.rb index b5050257688..10f35a2dbc1 100644 --- a/lib/gitlab/ci/config/entry/image.rb +++ b/lib/gitlab/ci/config/entry/image.rb @@ -7,9 +7,16 @@ module Gitlab # class Image < Node include Validatable + include DockerImage + + ALLOWED_KEYS = %i[name entrypoint].freeze validations do - validates :config, type: String + validates :config, hash_or_string: true + validates :config, allowed_keys: ALLOWED_KEYS + + validates :name, type: String, presence: true + validates :entrypoint, type: String, allow_nil: true end end end diff --git a/lib/gitlab/ci/config/entry/service.rb b/lib/gitlab/ci/config/entry/service.rb new file mode 100644 index 00000000000..fd0aec23803 --- /dev/null +++ b/lib/gitlab/ci/config/entry/service.rb @@ -0,0 +1,35 @@ +module Gitlab + module Ci + class Config + module Entry + ## + # Entry that represents a configuration of Docker service. + # + class Service < Node + include Validatable + include DockerImage + + ALLOWED_KEYS = %i[name entrypoint command alias].freeze + + validations do + validates :config, hash_or_string: true + validates :config, allowed_keys: ALLOWED_KEYS + + validates :name, type: String, presence: true + validates :entrypoint, type: String, allow_nil: true + validates :command, type: String, allow_nil: true + validates :alias, type: String, allow_nil: true + end + + def alias + value[:alias] + end + + def command + value[:command] + end + end + end + end + end +end diff --git a/lib/gitlab/ci/config/entry/services.rb b/lib/gitlab/ci/config/entry/services.rb index 84f8ab780f5..0066894e069 100644 --- a/lib/gitlab/ci/config/entry/services.rb +++ b/lib/gitlab/ci/config/entry/services.rb @@ -9,7 +9,30 @@ module Gitlab include Validatable validations do - validates :config, array_of_strings: true + validates :config, type: Array + end + + def compose!(deps = nil) + super do + @entries = [] + @config.each do |config| + @entries << Entry::Factory.new(Entry::Service) + .value(config || {}) + .create! + end + + @entries.each do |entry| + entry.compose!(deps) + end + end + end + + def value + @entries.map(&:value) + end + + def descendants + @entries end end end diff --git a/lib/gitlab/ci/config/entry/validators.rb b/lib/gitlab/ci/config/entry/validators.rb index bd7428b1272..b2ca3c881e4 100644 --- a/lib/gitlab/ci/config/entry/validators.rb +++ b/lib/gitlab/ci/config/entry/validators.rb @@ -44,6 +44,14 @@ module Gitlab end end + class HashOrStringValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + unless value.is_a?(Hash) || value.is_a?(String) + record.errors.add(attribute, 'should be a hash or a string') + end + end + end + class KeyValidator < ActiveModel::EachValidator include LegacyValidationHelpers -- cgit v1.2.1 From c70e9f2ed107ac3d0189a803eae6e7e7917f6224 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Mon, 5 Jun 2017 16:15:09 +0200 Subject: Send new configuration options with job's payload --- lib/api/entities.rb | 8 ++++++-- lib/gitlab/ci/build/image.rb | 11 +++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index a836df3dc81..8d3d34b844d 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -804,7 +804,11 @@ module API end class Image < Grape::Entity - expose :name + expose :name, :entrypoint + end + + class ServiceImage < Image + expose :alias, :command end class Artifacts < Grape::Entity @@ -848,7 +852,7 @@ module API expose :variables expose :steps, using: Step expose :image, using: Image - expose :services, using: Image + expose :services, using: ServiceImage expose :artifacts, using: Artifacts expose :cache, using: Cache expose :credentials, using: Credentials diff --git a/lib/gitlab/ci/build/image.rb b/lib/gitlab/ci/build/image.rb index c62aeb60fa9..b88b2e36d53 100644 --- a/lib/gitlab/ci/build/image.rb +++ b/lib/gitlab/ci/build/image.rb @@ -2,7 +2,7 @@ module Gitlab module Ci module Build class Image - attr_reader :name + attr_reader :alias, :command, :entrypoint, :name class << self def from_image(job) @@ -21,7 +21,14 @@ module Gitlab end def initialize(image) - @name = image + if image.is_a?(String) + @name = image + elsif image.is_a?(Hash) + @alias = image[:alias] + @command = image[:command] + @entrypoint = image[:entrypoint] + @name = image[:name] + end end def valid? -- cgit v1.2.1 From b7d6bb9763ce0c682fae787c452103102624da26 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Mon, 5 Jun 2017 16:39:45 +0200 Subject: Ensure that old API v1 generates still the same output --- lib/ci/api/entities.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/ci/api/entities.rb b/lib/ci/api/entities.rb index 792ff628b09..6733d1a33aa 100644 --- a/lib/ci/api/entities.rb +++ b/lib/ci/api/entities.rb @@ -45,7 +45,25 @@ module Ci expose :artifacts_expire_at, if: ->(build, _) { build.artifacts? } expose :options do |model| - model.options + options = model.options + + # This part ensures that output of old API is still the same after adding support + # for extended docker configuration options, used by new API + # + # I'm leaving this here, not in the model, because it should be removed at the same time + # when old API will be removed (planned for August 2017). + options[:image] = options[:image][:name] if options[:image].present? && options[:image].is_a?(Hash) + if options[:services].present? + options[:services].map! do |service| + if service.is_a?(Hash) + service[:name] + else + service + end + end + end + + options end expose :timeout do |model| -- cgit v1.2.1 From fa0a326bf2678b9386a79dfd5281dee1e5ee8af8 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Mon, 12 Jun 2017 12:48:15 +0200 Subject: Rename API::Entities::JobRequest::ServiceImage to API::Entities::JobRequest::Service --- lib/api/entities.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 8d3d34b844d..af55c372eea 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -807,7 +807,7 @@ module API expose :name, :entrypoint end - class ServiceImage < Image + class Service < Image expose :alias, :command end @@ -852,7 +852,7 @@ module API expose :variables expose :steps, using: Step expose :image, using: Image - expose :services, using: ServiceImage + expose :services, using: Service expose :artifacts, using: Artifacts expose :cache, using: Cache expose :credentials, using: Credentials -- cgit v1.2.1 From 7805eea3c705669f9a46bfabf25d550cd7422745 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Mon, 12 Jun 2017 12:56:14 +0200 Subject: Refactor old formad handling in lib/ci/api/entities.rb --- lib/ci/api/entities.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/ci/api/entities.rb b/lib/ci/api/entities.rb index 6733d1a33aa..6b82b2b4f13 100644 --- a/lib/ci/api/entities.rb +++ b/lib/ci/api/entities.rb @@ -45,15 +45,13 @@ module Ci expose :artifacts_expire_at, if: ->(build, _) { build.artifacts? } expose :options do |model| - options = model.options - # This part ensures that output of old API is still the same after adding support # for extended docker configuration options, used by new API # # I'm leaving this here, not in the model, because it should be removed at the same time # when old API will be removed (planned for August 2017). - options[:image] = options[:image][:name] if options[:image].present? && options[:image].is_a?(Hash) - if options[:services].present? + model.options.dup.tap do |options| + options[:image] = options[:image][:name] if options[:image].is_a?(Hash) options[:services].map! do |service| if service.is_a?(Hash) service[:name] @@ -62,8 +60,6 @@ module Ci end end end - - options end expose :timeout do |model| -- cgit v1.2.1 From 645c8651410961e61cf220e859f2faddaf66b5e7 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Mon, 12 Jun 2017 13:35:32 +0200 Subject: Remove Gitlab::Ci::Config::Entry::DockerImage --- lib/gitlab/ci/config/entry/docker_image.rb | 31 ------------------------------ lib/gitlab/ci/config/entry/image.rb | 23 +++++++++++++++++++++- lib/gitlab/ci/config/entry/service.rb | 3 +-- 3 files changed, 23 insertions(+), 34 deletions(-) delete mode 100644 lib/gitlab/ci/config/entry/docker_image.rb (limited to 'lib') diff --git a/lib/gitlab/ci/config/entry/docker_image.rb b/lib/gitlab/ci/config/entry/docker_image.rb deleted file mode 100644 index 1170e129bc0..00000000000 --- a/lib/gitlab/ci/config/entry/docker_image.rb +++ /dev/null @@ -1,31 +0,0 @@ -module Gitlab - module Ci - class Config - module Entry - module DockerImage - def hash? - @config.is_a?(Hash) - end - - def string? - @config.is_a?(String) - end - - def name - value[:name] - end - - def entrypoint - value[:entrypoint] - end - - def value - return { name: @config } if string? - return @config if hash? - {} - end - end - end - end - end -end diff --git a/lib/gitlab/ci/config/entry/image.rb b/lib/gitlab/ci/config/entry/image.rb index 10f35a2dbc1..897dcff8012 100644 --- a/lib/gitlab/ci/config/entry/image.rb +++ b/lib/gitlab/ci/config/entry/image.rb @@ -7,7 +7,6 @@ module Gitlab # class Image < Node include Validatable - include DockerImage ALLOWED_KEYS = %i[name entrypoint].freeze @@ -18,6 +17,28 @@ module Gitlab validates :name, type: String, presence: true validates :entrypoint, type: String, allow_nil: true end + + def hash? + @config.is_a?(Hash) + end + + def string? + @config.is_a?(String) + end + + def name + value[:name] + end + + def entrypoint + value[:entrypoint] + end + + def value + return { name: @config } if string? + return @config if hash? + {} + end end end end diff --git a/lib/gitlab/ci/config/entry/service.rb b/lib/gitlab/ci/config/entry/service.rb index fd0aec23803..b52faf48b58 100644 --- a/lib/gitlab/ci/config/entry/service.rb +++ b/lib/gitlab/ci/config/entry/service.rb @@ -5,9 +5,8 @@ module Gitlab ## # Entry that represents a configuration of Docker service. # - class Service < Node + class Service < Image include Validatable - include DockerImage ALLOWED_KEYS = %i[name entrypoint command alias].freeze -- cgit v1.2.1