replaces skylab with crosfleet

BUG=chromium:1187326
TEST=Tested by hand on chrotomation.mtv

Change-Id: I7e0bd2a99e85f288fb3b1b9ff52c8e38d25df245
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2773632
Reviewed-by: Caroline Tice <cmtice@chromium.org>
Commit-Queue: Caroline Tice <cmtice@chromium.org>
Tested-by: Caroline Tice <cmtice@chromium.org>
diff --git a/lock_machine.py b/lock_machine.py
index 776fe54..e702059 100755
--- a/lock_machine.py
+++ b/lock_machine.py
@@ -42,7 +42,7 @@
 class MachineType(enum.Enum):
   """Enum class to hold machine type."""
   LOCAL = 'local'
-  SKYLAB = 'skylab'
+  CROSFLEET = 'crosfleet'
 
 
 class LockManager(object):
@@ -53,15 +53,15 @@
   machines, using appropriate locking mechanisms for each.
   """
 
-  SKYLAB_PATH = 'skylab'
+  CROSFLEET_PATH = 'crosfleet'
 
   # TODO(zhizhouy): lease time may needs to be dynamically adjusted. For now we
   # set it long enough to cover the period to finish nightly rotation tests.
   LEASE_MINS = 1439
 
-  SKYLAB_CREDENTIAL = ('/usr/local/google/home/mobiletc-prebuild'
-                       '/sheriff_utils/credentials/skylab'
-                       '/chromeos-swarming-credential.json')
+  CROSFLEET_CREDENTIAL = ('/usr/local/google/home/mobiletc-prebuild'
+                          '/sheriff_utils/credentials/skylab'
+                          '/chromeos-swarming-credential.json')
   SWARMING = 'chromite/third_party/swarming.client/swarming.py'
   SUCCESS = 0
 
@@ -102,7 +102,7 @@
     self.force = force_option
 
     self.local_machines = []
-    self.skylab_machines = []
+    self.crosfleet_machines = []
 
   def CheckMachine(self, machine, error_msg):
     """Verifies that machine is responding to ping.
@@ -148,8 +148,8 @@
     """
     if m in self.local_machines:
       return MachineType.LOCAL
-    if m in self.skylab_machines:
-      return MachineType.SKYLAB
+    if m in self.crosfleet_machines:
+      return MachineType.CROSFLEET
 
   def PrintStatusHeader(self):
     """Prints the status header lines for machines."""
@@ -169,9 +169,9 @@
             (m, state['board'], '\t\t' if machine_type == MachineType.LOCAL else
              '', state['locked_by'], state['lock_time']))
     else:
-      print(
-          '%s (%s)\t\t%sunlocked' % (m, state['board'], '\t\t' if
-                                     machine_type == MachineType.LOCAL else ''))
+      print('%s (%s)\t\t%sunlocked' %
+            (m, state['board'],
+             '\t\t' if machine_type == MachineType.LOCAL else ''))
 
   def AddMachineToLocal(self, machine):
     """Adds a machine to local machine list.
@@ -182,14 +182,14 @@
     if machine not in self.local_machines:
       self.local_machines.append(machine)
 
-  def AddMachineToSkylab(self, machine):
-    """Adds a machine to skylab machine list.
+  def AddMachineToCrosfleet(self, machine):
+    """Adds a machine to crosfleet machine list.
 
     Args:
       machine: The machine to be added.
     """
-    if machine not in self.skylab_machines:
-      self.skylab_machines.append(machine)
+    if machine not in self.crosfleet_machines:
+      self.crosfleet_machines.append(machine)
 
   def ListMachineStates(self, machine_states):
     """Gets and prints the current status for a list of machines.
@@ -208,8 +208,8 @@
       state = machine_states[m]
       self.PrintStatus(m, state, machine_type)
 
-  def UpdateLockInSkylab(self, should_lock_machine, machine):
-    """Ask skylab to lease/release a machine.
+  def UpdateLockInCrosfleet(self, should_lock_machine, machine):
+    """Ask crosfleet to lease/release a machine.
 
     Args:
       should_lock_machine: Boolean indicating whether to lock the machine (True)
@@ -221,9 +221,9 @@
     """
     try:
       if should_lock_machine:
-        ret = self.LeaseSkylabMachine(machine)
+        ret = self.LeaseCrosfleetMachine(machine)
       else:
-        ret = self.ReleaseSkylabMachine(machine)
+        ret = self.ReleaseCrosfleetMachine(machine)
     except Exception:
       return False
     return ret
@@ -241,8 +241,8 @@
     """
     try:
       if should_lock_machine:
-        ret = file_lock_machine.Machine(machine, self.locks_dir).Lock(
-            True, sys.argv[0])
+        ret = file_lock_machine.Machine(machine,
+                                        self.locks_dir).Lock(True, sys.argv[0])
       else:
         ret = file_lock_machine.Machine(machine, self.locks_dir).Unlock(True)
     except Exception:
@@ -268,18 +268,18 @@
       # TODO(zhizhouy): Handling exceptions with more details when locking
       # doesn't succeed.
       machine_type = self.GetMachineType(m)
-      if machine_type == MachineType.SKYLAB:
-        ret = self.UpdateLockInSkylab(lock_machines, m)
+      if machine_type == MachineType.CROSFLEET:
+        ret = self.UpdateLockInCrosfleet(lock_machines, m)
       elif machine_type == MachineType.LOCAL:
         ret = self.UpdateFileLock(lock_machines, m)
 
       if ret:
-        self.logger.LogOutput(
-            '%s %s machine succeeded: %s.' % (action, machine_type.value, m))
+        self.logger.LogOutput('%s %s machine succeeded: %s.' %
+                              (action, machine_type.value, m))
         updated_machines.append(m)
       else:
