126 lines
3.8 KiB
Python
126 lines
3.8 KiB
Python
from django.apps import AppConfig
|
|
from django.http import HttpResponse
|
|
from django.shortcuts import render
|
|
from django.shortcuts import redirect
|
|
|
|
from django.core.files import File
|
|
from kubernetes import client, config
|
|
|
|
#from django.template.loader import render_to_string
|
|
import time
|
|
import random
|
|
import mimetypes
|
|
import ruamel.yaml
|
|
import yaml
|
|
import urllib.request
|
|
|
|
def hello(request):
|
|
return HttpResponse('<h1>Hello k8s!</h1>')
|
|
|
|
def scale_up(request,namespace_name,deploy_name):
|
|
config.load_kube_config(config_file='/code/kubeconfig')
|
|
api = client.AppsV1Api()
|
|
deployment = api.read_namespaced_deployment(name=deploy_name, namespace=namespace_name)
|
|
deployment.spec.replicas = 2
|
|
resp = api.patch_namespaced_deployment(
|
|
name=deploy_name, namespace=namespace_name, body=deployment);
|
|
return HttpResponse('<h1>Scaled up !</h1>')
|
|
|
|
|
|
def scale_down(request,namespace_name,deploy_name):
|
|
config.load_kube_config(config_file='/code/kubeconfig')
|
|
api = client.AppsV1Api()
|
|
deployment = api.read_namespaced_deployment(name=deploy_name, namespace=namespace_name)
|
|
deployment.spec.replicas = 0
|
|
resp = api.patch_namespaced_deployment(
|
|
name=deploy_name, namespace=namespace_name, body=deployment);
|
|
return HttpResponse('<h1>Scaled down !</h1>')
|
|
|
|
def nodes(request):
|
|
config.load_kube_config(config_file='/code/kubeconfig')
|
|
v1 = client.CoreV1Api()
|
|
# v1.list_node()
|
|
pod_manifest = {
|
|
'apiVersion': 'v1',
|
|
'kind': 'Pod',
|
|
'metadata': {
|
|
'labels': {
|
|
'bot': 'current-bot'
|
|
},
|
|
'name': 'podname',
|
|
'namespace': 'echo-server'
|
|
|
|
},
|
|
'spec': {
|
|
|
|
'containers': [{
|
|
'image': 'gitea.tryton.geeraert.eu/pi-ux-ce/bitwarden_cli:1.0.1',
|
|
# 'pod-running-timeout': '5m0s',
|
|
'name': 'testpython',
|
|
'env': [
|
|
{'name': 'env_variable', 'value': 'env_value'},
|
|
]
|
|
}],
|
|
'restartPolicy': 'Always',
|
|
# 'serviceAccountName': 'bots-service-account'
|
|
}
|
|
}
|
|
|
|
# print(f'POD MANIFEST:\n{pod_manifest}')
|
|
api_response = v1.create_namespaced_pod(body=pod_manifest,namespace='echo-server')
|
|
return HttpResponse(api_response)
|
|
|
|
def backup_job(request):
|
|
config.load_kube_config(config_file='/code/kubeconfig')
|
|
v1 = client.CoreV1Api()
|
|
#v1 = client.BatchV1Api()
|
|
# v1.list_node()
|
|
pod_manifest_job = {
|
|
'apiVersion': 'batch/v1',
|
|
'kind': 'Job',
|
|
'metadata': {
|
|
'name': 'bitwarden-backup-django',
|
|
'namespace': 'bwbackup'
|
|
|
|
},
|
|
'spec': {
|
|
'template': {
|
|
'spec': {
|
|
|
|
'containers': [{
|
|
'image': 'gitea.tryton.geeraert.eu/pi-ux-ce/bitwarden_cli:1.0.1',
|
|
'name': 'testpython',
|
|
'command': ['/bin/sh -c /config/export_bitwarden.sh'],
|
|
'env': [
|
|
{'name': 'env_variable', 'value': 'env_value'},
|
|
],
|
|
'volumeMounts': [
|
|
{'name': 'scripts', 'configMap': [{'name':'bitwarden-export-scripts'},]},
|
|
]
|
|
}],
|
|
'restartPolicy': 'Always',
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
api_response = v1.create_namespaced_job(body=pod_manifest_job)
|
|
return HttpResponse(api_response)
|
|
|
|
|
|
#help with yaml import
|
|
#https://github.com/kubernetes-client/python/issues/589
|
|
|
|
def backup_job_yaml(request):
|
|
config.load_kube_config(config_file='/code/kubeconfig')
|
|
v1 = client.BatchV1Api()
|
|
|
|
with urllib.request.urlopen(https://gitea.tryton.geeraert.eu/pi-ux-ce/Backup_kube/raw/branch/main/Infrequent_access/pod.yaml") as manifest_url:
|
|
|
|
# with open('/code/pod.yaml') as :
|
|
job = yaml.safe_load(manifest_url)
|
|
v1.create_namespaced_job(
|
|
job['metadata']['namespace'], job, pretty=True
|
|
)
|
|
return HttpResponse(job['metadata']['namespace'])
|