Skip to content

Commit

Permalink
Add volumes_from support to templates, template builder, serializer, …
Browse files Browse the repository at this point in the history
…and converters.
  • Loading branch information
rupakg committed Oct 2, 2014
1 parent efcdae5 commit c9443da
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 3 deletions.
7 changes: 7 additions & 0 deletions app/builders/template_builder/from_fig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
15 changes: 15 additions & 0 deletions app/models/converters/template_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/serializers/template_file_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TemplateFileImageSerializer < ActiveModel::Serializer
:links,
:environment,
:volumes,
:volumes_from,
:command

def category
Expand Down
2 changes: 2 additions & 0 deletions spec/builders/template_builder/from_fig_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 31 additions & 0 deletions spec/models/converters/template_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
environment: { var: 'val' },
command: '/boom shaka',
volumes: ['volumes'],
volumes_from: ['service1'],
type: 'mysql'
)
end
Expand Down Expand Up @@ -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
9 changes: 8 additions & 1 deletion spec/serializers/template_file_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }]
)
]
)
Expand Down Expand Up @@ -63,6 +65,11 @@
environment:
- variable: mno
value: pqr
volumes:
- host_path: "/tmp/foo"
container_path: "/tmp/bar"
volumes_from:
- service: baz
EXPECTED
end

Expand Down
2 changes: 2 additions & 0 deletions spec/support/fixtures/fig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ db:
environment:
- variable: MYSQL_ROOT_PASSWORD
value: mysecretpassword
volumes_from:
- web
9 changes: 7 additions & 2 deletions spec/support/fixtures/wordpress.pmx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
}
],
"category": "DB Tier",
"type": "mysql"
"type": "mysql",
"volumes_from": [
{
"service": "WP"
}
],
},
{
"name": "WP",
Expand All @@ -50,7 +55,7 @@
}
],
"volumes": [
{
{
"host_path": "cache/",
"container_path": "/tmp/cache"
}
Expand Down

0 comments on commit c9443da

Please sign in to comment.