-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoboFile.php
172 lines (143 loc) · 5.33 KB
/
RoboFile.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
<?php
use League\CLImate\CLImate;
use RenokiCo\PhpK8s\KubernetesCluster;
use Migrator\UtilityBelt;
/**
* This is project's console commands configuration for Robo task runner.
*
* @see https://robo.li/
*/
class RoboFile extends \Robo\Tasks
{
public function run(
$opts = [
'migrateYaml' => './scripts/default.yaml',
'token' => null, //This is the token used to chat to the k8s api
'kubeContext' => null,
'namespace' => null, // The namespace we're targeting in cluster
'project' => null, //project name, used for lagoon api calls
'environment' => null, //environment name, used for lagoon api calls
'sshKey' => null,
]
) {
// Bootstrap the environment
$dynamicEnv = new \Migrator\Step\DynamicEnvironment();
$dynamicEnv->fillDynamicEnvironmentFromEnv();
\Migrator\LagoonUtilityBelt::setUpLagoon_yml();
$climate = new CLImate;
$opts = array_merge($opts, $this->processEnvironment());
$migrateYaml = $opts['migrateYaml'];
$cluster = $this->grabCluster($opts['token'], $opts['kubeContext']);
$migration = $this->loadYaml($migrateYaml);
$args = new \Migrator\RunnerArgs();
$args->steps = $migration['steps'];
$args->cluster = $cluster;
$args->namespace = $this->grabNamespace($opts['namespace']);
$args->token = $this->getToken($opts['token']);
$args->project = !empty($opts['project']) ? $opts['project'] : getenv(
"LAGOON_PROJECT"
);
$args->sshKey = !empty($opts['sshKey']) ? $opts['sshKey'] : "/var/run/secrets/lagoon/ssh/ssh-privatekey";
$args->environment = !empty($opts['environment']) ? $opts['environment'] : getenv(
"LAGOON_GIT_BRANCH"
);
//Here we add a pre-runner set of steps - this can be used for assertions
//and does _not_ trigger a rollback
try {
if(!empty($migration['prerequisites'])) {
$climate->out("Found prerequisite steps - will run with no rollback\n\n");
$args->steps = $migration['prerequisites'];
$runner = new \Migrator\Runner($args, $dynamicEnv);
$runner->run();
} else {
$climate->out("No prerequisites found - proceeding to run main steps\n\n");
}
} catch (\Exception $ex) {
printf("Prerequistes failed with the following message: %s \n\n exiting", $ex->getMessage());
exit(1);
}
try {
$args->steps = $migration['steps'];
$runner = new \Migrator\Runner($args, $dynamicEnv);
$runner->run();
} catch (\Exception $ex) {
$climate->border('*');
$climate->border('*');
$climate->flank(sprintf("Got error running main steps: %s\n\n", $ex->getMessage()));
$climate->border('*');
$climate->border('*');
if(!empty($migration['rollback'])) {
$climate->out("Attempting to run rollback steps\n\n");
$args->steps = $migration['rollback'];
$runner = new \Migrator\Runner($args, $dynamicEnv);
$runner->run();
}
exit(1);
}
}
private function loadYaml($filename)
{
return \Symfony\Component\Yaml\Yaml::parse(
file_get_contents($filename)
);
}
/**
* @return array
*/
protected function processEnvironment() {
$payload = getenv("JSON_PAYLOAD");
if(!$payload) return [];
$payload = base64_decode($payload);
if(!$payload) return [];
$payload = json_decode($payload, true);
if(json_last_error()) {
var_dump(json_last_error_msg());
return [];
}
return $payload;
}
private function grabNamespace($nameSpace)
{
if (empty($nameSpace)) {
return trim(
file_get_contents(
"/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)
);
}
return $nameSpace;
}
/**
* @return \RenokiCo\PhpK8s\KubernetesCluster
*/
private function grabCluster($token = null, $kubeContext = null)
{
if (!empty($token) && !empty($kubeContext)) {
return KubernetesCluster::fromKubeConfigVariable($kubeContext)
->withToken($token);
}
$deployTokenFile = file_exists("/var/run/secrets/lagoon/deployer/token") ? "/var/run/secrets/lagoon/deployer/token" : "/var/run/secrets/kubernetes.io/serviceaccount/token";
$cluster = KubernetesCluster::inClusterConfiguration(
"https://kubernetes.default.svc.cluster.local"
)
->loadTokenFromFile($deployTokenFile);
return $cluster;
}
/**
* @return void
*/
protected function getToken($token = null)
{
if (!empty($token)) {
return $token;
}
$inClusterTokenFile = "/var/run/secrets/lagoon/deployer/token";
if (file_exists($inClusterTokenFile)) {
return trim(file_get_contents($inClusterTokenFile));
}
$inClusterTokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token";
if (file_exists($inClusterTokenFile)) {
return trim(file_get_contents($inClusterTokenFile));
}
}
}