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

Add auto_up option to exclude service from docker-compose up by default #7548

Closed
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions compose/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
]

ALLOWED_KEYS = DOCKER_CONFIG_KEYS + [
'auto_up',
'blkio_config',
'build',
'container_name',
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"blkio_config": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v2.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"blkio_config": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v2.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"blkio_config": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v2.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"blkio_config": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v2.4.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"blkio_config": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"deploy": {"$ref": "#/definitions/deployment"},
"build": {
"oneOf": [
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v3.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"deploy": {"$ref": "#/definitions/deployment"},
"build": {
"oneOf": [
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v3.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"deploy": {"$ref": "#/definitions/deployment"},
"build": {
"oneOf": [
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v3.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"deploy": {"$ref": "#/definitions/deployment"},
"build": {
"oneOf": [
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v3.4.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"deploy": {"$ref": "#/definitions/deployment"},
"build": {
"oneOf": [
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v3.5.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"deploy": {"$ref": "#/definitions/deployment"},
"build": {
"oneOf": [
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v3.6.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"type": "object",

"properties": {
"auto_up": {"type": "boolean"},
"deploy": {"$ref": "#/definitions/deployment"},
"build": {
"oneOf": [
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v3.7.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"id": "#/definitions/service",
"type": "object",
"properties": {
"auto_up": {"type": "boolean"},
"deploy": {
"$ref": "#/definitions/deployment"
},
Expand Down
1 change: 1 addition & 0 deletions compose/config/config_schema_v3.8.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"id": "#/definitions/service",
"type": "object",
"properties": {
"auto_up": {"type": "boolean"},
"deploy": {
"$ref": "#/definitions/deployment"
},
Expand Down
25 changes: 19 additions & 6 deletions compose/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,14 @@ def validate_service_names(self, service_names):
if name not in valid_names:
raise NoSuchService(name)

def get_services(self, service_names=None, include_deps=False):
def get_services(self, service_names=None, include_deps=False, auto_up_only=False):
"""
Returns a list of this project's services filtered
by the provided list of names, or all services if service_names is None
or [].
If auto_up_only is True and service_names is None or [], services which
have auto_up explicitly set to False and are not pulled in as a
dependency are excluded.

If include_deps is specified, returns a list including the dependencies for
service_names, in order of dependency.
Expand All @@ -183,7 +186,12 @@ def get_services(self, service_names=None, include_deps=False):
Raises NoSuchService if any of the named services do not exist.
"""
if service_names is None or len(service_names) == 0:
service_names = self.service_names
service_names = [
service.name
for service in self.services
# if auto_up_only is True exclude services with auto_up set to False
if not auto_up_only or service.options.get('auto_up', True)
]

unsorted = [self.get_service(name) for name in service_names]
services = [s for s in self.services if s in unsorted]
Expand All @@ -196,8 +204,8 @@ def get_services(self, service_names=None, include_deps=False):

return uniques

def get_services_without_duplicate(self, service_names=None, include_deps=False):
services = self.get_services(service_names, include_deps)
def get_services_without_duplicate(self, service_names=None, include_deps=False, auto_up_only=False):
services = self.get_services(service_names, include_deps, auto_up_only)
for service in services:
service.remove_duplicate_containers()
return services
Expand Down Expand Up @@ -404,7 +412,11 @@ def create(
strategy=ConvergenceStrategy.changed,
do_build=BuildAction.none,
):
services = self.get_services_without_duplicate(service_names, include_deps=True)
services = self.get_services_without_duplicate(
service_names,
include_deps=True,
auto_up_only=True
)

for svc in services:
svc.ensure_image_exists(do_build=do_build)
Expand Down Expand Up @@ -538,7 +550,8 @@ def up(self,

services = self.get_services_without_duplicate(
service_names,
include_deps=start_deps)
include_deps=start_deps,
auto_up_only=True)

for svc in services:
svc.ensure_image_exists(do_build=do_build, silent=silent, cli=cli)
Expand Down
101 changes: 101 additions & 0 deletions tests/unit/project_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,107 @@ def test_get_services_removes_duplicates_following_links(self):
project = Project('test', [web, db], None)
assert project.get_services(['web', 'db'], include_deps=True) == [db, web]

def test_get_services_includes_non_auto_up_services_without_args(self):
db = Service(
name='db',
image='foo',
)
web = Service(
name='web',
image='foo',
links=[(db, 'database')]
)
mock_api = Service(
auto_up=False,
name='mock_api',
image='foo',
)
project = Project('test', [web, db, mock_api], None)
assert project.get_services() == [web, db, mock_api]

def test_get_services_excludes_non_auto_up_services_without_service_names(self):
db = Service(
name='db',
image='foo',
)
web = Service(
name='web',
image='foo',
links=[(db, 'database')]
)
mock_api = Service(
auto_up=False,
name='mock_api',
image='foo',
)
project = Project('test', [web, db, mock_api], None)
assert project.get_services(auto_up_only=True) == [web, db]

def test_get_services_exludes_auto_up_services_as_dependencies_with_no_deps(self):
db = Service(
auto_up=False,
name='db',
image='foo',
)
web = Service(
name='web',
image='foo',
links=[(db, 'database')]
)
project = Project('test', [web, db], None)
assert project.get_services(include_deps=False, auto_up_only=True) == [web]

def test_get_services_includes_auto_up_services_as_dependencies_with_include_deps(self):
db = Service(
auto_up=False,
name='db',
image='foo',
)
web = Service(
name='web',
image='foo',
links=[(db, 'database')]
)
project = Project('test', [web, db], None)
assert project.get_services(include_deps=True, auto_up_only=True) == [db, web]

def test_get_services_returns_listed_non_auto_up_services_with_service_names(self):
db = Service(
name='db',
image='foo',
)
web = Service(
name='web',
image='foo',
links=[(db, 'database')]
)
mock_api = Service(
auto_up=False,
name='mock_api',
image='foo',
)
project = Project('test', [web, db, mock_api], None)
assert project.get_services(['mock_api'], auto_up_only=True) == [mock_api]

def test_get_services_returns_listed_non_auto_up_services_and_dependencies_with_service_names(self):
db = Service(
name='db',
image='foo',
)
web = Service(
name='web',
image='foo',
links=[(db, 'database')]
)
mock_api = Service(
auto_up=False,
name='mock_api',
image='foo',
links=[(db, 'database')]
)
project = Project('test', [web, db, mock_api], None)
assert project.get_services(['mock_api'], include_deps=True, auto_up_only=True) == [db, mock_api]

def test_use_volumes_from_container(self):
container_id = 'aabbccddee'
container_dict = dict(Name='aaa', Id=container_id)
Expand Down