gitgreen 1.0.2 → 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 +2 -0
- package/dist/init.js +20 -4
- package/package.json +1 -1
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`, {
|
|
896
|
-
vmExists =
|
|
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,6 +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
|
+
// 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();
|
|
951
|
+
}
|
|
952
|
+
catch { }
|
|
947
953
|
}
|
|
948
954
|
catch (err) {
|
|
949
955
|
console.log((0, kleur_1.red)('Failed to create VM: ' + err.message));
|
|
@@ -1144,7 +1150,17 @@ systemctl start gitlab-runner
|
|
|
1144
1150
|
// Create key
|
|
1145
1151
|
console.log((0, kleur_1.gray)('Creating key...'));
|
|
1146
1152
|
const tmpKeyPath = `/tmp/gitgreen-sa-key-${Date.now()}.json`;
|
|
1147
|
-
|
|
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
|
+
}
|
|
1148
1164
|
saKeyBase64 = fs_1.default.readFileSync(tmpKeyPath).toString('base64');
|
|
1149
1165
|
fs_1.default.unlinkSync(tmpKeyPath);
|
|
1150
1166
|
console.log((0, kleur_1.green)('Service account key created'));
|