summaryrefslogtreecommitdiffstats
path: root/src/Service.php
blob: 353200c8b4f34a0c6ce852926b6ccaad90d9a7c5 (plain)
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
316
317
<?php
/**
 * @file
 * The base Provision service class.
 *
 * @see Provision_Service
 */

namespace Aegir\Provision;

use Aegir\Provision\Common\ContextAwareTrait;
use Aegir\Provision\Common\ProvisionAwareTrait;
use Psr\Log\LoggerAwareTrait;
use Robo\Common\BuilderAwareTrait;
use Robo\Common\OutputAdapter;
use Robo\Contract\BuilderAwareInterface;

class Service implements BuilderAwareInterface
{
    use BuilderAwareTrait;
    use ProvisionAwareTrait;
    use ContextAwareTrait;
    use LoggerAwareTrait;

    public $type;
    
    public $properties;
    
    /**
     * @var Context;
     * The context that provides this service.
     *
     * @see \Aegir\Provision\Context
     */
    public $provider;
    
    /**
     * @var string
     * The machine name of the service.  ie. http, db
     */
    const SERVICE = 'service';
    
    /**
     * @var string
     * A descriptive name of the service.  ie. Web Server
     */
    const SERVICE_NAME = 'Service Name';
    
    function __construct($service_config, Context $provider_context)
    {
        $this->provider = $provider_context;
        $this->setContext($provider_context);
        $this->setProvision($provider_context->getProvision());
        $this->setLogger($provider_context->getProvision()->getLogger());
        
        $this->type = $service_config['type'];
        $this->properties = $service_config['properties'];
        if ($provider_context->getBuilder()) {
            $this->setBuilder($provider_context->getBuilder());
        }
    }
    
    /**
     * Retrieve the class name of a specific service type.
     *
     * @param $service
     *   The service requested. Typically http, db.
     *
     * @param $type
     *   The type of service requested. For example: apache, nginx, mysql.
     *
     * @return string
     */
    static function getClassName($service, $type = NULL) {
        $service = ucfirst($service);
        $type = ucfirst($type);
        
        if ($type) {
            return "\Aegir\Provision\Service\\{$service}\\{$service}{$type}Service";
        }
        else {
            return "\Aegir\Provision\Service\\{$service}Service";
        }
    }

    /**
     * React to the verify command. Passes off to the method verifySite, verifyServer, verifyPlatform.
     * @return mixed
     */
    public function verify() {
        $method = 'verify' . ucfirst($this->getContext()->type);
        $this->getProvision()->getLogger()->info("Running {method}", [
            'method' => $method
        ]);
        return $this::$method();
    }

//
//    /**
//     * React to the `provision verify` command.
//     */
//    function verifySite()
//    {
//        return [
//            'configuration' => $this->writeConfigurations(),
//            'service' => $this->restartService(),
//        ];
//    }
//
//    /**
//     * React to the `provision verify` command.
//     */
//    function verifyPlatform()
//    {
//        return [
//            'configuration' => $this->writeConfigurations(),
//            'service' => $this->restartService(),
//        ];
//    }
//
//    /**
//     * React to `provision verify` command when run on a subscriber, to verify the service's provider.
//     *
//     * This is used to allow skipping of the service restart.
//     */
//    function verifyServer()
//    {
//        return [
//            'configuration' => $this->writeConfigurations(),
//        ];
//    }

    /**
     * Run the services "restart_command".
     * @return bool
     */
    protected function restartService() {
        if (empty($this->properties['restart_command'])) {
            return TRUE;
        }
        else {
            $task = $this->getProvision()->getBuilder()->taskExec($this->properties['restart_command'])
                ->silent(!$this->getProvision()->io()->isVerbose())
            ;
            
            /** @var \Robo\Result $result */
            $result = $task->run();
            if ($result->wasSuccessful()) {
                $this->getProvision()->io()->successLite('Service restarted.');
                sleep(1);
                return TRUE;
            }
            else {
                    $this->getProvision()->io()->errorLite('Unable to restart service:' . $result->getOutputData());
            }
        }
        return FALSE;
    }

    /**
     * React to the `provision verify` command.
     */
    function verifySubscription(ServiceSubscription $serviceSubscription)
    {
        return [
            'configuration' => $this->writeConfigurations($serviceSubscription),
        ];
    }
    
    /**
     * List context types that are allowed to subscribe to this service.
     *
     * @return array
     */
    static function allowedContexts()
    {
        return [];
    }
    
    /**
     * Write this service's configurations.
     *
     * @param \Aegir\Provision\ServiceSubscription|null $serviceSubscription
     *
     * @return bool
     */
    protected function writeConfigurations(ServiceSubscription $serviceSubscription = NULL)
    {
        // If we are writing for a serviceSubscription, use the provider context.
        if ($serviceSubscription) {
            $context = $serviceSubscription->context;
        }
        else {
            $context = $this->provider;
        }
        
        if (empty($this->getConfigurations()[$context->type])) {
            return TRUE;
        }
        
        $success = TRUE;
        foreach ($this->getConfigurations()[$context->type] as $configuration_class) {
            try {
                $config = new $configuration_class($context, $this);
                $config->write();
                $context->getProvision()->io()->successLite(
                    'Wrote '.$config->description.' to '.$config->filename()
                );
            }
            catch (\Exception $e) {
                $context->getProvision()->io()->errorLite(
                    'Unable to write '.$config->description.' to '.$config->filename() . ': ' . $e->getMessage()
                );
                $success = FALSE;
            }
        }
        return $success;
    }
    
    /**
     * Stub for this services configurations.
     */
    protected function getConfigurations()
    {
        return [];
    }
    
    /**
     * Return the SERVICE_TYPE
     *
     * @return mixed
     */
    public function getType()
    {
        return $this::SERVICE_TYPE;
    }
    
    /**
     * Return the SERVICE_TYPE
     *
     * @return mixed
     */
    public function getName()
    {
        return $this::SERVICE;
    }


    /**
     * Get a specific property.
     *
     * @param $name
     * @return mixed
     * @throws \Exception
     */
    public function getProperty($name) {
        if (isset($this->properties[$name])) {
            return $this->properties[$name];
        }
        else {
            throw new \Exception("Property '$name' on service '$this->type' does not exist.");
        }
    }
    
    /**
     * Return the SERVICE_TYPE
     *
     * @return mixed
     */
    public function getFriendlyName()
    {
        return $this::SERVICE_NAME;
    }
    
    /**
     * Return a list of user configurable options that this service provides to Server Context objects.
     */
    static function server_options()
    {
        return [];
        //        return [
        //            'http_port' => 'The port which the web service is running on.',
        //            'web_group' => 'server with http: OS group for permissions; working default will be attempted',
        //        ];
    }
    
    /**
     * Return a list of user configurable options that this service provides to Platform Context objects.
     */
    static function platform_options()
    {
        return [];
        //        return [
        //            'platform_extra_config' => 'Extra lines of configuration to add to this platform.',
        //        ];
    }
    
    /**
     * Return a list of user configurable options that this service provides to Site Context objects.
     */
    static function site_options()
    {
        return [];
        //      return [
        //          'site_mail' => 'The email address to use for the ServerAdmin configuration.',
        //      ];
    }
    
    
    /**
     * @return \Aegir\Provision\Robo\ProvisionBuilder
     */
    function getBuilder()
    {
        return $this->builder;
    }
}