@agilecustoms/envctl 0.29.0 → 0.30.1
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.
|
@@ -34,7 +34,11 @@ export class EnvApiClient {
|
|
|
34
34
|
env.status = EnvStatus.Active;
|
|
35
35
|
}
|
|
36
36
|
async lockForUpdate(env) {
|
|
37
|
-
const
|
|
37
|
+
const body = {
|
|
38
|
+
stateFile: env.stateFile,
|
|
39
|
+
lockFile: env.lockFile
|
|
40
|
+
};
|
|
41
|
+
const { status } = await this.httpClient.post(`/ci/env/${env.key}/lock-for-update`, body);
|
|
38
42
|
env.status = status;
|
|
39
43
|
}
|
|
40
44
|
async delete(env) {
|
|
@@ -19,7 +19,7 @@ export class TerraformAdapter {
|
|
|
19
19
|
return acc;
|
|
20
20
|
}, new Map());
|
|
21
21
|
}
|
|
22
|
-
|
|
22
|
+
getStateFile(cwd) {
|
|
23
23
|
const dir = cwd ?? process.cwd();
|
|
24
24
|
const statePath = path.join(dir, '.terraform', 'terraform.tfstate');
|
|
25
25
|
if (!fs.existsSync(statePath)) {
|
|
@@ -32,12 +32,29 @@ export class TerraformAdapter {
|
|
|
32
32
|
catch (err) {
|
|
33
33
|
throw new KnownException(`Failed to read terraform state file: ${statePath}`, { cause: err });
|
|
34
34
|
}
|
|
35
|
+
return content;
|
|
36
|
+
}
|
|
37
|
+
getLockFile(cwd) {
|
|
38
|
+
const dir = cwd ?? process.cwd();
|
|
39
|
+
const lockPath = path.join(dir, '.terraform.lock.hcl');
|
|
40
|
+
if (!fs.existsSync(lockPath)) {
|
|
41
|
+
throw new KnownException(`Terraform lock file not found at: ${lockPath}`);
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
return fs.readFileSync(lockPath, 'utf8');
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
throw new KnownException(`Failed to read terraform lock file: ${lockPath}`, { cause: err });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
getKey(cwd) {
|
|
51
|
+
const stateFile = this.getStateFile(cwd);
|
|
35
52
|
let tfstate;
|
|
36
53
|
try {
|
|
37
|
-
tfstate = JSON.parse(
|
|
54
|
+
tfstate = JSON.parse(stateFile);
|
|
38
55
|
}
|
|
39
56
|
catch (err) {
|
|
40
|
-
throw new KnownException(`Failed to parse terraform state file:
|
|
57
|
+
throw new KnownException(`Failed to parse terraform state file: .terraform/terraform.tfstate`, { cause: err });
|
|
41
58
|
}
|
|
42
59
|
const backendJson = tfstate.backend;
|
|
43
60
|
const type = backendJson?.type;
|
package/dist/service/EnvCtl.js
CHANGED
|
@@ -104,6 +104,11 @@ export class EnvCtl {
|
|
|
104
104
|
const createEnv = { key };
|
|
105
105
|
return await this.envApi.createEphemeral(createEnv);
|
|
106
106
|
}
|
|
107
|
+
async lockTerraform(env, cwd) {
|
|
108
|
+
await this.terraformAdapter.lockProviders(cwd);
|
|
109
|
+
env.stateFile = this.terraformAdapter.getStateFile(cwd);
|
|
110
|
+
env.lockFile = this.terraformAdapter.getLockFile(cwd);
|
|
111
|
+
}
|
|
107
112
|
async deploy(tfArgs, cwd) {
|
|
108
113
|
const key = this.terraformAdapter.getKey(cwd);
|
|
109
114
|
let env = await this.tryGetEnv(key);
|
|
@@ -111,14 +116,14 @@ export class EnvCtl {
|
|
|
111
116
|
const kind = this.cliHelper.getKind(cwd);
|
|
112
117
|
console.log('Creating env metadata');
|
|
113
118
|
const userName = this.configService.getUserName();
|
|
114
|
-
await this.terraformAdapter.lockProviders(cwd);
|
|
115
119
|
const createEnv = { key, owner: userName, kind };
|
|
120
|
+
await this.lockTerraform(createEnv, cwd);
|
|
116
121
|
env = await this.envApi.create(createEnv);
|
|
117
122
|
}
|
|
118
123
|
else {
|
|
119
124
|
this.checkInput(env, cwd);
|
|
120
125
|
this.checkStatus(env);
|
|
121
|
-
await this.
|
|
126
|
+
await this.lockTerraform(env, cwd);
|
|
122
127
|
if (env.status === EnvStatus.Init) {
|
|
123
128
|
await this.envApi.lockForUpdate(env);
|
|
124
129
|
}
|
|
@@ -181,7 +186,7 @@ export class EnvCtl {
|
|
|
181
186
|
return;
|
|
182
187
|
}
|
|
183
188
|
}
|
|
184
|
-
console.log('
|
|
189
|
+
console.log('Sending delete command');
|
|
185
190
|
const message = await this.envApi.delete(env);
|
|
186
191
|
console.log(message);
|
|
187
192
|
}
|
|
@@ -223,6 +228,7 @@ export class EnvCtl {
|
|
|
223
228
|
if (!env) {
|
|
224
229
|
throw new KnownException(`Environment ${key} does not exist`);
|
|
225
230
|
}
|
|
231
|
+
console.log(`Env ${key} status: ${env.status}`);
|
|
226
232
|
return env;
|
|
227
233
|
}
|
|
228
234
|
async promptUnlock(env, statuses = [EnvStatus.Deploying, EnvStatus.Updating]) {
|