@catladder/cli 0.0.0-main-70bd51e4 → 0.0.0-main-82f64437

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/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@bitwarden/cli": "^1.8.0",
20
- "@catladder/pipeline": "0.0.0-main-70bd51e4",
20
+ "@catladder/pipeline": "0.0.0-main-82f64437",
21
21
  "@kubernetes/client-node": "^0.16.1",
22
22
  "child-process-promise": "^2.2.1",
23
23
  "clipboardy": "^2.2.0",
@@ -60,6 +60,6 @@
60
60
  "eslint": "^8.7.0",
61
61
  "typescript": "^4.5.4"
62
62
  },
63
- "version": "0.0.0-main-70bd51e4",
63
+ "version": "0.0.0-main-82f64437",
64
64
  "stableVersion": "0.0.0-test7"
65
65
  }
@@ -1,5 +1,5 @@
1
1
  import { Config } from "@catladder/pipeline";
2
- import { spawn } from "child-process-promise";
2
+ import { spawn, exec } from "child-process-promise";
3
3
  import { writeFile } from "fs-extra";
4
4
  import { safeDump } from "js-yaml";
5
5
  import { format } from "prettier";
@@ -44,9 +44,10 @@ export const writeConfig = async (
44
44
  encoding: "utf-8",
45
45
  });
46
46
  vorpal.log("adding type @catladder/pipeline....");
47
- await spawn("yarn add @catladder/pipeline -D", {
47
+ await spawn("yarn add @catladder/pipeline -DW", {
48
48
  shell: true,
49
49
  });
50
+ await exec("git add " + gitRoot + "/catladder.ts");
50
51
  } else {
51
52
  const content = safeDump(config);
52
53
 
@@ -61,6 +62,7 @@ export const writeConfig = async (
61
62
  encoding: "utf-8",
62
63
  }
63
64
  );
65
+ await exec("git add " + gitRoot + "/catladder.ts");
64
66
  }
65
67
  vorpal.log("done!");
66
68
  vorpal.log("");
@@ -1,17 +1,26 @@
1
1
  import {
2
2
  BuildConfig,
3
+ ComponentConfig,
3
4
  Config,
4
5
  DeployConfigKubernetes,
5
6
  Env,
6
7
  } from "@catladder/pipeline";
7
- import { readFile, writeFile } from "fs-extra";
8
+ import {
9
+ existsSync,
10
+ lstatSync,
11
+ readdirSync,
12
+ readFile,
13
+ writeFile,
14
+ } from "fs-extra";
8
15
  import { isEmpty } from "lodash";
16
+ import { join } from "path";
9
17
  import Vorpal from "vorpal";
10
18
  import {
11
19
  getGitlabCi,
12
20
  getGitlabCiFilePath,
13
21
  } from "../../../../config/getProjectConfig";
14
22
  import { readYaml } from "../../../../utils/files";
23
+ import { syncBitwarden } from "../../../../utils/passwordstore";
15
24
  import { getGitRoot } from "../../../../utils/projects";
16
25
  import { writeConfig } from "../../config/writeConfig";
17
26
  import { migrateSecrets } from "./migrateSecrets";
@@ -44,6 +53,16 @@ const transformVars = (rawVars: {
44
53
  };
45
54
  };
46
55
 
