forked from acquia/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeStudioWizardCommand.php
More file actions
328 lines (301 loc) · 15.5 KB
/
CodeStudioWizardCommand.php
File metadata and controls
328 lines (301 loc) · 15.5 KB
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
318
319
320
321
322
323
324
325
326
327
328
<?php
declare(strict_types=1);
namespace Acquia\Cli\Command\CodeStudio;
use Acquia\Cli\Command\WizardCommandBase;
use Acquia\Cli\Output\Checklist;
use AcquiaCloudApi\Endpoints\Account;
use DateTime;
use Gitlab\Exception\ValidationFailedException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'codestudio:wizard', description: 'Create and/or configure a new Code Studio project for a given Cloud Platform application', aliases: ['cs:wizard'])]
final class CodeStudioWizardCommand extends WizardCommandBase
{
use CodeStudioCommandTrait;
private Checklist $checklist;
protected function configure(): void
{
$this
->addOption('key', null, InputOption::VALUE_REQUIRED, 'The Cloud Platform API token that Code Studio will use')
->addOption('secret', null, InputOption::VALUE_REQUIRED, 'The Cloud Platform API secret that Code Studio will use')
->addOption('gitlab-token', null, InputOption::VALUE_REQUIRED, 'The GitLab personal access token that will be used to communicate with the GitLab instance')
->addOption('gitlab-project-id', null, InputOption::VALUE_REQUIRED, 'The project ID (an integer) of the GitLab project to configure.')
->addOption('gitlab-host-name', null, InputOption::VALUE_REQUIRED, 'The GitLab hostname.');
$this->acceptApplicationUuid();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->checklist = new Checklist($output);
$this->authenticateWithGitLab();
$this->writeApiTokenMessage($input);
$cloudKey = $this->determineApiKey();
$cloudSecret = $this->determineApiSecret();
// We may already be authenticated with Acquia Cloud Platform via a refresh token.
// But, we specifically need an API Token key-pair of Code Studio.
// So we reauthenticate to be sure we're using the provided credentials.
$this->reAuthenticate($cloudKey, $cloudSecret, $this->cloudCredentials->getBaseUri(), $this->cloudCredentials->getAccountsUri());
$phpVersion = null;
$nodeVersion = null;
$nodeHostingType = null;
$projectType = $this->getListOfProjectType();
$projectSelected = $this->io->choice('Select a project type', $projectType, "Drupal_project");
switch ($projectSelected) {
case "Drupal_project":
$phpVersions = [
'PHP_version_8.1' => "8.1",
'PHP_version_8.2' => "8.2",
'PHP_version_8.3' => "8.3",
'PHP_version_8.4' => "8.4",
];
$project = $this->io->choice('Select a PHP version', array_values($phpVersions), "8.3");
$project = array_search($project, $phpVersions, true);
$phpVersion = $phpVersions[$project];
break;
case "Node_project":
$nodeHostingTypes = [
'advanced' => "Advanced Frontend Hosting",
'basic' => "Basic Frontend Hosting",
];
$project = $this->io->choice('Select a NODE hosting type', array_values($nodeHostingTypes), "Basic Frontend Hosting");
$nodeHostingType = array_search($project, $nodeHostingTypes, true);
$nodeVersions = [
'NODE_version_18' => "18",
'NODE_version_20' => "20",
'NODE_version_22' => "22",
];
$project = $this->io->choice('Select a NODE version', array_values($nodeVersions), "20");
$project = array_search($project, $nodeVersions, true);
$nodeVersion = $nodeVersions[$project];
break;
}
$appUuid = $this->determineCloudApplication();
// Get Cloud account.
$acquiaCloudClient = $this->cloudApiClientService->getClient();
$accountAdapter = new Account($acquiaCloudClient);
$account = $accountAdapter->get();
$this->validateRequiredCloudPermissions(
$acquiaCloudClient,
$appUuid,
$account,
[
"deploy to non-prod",
// Add SSH key to git repository.
"add ssh key to git",
// Add SSH key to non-production environments.
"add ssh key to non-prod",
// Add a CD environment.
"add an environment",
// Delete a CD environment.
"delete an environment",
// Manage environment variables on a non-production environment.
"administer environment variables on non-prod",
]
);
$this->setGitLabProjectDescription($appUuid);
// Get Cloud application.
$cloudApplication = $this->getCloudApplication($appUuid);
$project = $this->determineGitLabProject($cloudApplication);
$this->io->writeln([
"",
"This command will configure the Code Studio project <comment>{$project['path_with_namespace']}</comment> for automatic deployment to the",
"Acquia Cloud Platform application <comment>$cloudApplication->name</comment> (<comment>$appUuid</comment>)",
"using credentials (API Token and SSH Key) belonging to <comment>$account->mail</comment>.",
"",
"If the <comment>$account->mail</comment> Cloud account is deleted in the future, this Code Studio project will need to be re-configured.",
]);
$answer = $this->io->confirm('Do you want to continue?');
if (!$answer) {
return Command::SUCCESS;
}
$projectAccessTokenName = 'acquia-codestudio';
$projectAccessToken = $this->createProjectAccessToken($project, $projectAccessTokenName);
$this->updateGitLabProject($project);
switch ($projectSelected) {
case "Drupal_project":
$this->setGitLabCiCdVariablesForPhpProject($project, $appUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $phpVersion);
$this->createScheduledPipeline($project);
break;
case "Node_project":
$parameters = [
'ci_config_path' => 'gitlab-ci/Auto-DevOps.acquia.gitlab-ci.yml@acquia/node-template',
];
$client = $this->getGitLabClient();
$client->projects()->update($project['id'], $parameters);
$this->setGitLabCiCdVariablesForNodeProject($project, $appUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $nodeVersion, $nodeHostingType);
break;
}
$this->io->success([
"Successfully configured the Code Studio project!",
"This project will now use Acquia's Drupal optimized AutoDevOps to build, test, and deploy your code automatically to Acquia Cloud Platform via CI/CD pipelines.",
"You can visit it here:",
$project['web_url'],
"",
"Next, you should use git to push code to your Code Studio project. E.g.,",
" git remote add codestudio {$project['http_url_to_repo']}",
" git push codestudio",
]);
$this->io->note(["If the $account->mail Cloud account is deleted in the future, this Code Studio project will need to be re-configured."]);
return Command::SUCCESS;
}
/**
* @return array<mixed>|null
*/
private function getGitLabScheduleByDescription(array $project, string $scheduledPipelineDescription): ?array
{
$existingSchedules = $this->gitLabClient->schedules()
->showAll($project['id']);
foreach ($existingSchedules as $schedule) {
if ($schedule['description'] == $scheduledPipelineDescription) {
return $schedule;
}
}
return null;
}
/**
* @return array<mixed>|null ?
*/
private function getGitLabProjectAccessTokenByName(array $project, string $name): ?array
{
$existingProjectAccessTokens = $this->gitLabClient->projects()
->projectAccessTokens($project['id']);
foreach ($existingProjectAccessTokens as $key => $token) {
if ($token['name'] == $name) {
return $token;
}
}
return null;
}
/**
* @return array<mixed>|null ?
*/
private function getListOfProjectType(): ?array
{
$array = [
'Drupal_project',
'Node_project',
];
return $array;
}
private function createProjectAccessToken(array $project, string $projectAccessTokenName): string
{
$this->io->writeln("Creating project access token...");
if ($existingToken = $this->getGitLabProjectAccessTokenByName($project, $projectAccessTokenName)) {
$this->checklist->addItem("Deleting access token named <comment>$projectAccessTokenName</comment>");
$this->gitLabClient->projects()
->deleteProjectAccessToken($project['id'], $existingToken['id']);
$this->checklist->completePreviousItem();
}
$this->checklist->addItem("Creating access token named <comment>$projectAccessTokenName</comment>");
$projectAccessToken = $this->gitLabClient->projects()
->createProjectAccessToken($project['id'], [
'expires_at' => new DateTime('+365 days'),
'name' => $projectAccessTokenName,
'scopes' => ['api', 'write_repository'],
]);
$this->checklist->completePreviousItem();
return $projectAccessToken['token'];
}
private function setGitLabCiCdVariablesForPhpProject(array $project, string $cloudApplicationUuid, string $cloudKey, string $cloudSecret, string $projectAccessTokenName, string $projectAccessToken, string $phpVersion): void
{
$this->io->writeln("Setting GitLab CI/CD variables for {$project['path_with_namespace']}..");
$gitlabCicdVariables = CodeStudioCiCdVariables::getDefaultsForPhp($cloudApplicationUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $phpVersion);
$gitlabCicdExistingVariables = $this->gitLabClient->projects()
->variables($project['id']);
$gitlabCicdExistingVariablesKeyed = [];
foreach ($gitlabCicdExistingVariables as $variable) {
$key = $variable['key'];
$gitlabCicdExistingVariablesKeyed[$key] = $variable;
}
foreach ($gitlabCicdVariables as $variable) {
$this->checklist->addItem("Setting GitLab CI/CD variables for <comment>{$variable['key']}</comment>");
if (!array_key_exists($variable['key'], $gitlabCicdExistingVariablesKeyed)) {
$this->gitLabClient->projects()
->addVariable($project['id'], $variable['key'], $variable['value'], $variable['protected'], null, [
'masked' => $variable['masked'],
'variable_type' => $variable['variable_type'],
]);
} else {
$this->gitLabClient->projects()
->updateVariable($project['id'], $variable['key'], $variable['value'], $variable['protected'], null, [
'masked' => $variable['masked'],
'variable_type' => $variable['variable_type'],
]);
}
$this->checklist->completePreviousItem();
}
}
private function setGitLabCiCdVariablesForNodeProject(array $project, string $cloudApplicationUuid, string $cloudKey, string $cloudSecret, string $projectAccessTokenName, string $projectAccessToken, string $nodeVersion, string $nodeHostingType): void
{
$this->io->writeln("Setting GitLab CI/CD variables for {$project['path_with_namespace']}..");
$gitlabCicdVariables = CodeStudioCiCdVariables::getDefaultsForNode($cloudApplicationUuid, $cloudKey, $cloudSecret, $projectAccessTokenName, $projectAccessToken, $nodeVersion, $nodeHostingType);
$gitlabCicdExistingVariables = $this->gitLabClient->projects()
->variables($project['id']);
$gitlabCicdExistingVariablesKeyed = [];
foreach ($gitlabCicdExistingVariables as $variable) {
$key = $variable['key'];
$gitlabCicdExistingVariablesKeyed[$key] = $variable;
}
foreach ($gitlabCicdVariables as $variable) {
$this->checklist->addItem("Setting CI/CD variable <comment>{$variable['key']}</comment>");
if (!array_key_exists($variable['key'], $gitlabCicdExistingVariablesKeyed)) {
$this->gitLabClient->projects()
->addVariable($project['id'], $variable['key'], $variable['value'], $variable['protected'], null, [
'masked' => $variable['masked'],
'variable_type' => $variable['variable_type'],
]);
} else {
$this->gitLabClient->projects()
->updateVariable($project['id'], $variable['key'], $variable['value'], $variable['protected'], null, [
'masked' => $variable['masked'],
'variable_type' => $variable['variable_type'],
]);
}
$this->checklist->completePreviousItem();
}
}
private function createScheduledPipeline(array $project): void
{
$this->io->writeln("Creating scheduled pipeline");
$scheduledPipelineDescription = "Code Studio Automatic Updates";
if (!$this->getGitLabScheduleByDescription($project, $scheduledPipelineDescription)) {
$this->checklist->addItem("Creating scheduled pipeline <comment>$scheduledPipelineDescription</comment>");
$pipeline = $this->gitLabClient->schedules()
->create($project['id'], [
// Every Thursday at midnight.
'cron' => '0 0 * * 4',
'description' => $scheduledPipelineDescription,
'ref' => $project['default_branch'],
]);
$this->gitLabClient->schedules()
->addVariable($project['id'], $pipeline['id'], [
'key' => 'ACQUIA_JOBS_DEPRECATED_UPDATE',
'value' => 'true',
]);
$this->gitLabClient->schedules()
->addVariable($project['id'], $pipeline['id'], [
'key' => 'ACQUIA_JOBS_COMPOSER_UPDATE',
'value' => 'true',
]);
} else {
$this->checklist->addItem("Scheduled pipeline named <comment>$scheduledPipelineDescription</comment> already exists");
}
$this->checklist->completePreviousItem();
}
private function updateGitLabProject(array $project): void
{
// Setting the description to match the known pattern will allow us to automatically find the project next time.
if ($project['description'] !== $this->gitLabProjectDescription) {
$this->gitLabClient->projects()
->update($project['id'], $this->getGitLabProjectDefaults());
try {
$this->gitLabClient->projects()
->uploadAvatar($project['id'], __DIR__ . '/drupal_icon.png');
} catch (ValidationFailedException) {
$this->io->warning("Failed to upload project avatar");
}
}
}
}