You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/app/controllers/kubernete_controller.rb

73 lines
3.0 KiB

6 years ago
class KuberneteController < ApplicationController
require 'open3'
layout "base_edu"
RC_PATH = "files/k8s/webssh/controller.yaml"
SVC_PATH = "files/k8s/webssh/service.yaml"
SERVER_URL = "https://106.75.96.108:6443"
TOKEN = "c7dd73a7c86992fb"
# YAML.load(File.open(rc_PATH))
# REDO:销毁的时候文件一并要销毁
def exec_pod
# 生成ymal文件
nod_name = params[:name]
path_type = params[:type]
rc = YAML.load(File.open(RC_PATH))
svc = YAML.load(File.open(SVC_PATH))
File.new(File.join("files/k8s/#{path_type}", "controller-#{nod_name}.yaml"),"w+")
File.new(File.join("files/k8s/#{path_type}", "service-#{nod_name}.yaml"),"w+")
rc = change_yaml_info(rc, nod_name, "rc")
svc = change_yaml_info(svc, nod_name, "svc")
current_rc_path = "files/k8s/#{path_type}/controller-#{nod_name}.yaml"
current_svc_path = "files/k8s/#{path_type}/service-#{nod_name}.yaml"
open(current_rc_path, 'w') { |f| YAML.dump(rc, f) }
open(current_svc_path, 'w') { |f| YAML.dump(svc, f) }
# 启动容器
# 删除容器命令
# 如果连接到其它服务器可以使用ssh -t
kubenete_rc = "kubectl --server='" + SERVER_URL + "' --token='" + TOKEN + "' --insecure-skip-tls-verify=true " + "create -f " + current_rc_path
kubenete_svc = "kubectl --server='" + SERVER_URL + "' --token='" + TOKEN + "' --insecure-skip-tls-verify=true " + "create -f " + current_svc_path
logger.info("webss kubenete_rc is" + kubenete_rc)
logger.info("webss kubenete_svc is" + kubenete_svc)
# stdin, stdout, stderr, wait_thr = Open3.popen3([env,] cmd... [, opts])
# 可以多少秒以后执行
stdin, stdout, stderr = Open3.popen3(kubenete_rc)
logger.info("Open3 result is ###"+ stdout.gets)
stdin2, stdout2, stderr2 = Open3.popen3(kubenete_svc)
logger.info("Open3 result is ###"+ stdout2.gets)
render :json => {:status => "good"}
end
def exec_container
pod_name = "webssh-master#{parmas[:myshixun_id]}"
git_url =
# kubectl exec webssh-master1-gn1ww -c webssh python -v
# webssh 容器中执行命令,克隆代码,注意代码存放位置
git_clone = "kubectl --server='#{SERVER_URL}' --token='#{TOKEN}' --insecure-skip-tls-verify=true exec webssh-master1-gn1ww -c webssh clone #{git_url}"
stdin, stdout, stderr = Open3.popen3(git_clone)
format.js
end
def new
end
def create
end
private
# yaml数据处理
# 处理数据类型如下
def change_yaml_info rc, nod_name, type
rc['metadata']['name'] = rc['metadata']['name'] + nod_name
rc['metadata']['labels']['name'] = rc['metadata']['labels']['name'] + nod_name
rc['spec']['selector']['name'] = rc['spec']['selector']['name'] + nod_name
if type == "rc"
rc['spec']['template']['metadata']['labels']['name'] = rc['spec']['template']['metadata']['labels']['name'] + nod_name
elsif type == "svc"
rc['spec']['ports'][0]['nodePort'] = rc['spec']['ports'][0]['nodePort'].to_i + nod_name.to_i
end
return rc
end
end