From fce4f869876a4453ed8affb942f8111e4e251671 Mon Sep 17 00:00:00 2001 From: kleph Date: Wed, 25 Mar 2020 01:37:41 +0100 Subject: [PATCH] Add CI/CD --- .drone.yml | 78 ++++++++++++++++++++++++++++++ Dockerfile | 9 ++++ kube/q3-serviceaccount-deploy.yaml | 31 ++++++++++++ kube/q3-status-deployment.yaml | 24 +++++++++ kube/q3-status-service.yaml | 13 +++++ quake3_status.py | 36 ++++++++++++++ 6 files changed, 191 insertions(+) create mode 100644 .drone.yml create mode 100644 Dockerfile create mode 100644 kube/q3-serviceaccount-deploy.yaml create mode 100644 kube/q3-status-deployment.yaml create mode 100644 kube/q3-status-service.yaml create mode 100644 quake3_status.py diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..9307d6d --- /dev/null +++ b/.drone.yml @@ -0,0 +1,78 @@ +--- +kind: pipeline +name: build + +steps: +- name: build image + image: plugins/docker + settings: + repo: kleph/quake3-status + tags: ${DRONE_COMMIT_SHA:0:8} + username: + from_secret: dockerhub_username + password: + from_secret: dockerhub_password + when: + event: + exclude: + - pull_request + +--- +deploy_commands_alias: +- &deploy_commands + commands: + - sed "s/__LEVEL__/$${LEVEL}/g" kube/q3-status-deployment.yaml > kube/q3-status-deployment-$${LEVEL}.yaml + - sed "s/__NAMESPACE__/$${NAMESPACE}/g" kube/q3-status-deployment-$${LEVEL}.yaml > kube/q3-status-deployment-$${NAMESPACE}-$${LEVEL}.yaml + - sed "s/__IMAGE_TAG__/$${IMAGE_TAG}/g" kube/q3-status-deployment-$${NAMESPACE}-$${LEVEL}.yaml > kube/q3-status-deployment-$${NAMESPACE}-$${LEVEL}2.yaml + - cat kube/q3-status-deployment-$${NAMESPACE}-$${LEVEL}2.yaml + - echo "applying" + - kubectl apply -f kube/q3-status-deployment-$${NAMESPACE}-$${LEVEL}2.yaml --namespace=q3 + - echo "== post deploy ($${LEVEL}) status:" + - kubectl get deployments --namespace=q3 + - kubectl get pods --namespace=q3 + +kind: pipeline +name: deploy + +steps: +- name: deploy staging + image: sinlead/drone-kubectl + settings: + kubernetes_server: + from_secret: kubectl_uri + kubernetes_cert: + from_secret: kubectl_cert + kubernetes_token: + from_secret: kubectl_token + environment: + IMAGE_TAG: ${DRONE_COMMIT_SHA:0:8} + NAMESPACE: q3 + LEVEL: staging + <<: *deploy_commands + when: + branch: + exclude: + - master + +- name: deploy live + image: sinlead/drone-kubectl + settings: + kubernetes_server: + from_secret: kubectl_uri + kubernetes_cert: + from_secret: kubectl_cert + kubernetes_token: + from_secret: kubectl_token + environment: + IMAGE_TAG: ${DRONE_COMMIT_SHA:0:8} + NAMESPACE: q3 + LEVEL: live + <<: *deploy_commands + when: + branch: + include: + - master + +depends_on: +- build + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..013a737 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:2-slim + +RUN pip install flask +COPY /quake3_status.py /quake3_status.py +COPY /pyquake3.py /pyquake3.py +EXPOSE 5000 + +CMD ["python", "/quake3_status.py"] + diff --git a/kube/q3-serviceaccount-deploy.yaml b/kube/q3-serviceaccount-deploy.yaml new file mode 100644 index 0000000..6c040f8 --- /dev/null +++ b/kube/q3-serviceaccount-deploy.yaml @@ -0,0 +1,31 @@ +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: q3-deploy + namespace: q3 + +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: q3-deploy-role + namespace: q3 # Should be namespace you are granting access to +rules: +- apiGroups: ["*"] + resources: ["*"] + verbs: ["*"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: q3-rolebinding + namespace: q3 +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: q3-deploy-role +subjects: +- namespace: q3 + kind: ServiceAccount + name: q3-deploy diff --git a/kube/q3-status-deployment.yaml b/kube/q3-status-deployment.yaml new file mode 100644 index 0000000..30fcd3e --- /dev/null +++ b/kube/q3-status-deployment.yaml @@ -0,0 +1,24 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: q3-status-deployment-__LEVEL__ + namespace: __NAMESPACE__ +spec: + selector: + matchLabels: + app: q3-status + level: __LEVEL__ + replicas: 1 + template: + metadata: + labels: + app: q3-status + level: __LEVEL__ + spec: + containers: + - name: q3-status + image: kleph/q3-status:__IMAGE_TAG__ + imagePullPolicy: Always + ports: + - containerPort: 5000 diff --git a/kube/q3-status-service.yaml b/kube/q3-status-service.yaml new file mode 100644 index 0000000..f5e55fa --- /dev/null +++ b/kube/q3-status-service.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: q3-status + namespace: q3 +spec: + selector: + app: q3-status + level: live + ports: + - protocol: TCP + port: 80 + targetPort: 5000 diff --git a/quake3_status.py b/quake3_status.py new file mode 100644 index 0000000..d9aaa41 --- /dev/null +++ b/quake3_status.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3.6 +# -*- coding:utf-8 -*- +""" basic q3 status page for my home server """ + +from flask import Flask +from flask import render_template +from pyquake3 import PyQuake3 +import os + +# pylint: disable=invalid-name +app = Flask(__name__) +app.url_map.strict_slashes = False + +@app.route('/') +def index(hours=None, minutes=None): + """ main and only app """ + + q3server = os.environ.get("Q3_SERVER") + rcon_password = os.environ.get("RCONN_PASSWORD") + + q = PyQuake3(q3server, rcon_password) + q.update() + + data = 'The name of %s is %s, running map %s with %s player(s).' % \ + (q.get_address(), q.vars['sv_hostname'], \ + q.vars['mapname'], len(q.players)) + + return data + +def main(): + """ main func """ + app.run(host='::') + + +if __name__ == '__main__': + main()