forked from jgmullor/AzureDistributionBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceDefinition.php
316 lines (270 loc) · 10 KB
/
ServiceDefinition.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
* WindowsAzure DistributionBundle
*
* LICENSE
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/
namespace WindowsAzure\DistributionBundle\Deployment;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;
/**
* Wraps the ServiceDefinition.csdef file and allows convenient access.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class ServiceDefinition
{
/**
* @var string
*/
private $serviceDefinitionFile;
/**
* @var DOMDocument
*/
private $dom;
/**
* @var array
*/
private $roleFiles = array();
/**
* @param string $serviceDefinitionFile
*/
public function __construct($serviceDefinitionFile, array $roleFiles = array())
{
if (!file_exists($serviceDefinitionFile)) {
throw new \InvalidArgumentException(sprintf(
"No valid file-path given. The ServiceDefinition should be at %s but could not be found.",
$serviceDefinitionFile
));
}
$this->serviceDefinitionFile = $serviceDefinitionFile;
$this->dom = new \DOMDocument('1.0', 'UTF-8');
$this->dom->load($this->serviceDefinitionFile);
$this->mergeRoleFilesConfig($roleFiles);
}
private function mergeRoleFilesConfig($roleFiles)
{
$this->roleFiles = array(
'ignoreVCS' => (isset($roleFiles['ignoreVCS'])) ? $roleFiles['ignoreVCS'] : true,
'exclude' => array('build', 'cache', 'logs', 'tests', 'Tests', 'docs', 'test-suite', 'role_template'),
'notName' => array('#(.*)\.swp$#')
);
if (isset($roleFiles['exclude'])) {
$this->roleFiles['exclude'] = array_merge($this->roleFiles['exclude'], $roleFiles['exclude']);
}
if (isset($roleFiles['include'])) {
foreach ($roleFiles['include'] as $include) {
$key = array_search($include, $this->roleFiles['exclude']);
if ($key !== false) {
unset($this->roleFiles[$key]);
}
}
}
if (isset($roleFiles['ignorePatterns'])) {
$this->roleFiles['notName'] = array_merge($this->roleFiles['notName'], $roleFiles['ignorePatterns']);
}
}
public function getPath()
{
return $this->serviceDefinitionFile;
}
public function getNewBuildNumber()
{
$dir = dirname($this->getPath());
$buildFile = $dir . DIRECTORY_SEPARATOR . "build.number";
if (!file_exists($buildFile)) {
file_put_contents($buildFile, "0");
}
$buildNumber = (int)file_get_contents($buildFile);
$buildNumber++;
file_put_contents($buildFile, (string)$buildNumber);
// TODO: Refactor
$parametersAzureFile = $dir . "/../config/parameters_azure.yml";
$config = Yaml::parse($parametersAzureFile);
$config['parameters']['assets_azure_version'] = $buildNumber;
$yaml = Yaml::dump($config, 2);
file_put_contents($parametersAzureFile, $yaml);
return $buildNumber;
}
public function getWebRoleNames()
{
return $this->getValues('WebRole', 'name');
}
public function getWorkerRoleNames()
{
return $this->getValues('WorkerRole', 'name');
}
public function getRoleNames()
{
return array_merge($this->getWebRoleNames(), $this->getWorkerRoleNames());
}
public function addWebRole($name)
{
$existingRoles = $this->getRoleNames();
if (in_array($name, $existingRoles)) {
throw new \RuntimeException(sprintf("Role with name %s already exists.", $name));
}
$webrole = new \DOMDocument('1.0', 'UTF-8');
$webrole->load(__DIR__ . '/../Resources/role_template/WebRole.xml');
$roles = $webrole->getElementsByTagName('WebRole');
$webRoleNode = $roles->item(0);
$webRoleNode->setAttribute('name', $name);
$sites = $webrole->getElementsByTagName('Site');
$siteNode = $sites->item(0);
$siteNode->setAttribute('physicalDirectory', $name . '\\' );
$webRoleNode = $this->dom->importNode($webRoleNode, true);
$this->dom->documentElement->appendChild($webRoleNode);
$this->save();
}
private function save()
{
if ($this->dom->save($this->serviceDefinitionFile) === false) {
throw new \RuntimeException(sprintf("Could not write ServiceDefinition to '%s'",
$this->serviceDefinitionFile));
}
}
public function addImport($moduleName)
{
$importNode = $this->dom->createElement('Import');
$importNode->setAttribute('moduleName', $moduleName);
$imports = $this->dom->getElementsByTagName('Imports')->item(0);
$imports->appendChild($importNode);
$this->save();
}
private function getValues($tagName, $attributeName)
{
$nodes = $this->dom->getElementsByTagName($tagName);
$values = array();
foreach ($nodes as $node) {
$values[] = $node->getAttribute($attributeName);
}
return $values;
}
public function getPhysicalDirectories()
{
$nodes = $this->dom->getElementsByTagName('WebRole');
$dirs = array();
foreach ($nodes as $node) {
$sites = $node->getElementsByTagName('Site');
if (count($sites)) {
$dirs[$node->getAttribute('name')] = realpath(
dirname($this->serviceDefinitionFile) . DIRECTORY_SEPARATOR .
rtrim($sites->item(0)->getAttribute('physicalDirectory'), "\\")
);
}
}
return $dirs;
}
public function getPhysicalDirectory($name)
{
$dirs = $this->getPhysicalDirectories();
if (!isset($dirs[$name])) {
throw new \RuntimeException(sprintf("There exists no role named '%s'.", $name));
}
return $dirs[$name];
}
/**
* Create the role files for this service definition.
*
* A role file is a semicolon seperated list of target and destination
* paths. Only these files are then copied during the cspack.exe process to
* the target deployment directory or package file.
*
* @param string $inputDir
* @param string $outputDir
* @param string $roleFileDir
* @return array
*/
public function createRoleFiles($inputDir, $outputDir, $roleFileDir = null)
{
$roleFileDir = $roleFileDir ?: $inputDir;
$outputDir = realpath($outputDir);
$seenDirs = array();
$longPaths = array();
$roleFiles = array();
foreach ($this->getWebRoleNames() as $roleName) {
$dir = realpath($inputDir);
$roleFilePath = sprintf('%s/%s.roleFiles.txt', $roleFileDir, $roleName);
$roleFiles[$roleName] = $roleFilePath;
if (isset($seenDirs[$dir])) {
// we have seen this directory already, just copy the known
// file with a new role file name.
copy($seenDirs[$dir], $roleFilePath);
continue;
}
$seenDirs[$dir] = $roleFilePath;
$roleFile = $this->computeRoleFileContents($dir, $roleName, $outputDir);
file_put_contents($roleFilePath, $roleFile);
}
if ($longPaths) {
throw new \RuntimeException("Paths are too long. Not more than 248 chars per directory and 260 per file name allowed:\n" . implode("\n", $longPaths));
}
return $roleFiles;
}
/**
* Compute the roleFiles.txt content that is necessary for a given role.
*
* @param string $dir
* @param string $roleName
* @param string $outputPath
* @return string
*/
private function computeRoleFileContents($dir, $roleName, $outputDir)
{
$roleFile = "";
$iterator = $this->getIterator($dir);
// optimization to inline vendor role files. Since vendor files
// never change during development, their list can be computed
// during vendor initialization (composer or bin/vendors scripts)
// and does not need to be reperformed.
if (file_exists($dir . '/vendor/azureRoleFiles.txt') &&
! in_array("vendor", $this->roleFiles['exclude'])) {
$roleFile .= file_get_contents($dir . '/vendor/azureRoleFiles.txt');
}
$length = strlen($dir) + 1;
foreach ($iterator as $file) {
if (is_dir($file)) {
continue;
}
$path = str_replace(DIRECTORY_SEPARATOR, "\\", substr($file, $length));
$checkPath = sprintf('%s/roles/%s/approot/%s', $outputDir, $roleName, $path);
if (strlen($checkPath) >= 248) {
$longPaths[] = $checkPath . " (". strlen($checkPath) . ")";
}
$roleFile .= $path .";".$path."\r\n";
}
return $roleFile;
}
private function getIterator($dir)
{
$dirs = new Finder();
$subdirs = array();
foreach ($dirs->directories()->in($dir)->depth(0) as $subdir) {
$subdir = (string)$subdir;
if (!in_array(basename($subdir), $this->roleFiles['exclude'])) {
$subdirs[basename($subdir)] = $subdir;
}
}
if (file_exists($dir . '/vendor/azureRoleFiles.txt')) {
unset($subdirs["vendor"]);
}
$finder = new Finder();
$iterator = $finder->files()
->in($subdirs)
->ignoreDotFiles(false)
->ignoreVCS($this->roleFiles['ignoreVCS']);
foreach ($this->roleFiles['exclude'] as $exclude) {
$iterator->exclude($exclude);
}
foreach ($this->roleFiles['notName'] as $notName) {
$iterator->notName($notName);
}
return $iterator;
}
}