Skip to content

Commit

Permalink
feat(sandbox): basic-ci-setup (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescrowley321 authored and yeshamavani committed Aug 12, 2021
1 parent 3b09e1f commit f33ee62
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 27 deletions.
Binary file added docker-compose
Binary file not shown.
15 changes: 15 additions & 0 deletions sandbox/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pipeline {
agent { label 'jenkins-dynamic-slave' }
stages {
stage('Build') {
steps {
dir("sandbox"){
sh "chmod +x ./ci_build.sh"
withCredentials([usernamePassword('credentialsId': 'sf-reference-arch-dockerhub', 'usernameVariable': 'DOCKER_USERNAME', 'passwordVariable': 'DOCKER_PASSWORD')]) {
sh "./ci_build.sh ${env.WORKSPACE}/sandbox true"
}
}
}
}
}
}
27 changes: 20 additions & 7 deletions sandbox/build.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
#!/bin/bash
REGISTRY=$1
CURRENT_DIR=$2

if [ -z "$REGISTRY" ];
then
REGISTRY="localhost:32000";
echo "${REGISTRY}"
else
echo "${REGISTRY}"
# TODO: remove sudo once build agent is fixed

if [ -z "$REGISTRY" ]; then
REGISTRY="localhost:32000"
echo "${REGISTRY}"
else
echo "${REGISTRY}"
fi

if [ ! -z "${DOCKER_USERNAME}" ] && [ ! -z "${DOCKER_PASSWORD}" ]; then
echo "Logging in to Docker"
echo "${DOCKER_PASSWORD}" | docker login --username ${DOCKER_USERNAME} --password-stdin
fi

export REGISTRY=$REGISTRY; docker-compose build
if [ -z "$CURRENT_DIR" ]; then
CURRENT_DIR=$(echo $PWD)
fi

echo "CURRENT_DIR=$CURRENT_DIR"

export REGISTRY=$REGISTRY; docker-compose -f "${CURRENT_DIR}/docker-compose.yml" build
docker push ${REGISTRY}/auth-multitenant-example
docker push ${REGISTRY}/notification-socket-example
docker push ${REGISTRY}/workflow-ms-example
Expand Down
103 changes: 103 additions & 0 deletions sandbox/ci_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/bash
CURRENT_DIR=$1
INSTALL_MICROK8S=$2
LOCAL_REGISTRY='localhost:32000'

install_kubectl() {
sudo apt-get update -y
sudo apt-get install -y apt-transport-https ca-certificates curl
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update -y
sudo apt-get install -y kubectl
kubectl cluster-info
}

install_microk8s() {
sudo snap install microk8s --classic
microk8s status --wait-ready
microk8s enable dns registry ingress
microk8s kubectl get all --all-namespaces
pushd $HOME
mkdir -p .kube
microk8s config >.kube/config
popd
}

local_docker_push() {
docker push ${LOCAL_REGISTRY}/auth-multitenant-example
docker push ${LOCAL_REGISTRY}/notification-socket-example
docker push ${LOCAL_REGISTRY}/workflow-ms-example
docker push ${LOCAL_REGISTRY}/audit-ms-example
docker push ${LOCAL_REGISTRY}/scheduler-example
docker push ${LOCAL_REGISTRY}/video-conferencing-ms-example
docker push ${LOCAL_REGISTRY}/in-mail-example
}

docker_push() {
docker tag ${LOCAL_REGISTRY}/auth-multitenant-example ${DOCKER_USERNAME}/auth-multitenant-example
docker tag ${LOCAL_REGISTRY}/notification-socket-example ${DOCKER_USERNAME}/notification-socket-example
docker tag ${LOCAL_REGISTRY}/workflow-ms-example ${DOCKER_USERNAME}/workflow-ms-example
docker tag ${LOCAL_REGISTRY}/audit-ms-example ${DOCKER_USERNAME}/audit-ms-example
docker tag ${LOCAL_REGISTRY}/scheduler-example ${DOCKER_USERNAME}/scheduler-example
docker tag ${LOCAL_REGISTRY}/video-conferencing-ms-example ${DOCKER_USERNAME}/video-conferencing-ms-example
docker tag ${LOCAL_REGISTRY}/in-mail-example ${DOCKER_USERNAME}/in-mail-example

docker push ${DOCKER_USERNAME}/auth-multitenant-example
docker push ${DOCKER_USERNAME}/notification-socket-example
docker push ${DOCKER_USERNAME}/workflow-ms-example
docker push ${DOCKER_USERNAME}/audit-ms-example
docker push ${DOCKER_USERNAME}/scheduler-example
docker push ${DOCKER_USERNAME}/video-conferencing-ms-example
docker push ${DOCKER_USERNAME}/in-mail-example

# TODO: should we clean up after build? Since agent is ephemeral, some caching may be helpful after an initial run
# TODO: remove specific images and cache
# docker system prune -a -f
}

terraform_apply() {
pushd ${CURRENT_DIR}/k8s/tf-sourceloop-sandbox
terraform init
terraform apply -auto-approve
popd
}

terraform_destroy() {
pushd ${CURRENT_DIR}/k8s/tf-sourceloop-sandbox
terraform destroy -auto-approve
popd
}

run_tests() {
echo "Sleeping to wait for services to come online."
sleep 180
${CURRENT_DIR}/health_check.sh sourceloop.local true
}

if [ ! -z "${DOCKER_USERNAME}" ] && [ ! -z "${DOCKER_PASSWORD}" ]; then
echo "Logging in to Docker"
echo "${DOCKER_PASSWORD}" | docker login --username ${DOCKER_USERNAME} --password-stdin
fi

if [ -z "$CURRENT_DIR" ]; then
CURRENT_DIR=$(echo $PWD)
fi

echo "CURRENT_DIR=$CURRENT_DIR"

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o ${CURRENT_DIR}/docker-compose --insecure
sudo chmod +x ${CURRENT_DIR}/docker-compose

sudo REGISTRY=$LOCAL_REGISTRY ./docker-compose -f "${CURRENT_DIR}/docker-compose.yml" build

if [ "${INSTALL_MICROK8S}" = "true" ]; then
install_microk8s
install_kubectl
fi

local_docker_push
terraform_apply
run_tests
terraform_destroy
docker_push
39 changes: 39 additions & 0 deletions sandbox/health_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
DOMAIN=$1
IS_LOCALHOST=$2
ERROR_COUNT=0

# check the basic health check on its own
if [ "$IS_LOCALHOST" = "true" ]; then
curl_response=$(curl -Is -H "Host: health-check.${DOMAIN}" https://localhost --insecure -f)
else
curl_response=$(curl -Is -H "Host: health-check.${DOMAIN}" https://health-check.${DOMAIN} --insecure -f)
fi

if [ -z "$curl_response" ]; then
echo "health-check service is unhealthy"
ERROR_COUNT=$((ERROR_COUNT + 1))
fi

# TODO: Add video when it works - "video"
declare -a services=("workflow" "scheduler" "notification" "in-mail" "auth" "audit")

for service in "${services[@]}"; do
if [ "$IS_LOCALHOST" = "true" ]; then
curl_response=$(curl -Is -H "Host: ${service}.${DOMAIN}" https://localhost/openapi.json --insecure -f)
else
curl_response=$(curl -Is -H "Host: ${service}.${DOMAIN}" https://${service}.${DOMAIN}/openapi.json --insecure -f)
fi

if [ -z "$curl_response" ]; then
echo "${service} service is unhealthy"
ERROR_COUNT=$((ERROR_COUNT + 1))
fi
done

if (($ERROR_COUNT > 0)); then
echo "${ERROR_COUNT} services are unhealthy"
exit 1
else
echo "All services are healthy"
fi
10 changes: 10 additions & 0 deletions sandbox/k8s/ingress/sourceloop-sandbox-ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ spec:
name: health-check-svc
port:
number: 80
- host: health-check.sourceloop.local
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: health-check-svc
port:
number: 80
- host: pgadmin.sourceloop.local
http:
paths:
Expand Down
15 changes: 8 additions & 7 deletions sandbox/k8s/postgres/postgres-orchestrator-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ spec:
- bash
- -c
- |-
export PGPASSWORD=changeme && sleep 30 &&
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database authentication_db' &&
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database notification_db' &&
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database workflow_db' &&
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database audit_db' &&
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database scheduler_db'
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database in_mail_db'
export PGPASSWORD=${POSTGRES_PASSWORD:-changeme};
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database authentication_db' 2>&1;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database notification_db' 2>&1;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database workflow_db' 2>&1;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database audit_db' 2>&1;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database scheduler_db' 2>&1;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database in_mail_db' 2>&1;
exit 0
env:
- name: PGDATA
value: /data/postgres
Expand Down
2 changes: 1 addition & 1 deletion sandbox/k8s/tf-sourceloop-sandbox/.terraform-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
latest:^0.14
latest:^1.0.3
2 changes: 1 addition & 1 deletion sandbox/k8s/tf-sourceloop-sandbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module "tf-sourceloop-sandbox" {

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.3 |
| <a name="requirement_kubectl"></a> [kubectl](#requirement\_kubectl) | >= 1.7.0 |
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | >= 2.0 |
| <a name="requirement_null"></a> [null](#requirement\_null) | 3.1.0 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ spec:
name: health-check-svc
port:
number: 80
- host: health-check.sourceloop.local
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: health-check-svc
port:
number: 80
- host: pgadmin.sourceloop.local
http:
paths:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ spec:
- bash
- -c
- |-
export PGPASSWORD=changeme && sleep 30 &&
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database authentication_db' &&
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database notification_db' &&
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database workflow_db' &&
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database audit_db' &&
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database scheduler_db'
psql -U postgres -d postgres -h postgres.sourceloop-sandbox.svc.cluster.local -c 'create database in_mail_db'
export PGPASSWORD=${POSTGRES_PASSWORD:-changeme}; sleep 30;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database authentication_db' 2>&1;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database notification_db' 2>&1;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database workflow_db' 2>&1;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database audit_db' 2>&1;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database scheduler_db' 2>&1;
psql -U ${POSTGRES_USER:-postgres} -d postgres -h postgres -c 'create database in_mail_db' 2>&1;
exit 0
env:
- name: PGDATA
value: /data/postgres
Expand Down
2 changes: 0 additions & 2 deletions sandbox/k8s/tf-sourceloop-sandbox/provider.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// TODO: make variables
provider "kubectl" {
load_config_file = true
config_context = "sourceloop-sandbox"
config_context_cluster = "microk8s-cluster"
config_path = "~/.kube/config"
}

// TODO: make variables
provider "kubernetes" {
config_context = "sourceloop-sandbox"
config_context_cluster = "microk8s-cluster"
config_path = "~/.kube/config"
}
2 changes: 1 addition & 1 deletion sandbox/k8s/tf-sourceloop-sandbox/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 0.14"
required_version = ">= 1.0.3"


required_providers {
Expand Down
2 changes: 1 addition & 1 deletion sandbox/workflow-ms-example/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
} from '@loopback/rest-explorer';
import {ServiceMixin} from '@loopback/service-proxy';
import {
BPMTask,
WorkflowServiceBindings,
WorkflowServiceComponent,
} from '@sourceloop/bpmn-service';
import {BPMTask} from '@sourceloop/bpmn-service/dist/bpm-task';
import path from 'path';
import {SayHelloCommand} from './commands/sayhello.command';
import {BpmnProvider} from './providers/bpmn.provider';
Expand Down

0 comments on commit f33ee62

Please sign in to comment.