Source of file DNDataTransfer.php
Size: 6,628 Bytes - Last Modified: 2021-12-23T10:29:15+00:00
/var/www/docs.ssmods.com/process/src/code/model/jobs/DNDataTransfer.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | <?php /** * Class representing a single data transfer in a project, * which can include a database export, an archive of all assets, or both. * * It can be one of two directions: * - Backup: Package up data on an environment and store it in a local file * - Restore: Transfer data from a local file into an environment, extract assets and/or restore a database * * The choice of database and/or assets is represented in the "Mode". * There's always one file archive involved (stored as the has_one "ArchiveFile") on the local Deploynaut environment. * * Each transfer is executed by a Resque job, so the model also contains * a reference to a Resque token (which might still be in progress). * * The "Environment" points to the source or target involved. * * @method DNDataArchive DataArchive() */ class DNDataTransfer extends DataObject { private static $db = array( "ResqueToken" => "Varchar(255)", // Observe that this is not the same as Resque status, since ResqueStatus is not persistent. "Status" => "Enum('Queued, Started, Finished, Failed, n/a', 'n/a')", "Direction" => "Enum('get, push', 'get')", "Mode" => "Enum('all, assets, db', '')", "Origin" => "Enum('EnvironmentTransfer,ManualUpload', 'EnvironmentTransfer')", ); private static $has_one = array( "Environment" => "DNEnvironment", "Author" => "Member", "DataArchive" => "DNDataArchive", "BackupDataTransfer" => "DNDataTransfer" // denotes an automated backup done for a push of this data transfer ); private static $singular_name = 'Data Transfer'; private static $plural_name = 'Data Transfers'; private static $summary_fields = array( 'Created' => 'Created', 'Author.Title' => 'Author', 'Environment.Project.Name' => 'Project', 'Environment.Name' => 'Environment', 'Status' => 'Status', 'Origin' => 'Origin', ); private static $searchable_fields = array( 'Environment.Project.Name' => array( 'title' => 'Project', ), 'Environment.Name' => array( 'title' => 'Environment', ), 'Status' => array( 'title' => 'Status', ), 'Origin' => array( 'title' => 'Origin', ), 'Mode' => array( 'title' => 'Mode', ), 'Direction' => array( 'title' => 'Direction', ), ); /** * When running the transfer, should a backup be performed before pushing the data? * @var bool */ protected $backupBeforePush = true; public function setBackupBeforePush($value) { $this->backupBeforePush = $value; } public function getTitle() { return $this->dbObject('Created')->Nice() . " (Status: {$this->Status})"; } public function Link() { return Controller::join_links($this->Environment()->Project()->Link(), 'transfer', $this->ID); } public function LogLink() { return $this->Link() . '/log'; } public function getDefaultSearchContext() { $context = parent::getDefaultSearchContext(); $context->getFields()->dataFieldByName('Status')->setHasEmptyDefault(true); $context->getFields()->dataFieldByName('Origin')->setHasEmptyDefault(true); return $context; } public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName('EnvironmentID'); $fields->removeByName('ArchiveFile'); $fields->addFieldsToTab( 'Root.Main', array( new ReadonlyField('ProjectName', 'Project', $this->Environment()->Project()->Name), new ReadonlyField('EnvironmentName', 'Environment', $this->Environment()->Name), new ReadonlyField( 'DataArchive', 'Archive File', sprintf( '<a href="%s">%s</a>', $this->DataArchive()->ArchiveFile()->AbsoluteURL, $this->DataArchive()->ArchiveFile()->Filename ) ), ) ); $linkField->dontEscape = true; $fields = $fields->makeReadonly(); return $fields; } /** * Queue a transfer job */ public function start() { $env = $this->Environment(); $log = $this->log(); $args = array( 'dataTransferID' => $this->ID, 'logfile' => $this->logfile(), 'backupBeforePush' => $this->backupBeforePush ); if(!$this->AuthorID) { $this->AuthorID = Member::currentUserID(); } if($this->AuthorID) { $author = $this->Author(); $message = sprintf( 'Data transfer on %s (%s, %s) initiated by %s (%s), with IP address %s', $env->getFullName(), $this->Direction, $this->Mode, $author->getName(), $author->Email, Controller::curr()->getRequest()->getIP() ); $log->write($message); } $token = Resque::enqueue('git', 'DataTransferJob', $args, true); $this->ResqueToken = $token; $this->write(); $message = sprintf('Data transfer queued as job %s', $token); $log->write($message); } /** * * @param Member $member * @return bool */ public function canView($member = null) { return $this->Environment()->canView($member); } /** * Return a path to the log file. * @return string */ protected function logfile() { return sprintf( '%s.datatransfer.%s.log', $this->Environment()->getFullName('.'), $this->ID ); } /** * * @return \DeploynautLogFile */ public function log() { return new DeploynautLogFile($this->logfile()); } /** * * @return string */ public function LogContent() { return $this->log()->content(); } public function getDescription() { $envName = $this->Environment()->FullName; if($this->Direction == 'get') { if($this->Origin == 'ManualUpload') { $description = 'Manual upload of ' . $this->getModeNice() . ' to ' . $envName; } elseif($this->IsBackupDataTransfer()) { $description = 'Automated backup of ' . $this->getModeNice() . ' from ' . $envName; } else { $description = 'Backup of ' . $this->getModeNice() . ' to ' . $envName; } } else { $description = 'Restore ' . $this->getModeNice() . ' to ' . $envName; } return $description; } public function getModeNice() { if($this->Mode == 'all') { return 'database and assets'; } else { return $this->Mode; } } /** * Is this transfer an automated backup of a push transfer? * @return boolean */ public function IsBackupDataTransfer() { return DB::query(sprintf( 'SELECT COUNT("ID") FROM "DNDataTransfer" WHERE "BackupDataTransferID" = %d', $this->ID ))->value(); } /** * * @return string */ public function ResqueStatus() { $status = new Resque_Job_Status($this->ResqueToken); $remap = array( Resque_Job_Status::STATUS_WAITING => "Queued", Resque_Job_Status::STATUS_RUNNING => "Running", Resque_Job_Status::STATUS_FAILED => "Failed", Resque_Job_Status::STATUS_COMPLETE => "Complete", false => "Invalid", ); return $remap[$status->get()]; } } |