فهرست منبع

Show a warning when engine is in swarm mode

Signed-off-by: Aanand Prasad <[email protected]>
Aanand Prasad 9 سال پیش
والد
کامیت
2c9e46f60f
2فایلهای تغییر یافته به همراه48 افزوده شده و 0 حذف شده
  1. 16 0
      compose/project.py
  2. 32 0
      tests/unit/project_test.py

+ 16 - 0
compose/project.py

@@ -369,6 +369,8 @@ class Project(object):
            detached=False,
            remove_orphans=False):
 
+        warn_for_swarm_mode(self.client)
+
         self.initialize()
         self.find_orphan_containers(remove_orphans)
 
@@ -533,6 +535,20 @@ def get_volumes_from(project, service_dict):
     return [build_volume_from(vf) for vf in volumes_from]
 
 
+def warn_for_swarm_mode(client):
+    info = client.info()
+    if info.get('Swarm', {}).get('LocalNodeState') == 'active':
+        log.warn(
+            "The Docker Engine you're using is running in swarm mode.\n\n"
+            "Compose does not use swarm mode to deploy services to multiple nodes in a swarm. "
+            "All containers will be scheduled on the current node.\n\n"
+            "To deploy your application across the swarm, "
+            "use the bundle feature of the Docker experimental build.\n\n"
+            "More info:\n"
+            "https://github.com/docker/docker/tree/master/experimental\n"
+        )
+
+
 class NoSuchService(Exception):
     def __init__(self, name):
         self.name = name

+ 32 - 0
tests/unit/project_test.py

@@ -510,3 +510,35 @@ class ProjectTest(unittest.TestCase):
 
         project.down(ImageType.all, True)
         self.mock_client.remove_image.assert_called_once_with("busybox:latest")
+
+    def test_warning_in_swarm_mode(self):
+        self.mock_client.info.return_value = {'Swarm': {'LocalNodeState': 'active'}}
+        project = Project('composetest', [], self.mock_client)
+
+        with mock.patch('compose.project.log') as fake_log:
+            project.up()
+            assert fake_log.warn.call_count == 1
+
+    def test_no_warning_on_stop(self):
+        self.mock_client.info.return_value = {'Swarm': {'LocalNodeState': 'active'}}
+        project = Project('composetest', [], self.mock_client)
+
+        with mock.patch('compose.project.log') as fake_log:
+            project.stop()
+            assert fake_log.warn.call_count == 0
+
+    def test_no_warning_in_normal_mode(self):
+        self.mock_client.info.return_value = {'Swarm': {'LocalNodeState': 'inactive'}}
+        project = Project('composetest', [], self.mock_client)
+
+        with mock.patch('compose.project.log') as fake_log:
+            project.up()
+            assert fake_log.warn.call_count == 0
+
+    def test_no_warning_with_no_swarm_info(self):
+        self.mock_client.info.return_value = {}
+        project = Project('composetest', [], self.mock_client)
+
+        with mock.patch('compose.project.log') as fake_log:
+            project.up()
+            assert fake_log.warn.call_count == 0