Derek Beckett | f73baca | 2020-08-19 15:08:47 -0700 | [diff] [blame] | 1 | # Lint as: python2, python3 |
Fang Deng | 0ca40e2 | 2013-08-27 17:47:44 -0700 | [diff] [blame] | 2 | """This class defines the Remote host class.""" |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 3 | |
Derek Beckett | f73baca | 2020-08-19 15:08:47 -0700 | [diff] [blame] | 4 | from __future__ import absolute_import |
| 5 | from __future__ import division |
| 6 | from __future__ import print_function |
| 7 | import os, logging, time |
| 8 | import six |
| 9 | from six.moves import urllib |
Alex Khouderchah | c44e777 | 2018-07-16 10:53:14 -0700 | [diff] [blame] | 10 | import re |
Derek Beckett | fccbb62 | 2021-02-08 16:44:53 -0800 | [diff] [blame] | 11 | |
| 12 | import common |
| 13 | |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 14 | from autotest_lib.client.common_lib import error |
Derek Beckett | fccbb62 | 2021-02-08 16:44:53 -0800 | [diff] [blame] | 15 | from autotest_lib.client.common_lib.global_config import global_config |
jadmanski | 96b7807 | 2009-05-21 22:21:04 +0000 | [diff] [blame] | 16 | from autotest_lib.server import utils |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 17 | from autotest_lib.server.hosts import base_classes |
Derek Beckett | c09cc5e | 2021-03-18 10:40:11 -0700 | [diff] [blame] | 18 | from autotest_lib.server.hosts.tls_client.connection import TLSConnection |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 19 | |
| 20 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 21 | class RemoteHost(base_classes.Host): |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 22 | """ |
| 23 | This class represents a remote machine on which you can run |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 24 | programs. |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 25 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 26 | It may be accessed through a network, a serial line, ... |
| 27 | It is not the machine autoserv is running on. |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 28 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 29 | Implementation details: |
| 30 | This is an abstract class, leaf subclasses must implement the methods |
| 31 | listed here and in parent classes which have no implementation. They |
| 32 | may reimplement methods which already have an implementation. You |
| 33 | must not instantiate this class but should instantiate one of those |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 34 | leaf subclasses. |
| 35 | """ |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 36 | |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 37 | DEFAULT_REBOOT_TIMEOUT = base_classes.Host.DEFAULT_REBOOT_TIMEOUT |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 38 | DEFAULT_HALT_TIMEOUT = 2 * 60 |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 39 | _LABEL_FUNCTIONS = [] |
| 40 | _DETECTABLE_LABELS = [] |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 41 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 42 | VAR_LOG_MESSAGES_COPY_PATH = "/var/tmp/messages.autotest_start" |
Mike Frysinger | b718b03 | 2019-07-01 07:55:44 -0400 | [diff] [blame] | 43 | TMP_DIR_TEMPLATE = '/usr/local/tmp/autoserv-XXXXXX' |
jadmanski | 4900b3b | 2009-07-02 22:12:08 +0000 | [diff] [blame] | 44 | |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 45 | |
jadmanski | f656291 | 2008-10-21 17:59:01 +0000 | [diff] [blame] | 46 | def _initialize(self, hostname, autodir=None, *args, **dargs): |
| 47 | super(RemoteHost, self)._initialize(*args, **dargs) |
mbligh | 321b1f5 | 2008-04-09 16:23:43 +0000 | [diff] [blame] | 48 | |
jadmanski | 1c5e3a1 | 2008-08-15 23:08:20 +0000 | [diff] [blame] | 49 | self.hostname = hostname |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 50 | self.autodir = autodir |
| 51 | self.tmp_dirs = [] |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 52 | |
Derek Beckett | fccbb62 | 2021-02-08 16:44:53 -0800 | [diff] [blame] | 53 | get_value = global_config.get_config_value |
| 54 | |
| 55 | self.tls_connection = None |
Derek Beckett | 3b9974a | 2021-03-18 11:16:00 -0700 | [diff] [blame] | 56 | try: |
Derek Beckett | c09cc5e | 2021-03-18 10:40:11 -0700 | [diff] [blame] | 57 | self.tls_connection = TLSConnection() |
Derek Beckett | 3b9974a | 2021-03-18 11:16:00 -0700 | [diff] [blame] | 58 | except Exception as e: |
| 59 | logging.warning("Could not establish TLS connection %s", e) |
jadmanski | a2db941 | 2008-08-22 21:47:24 +0000 | [diff] [blame] | 60 | |
jadmanski | edf33e0 | 2009-05-22 16:47:27 +0000 | [diff] [blame] | 61 | def __repr__(self): |
| 62 | return "<remote host: %s>" % self.hostname |
| 63 | |
| 64 | |
jadmanski | 53aaf38 | 2008-11-17 16:22:31 +0000 | [diff] [blame] | 65 | def close(self): |
Richard Barnette | 73b3517 | 2018-07-27 10:59:01 -0700 | [diff] [blame] | 66 | # pylint: disable=missing-docstring |
jadmanski | 53aaf38 | 2008-11-17 16:22:31 +0000 | [diff] [blame] | 67 | super(RemoteHost, self).close() |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 68 | self.stop_loggers() |
| 69 | |
| 70 | if hasattr(self, 'tmp_dirs'): |
| 71 | for dir in self.tmp_dirs: |
| 72 | try: |
Allen Li | ad719c1 | 2017-06-27 23:48:04 +0000 | [diff] [blame] | 73 | self.run('rm -rf "%s"' % (utils.sh_escape(dir))) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 74 | except error.AutoservRunError: |
| 75 | pass |
Derek Beckett | fccbb62 | 2021-02-08 16:44:53 -0800 | [diff] [blame] | 76 | if self.tls_connection: |
| 77 | self.tls_connection.close() |
| 78 | self.tls_connection = None |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 79 | |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 80 | def job_start(self): |
| 81 | """ |
| 82 | Abstract method, called the first time a remote host object |
| 83 | is created for a specific host after a job starts. |
| 84 | |
| 85 | This method depends on the create_host factory being used to |
| 86 | construct your host object. If you directly construct host objects |
| 87 | you will need to call this method yourself (and enforce the |
| 88 | single-call rule). |
| 89 | """ |
jadmanski | 4900b3b | 2009-07-02 22:12:08 +0000 | [diff] [blame] | 90 | try: |
Andrey Ulanov | ad47290 | 2016-01-11 17:31:18 -0800 | [diff] [blame] | 91 | cmd = ('test ! -e /var/log/messages || cp -f /var/log/messages ' |
| 92 | '%s') % self.VAR_LOG_MESSAGES_COPY_PATH |
Allen Li | ad719c1 | 2017-06-27 23:48:04 +0000 | [diff] [blame] | 93 | self.run(cmd) |
Derek Beckett | f73baca | 2020-08-19 15:08:47 -0700 | [diff] [blame] | 94 | except Exception as e: |
jadmanski | 4900b3b | 2009-07-02 22:12:08 +0000 | [diff] [blame] | 95 | # Non-fatal error |
| 96 | logging.info('Failed to copy /var/log/messages at startup: %s', e) |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 97 | |
| 98 | |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 99 | def get_autodir(self): |
| 100 | return self.autodir |
| 101 | |
| 102 | |
| 103 | def set_autodir(self, autodir): |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 104 | """ |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 105 | This method is called to make the host object aware of the |
| 106 | where autotest is installed. Called in server/autotest.py |
| 107 | after a successful install |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 108 | """ |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 109 | self.autodir = autodir |
| 110 | |
| 111 | |
| 112 | def sysrq_reboot(self): |
Richard Barnette | 73b3517 | 2018-07-27 10:59:01 -0700 | [diff] [blame] | 113 | # pylint: disable=missing-docstring |
J. Richard Barnette | 9af1963 | 2015-09-25 12:18:03 -0700 | [diff] [blame] | 114 | self.run_background('echo b > /proc/sysrq-trigger') |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 115 | |
| 116 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 117 | def halt(self, timeout=DEFAULT_HALT_TIMEOUT, wait=True): |
Richard Barnette | ab9769f | 2016-06-01 15:01:44 -0700 | [diff] [blame] | 118 | """ |
| 119 | Shut down the remote host. |
| 120 | |
| 121 | N.B. This method makes no provision to bring the target back |
| 122 | up. The target will be offline indefinitely if there's no |
| 123 | independent hardware (servo, RPM, etc.) to force the target to |
| 124 | power on. |
| 125 | |
| 126 | @param timeout Maximum time to wait for host down, in seconds. |
| 127 | @param wait Whether to wait for the host to go offline. |
| 128 | """ |
J. Richard Barnette | 9af1963 | 2015-09-25 12:18:03 -0700 | [diff] [blame] | 129 | self.run_background('sleep 1 ; halt') |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 130 | if wait: |
| 131 | self.wait_down(timeout=timeout) |
| 132 | |
| 133 | |
Richard Barnette | ab9769f | 2016-06-01 15:01:44 -0700 | [diff] [blame] | 134 | def reboot(self, timeout=DEFAULT_REBOOT_TIMEOUT, wait=True, |
| 135 | fastsync=False, reboot_cmd=None, **dargs): |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 136 | """ |
| 137 | Reboot the remote host. |
| 138 | |
| 139 | Args: |
| 140 | timeout - How long to wait for the reboot. |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 141 | wait - Should we wait to see if the machine comes back up. |
Hidehiko Abe | 3d512d3 | 2017-04-27 15:11:33 +0900 | [diff] [blame] | 142 | If this is set to True, ignores reboot_cmd's error |
| 143 | even if occurs. |
mbligh | 2b94977 | 2009-02-26 00:59:36 +0000 | [diff] [blame] | 144 | fastsync - Don't wait for the sync to complete, just start one |
| 145 | and move on. This is for cases where rebooting prompty |
| 146 | is more important than data integrity and/or the |
| 147 | machine may have disks that cause sync to never return. |
mbligh | 959ed87 | 2009-04-17 22:18:25 +0000 | [diff] [blame] | 148 | reboot_cmd - Reboot command to execute. |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 149 | """ |
Richard Barnette | ab9769f | 2016-06-01 15:01:44 -0700 | [diff] [blame] | 150 | self.reboot_setup(**dargs) |
J. Richard Barnette | 9af1963 | 2015-09-25 12:18:03 -0700 | [diff] [blame] | 151 | if not reboot_cmd: |
| 152 | reboot_cmd = ('sync & sleep 5; ' |
| 153 | 'reboot & sleep 60; ' |
| 154 | 'reboot -f & sleep 10; ' |
| 155 | 'reboot -nf & sleep 10; ' |
| 156 | 'telinit 6') |
| 157 | |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 158 | def reboot(): |
Richard Barnette | ab9769f | 2016-06-01 15:01:44 -0700 | [diff] [blame] | 159 | # pylint: disable=missing-docstring |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 160 | self.record("GOOD", None, "reboot.start") |
Richard Barnette | 73b3517 | 2018-07-27 10:59:01 -0700 | [diff] [blame] | 161 | current_boot_id = None |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 162 | try: |
jadmanski | c035491 | 2010-01-12 15:57:29 +0000 | [diff] [blame] | 163 | current_boot_id = self.get_boot_id() |
| 164 | |
jadmanski | d544a35 | 2009-01-14 23:36:28 +0000 | [diff] [blame] | 165 | # sync before starting the reboot, so that a long sync during |
| 166 | # shutdown isn't timed out by wait_down's short timeout |
mbligh | 2b94977 | 2009-02-26 00:59:36 +0000 | [diff] [blame] | 167 | if not fastsync: |
mbligh | 959ed87 | 2009-04-17 22:18:25 +0000 | [diff] [blame] | 168 | self.run('sync; sync', timeout=timeout, ignore_status=True) |
jadmanski | d544a35 | 2009-01-14 23:36:28 +0000 | [diff] [blame] | 169 | |
J. Richard Barnette | 9af1963 | 2015-09-25 12:18:03 -0700 | [diff] [blame] | 170 | self.run_background(reboot_cmd) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 171 | except error.AutoservRunError: |
Hidehiko Abe | 3d512d3 | 2017-04-27 15:11:33 +0900 | [diff] [blame] | 172 | # If wait is set, ignore the error here, and rely on the |
| 173 | # wait_for_restart() for stability, instead. |
| 174 | # reboot_cmd sometimes causes an error even if reboot is |
| 175 | # successfully in progress. This is difficult to be avoided, |
| 176 | # because we have no much control on remote machine after |
| 177 | # "reboot" starts. |
Richard Barnette | 73b3517 | 2018-07-27 10:59:01 -0700 | [diff] [blame] | 178 | if not wait or current_boot_id is None: |
Hidehiko Abe | 3d512d3 | 2017-04-27 15:11:33 +0900 | [diff] [blame] | 179 | # TODO(b/37652392): Revisit no-wait case, later. |
| 180 | self.record("ABORT", None, "reboot.start", |
| 181 | "reboot command failed") |
| 182 | raise |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 183 | if wait: |
jadmanski | c035491 | 2010-01-12 15:57:29 +0000 | [diff] [blame] | 184 | self.wait_for_restart(timeout, old_boot_id=current_boot_id, |
| 185 | **dargs) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 186 | |
| 187 | # if this is a full reboot-and-wait, run the reboot inside a group |
| 188 | if wait: |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 189 | self.log_op(self.OP_REBOOT, reboot) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 190 | else: |
| 191 | reboot() |
| 192 | |
Ravi Chandra Sadineni | 812e61b | 2018-07-09 11:16:50 -0700 | [diff] [blame] | 193 | def suspend(self, timeout, suspend_cmd, |
| 194 | allow_early_resume=False): |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 195 | """ |
| 196 | Suspend the remote host. |
| 197 | |
| 198 | Args: |
Ravi Chandra Sadineni | 812e61b | 2018-07-09 11:16:50 -0700 | [diff] [blame] | 199 | timeout - How long to wait for the suspend in integer seconds. |
| 200 | suspend_cmd - suspend command to execute. |
| 201 | allow_early_resume - Boolean that indicate whether resume |
| 202 | before |timeout| is ok. |
| 203 | Raises: |
| 204 | error.AutoservSuspendError - If |allow_early_resume| is False |
| 205 | and if device resumes before |
| 206 | |timeout|. |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 207 | """ |
| 208 | # define a function for the supend and run it in a group |
| 209 | def suspend(): |
Richard Barnette | ab9769f | 2016-06-01 15:01:44 -0700 | [diff] [blame] | 210 | # pylint: disable=missing-docstring |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 211 | self.record("GOOD", None, "suspend.start for %d seconds" % (timeout)) |
| 212 | try: |
J. Richard Barnette | 9af1963 | 2015-09-25 12:18:03 -0700 | [diff] [blame] | 213 | self.run_background(suspend_cmd) |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 214 | except error.AutoservRunError: |
| 215 | self.record("ABORT", None, "suspend.start", |
| 216 | "suspend command failed") |
| 217 | raise error.AutoservSuspendError("suspend command failed") |
| 218 | |
| 219 | # Wait for some time, to ensure the machine is going to sleep. |
| 220 | # Not too long to check if the machine really suspended. |
| 221 | time_slice = min(timeout / 2, 300) |
| 222 | time.sleep(time_slice) |
| 223 | time_counter = time_slice |
| 224 | while time_counter < timeout + 60: |
| 225 | # Check if the machine is back. We check regularely to |
| 226 | # ensure the machine was suspended long enough. |
| 227 | if utils.ping(self.hostname, tries=1, deadline=1) == 0: |
| 228 | return |
| 229 | else: |
| 230 | if time_counter > timeout - 10: |
| 231 | time_slice = 5 |
| 232 | time.sleep(time_slice) |
| 233 | time_counter += time_slice |
| 234 | |
| 235 | if utils.ping(self.hostname, tries=1, deadline=1) != 0: |
| 236 | raise error.AutoservSuspendError( |
| 237 | "DUT is not responding after %d seconds" % (time_counter)) |
| 238 | |
| 239 | start_time = time.time() |
| 240 | self.log_op(self.OP_SUSPEND, suspend) |
| 241 | lasted = time.time() - start_time |
Ravi Chandra Sadineni | 812e61b | 2018-07-09 11:16:50 -0700 | [diff] [blame] | 242 | logging.info("Device resumed after %d secs", lasted) |
| 243 | if (lasted < timeout and not allow_early_resume): |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 244 | raise error.AutoservSuspendError( |
| 245 | "Suspend did not last long enough: %d instead of %d" % ( |
| 246 | lasted, timeout)) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 247 | |
jadmanski | 4f90925 | 2008-12-01 20:47:10 +0000 | [diff] [blame] | 248 | def reboot_followup(self, *args, **dargs): |
Richard Barnette | 73b3517 | 2018-07-27 10:59:01 -0700 | [diff] [blame] | 249 | # pylint: disable=missing-docstring |
jadmanski | 4f90925 | 2008-12-01 20:47:10 +0000 | [diff] [blame] | 250 | super(RemoteHost, self).reboot_followup(*args, **dargs) |
| 251 | if self.job: |
| 252 | self.job.profilers.handle_reboot(self) |
| 253 | |
| 254 | |
jadmanski | d778ae4 | 2009-01-07 15:07:36 +0000 | [diff] [blame] | 255 | def wait_for_restart(self, timeout=DEFAULT_REBOOT_TIMEOUT, **dargs): |
jadmanski | d60321a | 2008-10-28 20:32:05 +0000 | [diff] [blame] | 256 | """ |
| 257 | Wait for the host to come back from a reboot. This wraps the |
| 258 | generic wait_for_restart implementation in a reboot group. |
| 259 | """ |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 260 | def op_func(): |
Richard Barnette | ab9769f | 2016-06-01 15:01:44 -0700 | [diff] [blame] | 261 | # pylint: disable=missing-docstring |
jadmanski | d778ae4 | 2009-01-07 15:07:36 +0000 | [diff] [blame] | 262 | super(RemoteHost, self).wait_for_restart(timeout=timeout, **dargs) |
Gwendal Grignou | 7a61d2f | 2014-05-23 11:05:51 -0700 | [diff] [blame] | 263 | self.log_op(self.OP_REBOOT, op_func) |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 264 | |
| 265 | |
Gregory Nisbet | ec615d6 | 2020-12-11 17:59:20 +0000 | [diff] [blame] | 266 | def cleanup(self): |
Richard Barnette | 73b3517 | 2018-07-27 10:59:01 -0700 | [diff] [blame] | 267 | # pylint: disable=missing-docstring |
mbligh | 1264b51 | 2008-11-05 22:21:49 +0000 | [diff] [blame] | 268 | super(RemoteHost, self).cleanup() |
Gregory Nisbet | ec615d6 | 2020-12-11 17:59:20 +0000 | [diff] [blame] | 269 | self.reboot() |
mbligh | 1264b51 | 2008-11-05 22:21:49 +0000 | [diff] [blame] | 270 | |
| 271 | |
mbligh | e48bcfb | 2008-11-11 17:09:44 +0000 | [diff] [blame] | 272 | def get_tmp_dir(self, parent='/tmp'): |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 273 | """ |
| 274 | Return the pathname of a directory on the host suitable |
| 275 | for temporary file storage. |
| 276 | |
| 277 | The directory and its content will be deleted automatically |
| 278 | on the destruction of the Host object that was used to obtain |
| 279 | it. |
| 280 | """ |
Alex Khouderchah | c44e777 | 2018-07-16 10:53:14 -0700 | [diff] [blame] | 281 | template = os.path.join(parent, self.TMP_DIR_TEMPLATE) |
Jae Hoon Kim | ba8080e | 2021-03-24 15:14:27 -0700 | [diff] [blame] | 282 | parent = os.path.dirname(template) |
Derek Beckett | 24c0e82 | 2020-06-10 12:54:04 -0700 | [diff] [blame] | 283 | dir_name = self.run('mkdir -p %s && mktemp -d %s' % (parent, template)).stdout.rstrip() |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 284 | self.tmp_dirs.append(dir_name) |
| 285 | return dir_name |
| 286 | |
| 287 | |
mbligh | 6b95b52 | 2010-02-19 19:17:41 +0000 | [diff] [blame] | 288 | def get_platform_label(self): |
| 289 | """ |
| 290 | Return the platform label, or None if platform label is not set. |
| 291 | """ |
| 292 | |
| 293 | if self.job: |
| 294 | keyval_path = os.path.join(self.job.resultdir, 'host_keyvals', |
| 295 | self.hostname) |
| 296 | keyvals = utils.read_keyval(keyval_path) |
| 297 | return keyvals.get('platform', None) |
| 298 | else: |
| 299 | return None |
| 300 | |
| 301 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 302 | def get_all_labels(self): |
| 303 | """ |
| 304 | Return all labels, or empty list if label is not set. |
| 305 | """ |
| 306 | if self.job: |
| 307 | keyval_path = os.path.join(self.job.resultdir, 'host_keyvals', |
| 308 | self.hostname) |
| 309 | keyvals = utils.read_keyval(keyval_path) |
| 310 | all_labels = keyvals.get('labels', '') |
| 311 | if all_labels: |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 312 | all_labels = all_labels.split(',') |
Derek Beckett | f73baca | 2020-08-19 15:08:47 -0700 | [diff] [blame] | 313 | return [urllib.parse.unquote(label) for label in all_labels] |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 314 | return [] |
| 315 | |
| 316 | |
jadmanski | ea45566 | 2009-03-25 22:25:39 +0000 | [diff] [blame] | 317 | def delete_tmp_dir(self, tmpdir): |
| 318 | """ |
| 319 | Delete the given temporary directory on the remote machine. |
Richard Barnette | ab9769f | 2016-06-01 15:01:44 -0700 | [diff] [blame] | 320 | |
| 321 | @param tmpdir The directory to delete. |
jadmanski | ea45566 | 2009-03-25 22:25:39 +0000 | [diff] [blame] | 322 | """ |
| 323 | self.run('rm -rf "%s"' % utils.sh_escape(tmpdir), ignore_status=True) |
| 324 | self.tmp_dirs.remove(tmpdir) |
| 325 | |
| 326 | |
Alex Khouderchah | c44e777 | 2018-07-16 10:53:14 -0700 | [diff] [blame] | 327 | def delete_all_tmp_dirs(self, parent='/tmp'): |
| 328 | """ |
| 329 | Delete all directories in parent that were created by get_tmp_dir |
| 330 | |
| 331 | Note that this may involve deleting directories created by calls to |
| 332 | get_tmp_dir on a different RemoteHost instance than the one running this |
| 333 | method. Only perform this operation when certain that this will not |
| 334 | cause unexpected behavior. |
| 335 | """ |
| 336 | # follow mktemp's behavior of only expanding 3 or more consecutive Xs |
Derek Beckett | 24c0e82 | 2020-06-10 12:54:04 -0700 | [diff] [blame] | 337 | if isinstance(parent, (list, tuple)): |
| 338 | parents = parent |
| 339 | else: |
| 340 | parents = [parent] |
| 341 | rm_paths = [] |
| 342 | for parent in parents: |
| 343 | base_template = re.sub('XXXX*', '*', self.TMP_DIR_TEMPLATE) |
| 344 | # distinguish between non-wildcard asterisks in parent directory name |
| 345 | # and wildcards inserted from the template |
Derek Beckett | f73baca | 2020-08-19 15:08:47 -0700 | [diff] [blame] | 346 | base = '*'.join( |
| 347 | ['"%s"' % utils.sh_escape(x) for x in base_template.split('*')]) |
Derek Beckett | 24c0e82 | 2020-06-10 12:54:04 -0700 | [diff] [blame] | 348 | path = '"%s' % os.path.join(utils.sh_escape(parent), base[1:]) |
| 349 | rm_paths.append(path) |
| 350 | # remove deleted directories from tmp_dirs |
| 351 | regex = os.path.join(parent, re.sub('(XXXX*)', |
| 352 | lambda match: '[a-zA-Z0-9]{%d}' % len(match.group(1)), |
| 353 | self.TMP_DIR_TEMPLATE)) |
| 354 | regex += '(/|$)' # remove if matches, or is within a dir that matches |
Derek Beckett | f73baca | 2020-08-19 15:08:47 -0700 | [diff] [blame] | 355 | self.tmp_dirs = [x for x in self.tmp_dirs if not re.match(regex, x)] |
Alex Khouderchah | c44e777 | 2018-07-16 10:53:14 -0700 | [diff] [blame] | 356 | |
Derek Beckett | 24c0e82 | 2020-06-10 12:54:04 -0700 | [diff] [blame] | 357 | self.run('rm -rf {}'.format(" ".join(rm_paths)), ignore_status=True) |
Alex Khouderchah | c44e777 | 2018-07-16 10:53:14 -0700 | [diff] [blame] | 358 | |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 359 | def check_uptime(self): |
| 360 | """ |
| 361 | Check that uptime is available and monotonically increasing. |
| 362 | """ |
mbligh | a43f6d2 | 2009-08-24 22:09:44 +0000 | [diff] [blame] | 363 | if not self.is_up(): |
| 364 | raise error.AutoservHostError('Client does not appear to be up') |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 365 | result = self.run("/bin/cat /proc/uptime", 30) |
| 366 | return result.stdout.strip().split()[0] |
| 367 | |
| 368 | |
Puthikorn Voravootivat | df29d64 | 2014-04-25 11:45:36 -0700 | [diff] [blame] | 369 | def check_for_lkdtm(self): |
| 370 | """ |
| 371 | Check for kernel dump test module. return True if exist. |
| 372 | """ |
| 373 | cmd = 'ls /sys/kernel/debug/provoke-crash/DIRECT' |
| 374 | return self.run(cmd, ignore_status=True).exit_status == 0 |
| 375 | |
| 376 | |
jadmanski | ca7da37 | 2008-10-21 16:26:52 +0000 | [diff] [blame] | 377 | def are_wait_up_processes_up(self): |
mbligh | f2c3376 | 2008-10-18 14:42:34 +0000 | [diff] [blame] | 378 | """ |
| 379 | Checks if any HOSTS waitup processes are running yet on the |
| 380 | remote host. |
| 381 | |
| 382 | Returns True if any the waitup processes are running, False |
| 383 | otherwise. |
| 384 | """ |
| 385 | processes = self.get_wait_up_processes() |
| 386 | if len(processes) == 0: |
| 387 | return True # wait up processes aren't being used |
| 388 | for procname in processes: |
| 389 | exit_status = self.run("{ ps -e || ps; } | grep '%s'" % procname, |
| 390 | ignore_status=True).exit_status |
| 391 | if exit_status == 0: |
| 392 | return True |
| 393 | return False |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 394 | |
| 395 | |
| 396 | def get_labels(self): |
| 397 | """Return a list of labels for this given host. |
| 398 | |
| 399 | This is the main way to retrieve all the automatic labels for a host |
| 400 | as it will run through all the currently implemented label functions. |
| 401 | """ |
| 402 | labels = [] |
| 403 | for label_function in self._LABEL_FUNCTIONS: |
| 404 | try: |
| 405 | label = label_function(self) |
Aviv Keshet | 5ae0a00 | 2017-05-05 10:23:33 -0700 | [diff] [blame] | 406 | except Exception: |
| 407 | logging.exception('Label function %s failed; ignoring it.', |
| 408 | label_function.__name__) |
Kevin Cheng | 3a4a57a | 2015-09-30 12:09:50 -0700 | [diff] [blame] | 409 | label = None |
| 410 | if label: |
| 411 | if type(label) is str: |
| 412 | labels.append(label) |
| 413 | elif type(label) is list: |
| 414 | labels.extend(label) |
| 415 | return labels |
Otabek Kasimov | 480e7fa | 2020-11-23 18:52:23 -0800 | [diff] [blame] | 416 | |
| 417 | def get_result_dir(self): |
| 418 | """Return the result directory path if passed or None if not. |
| 419 | |
| 420 | @return string |
| 421 | """ |
| 422 | if self.job and hasattr(self.job, 'resultdir'): |
| 423 | return self.job.resultdir |
| 424 | return None |