1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Loads configuration information for the lib. |
4
|
|
|
*/ |
5
|
|
|
namespace Graviton\Deployment; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
8
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
9
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
10
|
|
|
use Symfony\Component\Config\Definition\Processor; |
11
|
|
|
use Symfony\Component\Config\FileLocator; |
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/deploy-scripts/graphs/contributors> |
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
17
|
|
|
* @link http://swisscom.ch |
18
|
|
|
*/ |
19
|
|
|
class Configuration implements ConfigurationInterface |
20
|
|
|
{ |
21
|
|
|
/** @var Processor */ |
22
|
|
|
private $processor; |
23
|
|
|
|
24
|
|
|
/** @var FileLocator */ |
25
|
|
|
private $fileLocator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Processor $processor Configuration processor. |
29
|
|
|
* @param FileLocator $fileLocator Helper class to find files. |
30
|
|
|
*/ |
31
|
3 |
|
public function __construct(Processor $processor, FileLocator $fileLocator) |
32
|
|
|
{ |
33
|
3 |
|
$this->processor = $processor; |
34
|
3 |
|
$this->fileLocator = $fileLocator; |
35
|
3 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Loads the current configuration. |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
7 |
|
public function load() |
43
|
|
|
{ |
44
|
7 |
|
$yamlFiles = $this->fileLocator->locate('deploy.yml', null, false); |
45
|
7 |
|
$config = Yaml::parse(file_get_contents($yamlFiles[0])); |
46
|
|
|
|
47
|
7 |
|
$this->validateParsedConfiguration( |
48
|
7 |
|
$config, |
49
|
7 |
|
'Unable to parse the provided configuration file (' . $yamlFiles[0] . ').' |
50
|
7 |
|
); |
51
|
|
|
|
52
|
7 |
|
$configuration = $this->processor->processConfiguration($this, $config); |
53
|
|
|
|
54
|
7 |
|
$this->validateParsedConfiguration( |
55
|
7 |
|
$configuration, |
56
|
7 |
|
'Parsing the provided configuration file (' . $yamlFiles[0] . ') did not convert into an array.' . |
57
|
7 |
|
PHP_EOL . |
58
|
|
|
'Please check the setup of »\Symfony\Component\Config\Definition\BaseNode::$finalValidationClosures«.' |
59
|
7 |
|
); |
60
|
|
|
|
61
|
6 |
|
return $configuration; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Generates the configuration tree builder. |
66
|
|
|
* |
67
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder |
68
|
|
|
*/ |
69
|
6 |
|
public function getConfigTreeBuilder() |
70
|
|
|
{ |
71
|
6 |
|
$treeBuilder = new TreeBuilder(); |
72
|
6 |
|
$rootNode = $treeBuilder->root('deploy-scripts'); |
73
|
|
|
|
74
|
|
|
$rootNode |
75
|
6 |
|
->children() |
76
|
6 |
|
->scalarNode('cf_bin')->cannotBeEmpty()->isRequired()->end() |
77
|
6 |
|
->scalarNode('cf_process_timeout')->cannotBeEmpty()->isRequired()->end() |
78
|
6 |
|
->scalarNode('cf_api_url')->cannotBeEmpty()->isRequired()->end() |
79
|
6 |
|
->scalarNode('cf_username')->cannotBeEmpty()->isRequired()->end() |
80
|
6 |
|
->scalarNode('cf_password')->cannotBeEmpty()->isRequired()->end() |
81
|
6 |
|
->scalarNode('cf_org')->cannotBeEmpty()->isRequired()->end() |
82
|
6 |
|
->scalarNode('cf_space')->cannotBeEmpty()->isRequired()->end() |
83
|
6 |
|
->scalarNode('cf_domain')->cannotBeEmpty()->isRequired()->end() |
84
|
6 |
|
->arrayNode('cf_services') |
85
|
6 |
|
->useAttributeAsKey('name') |
86
|
6 |
|
->prototype('variable') |
87
|
6 |
|
->end() |
88
|
6 |
|
->end() |
89
|
6 |
|
->arrayNode('cf_environment_vars') |
90
|
6 |
|
->useAttributeAsKey('name') |
91
|
6 |
|
->prototype('scalar') |
92
|
6 |
|
->end() |
93
|
6 |
|
->end() |
94
|
6 |
|
->end(); |
95
|
|
|
|
96
|
6 |
|
return $treeBuilder; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Determines the parsed or processed configuration is valid. |
101
|
|
|
* |
102
|
|
|
* @param array $configuration Configuration to be validated |
103
|
|
|
* @param string $message Error message to be passed to thrown exception. |
104
|
|
|
* |
105
|
|
|
* @throws \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
7 |
|
protected function validateParsedConfiguration(array $configuration, $message = '') |
110
|
|
|
{ |
111
|
7 |
|
if (!is_array($configuration) || empty($configuration)) { |
112
|
1 |
|
throw new InvalidConfigurationException($message); |
113
|
|
|
} |
114
|
7 |
|
} |
115
|
|
|
} |
116
|
|
|
|