Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Instance.php « Aws « TestRunner « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: db706e36e00924a93b8190cf5bee88b16f33bd76 (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
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Plugins\TestRunner\Aws;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Aws\Ec2\Ec2Client;

class Instance
{

    /**
     * @var Config
     */
    private $config;

    /**
     * @var Ec2Client
     */
    private $client;

    private $testSuite;

    private $useOneInstancePerTestSuite = false;

    public function __construct(Config $config, $testSuite)
    {
        $this->config    = $config;
        $this->testSuite = $testSuite;
        $this->client    = $this->createEc2Client();
    }

    public function enableUseOneInstancePerTestSuite()
    {
        $this->useOneInstancePerTestSuite = true;
    }

    public function findExisting()
    {
        $filters = array(
            array('Name' => 'image-id', 'Values' => array($this->config->getAmi())),
            array('Name' => 'key-name', 'Values' => array($this->config->getKeyName())),
            array('Name' => 'instance-state-name', 'Values' => array('running')),
        );

        if (!empty($this->testSuite) && $this->useOneInstancePerTestSuite) {
            $filters[] = array('Name' => 'tag:TestSuite', 'Values' => array($this->testSuite));
        }

        $instances = $this->client->describeInstances(array('Filters' => $filters));

        $reservations = $instances->getPath('Reservations');

        if (!empty($reservations)) {
            $host = $this->getHostFromDescribedInstances($instances);

            $instanceIds = $instances->getPath('Reservations/*/Instances/*/InstanceId');
            $this->verifySetup($instanceIds);

            return $host;
        }
    }

    public function terminate($instanceIds)
    {
        $this->client->terminateInstances(array(
            'InstanceIds' => $instanceIds
        ));

        $this->client->waitUntilInstanceTerminated(array(
            'InstanceIds' => $instanceIds
        ));
    }

    public function launch()
    {
        // user data is executed when instance launches, it is important that this file starts with "#!"
        $userData = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/TestRunner/scripts/on_instance_launch.sh');

        $result = $this->client->runInstances(array(
            'ImageId' => $this->config->getAmi(),
            'MinCount' => 1,
            'MaxCount' => 1,
            'InstanceType' => $this->config->getInstanceType(),
            'KeyName' => $this->config->getKeyName(),
            'SecurityGroups' => $this->config->getSecurityGroups(),
            'InstanceInitiatedShutdownBehavior' => 'terminate',
            'UserData' => base64_encode($userData)
        ));

        $instanceIds = $result->getPath('Instances/*/InstanceId');

        $this->client->waitUntilInstanceRunning(array(
            'InstanceIds' => $instanceIds,
        ));

        return $instanceIds;
    }

    public function setup($instanceIds)
    {
        $awsCloudWatch = new CloudWatch($this->config);
        $awsCloudWatch->terminateInstanceIfIdleForTooLong($instanceIds);

        $awsTags = new Tags($this->client);
        $awsTags->assignTagsToInstances($instanceIds, $this->testSuite);

        $instances = $this->client->describeInstances(array(
            'InstanceIds' => $instanceIds,
        ));

        $host = $this->getHostFromDescribedInstances($instances);

        return $host;
    }

    public function verifySetup($instanceIds)
    {
        $awsCloudWatch = new CloudWatch($this->config);
        $hasAlarms     = $awsCloudWatch->hasAssignedAlarms($instanceIds);

        if (!$hasAlarms) {
            $this->setup($instanceIds); // try setup again

            $hasAlarms = $awsCloudWatch->hasAssignedAlarms($instanceIds);

            if (!$hasAlarms) { // declare it as failed if it still does not work
                throw new \Exception('Failed to assign alarms for InstanceIds: ' . implode(', ' , $instanceIds));
            }
        }
    }

    /**
     * @param \Guzzle\Service\Resource\Model $resources
     * @return mixed
     */
    private function getHostFromDescribedInstances($resources)
    {
        $instances = $resources->getPath('Reservations/*/Instances');

        if (empty($instances)) {
            return;
        }

        $instanceToUse = null;

        foreach ($instances as $index => $instance) {
            if (!empty($instance['Tags'])) {
                foreach ($instance['Tags'] as $tag) {
                    if (!empty($this->testSuite)
                        && $tag['Key'] === 'TestSuite'
                        && $tag['Value'] === $this->testSuite) {

                        $instanceToUse = $instance;
                    }
                }
            }
        }

        if (empty($instanceToUse)) {
            $instanceToUse = array_shift($instances);
        }

        $host = $instanceToUse['PublicDnsName'];

        return $host;
    }

    private function createEc2Client()
    {
        return Ec2Client::factory($this->getConnectionOptions());
    }

    private function getConnectionOptions()
    {
        return array(
            'key'    => $this->config->getAccessKey(),
            'secret' => $this->config->getSecretKey(),
            'region' => $this->config->getRegion()
        );
    }
}