-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration warning and option to migrate to labels.
Signed-off-by: Daniel Nephin <[email protected]>
- Loading branch information
Showing
6 changed files
with
144 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import logging | ||
import re | ||
|
||
from .container import get_container_name, Container | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
# TODO: remove this section when migrate_project_to_labels is removed | ||
NAME_RE = re.compile(r'^([^_]+)_([^_]+)_(run_)?(\d+)$') | ||
|
||
|
||
def is_valid_name(name): | ||
match = NAME_RE.match(name) | ||
return match is not None | ||
|
||
|
||
def add_labels(project, container, name): | ||
project_name, service_name, one_off, number = NAME_RE.match(name).groups() | ||
if project_name != project.name or service_name not in project.service_names: | ||
return | ||
service = project.get_service(service_name) | ||
service.recreate_container(container) | ||
|
||
|
||
def migrate_project_to_labels(project): | ||
log.info("Running migration to labels for project %s", project.name) | ||
|
||
client = project.client | ||
for container in client.containers(all=True): | ||
name = get_container_name(container) | ||
if not is_valid_name(name): | ||
continue | ||
add_labels(project, Container.from_ps(client, container), name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import mock | ||
|
||
from compose import service, migration | ||
from compose.project import Project | ||
from .testcases import DockerClientTestCase | ||
|
||
|
||
class ProjectTest(DockerClientTestCase): | ||
|
||
def test_migration_to_labels(self): | ||
web = self.create_service('web') | ||
db = self.create_service('db') | ||
project = Project('composetest', [web, db], self.client) | ||
|
||
self.client.create_container(name='composetest_web_1', **web.options) | ||
self.client.create_container(name='composetest_db_1', **db.options) | ||
|
||
with mock.patch.object(service, 'log', autospec=True) as mock_log: | ||
self.assertEqual(project.containers(stopped=True), []) | ||
self.assertEqual(mock_log.warn.call_count, 2) | ||
|
||
migration.migrate_project_to_labels(project) | ||
self.assertEqual(len(project.containers(stopped=True)), 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters