Skip to content
Snippets Groups Projects
Commit 1e0b8dbc authored by OZGCloud's avatar OZGCloud
Browse files

ozg-2877 e2e and ansible

parent 79b1a6f4
No related branches found
No related tags found
No related merge requests found
...@@ -141,6 +141,7 @@ pipeline { ...@@ -141,6 +141,7 @@ pipeline {
sh 'helm version' sh 'helm version'
} }
setAnsibleKubeConfig()
} }
} }
stage('Deploy Maven Artifacts to Nexus') { stage('Deploy Maven Artifacts to Nexus') {
...@@ -227,13 +228,12 @@ pipeline { ...@@ -227,13 +228,12 @@ pipeline {
script { script {
def stageName = env.STAGE_NAME def stageName = env.STAGE_NAME
def bezeichner = generateBezeichner(stageName) def bezeichner = generateBezeichner(stageName)
def namespace = generateNamespaceName(bezeichner)
startEnvironment(namespace, 'ea-values.yaml', 'ea-values.yaml', IMAGE_TAG, bezeichner) startEnvironment(bezeichner, stageName, IMAGE_TAG, true)
def testResult = runTests(stageName, 'einheitlicher-ansprechpartner') def testResult = runTests(stageName, bezeichner, 'einheitlicher-ansprechpartner')
shutdownEnvironment(namespace) deleteKopStack(bezeichner, stageName)
if(!testResult) { if(!testResult) {
E2E_FAILED += "${stageName}, " E2E_FAILED += "${stageName}, "
...@@ -254,13 +254,12 @@ pipeline { ...@@ -254,13 +254,12 @@ pipeline {
script { script {
def stageName = env.STAGE_NAME def stageName = env.STAGE_NAME
def bezeichner = generateBezeichner(stageName) def bezeichner = generateBezeichner(stageName)
def namespace = generateNamespaceName(bezeichner)
startEnvironment(namespace, 'values.yaml', 'values.yaml', IMAGE_TAG, bezeichner) startEnvironment(bezeichner, stageName, IMAGE_TAG, false)
def testResult = runTests(stageName, 'main-tests') def testResult = runTests(stageName, bezeichner, 'main-tests')
shutdownEnvironment(namespace) deleteKopStack(bezeichner, stageName)
if(!testResult) { if(!testResult) {
E2E_FAILED += "${stageName}, " E2E_FAILED += "${stageName}, "
...@@ -300,93 +299,188 @@ pipeline { ...@@ -300,93 +299,188 @@ pipeline {
} }
} }
Void startEnvironment(String namespace, String goofyValues, String plutoValues, String imageTag, String bezeichner){ Void startEnvironment(String bezeichner, String stage, String imageTag, Boolean isEa) {
checkIfNamespaceExists(namespace) setupAnsible(imageTag, stage, isEa)
setupAnsible(imageTag) try {
deleteKopStack(bezeichner, stage)
} catch (Exception e) {
echo "deleteKopStack Exception"
}
rolloutKopStack(bezeichner, stage)
addKeycloakGroups(bezeichner, stage)
addKeycloakUser(bezeichner, stage)
}
Void setupAnsible(String imageTag, String stage, Boolean isEa) {
checkoutProvisioningRepo(stage)
if (env.BRANCH_NAME == 'release') {
copyTestEnvironmentToDev(stage)
}
rolloutKopStack(bezeichner) editEnvironemntVersion(stage, imageTag, isEa)
addKeycloakGroups(bezeichner)
addKeycloakUser(bezeichner) if (isEa) {
setupEaEnvironment(stage)
} }
Void setupAnsible(String goofyImageTag) { setPlutoDatabasePassword(stage)
checkoutProvisioningRepo() }
setGoofyImageVersion(goofyImageTag) Void setAnsibleKubeConfig() {
container("ansible") {
configFileProvider([configFile(fileId: 'jenkins-kuby-kubeconfig', variable: 'KUBE_CONFIG')]) {
sh 'mkdir ~/.kube'
sh 'cp ${KUBE_CONFIG} ~/.kube/config'
}
}
} }
Void checkoutProvisioningRepo() { Void checkoutProvisioningRepo(String stage) {
withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) { withCredentials([usernamePassword(credentialsId: 'jenkins-gitea-access-token', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {
dir(stage) {
sh 'git clone https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/provisioning.git' sh 'git clone https://${USER}:${TOKEN}@git.ozg-sh.de/mgm/provisioning.git'
sh 'git checkout ozg-2552'
// todo remove git checkout
dir('provisioning') {
sh 'git checkout ozg-2552-jenkins-e2e'
}
}
} }
} }
Void setGoofyImageVersion(String goofyImageTag) { Void copyTestEnvironmentToDev(stage) {
dir('provisioning') { dir("${stage}/provisioning") {
def devVersions = readYaml file: "playbook/versions/dev.yaml" def devEnvFile = "playbook/inventory/versions/dev.yml"
def testEnvFile = "playbook/inventory/versions/test.yml"
def devVersions = readYaml file: devEnvFile
def testVersions = readYaml file: testEnvFile
devVersions.charts = testVersions.charts
devVersions.versions = testVersions.versions
writeYaml file: devEnvFile, data: devVersions, overwrite: true
}
}
Void editEnvironemntVersion(String stage, String imageTag, Boolean isEa) {
dir("${stage}/provisioning") {
def editFile = "playbook/inventory/versions/dev.yml"
def devVersions = readYaml file: editFile
overrideSpringProfiles = getSpringProfile(isEa)
devVersions.values.goofy.put('env', ['overrideSpringProfiles': overrideSpringProfiles])
devVersions.values.pluto.put('env', ['overrideSpringProfiles': overrideSpringProfiles])
devVersions.versions.goofy.image.tag = imageTag
writeYaml file: editFile, data: devVersions, overwrite: true
sh "cat ${editFile}"
}
}
devVersions.versions.goofy.image.tag = goofyImageTag String getSpringProfile(Boolean isEa) {
if (isEa) {
return "oc,ea,e2e,dev"
}
return "oc,e2e,dev"
}
Void setupEaEnvironment(String stage) {
dir("${stage}/provisioning") {
def editFile = "playbook/inventory/group_vars/all.yml"
def groupVars = readYaml file: editFile
groupVars.kop_einheitlicher_ansprechpartner = true
writeYaml file: editFile, data: groupVars, overwrite: true
}
}
Void setPlutoDatabasePassword(String stage) {
dir("${stage}/provisioning") {
def editFile = "playbook/inventory/versions/dev.yml"
def devVars = readYaml file: editFile
writeYaml file: "playbook/versions/dev.yaml", data: devVersions devVars.values.pluto.database.password = "XnHhfznNWg65NNd"
sh "cat playbook/versions/dev.yaml" writeYaml file: editFile, data: devVars, overwrite: true
} }
} }
Void rolloutKopStack(String bezeichner) { Void rolloutKopStack(String bezeichner, String stage) {
container("ansible") { container("ansible") {
dir('provisioning') { dir("${stage}/provisioning") {
def ansibleVars = """{ def ansibleVars = """{"k8s_context":"ozg-dev", \
"k8s_context": "ozg-dev", "kop_env":"dev", \
"kop_env": "dev", "kop_bezeichner":${bezeichner}, \
"kop_bezeichner": ${bezeichner}, "kop_displayname":${bezeichner}, \
"kop_displayname": ${bezeichner}, "kop_postfach_api_key":"", \
"kop_postfach_api_key": "", "install_afm_adapter":false, \
"install_afm_adapter": false, "install_fs_adapter":false, \
"install_fs_adapter": false, "external_db_enabled":false}"""
"external_db_enabled": false
}"""
sh "ansible-playbook playbook/rollout.yml --extra-vars ${ansibleVars}" sh "ansible-playbook playbook/rollout.yml --extra-vars '${ansibleVars}'"
} }
} }
} }
Void addKeycloakGroups(bezeichner) { Void addKeycloakGroups(String bezeichner, String stage) {
container("ansible") { container("ansible") {
def groupFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/group', returnStdout: true) def groupFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/group', returnStdout: true)
groupFiles.split("\\n").each { group -> groupFiles.split("\\n").each { group ->
def groupJson = sh (script: "cat goofy-client/apps/goofy-e2e/src/fixtures/group/${group}", returnStdout: true) def groupJson = sh (script: "cat goofy-client/apps/goofy-e2e/src/fixtures/group/${group}", returnStdout: true)
def ansibleVars = """{ def ansibleVars = """{"k8s_context":"ozg-dev", \
"k8s_context": "ozg-dev", "kop_env":"dev", \
"kop_env": "dev", "keycloak_realm":"sh-${bezeichner}-dev", \
"keycloak_realm": "sh-${bezeichner}-dev",
"group":${groupJson} "group":${groupJson}
}""" }"""
sh "ansible-playbook playbook/rollout.yml --extra-vars ${ansibleVars}" dir("${stage}/provisioning") {
sh "ansible-playbook playbook/add-keycloak-group.yml --extra-vars '${ansibleVars}'"
}
} }
} }
} }
Void addKeycloakUser(String bezeichner) { Void addKeycloakUser(String bezeichner, String stage) {
container("ansible") {
def userFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/user', returnStdout: true) def userFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/user', returnStdout: true)
userFiles.split("\\n").each { user -> userFiles.split("\\n").each { user ->
def userJson = readJSON file: 'goofy-client/apps/goofy-e2e/src/fixtures/user/'+user def userJson = sh (script: "cat goofy-client/apps/goofy-e2e/src/fixtures/user/${user}", returnStdout: true)
def ansibleVars = """{ def ansibleVars = """{"k8s_context":"ozg-dev", \
"k8s_context": "ozg-dev", "kop_env":"dev", \
"kop_env": "dev", "keycloak_realm":"sh-${bezeichner}-dev", \
"keycloak_realm": "sh-${bezeichner}-dev",
"user":${userJson} "user":${userJson}
}""" }"""
sh "ansible-playbook playbook/rollout.yml --extra-vars ${ansibleVars}" dir("${stage}/provisioning") {
sh "ansible-playbook playbook/add-keycloak-user.yml --extra-vars '${ansibleVars}'"
}
}
}
}
Void deleteKopStack(String bezeichner, String stage) {
container("ansible") {
dir("${stage}/provisioning") {
def ansibleVars = """{"k8s_context":"ozg-dev", \
"kop_env":"dev", \
"kop_bezeichner":${bezeichner}}"""
sh "ansible-playbook playbook/delete-commune.yml --extra-vars '${ansibleVars}'"
}
} }
} }
...@@ -403,79 +497,11 @@ Void publishE2ETestResult(String reportFolder, String reportName) { ...@@ -403,79 +497,11 @@ Void publishE2ETestResult(String reportFolder, String reportName) {
) )
} }
Void checkIfNamespaceExists(String namespace) { String runTests(String stageName, String bezeichner, String reportFolder) {
container("k8s") { def configFile = generateCypressConfig(stageName, bezeichner, reportFolder)
def namespaceList = sh (script: 'kubectl get namespaces', returnStdout: true)
if(namespaceList.contains(namespace)) {
deleteNamespace(namespace)
}
}
}
Void deleteNamespace(String namespace) {
sh "kubectl delete namespace ${namespace}"
}
Void startPluto(String namespace, String values, String bezeichner) {
container("k8s") {
dir('goofy-client/apps/goofy-e2e/deployment-values/pluto') {
if(env.BRANCH_NAME == 'release') {
startPlutoForReleaseTests(namespace, values, bezeichner)
}
else {
startPlutoForSnapshotTests(namespace, values, bezeichner)
}
}
sh "kubectl rollout status statefulset/pluto-database -n ${namespace}"
}
}
Void startPlutoForReleaseTests(String namespace, String values, String bezeichner) {
sh "helm upgrade --install --create-namespace pluto ozg-base-apps/pluto -f ${values} --set kop.bezeichner=${bezeichner} --namespace ${namespace} --wait --wait-for-jobs"
}
Void startPlutoForSnapshotTests(String namespace, String values, String bezeichner) {
sh "helm upgrade --install --create-namespace pluto ozg-base-apps-snapshot/pluto -f ${values} --set kop.bezeichner=${bezeichner} --namespace ${namespace} --version ${getLatestChartVersion('pluto').trim()} --wait --wait-for-jobs"
}
Void startGoofy(String namespace, String values, String imageTag, String bezeichner) {
container("k8s") {
dir('goofy-client/apps/goofy-e2e/deployment-values/goofy') {
if(env.BRANCH_NAME == 'release') {
startGoofyForReleaseTests(namespace, values, bezeichner, imageTag)
}
else {
startGoofyForSnapshotTests(namespace, values, bezeichner, imageTag)
}
}
waitForKeycloakClientCreation(namespace)
createKeycloakGroups(namespace)
generateKeycloakUserYaml(namespace)
applyKeycloakUser(namespace)
}
}
Void startGoofyForReleaseTests(String namespace, String values, String bezeichner, String imageTag) {
sh "helm upgrade --install --create-namespace goofy ozg-base-apps/goofy -f ${values} --set image.tag=${imageTag} --set kop.bezeichner=${bezeichner} --namespace ${namespace} --wait --wait-for-jobs"
}
Void startGoofyForSnapshotTests(String namespace, String values, String bezeichner, String imageTag) {
sh "helm upgrade --install --create-namespace goofy ozg-base-apps-snapshot/goofy -f ${values} --set image.tag=${imageTag} --set kop.bezeichner=${bezeichner} --namespace ${namespace} --version ${getLatestChartVersion('goofy').trim()} --wait --wait-for-jobs"
}
String getLatestChartVersion(String chart) {
container("k8s") {
return sh (script: "helm search repo ozg-base-apps-snapshot --devel -l -o json | jq -r 'first(.[] | select((.name==\"ozg-base-apps-snapshot/${chart}\") and (.version|match(\"SNAPSHOT\$\"))) | .version)'", returnStdout: true)
}
}
String runTests(String stageName, String reportFolder) {
container("cypress") { container("cypress") {
try { try {
def configFile = generateCypressConfig(stageName, reportFolder)
dir("goofy-client") { dir("goofy-client") {
sh "npm run cypress:version" sh "npm run cypress:version"
sh "npm run cypress:ci-run --CONFIG_FILE=${configFile} --REPORT_FOLDER=${reportFolder}" sh "npm run cypress:ci-run --CONFIG_FILE=${configFile} --REPORT_FOLDER=${reportFolder}"
...@@ -490,16 +516,6 @@ String runTests(String stageName, String reportFolder) { ...@@ -490,16 +516,6 @@ String runTests(String stageName, String reportFolder) {
} }
} }
Void shutdownEnvironment(String namespace) {
container("k8s") {
sh "helm uninstall goofy --namespace ${namespace} --wait"
sh "helm uninstall pluto --namespace ${namespace} --wait"
removeKeycloakUser(namespace)
sh "kubectl delete namespace ${namespace}"
}
}
String makeUrlConform(String input) { String makeUrlConform(String input) {
return input.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase() return input.replaceAll(/[^a-zA-Z0-9]+/, "").toLowerCase()
...@@ -524,89 +540,15 @@ String cutBranchNameForKeycloakRealm(String branchName, String stageName) { ...@@ -524,89 +540,15 @@ String cutBranchNameForKeycloakRealm(String branchName, String stageName) {
return branchName return branchName
} }
String generateNamespaceName(String bezeichner) {
return "e2e-${bezeichner}-dev"
}
Void generateKeycloakUserYaml(String namespace) {
def e2eUserFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/user', returnStdout: true)
e2eUserFiles.split("\\n").each { user ->
def newUserYaml = readYaml file: "goofy-client/apps/goofy-e2e/deployment-values/goofy/user/user.yaml"
def userJson = readJSON file: 'goofy-client/apps/goofy-e2e/src/fixtures/user/'+user
newUserYaml.metadata.name = namespace + "-" + userJson.name
newUserYaml.metadata.namespace = namespace
newUserYaml.metadata.labels.realm = namespace
newUserYaml.spec.realmSelector.matchLabels.realm = namespace
newUserYaml.spec.user.username = userJson.name
newUserYaml.spec.user.credentials = [[type: 'password', value: userJson.password]]
if(userJson.firstName) {
newUserYaml.spec.user.firstName = userJson.firstName
}
if(userJson.lastName) {
newUserYaml.spec.user.lastName = userJson.lastName
}
if(userJson.clientRoles) {
newUserYaml.spec.user.clientRoles = [(namespace+"-goofy"): userJson.clientRoles]
}
if(userJson.groups) {
newUserYaml.spec.user.groups = userJson.groups
}
dir (namespace) {
writeYaml file: userJson.name+".yaml", data: newUserYaml
}
}
}
Void createKeycloakGroups(String realm) {
def groupFiles = sh (script: 'ls goofy-client/apps/goofy-e2e/src/fixtures/group', returnStdout: true)
groupFiles.split("\\n").each { group ->
def groupJson = sh (script: "cat goofy-client/apps/goofy-e2e/src/fixtures/group/${group}", returnStdout: true)
sh """curl -X POST 'https://sso.dev.ozg-sh.de/auth/admin/realms/${realm}/groups' \
-H 'Content-Type: application/json' \
-H 'Authorization: bearer ${getKeycloakAccessToken()}' \
--data-raw '${groupJson}'
"""
}
}
Void applyKeycloakUser(String namespace) {
dir(namespace){
def kcUserFiles = sh (script: "ls", returnStdout: true)
kcUserFiles.split("\\n").each { user ->
sh "kubectl apply -f ${user}"
}
}
}
Void removeKeycloakUser(String namespace) {
dir(namespace){
def kcUserFiles = sh (script: "ls", returnStdout: true)
kcUserFiles.split("\\n").each { user ->
sh "kubectl delete -f ${user}"
}
}
}
String generateCypressConfig(String stage, String testFolder) { String generateCypressConfig(String stage, String bezeichner, String testFolder) {
def bezeichner = generateBezeichner(stage) def namespace = "sh-${bezeichner}-dev"
def namespace = generateNamespaceName(bezeichner)
def configName = "cypress-ci-"+testFolder+".json" def configName = "cypress-ci-"+testFolder+".json"
dir('goofy-client/apps/goofy-e2e/'){ dir('goofy-client/apps/goofy-e2e/'){
def config = readJSON file: 'cypress-ci.json' def config = readJSON file: 'cypress-ci.json'
config.baseUrl = "https://${makeUrlConform(env.BRANCH_NAME)}${makeUrlConform(stage)}.dev.ozg-sh.de" as String config.baseUrl = "https://${bezeichner}.dev.ozg-sh.de" as String
config.env.dbUrl = "mongodb+srv://pluto-database-user:XnHhfznNWg65NNd@pluto-database-svc.${namespace}.svc.cluster.local/admin?ssl=false" as String config.env.dbUrl = "mongodb+srv://pluto-database-user:XnHhfznNWg65NNd@pluto-database-svc.${namespace}.svc.cluster.local/admin?ssl=false" as String
config.env.keycloakRealm = namespace as String config.env.keycloakRealm = namespace as String
config.env.keycloakClient = namespace + "-goofy" as String config.env.keycloakClient = namespace + "-goofy" as String
...@@ -645,18 +587,6 @@ String getKeycloakAccessToken() { ...@@ -645,18 +587,6 @@ String getKeycloakAccessToken() {
} }
} }
Void waitForKeycloakClientCreation(realm) {
def shScript = """curl -H 'Content-Type: application/json' \
-H 'Authorization: bearer ${getKeycloakAccessToken()}' \
'https://sso.dev.ozg-sh.de/auth/admin/realms/${realm}/clients' \
| grep -q ${realm}-goofy
"""
while(sh(script: shScript, returnStatus: true)) {
sh 'sleep 5'
}
}
Void sendFailureMessage() { Void sendFailureMessage() {
def room = '' def room = ''
def data = """{"msgtype":"m.text", \ def data = """{"msgtype":"m.text", \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment