1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Parent class for managing a set of Deploynaut data |
5
|
|
|
*/ |
6
|
|
|
class DNData extends ViewableData { |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Path where the environment configurations can be found. |
10
|
|
|
* |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $environmentDir = ''; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Path where the keys are stored. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $keyDir = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Path where the signal files are created. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $signalDir = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Path where data transfers are stored. |
31
|
|
|
* Needs to be relative to webroot, and start with assets/ |
32
|
|
|
* since all files are also referenced in the SilverStripe database |
33
|
|
|
* through {@link File}. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $dataTransferDir = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* If set, this provides an alternate user to the current one |
41
|
|
|
* executing the Git shell commands. e.g. if this is set to |
42
|
|
|
* a user called "composer", any Git commands that Deploynaut |
43
|
|
|
* will perform will be run as "sudo -u composer git ..." |
44
|
|
|
* |
45
|
|
|
* @var string|null |
46
|
|
|
*/ |
47
|
|
|
protected $gitUser = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets the DNData singleton |
51
|
|
|
* |
52
|
|
|
* @return DNData |
53
|
|
|
*/ |
54
|
|
|
public static function inst() { |
55
|
|
|
return Injector::inst()->get('DNData'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function __construct($environmentDir = null, $keyDir = null, $signalDir = null, $dataTransferDir = null, $gitUser = null) { |
|
|
|
|
59
|
|
|
parent::__construct(); |
60
|
|
|
|
61
|
|
|
// Better to use injector to set these |
62
|
|
|
if(func_num_args() == 0) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
Deprecation::notice( |
66
|
|
|
'1.2.0', |
67
|
|
|
"Don't construct DNData with parameters. Assign settings via properties instead" |
68
|
|
|
); |
69
|
|
|
$this->setEnvironmentDir($environmentDir); |
70
|
|
|
$this->setKeyDir($keyDir); |
71
|
|
|
$this->setSignalDir($signalDir); |
72
|
|
|
$this->setDataTransferDir($dataTransferDir); |
73
|
|
|
$this->setGitUser($gitUser); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the directory environment code is saved |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getEnvironmentDir() { |
82
|
|
|
return $this->environmentDir; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set the directory environment code is saved |
87
|
|
|
* |
88
|
|
|
* @param string $environmentDir |
89
|
|
|
*/ |
90
|
|
|
public function setEnvironmentDir($environmentDir) { |
91
|
|
|
if($environmentDir[0] != "/") { |
92
|
|
|
$environmentDir = BASE_PATH . '/' . $environmentDir; |
93
|
|
|
} |
94
|
|
|
$this->environmentDir = realpath($environmentDir) ?: $environmentDir; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the directory where ssh are stored |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getKeyDir() { |
103
|
|
|
return $this->keyDir; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set the directory where ssh are stored |
108
|
|
|
* |
109
|
|
|
* @param string $keyDir |
110
|
|
|
*/ |
111
|
|
|
public function setKeyDir($keyDir) { |
112
|
|
|
if($keyDir[0] != "/") { |
113
|
|
|
$keyDir = BASE_PATH . '/' . $keyDir; |
114
|
|
|
} |
115
|
|
|
$this->keyDir = realpath($keyDir) ?: $keyDir; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getSignalDir() { |
122
|
|
|
return $this->signalDir; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $signalDir |
127
|
|
|
*/ |
128
|
|
|
public function setSignalDir($signalDir) { |
129
|
|
|
if($signalDir[0] != "/") { |
130
|
|
|
$signalDir = BASE_PATH . '/' . $signalDir; |
131
|
|
|
} |
132
|
|
|
$this->signalDir = realpath($signalDir) ?: $signalDir; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the username that git commands should be run as |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getGitUser() { |
141
|
|
|
return $this->gitUser; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the username that git commands should be run as |
146
|
|
|
* |
147
|
|
|
* @param string $user |
148
|
|
|
*/ |
149
|
|
|
public function setGitUser($user) { |
150
|
|
|
$this->gitUser = $user; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the directory where data transfers should be saved |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getDataTransferDir() { |
159
|
|
|
return $this->dataTransferDir; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the directory where data transfers should be saved |
164
|
|
|
* |
165
|
|
|
* This should either be an absolute path (beginning with /) or a path that can |
166
|
|
|
* be appended to the web root safely |
167
|
|
|
* |
168
|
|
|
* @param string $transferDir |
169
|
|
|
*/ |
170
|
|
|
public function setDataTransferDir($transferDir) { |
171
|
|
|
if($transferDir[0] != "/") { |
172
|
|
|
$transferDir = BASE_PATH . '/' . $transferDir; |
173
|
|
|
} |
174
|
|
|
if(strpos($transferDir, ASSETS_PATH) === false) { |
175
|
|
|
throw new LogicException(sprintf( |
176
|
|
|
'DNData::dataTransferDir needs to be located within <webroot>assets/ (location: %s)', |
177
|
|
|
$transferDir |
178
|
|
|
)); |
179
|
|
|
} |
180
|
|
|
$this->dataTransferDir = realpath($transferDir) ?: $transferDir; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Provide a list of all projects. |
185
|
|
|
* @return DataList |
186
|
|
|
*/ |
187
|
|
|
public function DNProjectList() { |
188
|
|
|
return DNProject::get(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Grabs a list of projects from the env directory. The projects |
193
|
|
|
* in the builds directory alone will not be picked up. |
194
|
|
|
* Returns an array of paths |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
* @throws Exception |
198
|
|
|
*/ |
199
|
|
|
public function getProjectPaths() { |
200
|
|
|
$paths = array(); |
201
|
|
|
if(!file_exists($this->getEnvironmentDir())) { |
202
|
|
|
$eMessage = 'The environment directory ' . $this->getEnvironmentDir() . ' doesn\'t exist. Create it ' |
203
|
|
|
. 'first and add some projects to it.'; |
204
|
|
|
throw new Exception($eMessage); |
205
|
|
|
} |
206
|
|
|
foreach(scandir($this->getEnvironmentDir()) as $project) { |
207
|
|
|
// Exlcude dot-prefixed directories (.git was getting in the way) |
208
|
|
|
if(preg_match('/^[^\.]/', $project)) { |
209
|
|
|
$path = $this->getEnvironmentDir() . '/' . $project; |
210
|
|
|
if(is_dir($path) && $project != '.' && $project != '..') { |
211
|
|
|
$paths[] = $project; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
sort($paths); |
216
|
|
|
return array_values($paths); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Scan the directory and enumerate all envs founds within. |
221
|
|
|
* Returns an array of paths |
222
|
|
|
*/ |
223
|
|
|
public function getEnvironmentPaths($project) { |
224
|
|
|
$baseDir = $this->getEnvironmentDir() . '/' . $project; |
225
|
|
|
|
226
|
|
|
$paths = array(); |
227
|
|
|
if(!file_exists($baseDir)) { |
228
|
|
|
throw new Exception('Environment directory ' . $baseDir . ' doesn\'t exist. Create it first.'); |
229
|
|
|
} |
230
|
|
|
// Search the directory for config files. |
231
|
|
|
foreach(scandir($baseDir) as $environmentFile) { |
232
|
|
|
if(preg_match('/\.rb$/', $environmentFile)) { |
233
|
|
|
// Config found, wrap it into an object. |
234
|
|
|
$paths[] = "$baseDir/$environmentFile"; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
sort($paths); |
238
|
|
|
return array_values($paths); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.