def RestoreBackup(self): """Restore the backup from this install state if it exists. If this installation has a backup stored in it (created by and update that used ReplaceWith(), above), it replaces this installation with the backup, using a temporary staging area. This installation is moved to the trash directory under the installation that exists after this is done. The trash directory can be removed at any point in the future. We just don't want to delete code that is running since some platforms have a problem with that. Returns: bool, True if there was a backup to restore, False otherwise. """ if not self.HasBackup(): return False self._ClearStaging() file_utils.MoveDir(self.__backup_directory, self.__sdk_staging_root) staging_state = InstallationState(self.__sdk_staging_root) # pylint: disable=protected-access, This is an instance of InstallationState staging_state._CreateStateDir() staging_state.ClearTrash() # pylint: disable=protected-access, This is an instance of InstallationState file_utils.MoveDir(self.__sdk_root, staging_state.__trash_directory) file_utils.MoveDir(staging_state.__sdk_root, self.__sdk_root) return True
def ReplaceWith(self, other_install_state, progress_callback=None): """Replaces this installation with the given other installation. This moves the current installation to the backup directory of the other installation. Then, it moves the entire second installation to replace this one on the file system. The result is that the other installation completely replaces the current one, but the current one is snapshotted and stored as a backup under the new one (and can be restored later). Args: other_install_state: InstallationState, The other state with which to replace this one. progress_callback: f(float), A function to call with the fraction of completeness. """ self._CreateStateDir() self.ClearBackup() self.ClearTrash() # pylint: disable=protected-access, This is an instance of InstallationState other_install_state._CreateStateDir() other_install_state.ClearBackup() # pylint: disable=protected-access, This is an instance of InstallationState file_utils.MoveDir(self.__sdk_root, other_install_state.__backup_directory) if progress_callback: progress_callback(0.5) file_utils.MoveDir(other_install_state.__sdk_root, self.__sdk_root) if progress_callback: progress_callback(1.0)
def CreateStagingFromDownload(self, url, progress_callback=None): """Creates a new staging area from a fresh download of the Cloud SDK. Args: url: str, The url to download the new SDK from. progress_callback: f(float), A function to call with the fraction of completeness. Returns: An InstallationState object for the new install. Raises: installers.URLFetchError: If the new SDK could not be downloaded. InvalidDownloadError: If the new SDK was malformed. """ self._ClearStaging() with file_utils.TemporaryDirectory() as t: download_dir = os.path.join(t, '.download') extract_dir = os.path.join(t, '.extract') installers.DownloadAndExtractTar( url, download_dir, extract_dir, progress_callback=progress_callback, command_path='components.reinstall') files = os.listdir(extract_dir) if len(files) != 1: raise InvalidDownloadError() sdk_root = os.path.join(extract_dir, files[0]) file_utils.MoveDir(sdk_root, self.__sdk_staging_root) staging_sdk = InstallationState(self.__sdk_staging_root) # pylint: disable=protected-access, This is an instance of InstallationState staging_sdk._CreateStateDir() self.CopyMachinePropertiesTo(staging_sdk) return staging_sdk
def CreateStagingFromDownload(self, url): """Creates a new staging area from a fresh download of the Cloud SDK. Args: url: str, The url to download the new SDK from. Returns: An InstallationState object for the new install. Raises: installers.URLFetchError: If the new SDK could not be downloaded. InvalidDownloadError: If the new SDK was malformed. """ self._ClearStaging() with file_utils.TemporaryDirectory() as t: download_dir = os.path.join(t, '.download') extract_dir = os.path.join(t, '.extract') installers.ComponentInstaller.DownloadAndExtractTar( url, download_dir, extract_dir) files = os.listdir(extract_dir) if len(files) != 1: raise InvalidDownloadError() sdk_root = os.path.join(extract_dir, files[0]) file_utils.MoveDir(sdk_root, self.__sdk_staging_root) staging_sdk = InstallationState(self.__sdk_staging_root) self.CopyMachinePropertiesTo(staging_sdk) return staging_sdk