duoops 0.2.4 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,10 +24,13 @@ const hasBinary = (command) => {
24
24
  const detectGlabToken = (host = 'gitlab.com') => {
25
25
  try {
26
26
  const token = execSync(`glab config get token --host ${host}`, { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'] }).trim();
27
- if (token)
27
+ if (!token)
28
+ return;
29
+ const status = execSync(`curl -sf -o /dev/null -w "%{http_code}" -H "Authorization: Bearer ${token}" "https://${host}/api/v4/user"`, { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'], timeout: 5000 }).trim();
30
+ if (status === '200')
28
31
  return { source: 'glab CLI', token };
29
32
  }
30
- catch { /* glab not installed or not authenticated */ }
33
+ catch { /* glab not installed, not authenticated, or token expired */ }
31
34
  };
32
35
  const detectGitRemotePath = () => {
33
36
  try {
@@ -559,7 +562,7 @@ export default class Init extends Command {
559
562
  if (!hasBinary('gcloud')) {
560
563
  this.error('gcloud CLI is required to provision a runner. Install it from https://cloud.google.com/sdk');
561
564
  }
562
- const vmName = await input({
565
+ let vmName = await input({
563
566
  default: 'duoops-runner',
564
567
  message: 'Runner VM name',
565
568
  });
@@ -592,6 +595,54 @@ export default class Init extends Command {
592
595
  if (!runnerToken) {
593
596
  this.error('Runner token required to register the VM.');
594
597
  }
598
+ let vmExists = false;
599
+ try {
600
+ execSync(`gcloud compute instances describe ${vmName} --project=${gcpProjectId} --zone=${gcpZone} --format="value(name)"`, { stdio: ['pipe', 'pipe', 'pipe'] });
601
+ vmExists = true;
602
+ }
603
+ catch { /* does not exist */ }
604
+ if (vmExists) {
605
+ const vmAction = await select({
606
+ choices: [
607
+ { name: 'Use existing VM', value: 'use' },
608
+ { name: 'Delete and recreate', value: 'recreate' },
609
+ { name: 'Use a different name', value: 'rename' },
610
+ ],
611
+ message: `VM "${vmName}" already exists in ${gcpZone}. What would you like to do?`,
612
+ });
613
+ if (vmAction === 'use') {
614
+ this.log(green(`Using existing VM "${vmName}".`));
615
+ let gcpInstanceId = '';
616
+ try {
617
+ gcpInstanceId = execSync(`gcloud compute instances describe ${vmName} --project=${gcpProjectId} --zone=${gcpZone} --format="value(id)"`, { encoding: 'utf8' }).trim();
618
+ }
619
+ catch {
620
+ this.warn('Unable to read instance ID automatically.');
621
+ }
622
+ return {
623
+ gcpInstanceId,
624
+ gcpZone,
625
+ instanceName: vmName,
626
+ machineType,
627
+ runnerTag,
628
+ };
629
+ }
630
+ if (vmAction === 'recreate') {
631
+ this.log(gray(`Deleting VM "${vmName}"...`));
632
+ try {
633
+ execSync(`gcloud compute instances delete ${vmName} --project=${gcpProjectId} --zone=${gcpZone} --quiet`, { stdio: 'inherit' });
634
+ this.log(green(`Deleted "${vmName}".`));
635
+ }
636
+ catch {
637
+ this.error(`Failed to delete VM "${vmName}". Delete it manually and retry.`);
638
+ }
639
+ }
640
+ if (vmAction === 'rename') {
641
+ vmName = await input({
642
+ message: 'New VM name',
643
+ });
644
+ }
645
+ }
595
646
  const startupScript = buildStartupScript({
596
647
  gitlabUrl: normalizeGitLabUrl(auth.baseUrl),
597
648
  runnerTag,
@@ -669,5 +669,5 @@
669
669
  ]
670
670
  }
671
671
  },
672
- "version": "0.2.4"
672
+ "version": "0.2.6"
673
673
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "duoops",
3
3
  "description": "Toolset for Explainable and Sustainable CI on Gitlab.",
4
- "version": "0.2.4",
4
+ "version": "0.2.6",
5
5
  "author": "Younes Laaroussi",
6
6
  "bin": {
7
7
  "duoops": "./bin/run.js"