Skip to content
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

Added support of cpuset and memswap_limit options in fig.yml. #685

Closed
wants to merge 1 commit into from
Closed
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, cpuset, memswap_limit

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

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

mem_limit: 1000000000
privileged: true
cpuset: “0-3,5”
memswap_limit: 1024
```
14 changes: 13 additions & 1 deletion fig/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
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', 'cpuset', 'memswap_limit']

DOCKER_CONFIG_HINTS = {
'link' : 'links',
'port' : 'ports',
Expand Down Expand Up @@ -375,6 +376,8 @@ def _get_container_create_options(self, override_options, one_off=False):
self.build()
container_options['image'] = self._build_tag_name()

if 'cpuset' in container_options:
container_options['cpuset'] = parse_cpuset(container_options['cpuset'])
# Delete options which are only used when starting
for key in ['privileged', 'net', 'dns']:
if key in container_options:
Expand Down Expand Up @@ -436,6 +439,7 @@ def pull(self, insecure_registry=False):


NAME_RE = re.compile(r'^([^_]+)_([^_]+)_(run_)?(\d+)$')
CPUSET_RE = re.compile(r'^[0-9]+((\s*,\s*[0-9]+)*|(\s*\-\s*[0-9]+((\s*,\s*[0-9]+)+|$)))*$')


def is_valid_name(name, one_off=False):
Expand Down Expand Up @@ -534,3 +538,11 @@ def resolve_env(key, val):
return key, os.environ[key]
else:
return key, ''


def parse_cpuset(cpuset):
if not cpuset:
return None
if not CPUSET_RE.match(cpuset):
raise ConfigError("cpuset has incorrect format. should contain only comma separated numbers and ranges (1-3). ")
return cpuset
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PyYAML==3.10
docker-py==0.5.3
docker-py==0.6.0
dockerpty==0.3.2
docopt==0.6.1
requests==2.2.1
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def find_version(*file_paths):
'requests >= 2.2.1, < 3',
'texttable >= 0.8.1, < 0.9',
'websocket-client >= 0.11.0, < 0.12',
'docker-py >= 0.5.3, < 0.6',
'docker-py == 0.6',
'dockerpty >= 0.3.2, < 0.4',
'six >= 1.3.0, < 2',
]
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
parse_volume_spec,
build_volume_binding,
APIError,
parse_cpuset,
)


Expand Down Expand Up @@ -205,6 +206,27 @@ def test_create_container_from_insecure_registry(self, mock_log):
self.mock_client.pull.assert_called_once_with('someimage:sometag', insecure_registry=True, stream=True)
mock_log.info.assert_called_once_with('Pulling image someimage:sometag...')

def test_valid_parse_cpusets(self):
valid_cpuset = "1,2,3-4,5"
valid_cpuset_spaced = "4, 2, 3-4 , 5"
self.assertEqual(parse_cpuset(None), None)
self.assertEqual(parse_cpuset(''), None)
self.assertEqual(parse_cpuset(valid_cpuset), valid_cpuset)
self.assertEqual(parse_cpuset(valid_cpuset_spaced), valid_cpuset_spaced)

def test_invalid_parse_cpusets(self):
with self.assertRaises(ConfigError):
parse_cpuset("1-")
with self.assertRaises(ConfigError):
parse_cpuset("1-2,")
with self.assertRaises(ConfigError):
parse_cpuset("4,")
with self.assertRaises(ConfigError):
parse_cpuset("1-3-4")
with self.assertRaises(ConfigError):
parse_cpuset("1-3-4-1-2-2-")
with self.assertRaises(ConfigError):
parse_cpuset("1-2,2,a")

class ServiceVolumesTest(unittest.TestCase):

Expand Down