From c9443dabe915db8f751a77efe386138398ac39f6 Mon Sep 17 00:00:00 2001 From: Rupak Ganguly Date: Tue, 30 Sep 2014 13:21:53 -0400 Subject: [PATCH] Add volumes_from support to templates, template builder, serializer, and converters. --- app/builders/template_builder/from_fig.rb | 7 +++++ app/models/converters/template_converter.rb | 15 +++++++++ app/serializers/template_file_serializer.rb | 1 + .../template_builder/from_fig_spec.rb | 2 ++ .../converters/template_converter_spec.rb | 31 +++++++++++++++++++ .../template_file_serializer_spec.rb | 9 +++++- spec/support/fixtures/fig.yml | 2 ++ spec/support/fixtures/wordpress.pmx | 9 ++++-- 8 files changed, 73 insertions(+), 3 deletions(-) diff --git a/app/builders/template_builder/from_fig.rb b/app/builders/template_builder/from_fig.rb index 08483b4..d51e554 100644 --- a/app/builders/template_builder/from_fig.rb +++ b/app/builders/template_builder/from_fig.rb @@ -30,6 +30,7 @@ def image_from_fig_service(name, service_def) image.expose = service_def['expose'] image.volumes = volumes(service_def['volumes']) image.environment = service_def['environment'] + image.volumes_from = shared_volumes(service_def['volumes_from']) end end @@ -54,5 +55,11 @@ def volumes(volumes_array) end end + def shared_volumes(vol_from_array) + Array(vol_from_array).map do |vol_from| + { 'service' => vol_from } + end + end + end end diff --git a/app/models/converters/template_converter.rb b/app/models/converters/template_converter.rb index d986778..ab13755 100644 --- a/app/models/converters/template_converter.rb +++ b/app/models/converters/template_converter.rb @@ -20,6 +20,13 @@ def to_app linked_from_service.links = service_links_from_image(image) end + # Set-up shared volumes for services + template.images.each do |image| + next unless image.volumes_from? + the_service = find_service(image.name) + the_service.volumes_from = shared_volumes_from_image(image) + end + App.new( name: template.name, from: "Template: #{template.name}", @@ -62,6 +69,14 @@ def service_links_from_image(image) end end + def shared_volumes_from_image(image) + image.volumes_from.map do |vol_from| + SharedVolume.new( + exported_from_service: find_service(vol_from['service']) + ) + end + end + def find_service(name) services.find { |service| service.name == name } end diff --git a/app/serializers/template_file_serializer.rb b/app/serializers/template_file_serializer.rb index 47c6c57..ce2d1b9 100644 --- a/app/serializers/template_file_serializer.rb +++ b/app/serializers/template_file_serializer.rb @@ -11,6 +11,7 @@ class TemplateFileImageSerializer < ActiveModel::Serializer :links, :environment, :volumes, + :volumes_from, :command def category diff --git a/spec/builders/template_builder/from_fig_spec.rb b/spec/builders/template_builder/from_fig_spec.rb index 2233181..5fc8a1d 100644 --- a/spec/builders/template_builder/from_fig_spec.rb +++ b/spec/builders/template_builder/from_fig_spec.rb @@ -54,6 +54,8 @@ expect(subject.images.find_by(name: 'db').volumes).to be_blank expect(subject.images.find_by(name: 'web').expose).to be_blank expect(subject.images.find_by(name: 'db').expose).to eq(['3306']) + expect(subject.images.find_by(name: 'web').volumes_from).to be_blank + expect(subject.images.find_by(name: 'db').volumes_from).to eq([{ 'service' => 'web' }]) end end diff --git a/spec/models/converters/template_converter_spec.rb b/spec/models/converters/template_converter_spec.rb index ea1fd2e..972d6ea 100644 --- a/spec/models/converters/template_converter_spec.rb +++ b/spec/models/converters/template_converter_spec.rb @@ -27,6 +27,7 @@ environment: { var: 'val' }, command: '/boom shaka', volumes: ['volumes'], + volumes_from: ['service1'], type: 'mysql' ) end @@ -121,5 +122,35 @@ end end + context 'with shared volumes' do + + let(:image1) { Image.new(name: 'I1', source: 'I1:latest') } + let(:image2) { Image.new(name: 'I2', source: 'I2:latest') } + + before do + image2.volumes_from = [{ 'service' => 'I1' }] + template.images = [image1, image2] + end + + it 'creates a service for both images' do + app = subject.to_app + expect(app.services.size).to eq 2 + end + + it 'creates shared volumes where appropriate' do + app = subject.to_app + s1, s2 = app.services + expect(s1.volumes_from.size).to eq 0 + expect(s2.volumes_from.size).to eq 1 + end + + it 'shared volumes linked to the services correctly' do + app = subject.to_app + s1, s2 = app.services + expect(s2.volumes_from.first.exported_from_service_id).to eq s2.id + expect(s2.volumes_from.first.mounted_on_service_id).to eq s1.id + end + end + end end diff --git a/spec/serializers/template_file_serializer_spec.rb b/spec/serializers/template_file_serializer_spec.rb index d465219..785828f 100644 --- a/spec/serializers/template_file_serializer_spec.rb +++ b/spec/serializers/template_file_serializer_spec.rb @@ -33,7 +33,9 @@ type: 'ghi', categories: ['jkl'], expose: [8000], - environment: [{ 'variable' => 'mno', 'value' => 'pqr' }] + environment: [{ 'variable' => 'mno', 'value' => 'pqr' }], + volumes: [{ 'host_path' => '/tmp/foo', 'container_path' => '/tmp/bar' }], + volumes_from: [{ 'service' => 'baz' }] ) ] ) @@ -63,6 +65,11 @@ environment: - variable: mno value: pqr + volumes: + - host_path: "/tmp/foo" + container_path: "/tmp/bar" + volumes_from: + - service: baz EXPECTED end diff --git a/spec/support/fixtures/fig.yml b/spec/support/fixtures/fig.yml index 191ec4b..d0c1675 100644 --- a/spec/support/fixtures/fig.yml +++ b/spec/support/fixtures/fig.yml @@ -18,3 +18,5 @@ db: environment: - variable: MYSQL_ROOT_PASSWORD value: mysecretpassword + volumes_from: + - web diff --git a/spec/support/fixtures/wordpress.pmx b/spec/support/fixtures/wordpress.pmx index 8385fe2..0e1d2db 100644 --- a/spec/support/fixtures/wordpress.pmx +++ b/spec/support/fixtures/wordpress.pmx @@ -24,7 +24,12 @@ } ], "category": "DB Tier", - "type": "mysql" + "type": "mysql", + "volumes_from": [ + { + "service": "WP" + } + ], }, { "name": "WP", @@ -50,7 +55,7 @@ } ], "volumes": [ - { + { "host_path": "cache/", "container_path": "/tmp/cache" }