def _Run(self, command, success_codes, reboot_codes, restart_retry): c = cache.Cache() logging.debug('Interpreting command %s', command) try: command = c.CacheFromLine(command, self._build_info) except cache.CacheError as e: raise ActionError(e) logging.info('Executing command %s', command) try: result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: output = result.stdout.readline() if not output and result.poll() is not None: break if output: logging.info(output.strip().decode('UTF-8')) except WindowsError as e: # pylint: disable=undefined-variable raise ActionError('Failed to execute command %s (%s)' % (command, str(e))) except KeyboardInterrupt: logging.debug('Child received KeyboardInterrupt. Ignoring.') if result.returncode in reboot_codes: raise RestartEvent( 'Restart triggered by exit code %d' % result.returncode, 5, retry_on_restart=restart_retry) elif result.returncode not in success_codes: raise ActionError('Command returned invalid exit code %d' % result.returncode) time.sleep(5)
def _Run(self, command: Text, success_codes: List[int], reboot_codes: List[int], restart_retry: bool, shell: bool): logging.debug('Interpreting command: %s', command) try: command_cache = cache.Cache().CacheFromLine(command, self._build_info) except cache.CacheError as e: raise ActionError(e) try: command_list = shlex.split(command_cache, posix=False) result = execute.execute_binary( command_list[0], command_list[1:], success_codes + reboot_codes, shell=shell) except (execute.Error, ValueError) as e: raise ActionError(e) except KeyboardInterrupt: raise ActionError('KeyboardInterrupt detected, exiting.') if result in reboot_codes: raise RestartEvent( 'Restart triggered by exit code: %d' % result, 5, retry_on_restart=restart_retry) elif result not in success_codes: raise ActionError('Command returned invalid exit code: %d' % result)
def setUp(self): self.cache = cache.Cache() fs = fake_filesystem.FakeFilesystem() fs.CreateDirectory(r'C:\Directory') os_module = fake_filesystem.FakeOsModule(fs) self.mock_open = fake_filesystem.FakeFileOpen(fs) cache.os = os_module cache.open = self.mock_open
def setUp(self): super(CacheTest, self).setUp() self.cache = cache.Cache() fs = fake_filesystem.FakeFilesystem() fs.create_dir(r'C:\Directory') os_module = fake_filesystem.FakeOsModule(fs) self.mock_open = fake_filesystem.FakeFileOpen(fs) cache.os = os_module cache.open = self.mock_open
def Cache(self): """The local build cache. Returns: An instance of the Cache class. """ if not self._cache: self._cache = cache.Cache() return self._cache
def Run(self): command: List[str] = self._args[0].split() success_codes: List[int] = [0] reboot_codes: List[int] = [] restart_retry: bool = False shell: bool = False log: bool = True if len(self._args) > 1: success_codes = self._args[1] if len(self._args) > 2: reboot_codes = self._args[2] if len(self._args) > 3: restart_retry = self._args[3] if len(self._args) > 4: shell = self._args[4] if len(self._args) > 5: log = self._args[5] # TODO: Remove once updated PowerShell is used in the image. # PSScript (which calls powershell.exe -File) does not accept non-string # parameters. Instead, if the command string starts with a PowerShell # script, cache it's location and run the script using powershell.exe # -Command. See link below for more context. # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1#-file----filepath-args if command[0] and command[0].endswith('.ps1'): logging.info('Interpreting PowerShell script: %s', command[0]) try: command[0] = cache.Cache().CacheFromLine( command[0], self._build_info) except cache.CacheError as e: raise ActionError(e) try: # Exit $LASTEXITCODE is necessary because PowerShell.exe -Command only # exits 0 or 1 by default. result = powershell.PowerShell(shell, log).RunCommand( command + ['; exit $LASTEXITCODE'], success_codes + reboot_codes) except powershell.PowerShellError as e: raise ActionError(str(e)) if result in reboot_codes: raise RestartEvent('Restart triggered by exit code %d' % result, 5, retry_on_restart=restart_retry) elif result not in success_codes: raise ActionError('Command returned invalid exit code %d' % result)
def Run(self): script = self._args[0] ps_args = None if len(self._args) > 1: ps_args = self._args[1] ps = powershell.PowerShell(echo_off=True) c = cache.Cache() logging.debug('Interpreting Powershell script %s', script) try: script = c.CacheFromLine(script, self._build_info) except cache.CacheError as e: raise ActionError(e) try: ps.RunLocal(script, args=ps_args) except powershell.PowerShellError as e: raise ActionError('Failure executing Powershell script. [%s]' % e)
def Run(self): script: str = self._args[0] ps_args: List[str] = [] success_codes: List[int] = [0] reboot_codes: List[int] = [] restart_retry: bool = False shell: bool = False log: bool = True if len(self._args) > 1: ps_args = self._args[1] if len(self._args) > 2: success_codes = self._args[2] if len(self._args) > 3: reboot_codes = self._args[3] if len(self._args) > 4: restart_retry = self._args[4] if len(self._args) > 5: shell = self._args[5] if len(self._args) > 6: log = self._args[6] logging.info('Interpreting PowerShell script: %s', script) try: script = cache.Cache().CacheFromLine(script, self._build_info) # pytype: disable=annotation-type-mismatch except cache.CacheError as e: raise ActionError(e) try: result = powershell.PowerShell(shell, log).RunLocal( script, ps_args, success_codes + reboot_codes) except powershell.PowerShellError as e: raise ActionError(str(e)) if result in reboot_codes: raise RestartEvent('Restart triggered by exit code %d' % result, 5, retry_on_restart=restart_retry) elif result not in success_codes: raise ActionError('Script returned invalid exit code %d' % result)
def _Run(self, command, success_codes, reboot_codes, restart_retry): result = 0 c = cache.Cache() logging.debug('Interpreting command %s', command) try: command = c.CacheFromLine(command, self._build_info) except cache.CacheError as e: raise ActionError(e) logging.info('Executing command %s', command) try: result = subprocess.call(command, shell=True) except WindowsError as e: # pylint: disable=undefined-variable raise ActionError('Failed to execute command %s (%s)' % (command, str(e))) except KeyboardInterrupt: logging.debug('Child received KeyboardInterrupt. Ignoring.') if result in reboot_codes: raise RestartEvent('Restart triggered by exit code %d' % result, 5, retry_on_restart=restart_retry) elif result not in success_codes: raise ActionError('Command returned invalid exit code %d' % result) time.sleep(5)