Skip to content

Commit

Permalink
vrt - add seed support (#176)
Browse files Browse the repository at this point in the history
* vrt - add seed support

Signed-off-by: André Bauer <[email protected]>

* fix superlinter

Signed-off-by: André Bauer <[email protected]>

* update postgres

Signed-off-by: André Bauer <[email protected]>

---------

Signed-off-by: André Bauer <[email protected]>
  • Loading branch information
monotek authored Jan 18, 2024
1 parent 8d065f6 commit c979c7a
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions charts/visual-regression-tracker/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
dependencies:
- name: postgresql
version: 13.2.23
version: 13.3.0
repository: https://charts.bitnami.com/bitnami
condition: vrtConfig.postgresql.enabled
98 changes: 98 additions & 0 deletions charts/visual-regression-tracker/files/seed.ts
Original file line number Diff line number Diff line change
@@ -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}`);
});
}
}
11 changes: 11 additions & 0 deletions charts/visual-regression-tracker/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
12 changes: 12 additions & 0 deletions charts/visual-regression-tracker/templates/secrets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
7 changes: 7 additions & 0 deletions charts/visual-regression-tracker/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions charts/visual-regression-tracker/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ imagePullSecrets: []
vrtConfig:
appFrontendUrl: "http://localhost:8080"
bodyParserJsonLimit: "5mb"
defaults:
apiKey: "DEFAULT_USER_API_KEY_TO_BE_CHANGED"
email: "[email protected]"
pass: "Change_Me"
project: "Default"
elasticsearch:
host: ""
pass: ""
Expand Down Expand Up @@ -166,6 +171,10 @@ persistence:
annotations: {}

secrets:
defaults:
useExisting: false
secretName: vrt
secretKey: seed.ts
elasticsearch:
useExisting: false
secretKey: es-pass
Expand Down

0 comments on commit c979c7a

Please sign in to comment.