-        self.logger.LogOutput(
-            '%s %s machine failed: %s.' % (action, machine_type.value, m))
+        self.logger.LogOutput('%s %s machine failed: %s.' %
+                              (action, machine_type.value, m))
 
     self.machines = updated_machines
     return updated_machines
@@ -323,16 +323,16 @@
                                  '(%s).' % k)
           self._InternalRemoveMachine(k)
 
-        # TODO(zhizhouy): Skylab doesn't support host info such as locked_by.
-        # Need to update this when skylab supports it.
+        # TODO(zhizhouy): Crosfleet doesn't support host info such as locked_by.
+        # Need to update this when crosfleet supports it.
         if (state['locked'] and state['locked_by'] and
             state['locked_by'] != self.user):
           raise DontOwnLock('Attempt to unlock machine (%s) locked by someone '
                             'else (%s).' % (k, state['locked_by']))
       elif cmd == 'lock':
         if state['locked']:
-          self.logger.LogWarning(
-              'Attempt to lock already locked machine (%s)' % k)
+          self.logger.LogWarning('Attempt to lock already locked machine (%s)' %
+                                 k)
           self._InternalRemoveMachine(k)
 
   def GetMachineStates(self, cmd=''):
@@ -352,10 +352,11 @@
     """
     machine_list = {}
     for m in self.machines:
-      # For local or skylab machines, we simply set {'locked': status} for them
-      # TODO(zhizhouy): This is a quick fix since skylab cannot return host info
-      # as afe does. We need to get more info such as locked_by when skylab
-      # supports that.
+      # For local or crosfleet machines, we simply set {'locked': status} for
+      # them
+      # TODO(zhizhouy): This is a quick fix since crosfleet cannot return host
+      # info as afe does. We need to get more info such as locked_by when
+      # crosfleet supports that.
       values = {
           'locked': 0 if cmd == 'lock' else 1,
           'board': '??',
@@ -368,15 +369,15 @@
 
     return machine_list
 
-  def CheckMachineInSkylab(self, machine):
-    """Run command to check if machine is in Skylab or not.
+  def CheckMachineInCrosfleet(self, machine):
+    """Run command to check if machine is in Crosfleet or not.
 
     Returns:
-      True if machine in skylab, else False
+      True if machine in crosfleet, else False
     """
     credential = ''
-    if os.path.exists(self.SKYLAB_CREDENTIAL):
-      credential = '--auth-service-account-json %s' % self.SKYLAB_CREDENTIAL
+    if os.path.exists(self.CROSFLEET_CREDENTIAL):
+      credential = '--auth-service-account-json %s' % self.CROSFLEET_CREDENTIAL
     swarming = os.path.join(self.chromeos_root, self.SWARMING)
     # TODO(zhizhouy): Swarming script doesn't support python3 so explicitly
     # launch it with python2 until migrated.
@@ -388,11 +389,11 @@
             machine.rstrip('.cros')))
     exit_code, stdout, stderr = self.ce.RunCommandWOutput(cmd)
     if exit_code:
-      raise ValueError(
-          'Querying bots failed (2); stdout: %r; stderr: %r' % (stdout, stderr))
+      raise ValueError('Querying bots failed (2); stdout: %r; stderr: %r' %
+                       (stdout, stderr))
 
-    # The command will return a json output as stdout. If machine not in skylab
-    # stdout will look like this:
+    # The command will return a json output as stdout. If machine not in
+    # crosfleet, stdout will look like this:
     #  {
     #    "death_timeout": "600",
     #    "now": "TIMESTAMP"
@@ -401,19 +402,20 @@
     # this keyword for result.
     return 'items' in stdout
 
-  def LeaseSkylabMachine(self, machine):
-    """Run command to lease dut from skylab.
+  def LeaseCrosfleetMachine(self, machine):
+    """Run command to lease dut from crosfleet.
 
     Returns:
       True if succeeded, False if failed.
     """
     credential = ''
-    if os.path.exists(self.SKYLAB_CREDENTIAL):
-      credential = '-service-account-json %s' % self.SKYLAB_CREDENTIAL
-    cmd = (('%s lease-dut -minutes %s %s %s') % \
-           (self.SKYLAB_PATH,
+    if os.path.exists(self.CROSFLEET_CREDENTIAL):
+      credential = '-service-account-json %s' % self.CROSFLEET_CREDENTIAL
+    cmd = (('%s dut lease -minutes %s %s %s %s') % \
+           (self.CROSFLEET_PATH,
             self.LEASE_MINS,
             credential,
+            '-host' if '.cros' in machine else '-board',
             machine.rstrip('.cros')))
     # Wait 120 seconds for server to start the lease task, if not started,
     # we will treat it as unavailable.
@@ -421,17 +423,17 @@
     retval = self.ce.RunCommand(cmd, command_timeout=check_interval_time)
     return retval == self.SUCCESS
 
-  def ReleaseSkylabMachine(self, machine):
-    """Run command to release dut from skylab.
+  def ReleaseCrosfleetMachine(self, machine):
+    """Run command to release dut from crosfleet.
 
     Returns:
       True if succeeded, False if failed.
     """
     credential = ''
-    if os.path.exists(self.SKYLAB_CREDENTIAL):
-      credential = '-service-account-json %s' % self.SKYLAB_CREDENTIAL
-    cmd = (('%s release-dut %s %s') % \
-           (self.SKYLAB_PATH,
+    if os.path.exists(self.CROSFLEET_CREDENTIAL):
+      credential = '-service-account-json %s' % self.CROSFLEET_CREDENTIAL
+    cmd = (('%s dut abandon %s %s') % \
+           (self.CROSFLEET_PATH,
             credential,
             machine.rstrip('.cros')))
     retval = self.ce.RunCommand(cmd)