xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 1 | # Copyright (c) 2016 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """A progress class for tracking CrOS auto-update process. |
| 6 | |
| 7 | This class is mainly designed for: |
| 8 | 1. Set the pattern for generating the filenames of |
| 9 | track_status_file/execute_log_file. |
| 10 | track_status_file: Used for record the current step of CrOS auto-update |
| 11 | process. Only has one line. |
| 12 | execute_log_file: Used for record the whole logging info of the CrOS |
| 13 | auto-update process, including any debug information. |
| 14 | 2. Write current auto-update process into the track_status_file. |
| 15 | 3. Read current auto-update process from the track_status_file. |
| 16 | |
| 17 | This file also offers external functions that are related to add/check/delete |
| 18 | the progress of the CrOS auto-update process. |
| 19 | """ |
| 20 | |
| 21 | from __future__ import print_function |
| 22 | |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 23 | import glob |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 24 | import logging |
| 25 | import os |
| 26 | |
xixuan | cf58dd3 | 2016-08-24 13:57:06 -0700 | [diff] [blame] | 27 | # only import setup_chromite before chromite import. |
| 28 | import setup_chromite # pylint: disable=unused-import |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 29 | try: |
| 30 | from chromite.lib import osutils |
| 31 | except ImportError as e: |
| 32 | logging.debug('chromite cannot be imported: %r', e) |
| 33 | osutils = None |
| 34 | |
| 35 | # Path for status tracking log. |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 36 | _TRACK_LOG_FILE_PATH = '/tmp/auto-update/tracking_log/%s_%s.log' |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 37 | |
| 38 | # Path for executing log. |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 39 | _EXECUTE_LOG_FILE_PATH = '/tmp/auto-update/executing_log/%s_%s.log' |
| 40 | |
| 41 | # Path and files for temporarily saving devserver codes, devserver and |
| 42 | # update engine log. |
| 43 | _CROS_UPDATE_TEMP_PATH = '/tmp/cros-update_%s_%s' |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 44 | |
| 45 | # The string for update process finished |
| 46 | FINISHED = 'Completed' |
| 47 | ERROR_TAG = 'Error' |
| 48 | |
| 49 | |
| 50 | def ReadOneLine(filename): |
| 51 | """Read one line from file. |
| 52 | |
| 53 | Args: |
| 54 | filename: The file to be read. |
| 55 | """ |
| 56 | return open(filename, 'r').readline().rstrip('\n') |
| 57 | |
| 58 | |
| 59 | def IsProcessAlive(pid): |
| 60 | """Detect whether a process is alive or not. |
| 61 | |
| 62 | Args: |
| 63 | pid: The process id. |
| 64 | """ |
| 65 | path = '/proc/%s/stat' % pid |
| 66 | try: |
| 67 | stat = ReadOneLine(path) |
| 68 | except IOError: |
| 69 | if not os.path.exists(path): |
| 70 | return False |
| 71 | |
| 72 | raise |
| 73 | |
| 74 | return stat.split()[2] != 'Z' |
| 75 | |
| 76 | |
| 77 | def GetExecuteLogFile(host_name, pid): |
| 78 | """Return the whole path of execute log file.""" |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 79 | if not os.path.exists(os.path.dirname(_EXECUTE_LOG_FILE_PATH)): |
| 80 | osutils.SafeMakedirs(os.path.dirname(_EXECUTE_LOG_FILE_PATH)) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 81 | |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 82 | return _EXECUTE_LOG_FILE_PATH % (host_name, pid) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def GetTrackStatusFile(host_name, pid): |
| 86 | """Return the whole path of track status file.""" |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 87 | if not os.path.exists(os.path.dirname(_TRACK_LOG_FILE_PATH)): |
| 88 | osutils.SafeMakedirs(os.path.dirname(_TRACK_LOG_FILE_PATH)) |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 89 | |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 90 | return _TRACK_LOG_FILE_PATH % (host_name, pid) |
| 91 | |
| 92 | |
| 93 | def GetAllTrackStatusFileByHostName(host_name): |
| 94 | """Return a list of existing track status files generated for a host.""" |
| 95 | return glob.glob(_TRACK_LOG_FILE_PATH % (host_name, '*')) |
| 96 | |
| 97 | |
| 98 | def GetAUTempDirectory(host_name, pid): |
| 99 | """Return the temp dir for storing codes and logs during auto-update.""" |
| 100 | au_tempdir = _CROS_UPDATE_TEMP_PATH % (host_name, pid) |
| 101 | if not os.path.exists(au_tempdir): |
| 102 | osutils.SafeMakedirs(au_tempdir) |
| 103 | |
| 104 | return au_tempdir |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 105 | |
| 106 | |
xixuan | 1bbfaba | 2016-10-13 17:53:22 -0700 | [diff] [blame] | 107 | def ReadExecuteLogFile(host_name, pid): |
| 108 | """Return the content of execute log file.""" |
| 109 | return osutils.ReadFile(GetExecuteLogFile(host_name, pid)) |
| 110 | |
| 111 | |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 112 | def DelTrackStatusFile(host_name, pid): |
| 113 | """Delete the track status log.""" |
| 114 | osutils.SafeUnlink(GetTrackStatusFile(host_name, pid)) |
| 115 | |
| 116 | |
xixuan | 1bbfaba | 2016-10-13 17:53:22 -0700 | [diff] [blame] | 117 | def DelExecuteLogFile(host_name, pid): |
| 118 | """Delete the track status log.""" |
| 119 | osutils.SafeUnlink(GetExecuteLogFile(host_name, pid)) |
| 120 | |
| 121 | |
xixuan | 3bc974e | 2016-10-18 17:21:43 -0700 | [diff] [blame] | 122 | def DelAUTempDirectory(host_name, pid): |
| 123 | """Delete the directory including auto-update-related logs.""" |
| 124 | osutils.RmDir(GetAUTempDirectory(host_name, pid)) |
| 125 | |
| 126 | |
xixuan | 52c2fba | 2016-05-20 17:02:48 -0700 | [diff] [blame] | 127 | class AUProgress(object): |
| 128 | """Used for tracking the CrOS auto-update progress.""" |
| 129 | |
| 130 | def __init__(self, host_name, pid): |
| 131 | """Initialize a CrOS update progress instance. |
| 132 | |
| 133 | Args: |
| 134 | host_name: The name of host, should be in the file_name of the status |
| 135 | tracking file of auto-update process. |
| 136 | pid: The process id, should be in the file_name too. |
| 137 | """ |
| 138 | self.host_name = host_name |
| 139 | self.pid = pid |
| 140 | |
| 141 | @property |
| 142 | def track_status_file(self): |
| 143 | """The track status file to record the CrOS auto-update progress.""" |
| 144 | return GetTrackStatusFile(self.host_name, self.pid) |
| 145 | |
| 146 | def WriteStatus(self, content): |
| 147 | """Write auto-update progress into status tracking file. |
| 148 | |
| 149 | Args: |
| 150 | content: The content to be recorded. |
| 151 | """ |
| 152 | if not self.track_status_file: |
| 153 | return |
| 154 | |
| 155 | try: |
| 156 | with open(self.track_status_file, 'w') as out_log: |
| 157 | out_log.write(content) |
| 158 | except Exception as e: |
| 159 | logging.error('Cannot write au status: %r', e) |
| 160 | |
| 161 | def ReadStatus(self): |
| 162 | """Read auto-update progress from status tracking file.""" |
xixuan | 28d9907 | 2016-10-06 12:24:16 -0700 | [diff] [blame] | 163 | with open(self.track_status_file, 'r') as out_log: |
| 164 | return out_log.read().rstrip('\n') |