Skip to content

Commit

Permalink
Add a no-build option to fig up, to save time when services were alre…
Browse files Browse the repository at this point in the history
…ady freshly built.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
  • Loading branch information
dnephin committed Dec 9, 2014
1 parent b0fd594 commit 367c3fd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
2 changes: 2 additions & 0 deletions fig/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ def up(self, project, options):
--no-color Produce monochrome output.
--no-deps Don't start linked services.
--no-recreate If containers already exist, don't recreate them.
--no-build Don't build an image if it's missing
"""
insecure_registry = options['--allow-insecure-ssl']
detached = options['-d']
Expand All @@ -432,6 +433,7 @@ def up(self, project, options):
start_links=start_links,
recreate=recreate,
insecure_registry=insecure_registry,
do_build=not options['--no-build'],
)

to_attach = [c for s in project.get_services(service_names) for c in s.containers()]
Expand Down
15 changes: 12 additions & 3 deletions fig/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,23 @@ def build(self, service_names=None, no_cache=False):
else:
log.info('%s uses an image, skipping' % service.name)

def up(self, service_names=None, start_links=True, recreate=True, insecure_registry=False):
def up(self,
service_names=None,
start_links=True,
recreate=True,
insecure_registry=False,
do_build=True):
running_containers = []
for service in self.get_services(service_names, include_links=start_links):
if recreate:
for (_, container) in service.recreate_containers(insecure_registry=insecure_registry):
for (_, container) in service.recreate_containers(
insecure_registry=insecure_registry,
do_build=do_build):
running_containers.append(container)
else:
for container in service.start_or_create_containers(insecure_registry=insecure_registry):
for container in service.start_or_create_containers(
insecure_registry=insecure_registry,
do_build=do_build):
running_containers.append(container)

return running_containers
Expand Down
15 changes: 10 additions & 5 deletions fig/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,18 @@ def create_container(self,
return Container.create(self.client, **container_options)
raise

def recreate_containers(self, insecure_registry=False, **override_options):
def recreate_containers(self, insecure_registry=False, do_build=True, **override_options):
"""
If a container for this service doesn't exist, create and start one. If there are
any, stop them, create+start new ones, and remove the old containers.
"""
containers = self.containers(stopped=True)
if not containers:
log.info("Creating %s..." % self._next_container_name(containers))
container = self.create_container(insecure_registry=insecure_registry, **override_options)
container = self.create_container(
insecure_registry=insecure_registry,
do_build=do_build,
**override_options)
self.start_container(container)
return [(None, container)]
else:
Expand Down Expand Up @@ -257,7 +260,7 @@ def recreate_container(self, container, **override_options):
container.remove()

options = dict(override_options)
new_container = self.create_container(**options)
new_container = self.create_container(do_build=False, **options)
self.start_container(new_container, intermediate_container=intermediate_container)

intermediate_container.remove()
Expand Down Expand Up @@ -302,12 +305,14 @@ def start_container(self, container, intermediate_container=None, **override_opt
)
return container

def start_or_create_containers(self, insecure_registry=False):
def start_or_create_containers(self, insecure_registry=False, do_build=True):
containers = self.containers(stopped=True)

if not containers:
log.info("Creating %s..." % self._next_container_name(containers))
new_container = self.create_container(insecure_registry=insecure_registry)
new_container = self.create_container(
insecure_registry=insecure_registry,
do_build=do_build)
return [self.start_container(new_container)]
else:
return [self.start_container_if_stopped(c) for c in containers]
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,23 @@ def test_create_container_from_insecure_registry(
mock_log.info.assert_called_once_with(
'Pulling image someimage:sometag...')

def test_create_container_with_build(self):
self.mock_client.images.return_value = []
service = Service('foo', client=self.mock_client, build='.')
service.build = mock.create_autospec(service.build)
service.create_container(do_build=True)

self.mock_client.images.assert_called_once_with(name=service.full_name)
service.build.assert_called_once_with()

def test_create_container_no_build(self):
self.mock_client.images.return_value = []
service = Service('foo', client=self.mock_client, build='.')
service.create_container(do_build=False)

self.assertFalse(self.mock_client.images.called)
self.assertFalse(self.mock_client.build.called)


class ServiceVolumesTest(unittest.TestCase):

Expand Down

0 comments on commit 367c3fd

Please sign in to comment.