public static function getServers($config_file) { $servers = @parse_ini_file($config_file, true); $return = array(); if (!$servers) { Helpers::error("File '{$config_file}' is not a valid .ini file."); } else { foreach ($servers as $uri => $options) { if (stristr($uri, "://") !== false) { $options = array_merge($options, parse_url($uri)); } # Throw in some default values, in case they're not set. $options = array_merge(array('skip' => false, 'scheme' => 'ftp', 'host' => '', 'user' => '', 'branch' => null, 'port' => 21, 'path' => '/', 'passive' => true, 'clean_directories' => array(), 'ignore_files' => array(), 'ignore_directories' => array(), 'upload_untracked' => array()), $options); if (!isset($options['pass']) && !isset($options['sftp_key'])) { $options['pass'] = self::promptPassword(); } if (isset($options['sftp_key'])) { if (substr($options['sftp_key'], 0, 2) == "~/") { $options['sftp_key'] = $_SERVER['HOME'] . substr($options['sftp_key'], 1); } } if ($options['skip']) { continue; } else { unset($options['skip']); $type = "Brunodebarros\\Gitdeploy\\" . ucfirst(strtolower($options['scheme'])); $return[$uri] = new $type($options, $config_file); } } } return $return; }
protected function exec($command, $suffix = "") { if (chdir($this->repo_path)) { $console = trim(shell_exec(self::$git_executable_path . " " . $command . " 2>&1 " . $suffix)); return $console; } else { Helpers::error("Unable to access the git repository's folder."); } }
public function set_file($file, $contents) { $this->connect(); $file = $file; $dir = explode("/", dirname($file)); $dir_part_count = count($dir); $path = ""; for ($i = 0; $i < $dir_part_count; $i++) { $path .= $dir[$i] . '/'; if (!isset($this->existing_paths_cache[$path])) { $origin = $this->connection->pwd(); if (!$this->connection->chdir($path)) { if (!$this->connection->mkdir($path)) { Helpers::error("Failed to create the directory '{$path}'. Upload to this server cannot continue."); } else { Helpers::logmessage("Created directory: {$path}"); $this->existing_paths_cache[$path] = true; } } else { $this->existing_paths_cache[$path] = true; } $this->connection->chdir($origin); } } if ($this->connection->put($file, $contents)) { Helpers::logmessage("Uploaded: {$file}"); return true; } else { Helpers::error("Failed to upload {$file}. Deployment will stop to allow you to check what went wrong."); } }
public function set_file($file, $contents, $die_if_fail = false) { $this->connect(); # Make sure the folder exists in the FTP server. $dir = explode("/", dirname($file)); $dir_part_count = count($dir); $path = ""; for ($i = 0; $i < $dir_part_count; $i++) { $path .= $dir[$i] . '/'; if (!isset($this->existing_paths_cache[$path])) { $origin = ftp_pwd($this->connection); if (!@ftp_chdir($this->connection, $path)) { if (!@ftp_mkdir($this->connection, $path)) { Helpers::error("Failed to create the directory '{$path}'. Upload to this server cannot continue."); } else { Helpers::logmessage("Created directory: {$path}"); $this->existing_paths_cache[$path] = true; } } else { $this->existing_paths_cache[$path] = true; } ftp_chdir($this->connection, $origin); } } $tmpFile = tempnam(sys_get_temp_dir(), 'GITDEPLOYPHP'); file_put_contents($tmpFile, $contents); $uploaded = ftp_put($this->connection, $file, $tmpFile, FTP_BINARY); if (!$uploaded) { if ($die_if_fail) { Helpers::error("Failed to upload {$file}. Deployment will stop to allow you to check what went wrong."); } else { # Try deleting the file and reuploading. # This resolves a CHMOD issue with some FTP servers. $this->unset_file($file); $this->set_file($file, $contents, true); } } else { Helpers::logmessage("Uploaded: {$file}"); return true; } }
public function revert($git, $list_only = false) { $target_commit = $this->get_file('PREVIOUS_REVISION', true); if (empty($target_commit)) { Helpers::error("Cannot revert: {$this->host} server has no PREVIOUS_REVISION file."); } else { $this->deploy($git, $target_commit, true, $list_only); } }