Having fun building wheels every day, this time you can create a test environment with one click

Coffee king maintain dozens of different types of projects, there are quite a number of projects have a high request for confidentiality, that is to say, the next version to online content is not leaked in advance, as the introduction of new apple products website not allowed out before product release, the confidential content in addition to outside the system to restrain them, also need some technical means to safeguard

This to introduce Aloid system has a certain effect on security, the main function of this system is quickly generate temporary environment, the temporary environment will have certain validity, expire automatically remove, of course you can also manually removed, at the same time, the environment will have only random access address, only people who know this random address can access

Involving technology

The whole project is built based on Django, and the front and back end framework code can be obtained through this article. Through the framework code, you can quickly build the project and add your own functions. It should be noted that the framework code is not the source of the project

subprocess

Compilation and deployment inevitably involves dealing with system commands. After investigating several methods of Executing system commands in Python, I chose Subprocess. Subprocess is the replacement module of OS. system and os.popen, which has more powerful functions and is a python module with no additional installation and is easy to use

If you need to execute system commands frequently, you can write a method like this to encapsulate command execution and return output, making the code concise and readable

import shlex, subprocess

def runCmd(tid, msg, cmd):
    try:
        p = subprocess.Popen(shlex.split(cmd), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        while p.poll() == None:
            out = p.stdout.readline().strip()
            if out:
                print('- >' + out.decode())

        res = 'failure ~ _ ~' if p.returncode else 'complete ^_^'
        print('- >' + msg + res)

        return p.returncode
    except Exception as e:
        print('---->Shell Exec Error: %s % str(e))
        return 999
Copy the code

kubernetes-api

All environments run on Top of Kubernetes. Creating or destroying temporary environments requires interaction with Kubernetes. I chose to use the Kubernetes Python SDK to do this

from kubernetes import client, config

class KubeApi:
    def __init__(self, namespace='alodi'):
        config.load_kube_config("/ops/coffee/kubeconfig.yaml")
        self.namespace = namespace

    def create_deployment(self, RAND, PROJ, ENVT):
        api_instance = client.AppsV1Api()
        body = client.V1Deployment(
            api_version="apps/v1",
            kind="Deployment",
            metadata=client.V1ObjectMeta(name=RAND),
            spec=client.V1DeploymentSpec(
                replicas=1,
                selector={'matchLabels': {'app': RAND}},
                template=client.V1PodTemplateSpec(
                    metadata=client.V1ObjectMeta(labels={"app": RAND}),
                    spec=client.V1PodSpec(
                        containers=[client.V1Container(
                            name=RAND,
                            image="k8s-harbor.blz.netease.com/alodi/" + RAND,
                            env=[{"name": "ENVT"."value": ENVT}, {"name": "PROJ"."value": PROJ}],
                            ports=[client.V1ContainerPort(container_port=80)],
                        )]
                    )
                ),
            )
        )

        try:
            r = api_instance.create_namespaced_deployment(
                namespace=self.namespace, body=body
            )

            return True, "Deployment created: %s" % r
        except Exception as e:
            return False, 'Deployment created: ' + str(e)

    def delete_deployment(self, RAND):
        api_instance = client.AppsV1Api()
        body = client.V1DeleteOptions(
            propagation_policy='Foreground',
            grace_period_seconds=5)

        try:
            r = api_instance.delete_namespaced_deployment(
                namespace=self.namespace,
                name=RAND,
                body=body
            )

            return True, "Deployment deleted. %s" % r
        except Exception as e:
            return False, 'Deployment deleted: ' + str(e)
Copy the code

Kube /config file load_kube_config = kubernetes ~/. Kube /config file load_kube_config = kubernetes ~/. Kube /config file load_kube_config = kubernetes ~/. Of course, authentication can also be implemented by token

In addition, special attention should be paid to the SDK version and kubernetes version of the corresponding relationship, and the operation of different resources on kubernetes API version requirements are different, when using more reference to the official documentation

For reasons of length, only two deployment operation examples are posted here. You can find more examples from me separately

The interface display

The whole project in addition to user management and other conventional pages, mainly consists of three pages, by these three pages to complete the implementation and display of the main process

Project Management page: This page allows you to create, edit, and delete projects

You can also create a temporary environment on the project management page, where you select the data environment and code TAG to use

After filling in the relevant information, click the Build & Deploy button to jump to the Task Details page, which displays the log output of the deployment process in real time, with a crawler button in the upper right corner to terminate the deployment

Deployment list page: You can view the deployment history on the Deployment list page. While the environment is running, you can click the Destroy button to destroy the project and clean up the Kubernetes resource

Write in the last

  1. We wrote a lot of projects and built a lot of wheels in order to make our work easier and achieve the vision of “a cup of coffee, easy operation”
  2. The project code is not open source, but most of the technologies used in the project are introduced in special articles, which are attached with codes, and some even provide Demo, which can quickly solve their own needs
  3. If you have any questions, please contact me. We can communicate and grow together. Look forward to adding my friend ~

Related articles recommended reading:

  • The Devops tools we developed ourselves
  • Open source recommended | CoDo source one-stop enterprise platform