Skip to content

Add restart option to Fig. #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ dns:
- 9.9.9.9
```

### working\_dir, entrypoint, user, hostname, domainname, mem\_limit, privileged
### working\_dir, entrypoint, user, hostname, domainname, mem\_limit, privileged, restart

Each of these is a single value, analogous to its [docker run](https://docs.docker.com/reference/run/) counterpart.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still true? Thinking of :max_retry here (or is that to be considered a "single" value? idk)


Expand All @@ -156,4 +156,6 @@ domainname: foo.com

mem_limit: 1000000000
privileged: true

restart: always
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps an an example using max_retry should be added somewhere. Should a list of valid options be shown or, alternatively, a link to the right section of the Docker documentation that explains this feature?

```
23 changes: 21 additions & 2 deletions fig/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
log = logging.getLogger(__name__)


DOCKER_CONFIG_KEYS = ['image', 'command', 'hostname', 'domainname', 'user', 'detach', 'stdin_open', 'tty', 'mem_limit', 'ports', 'environment', 'dns', 'volumes', 'entrypoint', 'privileged', 'volumes_from', 'net', 'working_dir']
DOCKER_CONFIG_KEYS = ['image', 'command', 'hostname', 'domainname', 'user', 'detach', 'stdin_open', 'tty', 'mem_limit', 'ports', 'environment', 'dns', 'volumes', 'entrypoint', 'privileged', 'volumes_from', 'net', 'working_dir', 'restart']
DOCKER_CONFIG_HINTS = {
'link' : 'links',
'port' : 'ports',
Expand Down Expand Up @@ -262,6 +262,8 @@ def start_container(self, container=None, intermediate_container=None, **overrid
net = options.get('net', 'bridge')
dns = options.get('dns', None)

restart = parse_restart_spec(options.get('restart', None))

container.start(
links=self._get_links(link_to_self=options.get('one_off', False)),
port_bindings=port_bindings,
Expand All @@ -270,6 +272,7 @@ def start_container(self, container=None, intermediate_container=None, **overrid
privileged=privileged,
network_mode=net,
dns=dns,
restart_policy=restart
)
return container

Expand Down Expand Up @@ -376,7 +379,7 @@ def _get_container_create_options(self, override_options, one_off=False):
container_options['image'] = self._build_tag_name()

# Delete options which are only used when starting
for key in ['privileged', 'net', 'dns']:
for key in ['privileged', 'net', 'dns', 'restart']:
if key in container_options:
del container_options[key]

Expand Down Expand Up @@ -466,6 +469,22 @@ def get_container_name(container):
return name[1:]


def parse_restart_spec(restart_config):
if not restart_config:
return None
parts = restart_config.split(':')
if len(parts) > 2:
raise ConfigError("Restart %s has incorrect format, should be "
"mode[:max_retry]" % restart_config)
if len(parts) == 2:
name, max_retry_count = parts
else:
name, = parts
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray comma here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I don't want the List but the only element inside

>>> a, = ['hello']
>>> a
'hello'
>>> a = ['hello']
>>> a
['hello']

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, neat.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name = parts[0] is much clearer. ;) Not a big deal.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough :)

max_retry_count = 0

return {'Name': name, 'MaximumRetryCount': int(max_retry_count)}


def parse_volume_spec(volume_config):
parts = volume_config.split(':')
if len(parts) > 3:
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,17 @@ def test_dns_list(self):
container = service.start_container().inspect()
self.assertEqual(container['HostConfig']['Dns'], ['8.8.8.8', '9.9.9.9'])

def test_restart_always_value(self):
service = self.create_service('web', restart='always')
container = service.start_container().inspect()
self.assertEqual(container['HostConfig']['RestartPolicy']['Name'], 'always')

def test_restart_on_failure_value(self):
service = self.create_service('web', restart='on-failure:5')
container = service.start_container().inspect()
self.assertEqual(container['HostConfig']['RestartPolicy']['Name'], 'on-failure')
self.assertEqual(container['HostConfig']['RestartPolicy']['MaximumRetryCount'], 5)

def test_working_dir_param(self):
service = self.create_service('container', working_dir='/working/dir/sample')
container = service.create_container().inspect()
Expand Down