56
+ const getLegacyMonorepoSubCiFiles = (dir: string) => {
57
+ return readdirSync(dir)
58
+ .filter((file) => lstatSync(file).isDirectory())
59
+ .map((dir) => ({
60
+ dir,
61
+ ci: join(dir, ".gitlab-ci.yml"),
62
+ }))
63
+ .filter(({ ci }) => existsSync(ci));
64
+ };
65
+
47
66
  const transformValues = (
48
67
  valuesIn: Record<string, any>
49
68
  ): DeployConfigKubernetes["values"] => {
@@ -110,61 +129,78 @@ export const migrateV2 = async (vorpal: Vorpal) => {
110
129
  vorpal.log("");
111
130
 
112
131
  if (shouldContinue) {
113
- const { env, ...baseValues } =
114
- (await readYaml(gitRoot + "/values.yml")) ?? {};
115
-
116
- const startCommand = baseValues?.application?.command?.join(" ");
117
-
132
+ const createComponent = async (
133
+ dir: string,
134
+ ciFile: OldGitlabCiFile
135
+ ): Promise<ComponentConfig> => {
136
+ const { env, ...baseValues } =
137
+ (await readYaml(dir + "/values.yml")) ?? {};
138
+
139
+ const startCommand = baseValues?.application?.command?.join(" ");
140
+ return {
141
+ vars: env ? transformVars(env) : undefined,
142
+ dir: dir,
143
+ build: {
144
+ type: detectBuildConfig(ciFile),
145
+ startCommand: startCommand,
146
+ } as BuildConfig,
147
+ deploy: {
148
+ type: "kubernetes",
149
+ values: transformValues(baseValues),
150
+ cluster: CLUSTER_NAME,
151
+ },
152
+ env: await LEGACY_ENVS.reduce<Promise<Env>>(
153
+ async (acc, envName) => {
154
+ if (envName === "stage" && !STAGING_ENABLED) {
155
+ return {
156
+ ...(await acc),
157
+ [envName]: false,
158
+ };
159
+ }
160
+ const newEnvName = envName === "dev-local" ? "local" : envName;
161
+
162
+ const envValues =
163
+ (await readYaml(dir + `/values-${envName}.yml`)) ?? {};
164
+ const { env, ...rest } = envValues;
165
+ const hostname = rest?.application?.hostname;
166
+ const values = transformValues(rest);
167
+ return {
168
+ ...(await acc),
169
+ [newEnvName]: {
170
+ hostname: hostname,
171
+ vars: env ? transformVars(env) : undefined,
172
+ deploy: values
173
+ ? {
174
+ values: values,
175
+ }
176
+ : undefined,
177
+ },
178
+ };
179
+ },
180
+ Promise.resolve({})
181
+ ),
182
+ };
183
+ };
184
+ let components: Record<string, ComponentConfig>;
185
+ if (detectBuildConfig(gitlabCi) === "monorepo") {
186
+ components = await getLegacyMonorepoSubCiFiles(gitRoot).reduce<
187
+ Promise<Record<string, ComponentConfig>>
188
+ >(async (acc, el) => {
189
+ return {
190
+ ...(await acc),
191
+ [el.dir]: await createComponent(el.dir, await readYaml(el.ci)),
192
+ };
193
+ }, Promise.resolve({}));
194
+ } else {
195
+ components = {
196
+ [COMPONENT_NAME]: await createComponent(APP_DIR, gitlabCi),
197
+ };
198
+ }
118
199
  // we only have one component
119
200
  const config: Config = {
120
201
  customerName: CUSTOMER_NAME,
121
202
  appName: APP_NAME,
122
- components: {
123
- [COMPONENT_NAME]: {
124
- vars: env ? transformVars(env) : undefined,
125
- dir: APP_DIR ?? ".",
126
- build: {
127
- type: detectBuildConfig(gitlabCi),
128
- startCommand: startCommand,
129
- } as BuildConfig,
130
- deploy: {
131
- type: "kubernetes",
132
- values: transformValues(baseValues),
133
- cluster: CLUSTER_NAME,
134
- },
135
- env: await LEGACY_ENVS.reduce<Promise<Env>>(
136
- async (acc, envName) => {
137
- if (envName === "stage" && !STAGING_ENABLED) {
138
- return {
139
- ...(await acc),
140
- [envName]: false,
141
- };
142
- }
143
- const newEnvName =
144
- envName === "dev-local" ? "local" : envName;
145
-
146
- const envValues =
147
- (await readYaml(gitRoot + `/values-${envName}.yml`)) ?? {};
148
- const { env, ...rest } = envValues;
149
- const hostname = rest?.application?.hostname;
150
- const values = transformValues(rest);
151
- return {
152
- ...(await acc),
153
- [newEnvName]: {
154
- hostname: hostname,
155
- vars: env ? transformVars(env) : undefined,
156
- deploy: values
157
- ? {
158
- values: values,
159
- }
160
- : undefined,
161
- },
162
- };
163
- },
164
- Promise.resolve({})
165
- ),
166
- },
167
- },
203
+ components,
168
204
  };
169
205
 
170
206
  const comment =
@@ -187,8 +223,10 @@ export const migrateV2 = async (vorpal: Vorpal) => {
187
223
 
188
224
  this.log("-------------------");
189
225
  this.log("migrate secrets");
226
+ await syncBitwarden();
190
227
  for (const env of LEGACY_ENVS) {
191
- await migrateSecrets(this, gitlabCi, env);
228
+ if (env === "stage" && !STAGING_ENABLED) return;
229
+ await migrateSecrets(this, config, env);
192
230
  }
193
231
 
194
232
  this.log("-------------------");
@@ -1,32 +1,37 @@
1
+ import { Config, getEnvironment } from "@catladder/pipeline";
1
2
  import { load } from "js-yaml";
3
+ import { pick } from "lodash";
2
4
  import { CommandInstance } from "vorpal";
3
5
  import { upsertAllVariables } from "../../../../utils/gitlab";
4
6
  import { readPass, trashItem } from "../../../../utils/passwordstore";
5
7
  import { LEGACY_ENVS } from "./fromv2";
6
- import { OldGitlabCiFile } from "./oldGitlabCi";
7
8
 
8
- const getPassPath = (gitlabCi: OldGitlabCiFile, env: string) => {
9
- return `${gitlabCi.variables.CUSTOMER_NAME}/${gitlabCi.variables.APP_NAME}/${env}/secrets.yml`;
9
+ const getPassPath = (newConfig: Config, env: string) => {
10
+ return `${newConfig.customerName}/${newConfig.appName}/${env}/secrets.yml`;
10
11
  };
11
12
 
12
13
  export const migrateSecrets = async (
13
14
  vorpal: CommandInstance,
14
- gitlabCi: OldGitlabCiFile,
15
- env: typeof LEGACY_ENVS[number]
15
+ newConfig: Config,
16
+ oldEnv: typeof LEGACY_ENVS[number]
16
17
  ) => {
17
- const path = getPassPath(gitlabCi, env);
18
+ const newEnv = oldEnv === "dev-local" ? "local" : oldEnv;
19
+ const path = getPassPath(newConfig, oldEnv);
18
20
  try {
19
21
  const yamlstring = await readPass(path);
20
22
  const secrets = load(yamlstring);
21
23
 
22
- await upsertAllVariables(
23
- vorpal,
24
- secrets,
25
- env === "dev-local" ? "local" : env,
26
- gitlabCi.variables.COMPONENT_NAME
27
- );
24
+ Object.keys(newConfig.components).forEach(async (componentName) => {
25
+ const environment = getEnvironment(newConfig, componentName, newEnv);
26
+ await upsertAllVariables(
27
+ vorpal,
28
+ pick(secrets, environment.secretEnvVarKeys),
29
+ newEnv,
30
+ componentName
31
+ );
32
+ });
28
33
  await trashItem(path);
29
34
  } catch (e) {
30
- console.warn(`could not migrate secrets for env '${env}': ${e}`);
35
+ console.warn(`could not migrate secrets for env '${oldEnv}': ${e}`);
31
36
  }
32
37
  };
@@ -18,12 +18,14 @@ export type OldGitlabCiFile = {
18
18
 
19
19
  export const detectBuildConfig = (
20
20
  gitlabCi: OldGitlabCiFile
21
- ): BuildConfig["type"] => {
21
+ ): BuildConfig["type"] | "monorepo" => {
22
22
  const firstInclude = gitlabCi.include[0];
23
23
  if (!firstInclude || gitlabCi.include[0]?.project !== "catladder/gitlab-ci") {
24
24
  throw new Error("unsupported gitlab-ci file");
25
25
  }
26
26
 
27
+ if (firstInclude.file === "monorepo.yml") return "monorepo";
28
+
27
29
  if (firstInclude.file === "node-kubernetes.yml") {
28
30
  return "node";
29
31
  }