This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
/
BaseDeployCommand.php
154 lines (134 loc) · 4.39 KB
/
BaseDeployCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Rocketeer\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
/**
* A basic deploy command with helpers
*/
abstract class BaseDeployCommand extends Command
{
/**
* Run the tasks
*
* @return void
*/
abstract public function fire();
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('pretend', 'p', InputOption::VALUE_NONE, 'Returns an array of commands to be executed instead of actually executing them'),
array('on', 'C', InputOption::VALUE_REQUIRED, 'The connection(s) to execute the Task in'),
array('stage', 'S', InputOption::VALUE_REQUIRED, 'The stage to execute the Task in')
);
}
/**
* Returns the command name.
*
* @return string The command name
*/
public function getName()
{
// Return commands without namespace if standalone
if (!isset($this->laravel['events'])) {
return str_replace('deploy:', null, $this->name);
}
return $this->name;
}
////////////////////////////////////////////////////////////////////
///////////////////////////// CORE METHODS /////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Fire a Tasks Queue
*
* @param string|array $tasks
*
* @return mixed
*/
protected function fireTasksQueue($tasks)
{
// Check for credentials
$this->getServerCredentials();
$this->getRepositoryCredentials();
// Start timer
$timerStart = microtime(true);
// Convert tasks to array if necessary
if (!is_array($tasks)) {
$tasks = array($tasks);
}
// Run tasks and display timer
$this->laravel['rocketeer.tasks']->run($tasks, $this);
$this->line('Execution time: <comment>'.round(microtime(true) - $timerStart, 4). 's</comment>');
}
/**
* Get the Repository's credentials
*
* @return void
*/
protected function getRepositoryCredentials()
{
// Check for repository credentials
$repositoryInfos = $this->laravel['rocketeer.rocketeer']->getCredentials();
$credentials = array('repository');
if (!array_get($repositoryInfos, 'repository') or $this->laravel['rocketeer.rocketeer']->needsCredentials()) {
$credentials = array('repository', 'username', 'password');
}
// Gather credentials
foreach ($credentials as $credential) {
${$credential} = array_get($repositoryInfos, $credential);
if (!${$credential}) {
${$credential} = $this->ask('No '.$credential. ' is set for the repository, please provide one :');
}
}
// Save them
$credentials = compact($credentials);
$this->laravel['rocketeer.server']->setValue('credentials', $credentials);
foreach ($credentials as $key => $credential) {
$this->laravel['config']->set('rocketeer::scm.'.$key, $credential);
}
}
/**
* Get the Server's credentials
*
* @return void
*/
protected function getServerCredentials()
{
if ($connections = $this->option('on')) {
$this->laravel['rocketeer.rocketeer']->setConnections($connections);
}
// Check for configured connections
$connections = $this->laravel['rocketeer.rocketeer']->getAvailableConnections();
$connectionName = $this->laravel['rocketeer.rocketeer']->getConnection();
if (is_null($connectionName)) {
$connectionName = $this->ask('No connections have been set, please create one : (production)', 'production');
}
// Check for server credentials
$connection = array_get($connections, $connectionName, array());
$credentials = array('host' => true, 'username' => true, 'password' => false, 'key' => false);
// Gather credentials
foreach ($credentials as $credential => $required) {
${$credential} = array_get($connection, $credential);
if (!${$credential} and $required) {
${$credential} = $this->ask('No '.$credential. ' is set for [' .$connectionName. '], please provide one :');
}
}
// Get password or key
if (!$password and !$key) {
$type = $this->ask('No password or SSH key is set for [' .$connectionName. '], which would you use ? [key/password]');
if ($type == 'key') {
$key = $this->ask('Please enter the full path to your key');
} else {
$password = $this->ask('Please enter your password');
}
}
// Save them
$credentials = compact(array_keys($credentials));
$this->laravel['rocketeer.server']->setValue('connections.'.$connectionName, $credentials);
$this->laravel['config']->set('remote.connections.'.$connectionName, $credentials);
}
}