-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathServicesCommand.php
305 lines (263 loc) · 10.9 KB
/
ServicesCommand.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
<?php
namespace Aegir\Provision\Command;
use Aegir\Provision\Command;
use Aegir\Provision\Console\ProvisionStyle;
use Aegir\Provision\Context;
use Aegir\Provision\Context\PlatformContext;
use Aegir\Provision\Context\ServerContext;
use Aegir\Provision\Context\SiteContext;
use Aegir\Provision\Property;
use Aegir\Provision\Provision;
use Aegir\Provision\Service;
use Consolidation\AnnotatedCommand\CommandFileDiscovery;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class ServicesCommand
*
* @package Aegir\Provision\Command
*/
class ServicesCommand extends Command
{
/**
* This command needs a context.
*/
const CONTEXT_REQUIRED = TRUE;
/**
* "list" (default), "add", "remove", or "configure"
* @var string
*/
protected $sub_command = '';
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('services')
->setDescription('Manage the services attached to servers.')
->setHelp(
'Use this command to add new services to servers, or to add service subscriptions to platforms and sites.'
)
->setDefinition($this->getCommandDefinition())
;
}
/**
* Generate the list of options derived from ProvisionContextType classes.
*
* @return \Symfony\Component\Console\Input\InputDefinition
*/
protected function getCommandDefinition()
{
$inputDefinition = $this::getCommandOptions();
$inputDefinition[] = new InputArgument(
'sub_command',
InputArgument::OPTIONAL,
'"list" (default), "add", "remove", or "configure".',
'list'
);
$inputDefinition[] = new InputArgument(
'service',
InputArgument::OPTIONAL,
'http, db, etc.'
);
$inputDefinition[] = new InputArgument(
'server',
InputArgument::OPTIONAL,
'The name of the server context to use for this service.'
);
$inputDefinition[] = new InputOption(
'service_type',
NULL,
InputOption::VALUE_OPTIONAL,
'The name of the service type to use.'
);
return new InputDefinition($inputDefinition);
}
/**
* Load all server_options, site_options, and platform_options from Service classes.
*
* Invoked from SaveCommand as well.
* @return array
*/
public static function getCommandOptions() {
$inputDefinition = [];
// Load all service options
$options = Context::getServiceOptions();
// For each service type...
foreach ($options as $service => $service_name) {
// Load every available service type.
foreach (Context::getServiceTypeOptions($service) as $service_type => $service_name) {
$class = Service::getClassName($service, $service_type);
Provision::getProvision()->getLogger()->debug("Loading options from $class $service_type");
// Load option_documentation() into input options.
foreach (Context::getContextTypeOptions() as $type => $type_name) {
$method = "{$type}_options";
foreach ($class::$method() as $option => $description) {
$description = "$type_name $service $service_name service: $description";
$inputDefinition[] = new InputOption($option, NULL, InputOption::VALUE_OPTIONAL, $description);
}
}
}
}
return $inputDefinition;
}
/**
* Validate that the context specified is a server.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @throws \Exception
*/
protected function initialize(
InputInterface $input,
OutputInterface $output
) {
//
// if ($input->getOption('context_name') == 'add') {
// $this->sub_command = $input->getArgument('context_name');
// $input->setArgument('context_name', NULL);
// }
// else {
// $this->sub_command = $input->getArgument('sub_command');
// }
$this->sub_command = $input->getArgument('sub_command');
parent::initialize(
$input,
$output
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$method = "execute_{$this->sub_command}";
if (method_exists($this, $method)) {
$this->$method($input, $output);
}
}
/**
* List all services attached to this server.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
protected function execute_list(InputInterface $input, OutputInterface $output) {
$this->io->comment("List Services");
$this->context->showServices($this->io);
}
/**
* Add a new service to a server.
*/
protected function execute_add(InputInterface $input, OutputInterface $output)
{
// Ask which service.
$this->io->comment("Add Services");
$service = $this->input->getArgument('service')?
$this->input->getArgument('service'):
$this->io->choice('Which service?', $this->context->getServiceOptions());
if (empty($service)) {
throw new \Exception("Argument 'service' must not be empty.");
}
// If server, ask which service type.
if ($this->context->type == 'server') {
if (empty($this->context->getServiceTypeOptions($service))) {
throw new \Exception("There was no class found for service $service. Create one named \\Aegir\\Provision\\Service\\{$service}Service");
}
// Check or ask for service_type option.
if ($this->input->getOption('service_type')) {
$service_type = $this->input->getOption('service_type');
$this->io->comment("Using option service_type=". $service_type);
}
else {
$service_type = $this->io->choice('Which service type?', $this->context->getServiceTypeOptions($service));
}
// If $service_type is still empty, throw an exception. Happens if using -n
if (empty($service_type)) {
throw new \Exception('Option --service_type must be specified.');
}
// Handle invalid service_type.
if (!class_exists(Service::getClassName($service, $service_type))) {
$types = Context::getServiceTypeOptions($service);
throw new \Exception(strtr("Class not found for service type !type !service. Expecting !class. Check your Class::SERVICE_TYPE constant.", [
'!service' => $service,
'!type' => $service_type,
'!class' => Service::getClassName($service, $service_type),
]));
}
if ($this->context->hasService($service)) {
$this->getProvision()->io()->helpBlock("Editing service {$service} provded by server '{$this->context->name}'...", ProvisionStyle::ICON_EDIT);
}
// Then ask for all options.
$properties = $this->askForServiceProperties($service, $service_type);
$this->io->info("Adding $service service $service_type...");
$services_key = 'services';
$service_info = [
'type' => $service_type,
];
}
// All other context types are associating with servers that provide the service.
else {
if (empty($this->getProvision()->getServerOptions($service))) {
throw new \Exception("No servers providing $service service were found. Create one with `provision save` or use `provision services` to add to an existing server.");
}
$server = $this->input->getArgument('server')?
$this->input->getArgument('server'):
$this->io->choice('Which server?', $this->getProvision()->getServerOptions($service));
// Then ask for all options.
$server_context = $this->getProvision()->getContext($server);
$properties = $this->askForServiceProperties($service);
$this->io->info("Using $service service from server $server...");
$services_key = 'service_subscriptions';
$service_info = [
'server' => $server,
];
}
try {
$this->context->config[$services_key][$service] = $service_info;
$this->context->config[$services_key][$service]['properties'] = $properties;
$this->context->setProperty($services_key, $this->context->config[$services_key]);
$this->context->save();
$this->io->success('Service saved to Context!');
}
catch (\Exception $e) {
throw new \Exception("Something went wrong when saving the context: " . $e->getMessage());
}
}
/**
* Loop through this context type's option_documentation() method and ask for each property.
*
* @return array
*/
private function askForServiceProperties($service, $service_type = NULL) {
$class = Service::getClassName($service, $service_type);
$method = "{$this->context->type}_options";
$options = $class::{$method}();
$properties = [];
foreach ($options as $name => $property) {
// Allows option_documentation to return array of strings for simple properties.
if ( !$property instanceof Property) {
$property = Provision::newProperty($property);
}
if ($this->context->hasService($service) && $this->context->getService($service)->hasProperty($name) && $this->context->getService($service)->getProperty($name)) {
$property->default = $this->context->getService($service)->getProperty($name);
}
// If option does not exist, ask for it.
if ($this->input->hasOption($name) && !empty($this->input->getOption($name))) {
$properties[$name] = $this->input->getOption($name);
$this->io->comment("Using option {$name}={$properties[$name]}");
}
else {
$properties[$name] = $this->io->ask("{$name} ({$property->description})", $property->default, $property->validate);
}
}
return $properties;
}
}