Raise an error if inventory-repo-dir is not clean.

Raise an exception when "atest server/host" is given an
inventory-repo-dir with local uncommitted changes.

BUG=chromium:832309
TEST="atest server/host list"

Change-Id: I085a29d4daf23ba2da68a65db854e33dd510b6c7
Reviewed-on: https://chromium-review.googlesource.com/1086378
Commit-Ready: Ningning Xia <nxia@chromium.org>
Tested-by: Ningning Xia <nxia@chromium.org>
Reviewed-by: Xixuan Wu <xixuan@chromium.org>
diff --git a/cli/server.py b/cli/server.py
index f389413..3316dfb 100644
--- a/cli/server.py
+++ b/cli/server.py
@@ -193,6 +193,25 @@
         return (options, leftover)
 
 
+    def execute_skylab(self):
+        """Execute 'atest server list --skylab'
+
+        @return: A list of servers matched the given hostname and role.
+        """
+        inventory_repo = skylab_utils.InventoryRepo(
+                        self.inventory_repo_dir)
+        inventory_repo.initialize()
+        infrastructure = text_manager.load_infrastructure(
+                inventory_repo.get_data_dir())
+
+        return skylab_server.get_servers(
+                infrastructure,
+                self.environment,
+                hostname=self.hostname,
+                role=self.role,
+                status=self.status)
+
+
     def execute(self):
         """Execute the command.
 
@@ -200,20 +219,10 @@
         """
         if self.skylab:
             try:
-                inventory_repo = skylab_utils.InventoryRepo(
-                        self.inventory_repo_dir)
-                inventory_repo.initialize()
-                infrastructure = text_manager.load_infrastructure(
-                        inventory_repo.get_data_dir())
-
-                return skylab_server.get_servers(
-                        infrastructure,
-                        self.environment,
-                        hostname=self.hostname,
-                        role=self.role,
-                        status=self.status)
+                return self.execute_skylab()
             except (skylab_server.SkylabServerActionError,
-                    revision_control.GitError) as e:
+                    revision_control.GitError,
+                    skylab_utils.InventoryRepoDirNotClean) as e:
                 self.failure(e, what_failed='Failed to list servers from skylab'
                              ' inventory.', item=self.hostname, fatal=True)
         else:
@@ -322,7 +331,8 @@
                 return self.execute_skylab()
             except (skylab_server.SkylabServerActionError,
                     revision_control.GitError,
-                    gob_util.GOBError) as e:
+                    gob_util.GOBError,
+                    skylab_utils.InventoryRepoDirNotClean) as e:
                 self.failure(e, what_failed='Failed to create server in skylab '
                              'inventory.', item=self.hostname, fatal=True)
         else:
@@ -389,7 +399,8 @@
                 return True
             except (skylab_server.SkylabServerActionError,
                     revision_control.GitError,
-                    gob_util.GOBError) as e:
+                    gob_util.GOBError,
+                    skylab_utils.InventoryRepoDirNotClean) as e:
                 self.failure(e, what_failed='Failed to delete server from '
                              'skylab inventory.', item=self.hostname,
                              fatal=True)
@@ -551,7 +562,8 @@
                 return self.execute_skylab()
             except (skylab_server.SkylabServerActionError,
                     revision_control.GitError,
-                    gob_util.GOBError) as e:
+                    gob_util.GOBError,
+                    skylab_utils.InventoryRepoDirNotClean) as e:
                 self.failure(e, what_failed='Failed to modify server in skylab'
                              ' inventory.', item=self.hostname, fatal=True)
         else: