gitgreen 1.0.1 → 1.0.3

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.
package/dist/cli.js CHANGED
@@ -9,6 +9,7 @@ const commander_1 = require("commander");
9
9
  const kleur_1 = require("kleur");
10
10
  const asciichart_1 = __importDefault(require("asciichart"));
11
11
  const config_1 = require("./config");
12
+ const package_json_1 = require("../package.json");
12
13
  const index_1 = require("./index");
13
14
  const init_1 = require("./init");
14
15
  const cloudwatch_1 = require("./lib/aws/cloudwatch");
@@ -279,6 +280,7 @@ const runCalculate = async (opts) => {
279
280
  };
280
281
  program
281
282
  .name('gitgreen')
283
+ .version(package_json_1.version)
282
284
  .description('GitGreen carbon calculator using real timeseries metrics')
283
285
  .option('-p, --provider <provider>', 'Cloud provider (gcp|aws)', config_1.config.defaultProvider)
284
286
  .option('-m, --machine <type>', 'Machine type (e.g., e2-standard-4)')
package/dist/init.js CHANGED
@@ -889,11 +889,11 @@ const runInit = async (opts = {}) => {
889
889
  (0, child_process_1.execSync)(`gcloud projects add-iam-policy-binding ${gcpProjectId} --member="serviceAccount:${saEmail}" --role="roles/monitoring.viewer" --quiet 2>/dev/null`, { stdio: 'pipe' });
890
890
  }
891
891
  catch { }
892
- // Check if VM exists
892
+ // Check if VM exists and is running
893
893
  let vmExists = false;
894
894
  try {
895
- (0, child_process_1.execSync)(`gcloud compute instances describe ${vmName} --project=${gcpProjectId} --zone=${gcpZone} 2>/dev/null`, { stdio: 'pipe' });
896
- vmExists = true;
895
+ const status = (0, child_process_1.execSync)(`gcloud compute instances describe ${vmName} --project=${gcpProjectId} --zone=${gcpZone} --format="value(status)" 2>/dev/null`, { encoding: 'utf8' }).trim();
896
+ vmExists = status === 'RUNNING';
897
897
  }
898
898
  catch { }
899
899
  const startupScript = `#!/usr/bin/env bash
@@ -944,20 +944,12 @@ systemctl start gitlab-runner
944
944
  try {
945
945
  (0, child_process_1.execSync)(`gcloud compute instances create ${vmName} --project=${gcpProjectId} --zone=${gcpZone} --machine-type=${vmMachineType} --service-account=${saEmail} --scopes=https://www.googleapis.com/auth/cloud-platform --metadata=runner-token="${runnerToken}" --metadata-from-file=startup-script=${tmpFile} --image-family=debian-12 --image-project=debian-cloud`, { stdio: 'inherit' });
946
946
  console.log((0, kleur_1.green)(`VM ${vmName} created. Runner will register in 2-3 minutes.`));
947
- console.log((0, kleur_1.gray)('Waiting for runner to register...'));
948
- // Wait for instance ID file
949
- for (let i = 0; i < 30; i++) {
950
- await new Promise(r => setTimeout(r, 10000));
951
- try {
952
- gcpInstanceId = (0, child_process_1.execSync)(`gcloud compute ssh ${vmName} --project=${gcpProjectId} --zone=${gcpZone} --command="cat /opt/gitlab-runner-instance-id.txt 2>/dev/null"`, { encoding: 'utf8' }).trim();
953
- if (gcpInstanceId) {
954
- console.log((0, kleur_1.green)(`Runner registered. Instance ID: ${gcpInstanceId}`));
955
- break;
956
- }
957
- }
958
- catch { }
959
- console.log((0, kleur_1.gray)(`Waiting... (${i + 1}/30)`));
947
+ // Get the instance ID of the newly created VM
948
+ try {
949
+ const instanceInfo = (0, child_process_1.execSync)(`gcloud compute instances describe ${vmName} --project=${gcpProjectId} --zone=${gcpZone} --format="value(id)"`, { encoding: 'utf8' });
950
+ gcpInstanceId = instanceInfo.trim();
960
951
  }
952
+ catch { }
961
953
  }
962
954
  catch (err) {
963
955
  console.log((0, kleur_1.red)('Failed to create VM: ' + err.message));
@@ -1158,7 +1150,17 @@ systemctl start gitlab-runner
1158
1150
  // Create key
1159
1151
  console.log((0, kleur_1.gray)('Creating key...'));
1160
1152
  const tmpKeyPath = `/tmp/gitgreen-sa-key-${Date.now()}.json`;
1161
- (0, child_process_1.execSync)(`gcloud iam service-accounts keys create ${tmpKeyPath} --iam-account=${saEmail}`, { stdio: 'inherit' });
1153
+ try {
1154
+ (0, child_process_1.execSync)(`gcloud iam service-accounts keys create ${tmpKeyPath} --iam-account=${saEmail}`, { stdio: 'inherit' });
1155
+ }
1156
+ catch {
1157
+ console.log((0, kleur_1.red)('\nFailed to create service account key.'));
1158
+ console.log((0, kleur_1.gray)('This usually means the service account has too many keys (limit: 10).'));
1159
+ console.log((0, kleur_1.gray)('\nTo fix, delete old keys:'));
1160
+ console.log((0, kleur_1.green)(` gcloud iam service-accounts keys list --iam-account=${saEmail}`));
1161
+ console.log((0, kleur_1.green)(` gcloud iam service-accounts keys delete KEY_ID --iam-account=${saEmail}`));
1162
+ process.exit(1);
1163
+ }
1162
1164
  saKeyBase64 = fs_1.default.readFileSync(tmpKeyPath).toString('base64');
1163
1165
  fs_1.default.unlinkSync(tmpKeyPath);
1164
1166
  console.log((0, kleur_1.green)('Service account key created'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gitgreen",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "GitGreen CLI for carbon reporting in GitLab pipelines (GCP/AWS)",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",