diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/Taskfile.yml b/Taskfile.yml index 3aac17e..51427e0 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -4,12 +4,18 @@ includes: docker-redis: taskfile: ./docker-redis/Taskfile.yml dir: ./docker-redis + docker-couchdb: + taskfile: ./docker-couchdb/Taskfile.yml + dir: ./docker-couchdb kube-kind: taskfile: ./kube-kind/Taskfile.yml dir: ./kube-kind kube-redis: taskfile: ./kube-redis/Taskfile.yml dir: ./kube-redis + kube-couchdb: + taskfile: ./kube-couchdb/Taskfile.yml + dir: ./kube-couchdb tasks: default: task --list-all \ No newline at end of file diff --git a/docker-couchdb/Taskfile.yml b/docker-couchdb/Taskfile.yml new file mode 100644 index 0000000..a8e40c9 --- /dev/null +++ b/docker-couchdb/Taskfile.yml @@ -0,0 +1,94 @@ +version: 3 + +vars: + IMAGE: couchdb:latest + CONTAINER_NAME: couchdb-server + PORT: "5984" + COUCHDB_USER: admin + COUCHDB_PASSWORD: admin + ENDPOINT: http://{{.COUCHDB_USER}}:{{.COUCHDB_PASSWORD}}@localhost:{{.PORT}} + +includes: + variables: + taskfile: ../variables/Taskfile.yml + dir: ../variables + +tasks: + default: task --list-all + + dbcliprereq: + silent: true + cmds: + - test -x "$(which curl)" || { echo "curl not available"; exit 1; } + + create: + desc: Run the Couchdb container + cmds: + - docker run -d --name {{.CONTAINER_NAME}} -e COUCHDB_USER={{.COUCHDB_USER}} -e COUCHDB_PASSWORD={{.COUCHDB_PASSWORD}} -p {{.PORT}}:{{.PORT}} {{.IMAGE}} + silent: true + + stop: + desc: Stop the Couchdb container + cmds: + - docker stop {{.CONTAINER_NAME}} + silent: true + + remove: + desc: Remove the Couchdb container + cmds: + - docker rm -f {{.CONTAINER_NAME}} + silent: true + + logs: + desc: View the logs of the Couchdb container + cmds: + - docker logs {{.CONTAINER_NAME}} + silent: false + + status: + desc: Check the status of the Couchdb container + cmds: + - docker ps -a --filter "name={{.CONTAINER_NAME}}" + silent: true + + start: + desc: Start the Couchdb container + cmds: + - | + if docker ps -a --format '{{`{{.Names}}`}}' | grep -q '^{{.CONTAINER_NAME}}$'; then + if [ "$(docker inspect -f '{{`{{.State.Status}}`}}' {{.CONTAINER_NAME}})" = "exited" ]; then + docker start {{.CONTAINER_NAME}} + else + echo "Container is already running or in different state" + fi + else + echo "Container does not exist. Use 'task create' first" + fi + silent: true + + listdbs: + desc: List the Couchdb databases + cmds: + - task: dbcliprereq + - curl '{{.ENDPOINT}}/_all_dbs' + silent: true + + createdb: + desc: Create a couchdb database + vars: + DB_NAME: '{{.DB_NAME | default ""}}' + cmds: + - task: dbcliprereq + - task: variables:check-var + vars: { VAR_NAME: "DB_NAME", VAR_VALUE: "{{.DB_NAME}}" } + - curl -XPUT '{{.ENDPOINT}}/{{.DB_NAME}}' + + test: + desc: Run a test command in the Couchdb container + cmds: + - task: create + - task: status + - task: logs + - task: remove + - task: status + silent: false \ No newline at end of file diff --git a/kube-couchdb/Taskfile.yml b/kube-couchdb/Taskfile.yml new file mode 100644 index 0000000..b1f2ed0 --- /dev/null +++ b/kube-couchdb/Taskfile.yml @@ -0,0 +1,98 @@ +version: 3 + +vars: + COUCHDB_USER: admin + COUCHDB_PASSWORD: admin + COUCHDB_USER_B64: + sh: 'echo -n "{{.COUCHDB_USER}}" | base64' + COUCHDB_PASSWORD_B64: + sh: 'echo -n "{{.COUCHDB_PASSWORD}}" | base64' + IMAGE: couchdb:latest + POD_NAME: couchdb-server + NAMESPACE: default + +tasks: + + default: task --list-all + + render-secret: + desc: Generates couchdb secret + env: + COUCHDB_USER: "{{.COUCHDB_USER_B64}}" + COUCHDB_PASSWORD: "{{.COUCHDB_PASSWORD_B64}}" + POD_NAME: "{{.POD_NAME}}" + NAMESPACE: "{{.NAMESPACE}}" + cmds: + - envsubst < couchdb-secret.tmpl.yaml > couchdb-secret.yaml + internal: true + + render-deployment: + desc: Render couchdb deployment manifest + env: + COUCHDB_USER: "{{.COUCHDB_USER_B64}}" + COUCHDB_PASSWORD: "{{.COUCHDB_PASSWORD_B64}}" + POD_NAME: "{{.POD_NAME}}" + NAMESPACE: "{{.NAMESPACE}}" + IMAGE: "{{.IMAGE}}" + cmds: + - echo "Rendering manifest..." + - envsubst < couchdb.tmpl.yaml > couchdb.yaml + internal: true + + render-templates: + internal: true + cmds: + - task: render-secret + - task: render-deployment + + create: + desc: Run the Couchdb pod + cmds: + - task: render-templates + - kubectl --dry-run=server apply -f couchdb-secret.yaml + - kubectl --dry-run=server apply -f couchdb.yaml + silent: true + + status: + desc: Check the status of the Couchdb pod + cmds: + - kubectl get pods -l app=couchdb + silent: true + + wait: + desc: Wait for the Couchdb pod to be ready + cmds: + - kubectl wait --for=condition=Ready pod -l app=couchdb --timeout=60s + silent: true + + remove: + desc: Remove the Couchdb pod + cmds: + - kubectl delete -f couchdb.tmpl.yaml + silent: true + + logs: + desc: View the logs of the Couchdb pod + cmds: + - kubectl logs -l app=couchdb + silent: false + + cli: + vars: + POD: + sh: | + kubectl get pods -l app=couchdb -o jsonpath='{.items[0].metadata.name}' + desc: Open a shell in the Couchdb pod + cmds: + - kubectl exec -it {{.POD}} -- couchdb-cli + silent: false + + test: + desc: Run a test command in the Couchdb pod + cmds: + - task: create + - task: wait + - task: status + - task: logs + - task: remove + silent: false \ No newline at end of file diff --git a/kube-couchdb/couchdb-secret.tmpl.yaml b/kube-couchdb/couchdb-secret.tmpl.yaml new file mode 100644 index 0000000..a29b08e --- /dev/null +++ b/kube-couchdb/couchdb-secret.tmpl.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Secret +metadata: + name: ${POD_NAME}-secret + namespace: ${NAMESPACE} +type: Opaque +data: + username: ${COUCHDB_USER} + password: ${COUCHDB_PASSWORD} diff --git a/kube-couchdb/couchdb-secret.yaml b/kube-couchdb/couchdb-secret.yaml new file mode 100644 index 0000000..74a0b40 --- /dev/null +++ b/kube-couchdb/couchdb-secret.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Secret +metadata: + name: couchdb-server-secret + namespace: default +type: Opaque +data: + username: YWRtaW4= + password: YWRtaW4= diff --git a/variables/Taskfile.yml b/variables/Taskfile.yml new file mode 100644 index 0000000..90f7774 --- /dev/null +++ b/variables/Taskfile.yml @@ -0,0 +1,15 @@ +version: 3 + +tasks: + check-var: + desc: "Checks if a required variable is set" + cmds: + - | + VAR_NAME="{{.VAR_NAME}}" + VAR_VALUE="{{.VAR_VALUE}}" + + if [ -z "$VAR_VALUE" ]; then + printf "Error: required variable '%s' is not set or empty.\n" "$VAR_NAME" >&2 + exit 1 + fi + silent: true \ No newline at end of file