Do not migrate hosts with invalid hostnames.

When a host is migrated, it'll be renamed to host-migrated-do-not-use
in AFE DB. hosts with names ending with "-migrated-do-not-use" can't
be migrated to skylab. This was noticed in CL:*636668

BUG=chromium:846915
TEST="atest host migrate"

Change-Id: I741f83615c7f42d84b3c63c5161bccd0dbc769bc
Reviewed-on: https://chromium-review.googlesource.com/1089479
Reviewed-by: Xixuan Wu <xixuan@chromium.org>
Tested-by: Ningning Xia <nxia@chromium.org>
Commit-Queue: Ningning Xia <nxia@chromium.org>
diff --git a/cli/host.py b/cli/host.py
index 3841016..cef62fd 100644
--- a/cli/host.py
+++ b/cli/host.py
@@ -1144,15 +1144,31 @@
         return (options, leftover)
 
 
-    def execute(self):
-        """Execute 'atest host migrate'."""
-        hostnames = self.hosts
+    def _remove_invalid_hostnames(self, hostnames, log_failure=False):
+        """Remove hostnames with MIGRATED_HOST_SUFFIX.
+
+        @param hostnames: A list of hostnames.
+        @param log_failure: Bool indicating whether to log invalid hostsnames.
+
+        @return A list of valid hostnames.
+        """
+        invalid_hostnames = set()
         for hostname in hostnames:
             if hostname.endswith(MIGRATED_HOST_SUFFIX):
-                self.failure('Cannot migrate host with suffix "%s" %s.' %
-                             (MIGRATED_HOST_SUFFIX, hostname),
-                             item=hostname, what_failed='Failed to rename')
-                hostnames.remove(hostname)
+                if log_failure:
+                    self.failure('Cannot migrate host with suffix "%s" %s.' %
+                                 (MIGRATED_HOST_SUFFIX, hostname),
+                                 item=hostname, what_failed='Failed to rename')
+                invalid_hostnames.add(hostname)
+
+        hostnames = list(set(hostnames) - invalid_hostnames)
+
+        return hostnames
+
+
+    def execute(self):
+        """Execute 'atest host migrate'."""
+        hostnames = self._remove_invalid_hostnames(self.hosts, log_failure=True)
 
         filters = {}
         check_results = {}
@@ -1166,6 +1182,11 @@
                         _add_hostname_suffix(h, MIGRATED_HOST_SUFFIX)
                         for h in hostnames]
                 filters['hostname__in'] = hostnames_with_suffix
+        else:
+            # TODO(nxia): add exclude_filter {'hostname__endswith':
+            # MIGRATED_HOST_SUFFIX} for --migration
+            if self.rollback:
+                filters['hostname__endswith'] = MIGRATED_HOST_SUFFIX
 
         labels = []
         if self.model:
@@ -1185,6 +1206,13 @@
                 op='get_hosts', filters=filters, check_results=check_results)
         hostnames = [h['hostname'] for h in results]
 
+        if self.migration:
+            hostnames = self._remove_invalid_hostnames(hostnames)
+        else:
+            # rollback
+            hostnames = [_remove_hostname_suffix(h, MIGRATED_HOST_SUFFIX)
+                         for h in hostnames]
+
         return self.execute_skylab_migration(hostnames)
 
 
@@ -1237,10 +1265,10 @@
             # rollback
             prod_devices = device.move_devices(
                     skylab_lab, prod_lab, 'duts', environment='prod',
-                    label_map=label_map, hostnames=self.hosts)
+                    label_map=label_map, hostnames=hostnames)
             staging_devices = device.move_devices(
                     staging_lab, skylab_lab, 'duts', environment='staging',
-                    label_map=label_map, hostnames=self.hosts)
+                    label_map=label_map, hostnames=hostnames)
 
             all_devices = prod_devices + staging_devices
             # Hostnames in afe_hosts tabel.