[REV] merge from code

This commit is contained in:
bay1ts 2017-12-15 16:49:24 +08:00
parent 4964ce9ec8
commit a9153e7437
28 changed files with 559 additions and 160 deletions

View File

@ -86,14 +86,6 @@ func Run(s *option.Builder) error {
go http.ListenAndServe(":3228", r)
//service reg
discoverManager, errD := discover.NewDiscoverManager(s.Config)
if errD != nil {
return errD
}
discoverManager.Start()
defer discoverManager.CancelIP()
logrus.Info("builder begin running...")
//step finally: listen Signal
term := make(chan os.Signal)

View File

@ -100,7 +100,12 @@ class AppImage():
}
if share_id is not None:
data["share_id"] = share_id
self.region_client.service_publish_new_region(data)
try:
self.region_client.service_publish_new_region(data)
except Exception as e:
self.region_client.service_publish_failure_region(data)
self.log.error("云帮应用本地发布失败,保存publish 失败。{0}".format(e.message),step="callback",status="failure")
pass
if self.is_region_image and not self.oss_registry.exist_image(
oss_image):
try:
@ -176,7 +181,13 @@ class AppImage():
}
if share_id is not None:
req["share_id"] = share_id
self.region_client.service_publish_new_region(req)
try:
self.region_client.service_publish_new_region(data)
except Exception as e:
self.region_client.service_publish_failure_region(data)
self.log.info("云帮应用本地发布失败,保存publish 失败。{0}".format(e.message),step="callback",status="failure")
pass
# self.region_client.service_publish_new_region(req)
self.log.info("开始上传镜像到云市")
# 修改image name
hub_image = self.hubclient.rename_image(image)
@ -233,7 +244,7 @@ class AppImage():
status="failure")
else:
self.log.info("镜像不存在,发布失败", step="callback", status="failure")
def download_and_deploy(self):
image = self.task['image']
namespace = self.task['namespace']
@ -348,8 +359,15 @@ class AppImage():
pass
version_status['final_status']="success"
self.log.info("应用同步完成,开始启动应用。", step="app-image", status="success")
body = {
"deploy_version": self.task['deploy_version'],
"event_id": self.event_id
}
try:
self.api.start_service(tenant_name, service_alias, event_id)
# self.api.start_service(tenant_name, service_alias, event_id)
self.api.upgrade_service(self.task['tenant_name'], self.task['service_alias'], json.dumps(body))
except Exception as e:
logger.exception(e)
self.log.error(

View File

@ -178,7 +178,15 @@ class AppSlug():
}
if share_id is not None:
data['share_id'] = share_id
self.region_client.service_publish_new_region(data)
try:
self.region_client.service_publish_new_region(data)
except Exception as e:
self.region_client.service_publish_failure_region(data)
self.log.error(
"云帮应用本地发布失败,保存publish 失败。{0}".format(e.message),
step="callback",
status="failure")
pass
if self.is_region_slug:
try:
self.log.info("开始上传应用到本地云帮")
@ -229,7 +237,15 @@ class AppSlug():
}
if share_id is not None:
data['share_id'] = share_id
self.region_client.service_publish_new_region(data)
try:
self.region_client.service_publish_new_region(data)
except Exception as e:
self.region_client.service_publish_failure_region(data)
self.log.error(
"云帮应用本地发布失败,保存publish 失败。{0}".format(e.message),
step="callback",
status="failure")
pass
if self.is_oss_ftp:
try:
self.log.info("开始上传应用到云市")
@ -244,6 +260,7 @@ class AppSlug():
self.region_client.service_publish_success_region(data)
except Exception as e:
logger.exception(e)
self.region_client.service_publish_failure_region(data)
pass
@ -342,15 +359,20 @@ class AppSlug():
""" 下载slug包 """
def start_service(service_id, deploy_version, operator):
# body = {
# "deploy_version": deploy_version,
# "operator": operator,
# "event_id": self.event_id
# }
body = {
"deploy_version": deploy_version,
"operator": operator,
"event_id": self.event_id
}
try:
# logger.info("mq_work.app_slug", "start service {}:{}".format(service_id, deploy_version))
self.log.info("开始调用api启动应用。")
self.region_api.start_service(service_id, json.dumps(body))
self.api.upgrade_service(self.tenant_name, self.service_alias, json.dumps(body))
# self.region_api.start_service(service_id, json.dumps(body))
except self.region_api.CallApiError, e:
self.log.info(
"开始调用api启动应用失败。{}".format(e.message),
@ -478,7 +500,12 @@ class AppSlug():
except Exception as e:
pass
try:
self.api.start_service(tenant_name, service_alias, event_id)
body = {
"deploy_version": self.task['deploy_version'],
"event_id": self.event_id
}
# self.api.start_service(tenant_name, service_alias, event_id)
self.api.upgrade_service(self.task['tenant_name'], self.task['service_alias'], json.dumps(body))
except Exception as e:
logger.exception(e)
self.log.error(

View File

@ -35,8 +35,8 @@ class UserConsoleAPI(BaseHttpClient):
return res, body
def service_publish_success(self, body):
pass
#url = self.base_url + '/api/tenants/services/publish'
# pass
url = self.base_url + '/api/tenants/services/publish'
# url = 'http://127.0.0.1:3228/api/publish'
#res, body = self._post(url, self.default_headers, body)
#return res, body
res, body = self._post(url, self.default_headers, body)
return res, body

View File

@ -136,8 +136,10 @@ class CodeCheck():
logger.info('mq_work.service_event',
"service_id=" + service_id + ";condition=" + condition)
res, bodyres = self.user_cs_client.code_check(json.dumps(body))
self.region_client.code_check_region(json.dumps(body))
try:
self.region_client.code_check_region(json.dumps(body))
except Exception, e:
pass
def main():
body = ""

View File

@ -130,6 +130,10 @@ class ImageManual():
"path": local_image,
"event_id": self.event_id
}
body = {
"deploy_version": self.task['deploy_version'],
"event_id": self.event_id,
}
version_status['final_status'] = "success"
try:
self.region_client.update_version_region(json.dumps(version_body))
@ -138,8 +142,10 @@ class ImageManual():
pass
try:
self.api.update_iamge(tenant_name, service_alias, local_image)
version = self.task['deploy_version']
self.log.info("应用信息更新完成,开始启动应用。", step="app-image", status="success")
self.api.start_service(tenant_name, service_alias, event_id)
self.api.upgrade_service(self.task['tenant_name'], self.task['service_alias'], json.dumps(body))
# self.api.start_service(tenant_name, service_alias, event_id)
except Exception as e:
logger.exception(e)
self.log.error(

View File

@ -39,8 +39,6 @@ import (
"github.com/goodrain/rainbond/pkg/api/handler"
httputil "github.com/goodrain/rainbond/pkg/util/http"
"sort"
"github.com/Sirupsen/logrus"
"github.com/renstorm/fuzzysearch/fuzzy"
)
@ -206,28 +204,22 @@ func (t *TenantStruct) TenantsGetByName(w http.ResponseWriter, r *http.Request)
v, err := handler.GetTenantManager().GetTenantsByName(tenantName)
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("get tenants names error, %v", err))
httputil.ReturnError(r, w, 404, fmt.Sprintf("get tenants names error, %v", err))
return
}
logrus.Infof("query tenant from db by name %s ,got %v", tenantName, v)
var res = &api_model.TenantResource{}
services, err := handler.GetServiceManager().GetService(v.UUID)
logrus.Infof("query tenant from db by name %s ,got %v",tenantName,v)
tenantServiceRes, err := handler.GetServiceManager().GetTenantRes(v.UUID)
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("get services by tenantID %s error, %v", v.UUID, err))
httputil.ReturnError(r, w, 500, fmt.Sprintf("get tenants service total resources error, %v", err))
return
}
totalResInfo, _ := handler.GetTenantManager().TotalMemCPU(services)
usedResInfo, _ := handler.GetTenantManager().StatsMemCPU(services)
tenantServiceRes.UUID=v.UUID
tenantServiceRes.Name=v.Name
tenantServiceRes.EID=v.EID
res.UUID = v.UUID
res.Name = v.Name
res.EID = v.EID
res.AllocatedCPU = totalResInfo.CPU
res.AllocatedMEM = totalResInfo.MEM
res.UsedCPU = usedResInfo.CPU
res.UsedMEM = usedResInfo.MEM
httputil.ReturnSuccess(r, w, res)
httputil.ReturnSuccess(r, w, tenantServiceRes)
return
}
@ -267,7 +259,9 @@ func (t *TenantStruct) TenantsWithResource(w http.ResponseWriter, r *http.Reques
pageLenStr := strings.TrimSpace(chi.URLParam(r, "pageLen"))
curPageStr := strings.TrimSpace(chi.URLParam(r, "curPage"))
pageLen, err := strconv.Atoi(pageLenStr)
pageLen,err:=strconv.Atoi(pageLenStr)
if err != nil {
httputil.ReturnError(r, w, 400, fmt.Sprintf("bad request, %v", err))
return
@ -282,41 +276,32 @@ func (t *TenantStruct) TenantsWithResource(w http.ResponseWriter, r *http.Reques
httputil.ReturnError(r, w, 500, fmt.Sprintf("get tenants error, %v", err))
return
}
var result []*api_model.TenantResource
for _, v := range rep {
services, err := handler.GetServiceManager().GetService(v.UUID)
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("get services by tenantID %s error, %v", v.UUID, err))
return
}
totalResInfo, _ := handler.GetTenantManager().TotalMemCPU(services)
usedResInfo, _ := handler.GetTenantManager().StatsMemCPU(services)
var res api_model.TenantResource
res.UUID = v.UUID
res.Name = v.Name
res.EID = v.EID
res.AllocatedCPU = totalResInfo.CPU
res.AllocatedMEM = totalResInfo.MEM
res.UsedCPU = usedResInfo.CPU
res.UsedMEM = usedResInfo.MEM
result = append(result, &res)
}
pList := api_model.TenantResList(result)
sort.Sort(pList)
var resultList []*api_model.TenantResource
if curPage*pageLen < len(rep) {
resultList = pList[(curPage-1)*pageLen : curPage*pageLen]
} else {
resultList = pList[(curPage-1)*pageLen : len(rep)]
resource,err:=handler.GetServiceManager().GetPagedTenantRes((curPage-1)*pageLen,pageLen)
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("get tenants error, %v", err))
return
}
for _,v:=range resource{
tenant,err:=handler.GetTenantManager().GetTenantsByUUID(v.UUID)
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("get tenants error, %v", err))
return
}
v.Name=tenant.Name
v.EID=tenant.EID
}
var ret api_model.PagedTenantResList
ret.List = resultList
ret.Length = len(resultList)
ret.List=resource
ret.Length=len(rep)
httputil.ReturnSuccess(r, w, ret)
return
}
//SumTenants 统计租户数量
func (t *TenantStruct) SumTenants(w http.ResponseWriter, r *http.Request) {
// swagger:operation GET /v2/resources/tenants/sum v2 sumTenants

View File

@ -511,6 +511,7 @@ func (t *TenantStruct) BuildService(w http.ResponseWriter, r *http.Request) {
version.Kind = build.Body.Kind
version.BuildVersion = build.Body.DeployVersion
build.Body.EventID = sEvent.EventID
if err := handler.GetServiceManager().ServiceBuild(tenantID, serviceID, &build); err != nil {
logrus.Debugf("build service error")

View File

@ -40,6 +40,8 @@ type ServiceHandler interface {
ServiceUpdate(sc map[string]interface{}) error
LanguageSet(langS *api_model.LanguageSet) error
GetService(tenantID string) ([]*dbmodel.TenantServices, error)
GetPagedTenantRes(offset,len int) ([]*api_model.TenantResource, error)
GetTenantRes(uuid string) (*api_model.TenantResource, error)
CodeCheck(c *api_model.CheckCodeStruct) error
ShareCloud(c *api_model.CloudShareStruct) error
ServiceDepend(action string, ds *api_model.DependService) error

View File

@ -28,7 +28,9 @@ import (
//TenantHandler tenant handler
type TenantHandler interface {
GetTenants() ([]*dbmodel.Tenants, error)
GetTenantsPaged(offset,len int) ([]*dbmodel.Tenants, error)
GetTenantsByName(name string) (*dbmodel.Tenants, error)
GetTenantsByUUID(uuid string) (*dbmodel.Tenants, error)
GetTenantsName() ([]string, error)
StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error)
TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error)

View File

@ -93,7 +93,20 @@ func CreateManager(conf option.Config) (*ServiceAction, error) {
EtcdCli: etcdCli,
}, nil
}
func checkDeployVersion(r *api_model.BuildServiceStruct) {
eventID := r.Body.EventID
logger := event.GetManager().GetLogger(eventID)
if len(r.Body.DeployVersion)==0 {
version,err:=db.GetManager().VersionInfoDao().GetVersionByEventID(eventID)
if err != nil {
logger.Info("获取部署版本信息失败",map[string]string{"step": "callback", "status": "failure"})
return
}
r.Body.DeployVersion=version.BuildVersion
logrus.Infof("change deploy version to %s",r.Body.DeployVersion)
}
}
//ServiceBuild service build
func (s *ServiceAction) ServiceBuild(tenantID, serviceID string, r *api_model.BuildServiceStruct) error {
eventID := r.Body.EventID
@ -109,6 +122,7 @@ func (s *ServiceAction) ServiceBuild(tenantID, serviceID string, r *api_model.Bu
switch r.Body.Kind {
case "source":
//源码构建
checkDeployVersion(r)
if err := s.sourceBuild(r, service); err != nil {
logger.Error("源码构建应用任务发送失败 "+err.Error(), map[string]string{"step": "callback", "status": "failure"})
return err
@ -117,6 +131,7 @@ func (s *ServiceAction) ServiceBuild(tenantID, serviceID string, r *api_model.Bu
return nil
case "slug":
//源码构建的分享至云市安装回平台
checkDeployVersion(r)
if err := s.slugBuild(r, service); err != nil {
logger.Error("slug构建应用任务发送失败"+err.Error(), map[string]string{"step": "callback", "status": "failure"})
return err
@ -125,6 +140,7 @@ func (s *ServiceAction) ServiceBuild(tenantID, serviceID string, r *api_model.Bu
return nil
case "image":
//镜像构建
checkDeployVersion(r)
if err := s.imageBuild(r, service); err != nil {
logger.Error("镜像构建应用任务发送失败 "+err.Error(), map[string]string{"step": "callback", "status": "failure"})
return err
@ -133,6 +149,7 @@ func (s *ServiceAction) ServiceBuild(tenantID, serviceID string, r *api_model.Bu
return nil
case "market":
//镜像构建分享至云市安装回平台
checkDeployVersion(r)
if err := s.marketBuild(r, service); err != nil {
logger.Error("云市构建应用任务发送失败 "+err.Error(), map[string]string{"step": "callback", "status": "failure"})
return err
@ -222,7 +239,6 @@ func (s *ServiceAction) slugBuild(r *api_model.BuildServiceStruct, service *dbmo
body["app_version"] = service.ServiceVersion
body["app_key"] = service.ServiceKey
body["namespace"] = service.Namespace
body["deploy_version"] = service.DeployVersion
body["operator"] = r.Body.Operator
body["event_id"] = r.Body.EventID
body["tenant_name"] = r.Body.TenantName
@ -455,7 +471,8 @@ func (s *ServiceAction) ServiceCreate(sc *api_model.ServiceStruct) error {
envs := sc.EnvsInfo
volumns := sc.VolumesInfo
dependIds := sc.DependIDs
logrus.Infof("creating new service,deploy version is %s",ts.DeployVersion)
ts.DeployVersion=""
tx := db.GetManager().Begin()
//服务信息表
@ -619,6 +636,47 @@ func (s *ServiceAction) GetService(tenantID string) ([]*dbmodel.TenantServices,
return services, nil
}
//GetPagedTenantRes get pagedTenantServiceRes(s)
func (s *ServiceAction) GetPagedTenantRes(offset,len int) ([]*api_model.TenantResource, error) {
services, err := db.GetManager().TenantServiceDao().GetPagedTenantService(offset,len)
if err != nil {
logrus.Errorf("get service by id error, %v, %v", services, err)
return nil, err
}
var result []*api_model.TenantResource
for _,v:=range services {
var res api_model.TenantResource
res.UUID=v["tenant"].(string)
res.Name=""
res.EID=""
res.AllocatedCPU=v["capcpu"].(int)
res.AllocatedMEM=v["capmem"].(int)
res.UsedCPU=v["usecpu"].(int)
res.UsedMEM=v["usemem"].(int)
result=append(result,&res)
}
return result, nil
}
//GetPagedTenantRes get pagedTenantServiceRes(s)
func (s *ServiceAction) GetTenantRes(uuid string) (*api_model.TenantResource, error) {
services, err := db.GetManager().TenantServiceDao().GetTenantServiceRes(uuid)
if err != nil {
logrus.Errorf("get service by id error, %v, %v", services, err)
return nil, err
}
logrus.Infof("get tenant service res is %v",services)
var res api_model.TenantResource
res.UUID=uuid
res.Name=""
res.EID=""
res.AllocatedCPU=services["capcpu"].(int)
res.AllocatedMEM=services["capmem"].(int)
res.UsedCPU=services["usecpu"].(int)
res.UsedMEM=services["usemem"].(int)
return &res, nil
}
//CodeCheck code check
func (s *ServiceAction) CodeCheck(c *api_model.CheckCodeStruct) error {
bodyJ, err := ffjson.Marshal(&c.Body)

View File

@ -32,10 +32,9 @@ import (
"github.com/goodrain/rainbond/pkg/db"
dbmodel "github.com/goodrain/rainbond/pkg/db/model"
"strings"
"github.com/Sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"strings"
)
//TenantAction tenant act
@ -92,9 +91,18 @@ func (t *TenantAction) GetTenants() ([]*dbmodel.Tenants, error) {
}
return tenants, err
}
func (t *TenantAction) GetTenantsPaged(offset,len int) ([]*dbmodel.Tenants, error) {
tenants, err := db.GetManager().TenantDao().GetALLTenants()
if err != nil {
return nil, err
}
return tenants, err
}
//TotalMemCPU StatsMemCPU
func (t *TenantAction) TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) {
//StatsMemCPU StatsMemCPU
func (t *TenantAction) TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error){
cpus := 0
mem := 0
for _, service := range services {
@ -132,6 +140,16 @@ func (t *TenantAction) GetTenantsByName(name string) (*dbmodel.Tenants, error) {
return tenant, err
}
//GetTenants get tenants
func (t *TenantAction) GetTenantsByUUID(uuid string) (*dbmodel.Tenants, error) {
tenant, err := db.GetManager().TenantDao().GetTenantByUUID(uuid)
if err != nil {
return nil, err
}
return tenant, err
}
//StatsMemCPU StatsMemCPU
func (t *TenantAction) StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) {

View File

@ -58,4 +58,4 @@ func (list TenantResList) Swap(i, j int) {
temp := list[i]
list[i] = list[j]
list[j] = temp
}
}

View File

@ -133,7 +133,9 @@ func GetCodeCheck(w http.ResponseWriter, r *http.Request) {
//findResultByServiceID
cr,err:=db.GetManager().CodeCheckResultDao().GetCodeCheckResult(serviceID)
if err!=nil {
logrus.Errorf("error get check result,details %s",err.Error())
httputil.ReturnError(r,w,500,err.Error())
return
}
httputil.ReturnSuccess(r,w,cr)
}

View File

@ -46,6 +46,7 @@ func AddAppPublish(w http.ResponseWriter, r *http.Request) {
b,err:=ioutil.ReadAll(r.Body)
if err != nil {
logrus.Errorf("error get request body ,details %s",err.Error())
httputil.ReturnError(r,w,404,err.Error())
return
}
defer r.Body.Close()
@ -53,12 +54,17 @@ func AddAppPublish(w http.ResponseWriter, r *http.Request) {
err=json.Unmarshal(b,&result)
if err != nil {
logrus.Errorf("error unmarshal use raw support,details %s",err.Error())
httputil.ReturnError(r,w,400,err.Error())
return
}
dbmodel:=convertPublishToDB(&result)
//checkAndGet
db.GetManager().AppPublishDao().AddModel(dbmodel)
err=db.GetManager().AppPublishDao().AddModel(dbmodel)
if err!=nil {
logrus.Errorf("error save publish record to db,details %s",err.Error())
httputil.ReturnError(r,w,500,err.Error())
}
httputil.ReturnSuccess(r, w, nil)
}
func convertPublishToDB(publish *model.AppPublish) *dbmodel.AppPublish {

View File

@ -227,8 +227,8 @@ func (e *exectorManager) codeCheck(in []byte) {
}
func (e *exectorManager) appBuild(in []byte) {
eventID := gjson.GetBytes(in, "event_id").String()
finalStatus:="failure"
dest := gjson.GetBytes(in, "dest").String()
//finalStatus:="failure"
//dest := gjson.GetBytes(in, "dest").String()
logger := event.GetManager().GetLogger(eventID)
logger.Info("应用编译构建任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"})
@ -246,26 +246,26 @@ func (e *exectorManager) appBuild(in []byte) {
logger.Info("应用编译构建任务执行失败", map[string]string{"step": "callback", "status": "failure"})
}
} else {
finalStatus="success"
updateBuildResult(eventID,finalStatus,dest)
//finalStatus="success"
//updateBuildResult(eventID,finalStatus,dest)
break
}
}
}()
updateBuildResult(eventID,finalStatus,dest)
}
func updateBuildResult(eventID,finalStatus,dest string) {
if dest == ""||!strings.Contains(dest,"y") {
v,err:=db.GetManager().VersionInfoDao().GetVersionByEventID(eventID)
if err != nil {
logrus.Errorf("error get version by eventID %s from db,details %s",eventID,err.Error())
return
}
v.FinalStatus=finalStatus
db.GetManager().VersionInfoDao().UpdateModel(v)
}
//updateBuildResult(eventID,finalStatus,dest)
}
//func updateBuildResult(eventID,finalStatus,dest string) {
// if dest == ""||!strings.Contains(dest,"y") {
// v,err:=db.GetManager().VersionInfoDao().GetVersionByEventID(eventID)
// if err != nil {
// logrus.Errorf("error get version by eventID %s from db,details %s",eventID,err.Error())
// return
// }
// v.FinalStatus=finalStatus
// db.GetManager().VersionInfoDao().UpdateModel(v)
// }
//
//}
func (e *exectorManager) pluginImageBuild1(in []byte) {
eventID := gjson.GetBytes(in, "event_id").String()
logger := event.GetManager().GetLogger(eventID)

View File

@ -37,6 +37,7 @@ type TenantDao interface {
GetTenantByUUID(uuid string) (*model.Tenants, error)
GetTenantIDByName(tenantName string) (*model.Tenants, error)
GetALLTenants() ([]*model.Tenants, error)
GetPagedTenants(offset,len int) ([]*model.Tenants, error)
}
//LicenseDao LicenseDao

View File

@ -86,6 +86,16 @@ func (t *TenantDaoImpl) GetALLTenants() ([]*model.Tenants, error) {
return tenants, nil
}
//GetALLTenants GetALLTenants
func (t *TenantDaoImpl) GetPagedTenants(offset,len int) ([]*model.Tenants, error) {
var tenants []*model.Tenants
if err := t.DB.Find(&tenants).Group("").Error; err != nil {
return nil, err
}
return tenants, nil
}
//TenantServicesDaoImpl 租户应用dao
type TenantServicesDaoImpl struct {
DB *gorm.DB
@ -123,6 +133,7 @@ func (t *TenantServicesDaoImpl) GetServiceByID(serviceID string) (*model.TenantS
return &service, nil
}
//GetCPUAndMEM GetCPUAndMEM
func (t *TenantServicesDaoImpl) GetCPUAndMEM(tenantName []string) ([]*map[string]interface{}, error) {
if len(tenantName) == 0 {
@ -166,6 +177,51 @@ func (t *TenantServicesDaoImpl) GetCPUAndMEM(tenantName []string) ([]*map[string
return rc, nil
}
//GetPagedTenantResource GetPagedTenantResource
func (t *TenantServicesDaoImpl) GetPagedTenantService(offset,len int) ([]map[string]interface{}, error) {
rows, err := t.DB.Raw("select tenant_id,sum(if (cur_status != 'closed' && cur_status != 'undeploy',container_cpu * replicas,0)) as use_cpu,sum(container_cpu*replicas) as cap_cpu,sum(if (cur_status != 'closed' && cur_status != 'undeploy',container_memory * replicas,0)) as use_memory,sum(container_memory*replicas) as cap_memory from tenant_services group by tenant_id order by use_memory desc limit ?,?",offset,len).Rows()
if err != nil {
return nil, err
}
defer rows.Close()
var rc []map[string]interface{}
for rows.Next() {
var tenantID string
var capCpu int
var useCpu int
var capMem int
var useMem int
rows.Scan(&tenantID, &useCpu,&capCpu,&useMem,&capMem)
res := make(map[string]interface{})
res["capcpu"] = capCpu
res["usecpu"] = useCpu
res["capmem"] = capMem
res["usemem"] = useMem
res["tenant"] =tenantID
rc = append(rc, res)
}
return rc, nil
}
//GetTenantServiceRes GetTenantServiceRes
func (t *TenantServicesDaoImpl) GetTenantServiceRes(uuid string) (map[string]interface{}, error) {
row := t.DB.Raw("select sum(if (cur_status != 'closed' && cur_status != 'undeploy',container_cpu * replicas,0)) as use_cpu,sum(container_cpu*replicas) as cap_cpu,sum(if (cur_status != 'closed' && cur_status != 'undeploy',container_memory * replicas,0)) as use_memory,sum(container_memory*replicas) as cap_memory from tenant_services where tenant_id =? order by use_memory desc",uuid).Row()
var capCpu int
var useCpu int
var capMem int
var useMem int
row.Scan( &useCpu,&capCpu,&useMem,&capMem)
res := make(map[string]interface{})
res["capcpu"] = capCpu
res["usecpu"] = useCpu
res["capmem"] = capMem
res["usemem"] = useMem
res["tenant"] =uuid
logrus.Infof("get tenant %s service resource :%v",res)
return res, nil
}
//GetServiceAliasByIDs 获取应用别名
func (t *TenantServicesDaoImpl) GetServiceAliasByIDs(uids []string) ([]*model.TenantServices, error) {
var services []*model.TenantServices

View File

@ -24,6 +24,8 @@ import (
"os/exec"
"github.com/goodrain/rainbond/pkg/grctl/clients"
"github.com/Sirupsen/logrus"
"strings"
"github.com/goodrain/rainbond/pkg/node/api/model"
)
func NewCmdDomain() cli.Command {
@ -79,17 +81,80 @@ func NewCmdCheckTask() cli.Command {
logrus.Errorf("error get task list,details %s",err.Error())
return err
}
var result []*ExecedTask
for _,v:=range tasks{
if taskStatus,ok:=v.Status[uuid];ok{
fmt.Printf("task %s status:%s,complete status %s\n",v.ID,taskStatus.Status,taskStatus.CompleStatus)
taskStatus,ok:=v.Status[uuid]
if ok{
status:=strings.ToLower(taskStatus.Status)
if status=="complete" ||status=="start"{
var taskentity =&ExecedTask{}
taskentity.ID=v.ID
taskentity.Status=taskStatus.Status
taskentity.Depends=[]string{}
dealDepend(taskentity,v)
dealNext(taskentity,tasks)
result=append(result, taskentity)
continue
}
}else {
_,scheduled:=v.Scheduler.Status[uuid]
if scheduled {
var taskentity =&ExecedTask{}
taskentity.ID=v.ID
taskentity.Depends=[]string{}
dealDepend(taskentity,v)
dealNext(taskentity,tasks)
allDepDone:=true
for _,dep:=range taskentity.Depends {
task,_:=clients.NodeClient.Tasks().Get(dep)
_,depOK:=task.Task.Status[uuid]
if !depOK {
allDepDone=false
break
}
}
if allDepDone {
taskentity.Status="start"
result=append(result, taskentity)
}
}
}
}
for _,v:=range result {
fmt.Printf("task %s is %s,depends is %v\n",v.ID,v.Status,v.Depends)
}
return nil
},
}
return c
}
func dealDepend(result *ExecedTask,task *model.Task) {
if task.Temp.Depends != nil {
for _,v:=range task.Temp.Depends{
result.Depends=append(result.Depends,v.DependTaskID)
}
}
}
func dealNext(task *ExecedTask, tasks []*model.Task) {
for _,v:=range tasks {
if v.Temp.Depends != nil {
for _,dep:=range v.Temp.Depends{
if dep.DependTaskID == task.ID {
task.Next=append(task.Next,v.ID)
}
}
}
}
}
type ExecedTask struct {
ID string
Status string
Depends []string
Next []string
}

View File

@ -581,41 +581,24 @@ func RegionRes(w http.ResponseWriter, r *http.Request) {
capMem+=v.NodeStatus.Capacity.Memory().Value()
}
}
//
//tenants, error := db.GetManager().TenantDao().GetALLTenants()
//if error != nil {
// logrus.Errorf("error get tenants ,details %s",error.Error())
//}
//s:=len(tenants)
nodeList, error := k8s.K8S.Core().Nodes().List(metav1.ListOptions{})
if error != nil {
logrus.Errorf("error get nodes from k8s ,details %s", error.Error())
api.ReturnError(r, w, 500, "failed,details "+error.Error())
return
ps, _ := k8s.GetPodsByNodeName("")
var cpuR int64= 0
var memR int64= 0
for _, pv := range ps {
rc := pv.Spec.Containers[0].Resources.Requests.Cpu().MilliValue()
rm := pv.Spec.Containers[0].Resources.Requests.Memory().Value()
cpuR += rc
memR += rm
}
cpuR := 0
memR := 0
for _, v := range nodeList.Items {
ps, _ := k8s.GetPodsByNodeName(v.Name)
for _, pv := range ps {
rc := pv.Spec.Containers[0].Resources.Requests.Cpu().String()
rm := pv.Spec.Containers[0].Resources.Requests.Memory().String()
cpuR += getCpuInt(rc)
memR += convertMemoryToMBInt(rm, true)
}
}
podMemRequestMB:=memR/1024/1024
logrus.Infof("get total cpu request %v,memory request %v by value",cpuR,podMemRequestMB)
result := new(model.ClusterResource)
result.CapCpu=int(capCpu)
result.CapMem=int(capMem)/1024/1024
result.ReqCpu = float32(cpuR)/1000
result.ReqMem = memR
result.ReqMem = int(podMemRequestMB)
result.Node=len(nodes)
result.Tenant=0
logrus.Infof("get cpu %v and mem %v", capCpu, capMem)
api.ReturnSuccess(r, w, result)
}
func UpdateNode(w http.ResponseWriter, r *http.Request) {

View File

@ -37,6 +37,7 @@ import (
"strconv"
"github.com/goodrain/rainbond/pkg/node/core/k8s"
"io/ioutil"
"errors"
)
func init() {
@ -145,6 +146,101 @@ func GetRuleNodes(w http.ResponseWriter, r *http.Request) {
httputil.ReturnSuccess(r, w, masternodes)
}
//UpNode 节点上线,计算节点操作
func CheckNode(w http.ResponseWriter, r *http.Request) {
nodeUID := strings.TrimSpace(chi.URLParam(r, "node_id"))
if len(nodeUID)==0 {
err:=utils.APIHandleError{
Code:404,
Err:errors.New(fmt.Sprintf("can't find node by node_id %s",nodeUID)),
}
err.Handle(r,w)
return
}
tasks, err := taskService.GetTasks()
if err != nil {
err.Handle(r, w)
return
}
var result []*ExecedTask
for _,v:=range tasks{
taskStatus,ok:=v.Status[nodeUID]
if ok{
status:=strings.ToLower(taskStatus.Status)
if status=="complete" ||status=="start"{
var execedTask =&ExecedTask{}
execedTask.ID=v.ID
execedTask.Status=taskStatus.Status
execedTask.Depends=[]string{}
dealDepend(execedTask,v)
dealNext(execedTask,tasks)
result=append(result, execedTask)
continue
}
}else {
_,scheduled:=v.Scheduler.Status[nodeUID]
if scheduled {
var execedTask =&ExecedTask{}
execedTask.ID=v.ID
execedTask.Depends=[]string{}
dealDepend(execedTask,v)
dealNext(execedTask,tasks)
allDepDone:=true
for _,dep:=range execedTask.Depends {
task,_:=taskService.GetTask(dep)
_,depOK:=task.Status[nodeUID]
if !depOK {
allDepDone=false
break
}
}
//所有依赖都完成了没有自己的status说明自己未完成已经被调度说明 正在执行
if allDepDone {
execedTask.Status="start"
result=append(result, execedTask)
}
}
}
}
httputil.ReturnSuccess(r, w, result)
}
func dealNext(task *ExecedTask, tasks []*model.Task) {
for _,v:=range tasks {
if v.Temp.Depends != nil {
for _,dep:=range v.Temp.Depends{
if dep.DependTaskID == task.ID {
task.Next=append(task.Next,v.ID)
}
}
}
}
}
func dealDepend(result *ExecedTask,task *model.Task) {
if task.Temp.Depends != nil {
for _,v:=range task.Temp.Depends{
result.Depends=append(result.Depends,v.DependTaskID)
}
}
}
type ExecedTask struct {
ID string
Status string
Depends []string
Next []string
}
//DeleteRainbondNode 节点删除
func DeleteRainbondNode(w http.ResponseWriter, r *http.Request) {
nodeID := chi.URLParam(r, "node_id")
@ -256,24 +352,26 @@ func Instances(w http.ResponseWriter, r *http.Request) {
pod.Name = v.Name
pod.Id = serviceId
lc := v.Spec.Containers[0].Resources.Limits.Cpu().Value()
lc := v.Spec.Containers[0].Resources.Limits.Cpu().MilliValue()
cpuL += lc
lm := v.Spec.Containers[0].Resources.Limits.Memory().Value()
memL += lm
rc := v.Spec.Containers[0].Resources.Requests.Cpu().Value()
rc := v.Spec.Containers[0].Resources.Requests.Cpu().MilliValue()
cpuR += rc
rm := v.Spec.Containers[0].Resources.Requests.Memory().Value()
memR += rm
logrus.Infof("namespace %s,podid %s :limit cpu %s,requests cpu %s,limit mem %s,request mem %s", pod.Namespace, pod.Id, lc, rc, lm, rm)
pod.CPURequests = strconv.Itoa(int(rc))
logrus.Infof("namespace %s,podid %s :limit cpu %v,requests cpu %v,limit mem %v,request mem %v,cap cpu is %v,cap mem is %v", pod.Namespace, pod.Name, lc, rc, lm, rm,capCPU,capMEM)
pod.CPURequestsR = strconv.FormatFloat(float64(rc*100)/float64(capCPU), 'f', 1, 64)
pod.CPURequests = strconv.FormatFloat(float64(rc)/float64(1000), 'f', 2, 64)
pod.CPULimits = strconv.Itoa(int(lc))
pod.CPULimitsR = strconv.FormatFloat(float64(lc*100)/float64(capCPU), 'f', 1, 64)
pod.CPURequestsR = strconv.FormatFloat(float64(rc/10)/float64(capCPU), 'f', 1, 64)
pod.CPULimits = strconv.FormatFloat(float64(lc)/float64(1000), 'f', 2, 64)
pod.CPULimitsR = strconv.FormatFloat(float64(lc/10)/float64(capCPU), 'f', 1, 64)
pod.MemoryRequests = strconv.Itoa(int(rm))
pod.MemoryRequestsR = strconv.FormatFloat(float64(rm*100)/float64(capMEM), 'f', 1, 64)

View File

@ -77,6 +77,25 @@ type HostNode struct {
ClusterNode
}
type NodeList []*HostNode
func (list NodeList) Len() int {
return len(list)
}
func (list NodeList) Less(i, j int) bool {
if list[i].InternalIP < list[j].InternalIP {
return true
} else {
return false
}
}
func (list NodeList) Swap(i, j int) {
var temp = list[i]
list[i] = list[j]
list[j] = temp
}
//GetNodeFromKV 从etcd解析node信息
func GetNodeFromKV(kv *mvccpb.KeyValue) *HostNode {
var node HostNode

View File

@ -70,6 +70,7 @@ func Routers(mode string) *chi.Mux {
r.Post("/{node_id}/down", controller.DownNode) //节点下线
r.Post("/{node_id}/up", controller.UpNode) //节点上线
r.Get("/{node_id}/instance", controller.Instances) //节点上线
r.Get("/{node_id}/check", controller.CheckNode)
//历史API
r.Get("/{node}/details", controller.GetNodeDetails)

View File

@ -28,6 +28,7 @@ import (
"github.com/goodrain/rainbond/pkg/node/masterserver"
"github.com/goodrain/rainbond/pkg/node/utils"
"github.com/twinj/uuid"
"sort"
)
//NodeService node service
@ -109,7 +110,10 @@ func (n *NodeService) GetAllNode() ([]*model.HostNode, *utils.APIHandleError) {
if n.nodecluster == nil {
return nil, utils.CreateAPIHandleError(400, fmt.Errorf("this node can not support this api"))
}
return n.nodecluster.GetAllNode(), nil
nodes:=n.nodecluster.GetAllNode()
sort.Sort(model.NodeList(nodes))
return nodes, nil
}
//CordonNode 设置节点不可调度熟悉

View File

@ -42,6 +42,7 @@ import (
"github.com/goodrain/rainbond/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"os"
"os/exec"
)
@ -176,18 +177,41 @@ func (n *NodeCluster) GetNode(id string) *model.HostNode {
n.lock.Lock()
defer n.lock.Unlock()
if node, ok := n.nodes[id]; ok {
if node.NodeStatus != nil {
if node.Unschedulable {
node.Status = "unschedulable"
} else {
node.Status = "schedulable"
}
node.AvailableCPU = node.NodeStatus.Allocatable.Cpu().Value()
node.AvailableMemory = node.NodeStatus.Allocatable.Memory().Value()
}
return node
}
return nil
}
func RegToHost(node *model.HostNode,opt string) {
uuid:=node.ID
internalIP:=node.InternalIP
cmd := exec.Command("bash", "/usr/share/gr-rainbond-node/gaops/jobs/cron/common/node_update_hosts.sh",uuid+" "+internalIP+" "+opt)
outbuf:=bytes.NewBuffer(nil)
cmd.Stdout=outbuf
err:=cmd.Run()
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}
func RegToHost(node *model.HostNode, opt string) {
if !exists("/usr/share/gr-rainbond-node/gaops/jobs/cron/common/node_update_hosts.sh") {
return
}
uuid := node.ID
internalIP := node.InternalIP
logrus.Infof("node 's hostname is %s", node.HostName)
cmd := exec.Command("bash", "/usr/share/gr-rainbond-node/gaops/jobs/cron/common/node_update_hosts.sh", uuid, internalIP, opt)
outbuf := bytes.NewBuffer(nil)
cmd.Stdout = outbuf
err := cmd.Run()
if err != nil {
logrus.Errorf("error update /etc/hosts,details %s",err.Error())
return
@ -206,12 +230,10 @@ func (n *NodeCluster) watchNodes() {
switch {
case ev.IsCreate(), ev.IsModify():
if node := n.getNodeFromKV(ev.Kv); node != nil {
RegToHost(node,"add")
n.CacheNode(node)
}
case ev.Type == client.EventTypeDelete:
if node := n.getNodeFromKey(string(ev.Kv.Key)); node != nil {
RegToHost(node,"del")
n.RemoveNode(node)
}
}
@ -221,14 +243,17 @@ func (n *NodeCluster) watchNodes() {
switch {
case ev.IsCreate(), ev.IsModify():
if node := n.getNodeFromKey(string(ev.Kv.Key)); node != nil {
node.Alived = true
node.UpTime = time.Now()
RegToHost(node, "add")
n.UpdateNode(node)
}
case ev.Type == client.EventTypeDelete:
if node := n.getNodeFromKey(string(ev.Kv.Key)); node != nil {
node.Alived = false
node.DownTime = time.Now()
RegToHost(node, "del")
n.UpdateNode(node)
}
}
@ -350,6 +375,15 @@ func (n *NodeCluster) GetAllNode() (nodes []*model.HostNode) {
n.lock.Lock()
defer n.lock.Unlock()
for _, node := range n.nodes {
if node.NodeStatus != nil {
if node.Unschedulable {
node.Status = "unschedulable"
} else {
node.Status = "schedulable"
}
node.AvailableCPU = node.NodeStatus.Allocatable.Cpu().Value()
node.AvailableMemory = node.NodeStatus.Allocatable.Memory().Value()
}
nodes = append(nodes, node)
}
return

View File

@ -45,7 +45,16 @@ func (m *manager) StartDeployment(serviceID string, logger event.Logger) (*v1bet
return nil, err
}
//判断应用镜像名称是否合法,非法镜像名进制启动
if !strings.HasPrefix(builder.service.ImageName, "goodrain.me/") {
imageName:=builder.service.ImageName
deployVersion,err:=m.dbmanager.VersionInfoDao().GetVersionByDeployVersion(builder.service.DeployVersion,serviceID)
if err != nil {
logrus.Warnf("error get version info by deployversion %s,details %s",builder.service.DeployVersion,err.Error())
}else{
if CheckVersionInfo(deployVersion) {
imageName=deployVersion.ImageName
}
}
if !strings.HasPrefix(imageName, "goodrain.me/") {
logger.Error(fmt.Sprintf("启动应用失败,镜像名(%s)非法,请重新构建应用", builder.service.ImageName), map[string]string{"step": "callback", "status": "error"})
return nil, fmt.Errorf("service image name invoid, it only can with prefix goodrain.me/")
}

View File

@ -44,13 +44,14 @@ func (m *manager) StartReplicationController(serviceID string, logger event.Logg
return nil, err
}
//判断应用镜像名称是否合法,非法镜像名进制启动
deployVersion, err := m.dbmanager.VersionInfoDao().GetVersionByDeployVersion(builder.service.DeployVersion, serviceID)
var imageName string
deployVersion,err:=m.dbmanager.VersionInfoDao().GetVersionByDeployVersion(builder.service.DeployVersion,serviceID)
imageName:=builder.service.ImageName
if err != nil {
logrus.Warnf("error get version info by deployversion %s,details %s", builder.service.DeployVersion, err.Error())
imageName = builder.service.ImageName
} else {
imageName = deployVersion.ImageName
logrus.Warnf("error get version info by deployversion %s,details %s",builder.service.DeployVersion,err.Error())
}else{
if CheckVersionInfo(deployVersion) {
imageName=deployVersion.ImageName
}
}
if !strings.HasPrefix(imageName, "goodrain.me/") {
logger.Error("启动应用失败,镜像名(%s)非法,请重新构建应用", map[string]string{"step": "callback", "status": "error"})
@ -87,7 +88,15 @@ func (m *manager) StartReplicationController(serviceID string, logger event.Logg
}
return result, nil
}
func CheckVersionInfo(version *model.VersionInfo) bool {
if !strings.Contains(strings.ToLower(version.FinalStatus),"success") {
return false
}
if len(version.ImageName)==0||!strings.Contains(version.ImageName,"goodrain.me/") {
return false
}
return true
}
//StopReplicationController 停止
func (m *manager) StopReplicationController(serviceID string, logger event.Logger) error {
logger.Info("停止删除ReplicationController资源开始", map[string]string{"step": "worker-appm", "status": "starting"})

View File

@ -45,13 +45,14 @@ func (m *manager) StartStatefulSet(serviceID string, logger event.Logger) (*v1be
return nil, err
}
//判断应用镜像名称是否合法,非法镜像名进制启动
deployVersion, err := m.dbmanager.VersionInfoDao().GetVersionByDeployVersion(builder.service.DeployVersion, serviceID)
var imageName string
deployVersion,err:=m.dbmanager.VersionInfoDao().GetVersionByDeployVersion(builder.service.DeployVersion,serviceID)
imageName:=builder.service.ImageName
if err != nil {
logrus.Warnf("error get version info by deployversion %s,details %s", builder.service.DeployVersion, err.Error())
imageName = builder.service.ImageName
} else {
imageName = deployVersion.ImageName
logrus.Warnf("error get version info by deployversion %s,details %s",builder.service.DeployVersion,err.Error())
}else{
if CheckVersionInfo(deployVersion) {
imageName=deployVersion.ImageName
}
}
if !strings.HasPrefix(imageName, "goodrain.me/") {
logger.Error("启动应用失败,镜像名(%s)非法,请重新构建应用", map[string]string{"step": "callback", "status": "error"})