From c979c7aceae38283687a8304b0a6d239d95b970d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bauer?= Date: Thu, 18 Jan 2024 19:44:47 +0100 Subject: [PATCH] vrt - add seed support (#176) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * vrt - add seed support Signed-off-by: André Bauer * fix superlinter Signed-off-by: André Bauer * update postgres Signed-off-by: André Bauer --------- Signed-off-by: André Bauer --- .github/workflows/ci.yaml | 2 + charts/visual-regression-tracker/Chart.yaml | 4 +- .../visual-regression-tracker/files/seed.ts | 98 +++++++++++++++++++ .../templates/_helpers.tpl | 11 +++ .../templates/secrets.yaml | 12 +++ .../templates/statefulset.yaml | 7 ++ charts/visual-regression-tracker/values.yaml | 9 ++ 7 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 charts/visual-regression-tracker/files/seed.ts diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index fe599c2..7238e80 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -45,6 +45,8 @@ jobs: VALIDATE_JSCPD: false VALIDATE_KUBERNETES_KUBECONFORM: false VALIDATE_YAML: false + VALIDATE_TYPESCRIPT_ES: false + VALIDATE_TYPESCRIPT_STANDARD: false kubeconform-chart: runs-on: ubuntu-22.04 diff --git a/charts/visual-regression-tracker/Chart.yaml b/charts/visual-regression-tracker/Chart.yaml index 64a8174..feec464 100644 --- a/charts/visual-regression-tracker/Chart.yaml +++ b/charts/visual-regression-tracker/Chart.yaml @@ -6,13 +6,13 @@ sources: - https://github.com/Visual-Regression-Tracker/Visual-Regression-Tracker - https://github.com/kokuwaio/helm-charts/tree/main/charts/visual-regression-tracker type: application -version: 3.0.0 +version: 3.1.0 appVersion: "5.0.3" maintainers: - name: monotek email: monotek23@gmail.com dependencies: - name: postgresql - version: 13.2.23 + version: 13.3.0 repository: https://charts.bitnami.com/bitnami condition: vrtConfig.postgresql.enabled diff --git a/charts/visual-regression-tracker/files/seed.ts b/charts/visual-regression-tracker/files/seed.ts new file mode 100644 index 0000000..3f3359d --- /dev/null +++ b/charts/visual-regression-tracker/files/seed.ts @@ -0,0 +1,98 @@ +/** +* @see https://www.prisma.io/docs/guides/migrate/seed-database +*/ +import { PrismaClient, Role } from '@prisma/client'; +import { genSalt, hash } from 'bcryptjs'; + +const prisma = new PrismaClient({ + // 'info' | 'query' | 'warn' | 'error' + log: ['query'], +}); + +async function seed() { + await prisma.$connect(); + console.log('Seeding default data...'); + await Promise.all([createDefaultUser(), createDefaultProject()]); + await prisma.$disconnect(); +} + +seed() + .catch((e) => console.error('e', e)) + .finally(async () => await prisma.$disconnect()); + +async function createDefaultUser() { + let userList = []; + try { + userList = await prisma.user.findMany(); + console.log(userList); + } + catch (error) { + // Expected to see that "user" table does not exist + console.log(error.message); + } + + const defaultApiKey = '{{ .Values.vrtConfig.defaults.apiKey }}'; + const defaultEmail = '{{ .Values.vrtConfig.defaults.email }}'; + const defaultPassword = '{{ .Values.vrtConfig.defaults.pass }}'; + const salt = await genSalt(10); + + await prisma.user + .upsert({ + where: { + email: defaultEmail, + }, + update: { + role: Role.admin, + }, + create: { + email: defaultEmail, + firstName: 'fname', + lastName: 'lname', + role: Role.admin, + apiKey: defaultApiKey, + password: await hash(defaultPassword, salt), + }, + }) + .then((user) => { + console.log('###########################'); + console.log('####### DEFAULT USER ######'); + console.log('###########################'); + console.log(''); + console.log( + `The user with the email "${defaultEmail}" and password "${defaultPassword}" was created (if not changed before)` + ); + console.log(`The Api key is: ${user.apiKey}`); + }); +} + +async function createDefaultProject() { + let projectList = []; + try { + projectList = await prisma.project.findMany(); + console.log(projectList); + } + catch (error) { + // Expected to see that "project" table does not exist + console.log(error.message); + } + + const defaultProject = '{{ .Values.vrtConfig.defaults.project }}'; + + if (projectList.length === 0) { + await prisma.project + .create({ + data: { + name: defaultProject, + }, + }) + .then((project) => { + console.log('##############################'); + console.log('## CREATING DEFAULT PROJECT ##'); + console.log('##############################'); + console.log(''); + console.log(`Project key: ${project.id}`); + console.log(`Project name ${project.name}`); + console.log(`Project name ${project.mainBranchName}`); + }); + } +} diff --git a/charts/visual-regression-tracker/templates/_helpers.tpl b/charts/visual-regression-tracker/templates/_helpers.tpl index 1f1c451..f7a32ca 100644 --- a/charts/visual-regression-tracker/templates/_helpers.tpl +++ b/charts/visual-regression-tracker/templates/_helpers.tpl @@ -93,3 +93,14 @@ postgresql secret name {{ template "visual-regression-tracker.fullname" . }}-{{ .Values.secrets.postgresql.secretName }} {{- end -}} {{- end -}} + +{{/* +vrt secret name +*/}} +{{- define "visual-regression-tracker.vrtSecretName" -}} +{{- if .Values.secrets.defaults.useExisting -}} +{{ .Values.secrets.defaults.secretName }} +{{- else -}} +{{ template "visual-regression-tracker.fullname" . }}-{{ .Values.secrets.defaults.secretName }} +{{- end -}} +{{- end -}} diff --git a/charts/visual-regression-tracker/templates/secrets.yaml b/charts/visual-regression-tracker/templates/secrets.yaml index e5922ef..d5c0c3d 100644 --- a/charts/visual-regression-tracker/templates/secrets.yaml +++ b/charts/visual-regression-tracker/templates/secrets.yaml @@ -34,3 +34,15 @@ type: Opaque data: {{ .Values.secrets.postgresql.secretKey }}: {{ .Values.vrtConfig.postgresql.pass | b64enc | quote }} {{ end }} +{{ if and (or .Values.vrtConfig.defaults.apiKey .Values.vrtConfig.defaults.pass) (not .Values.secrets.defaults.useExisting) }} +--- +apiVersion: v1 +kind: Secret +metadata: + name: {{ template "visual-regression-tracker.vrtSecretName" . }} + labels: + {{- include "visual-regression-tracker.labels" . | nindent 4 }} +type: Opaque +data: + {{ .Values.secrets.defaults.secretKey }}: {{ tpl (.Files.Get "files/seed.ts") $ | b64enc }} +{{ end }} diff --git a/charts/visual-regression-tracker/templates/statefulset.yaml b/charts/visual-regression-tracker/templates/statefulset.yaml index 949fb1e..74a02dd 100644 --- a/charts/visual-regression-tracker/templates/statefulset.yaml +++ b/charts/visual-regression-tracker/templates/statefulset.yaml @@ -53,6 +53,10 @@ spec: {{- toYaml .Values.vrtComponents.migration.resources | nindent 12 }} securityContext: {{- toYaml .Values.vrtComponents.migration.securityContext | nindent 12 }} + volumeMounts: + - name: vrt + mountPath: /app/seed.ts + subPath: seed.ts containers: - name: {{ .Chart.Name }}-api env: @@ -176,6 +180,9 @@ spec: defaultMode: 0777 - name: tmp {{- toYaml .Values.vrtComponents.ui.tmpDirVolume | nindent 10 }} + - name: vrt + secret: + secretName: {{ template "visual-regression-tracker.vrtSecretName" . }} {{- if and .Values.persistence.enabled .Values.persistence.existingClaim }} - name: {{ template "visual-regression-tracker.fullname" . }} persistentVolumeClaim: diff --git a/charts/visual-regression-tracker/values.yaml b/charts/visual-regression-tracker/values.yaml index a9bdccc..1fbb384 100644 --- a/charts/visual-regression-tracker/values.yaml +++ b/charts/visual-regression-tracker/values.yaml @@ -6,6 +6,11 @@ imagePullSecrets: [] vrtConfig: appFrontendUrl: "http://localhost:8080" bodyParserJsonLimit: "5mb" + defaults: + apiKey: "DEFAULT_USER_API_KEY_TO_BE_CHANGED" + email: "admin@example.com" + pass: "Change_Me" + project: "Default" elasticsearch: host: "" pass: "" @@ -166,6 +171,10 @@ persistence: annotations: {} secrets: + defaults: + useExisting: false + secretName: vrt + secretKey: seed.ts elasticsearch: useExisting: false secretKey: es-pass