feat(region): Check periodically whether Guest Template is valid

This commit is contained in:
rainzm
2020-05-22 13:57:37 +08:00
parent 1265e5d1e9
commit f9b7c7e571
4 changed files with 63 additions and 1 deletions
+20
View File
@@ -0,0 +1,20 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package compute
const (
GT_READY = "ready"
GT_INVALID = "invalid"
)
+38 -1
View File
@@ -17,6 +17,7 @@ package models
import (
"context"
"fmt"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
@@ -80,6 +81,8 @@ type SGuestTemplate struct {
// 其他配置信息
Content jsonutils.JSONObject `nullable:"false" list:"user" update:"user" create:"optional" json:"content"`
LastCheckTime time.Time
}
var GuestTemplateManager *SGuestTemplateManager
@@ -125,10 +128,19 @@ func (gtm *SGuestTemplateManager) ValidateCreateData(
func (gt *SGuestTemplate) PostCreate(ctx context.Context, userCred mcclient.TokenCredential,
ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
gt.SetStatus(userCred, "ready", "")
gt.SetStatus(userCred, computeapis.GT_READY, "")
gt.updateCheckTime()
logclient.AddActionLogWithContext(ctx, gt, logclient.ACT_CREATE, nil, userCred, true)
}
func (gt *SGuestTemplate) updateCheckTime() error {
_, err := db.Update(gt, func() error {
gt.LastCheckTime = time.Now()
return nil
})
return err
}
func (gt *SGuestTemplate) PostUpdate(ctx context.Context, userCred mcclient.TokenCredential,
query jsonutils.JSONObject, data jsonutils.JSONObject) {
logclient.AddActionLogWithContext(ctx, gt, logclient.ACT_UPDATE, nil, userCred, true)
@@ -749,3 +761,28 @@ func (g *SGuest) PerformSaveTemplate(ctx context.Context, userCred mcclient.Toke
}
return nil, nil
}
func (gm *SGuestTemplateManager) InspectAllTemplate(ctx context.Context, userCred mcclient.TokenCredential, isStart bool) {
lastCheckTime := time.Now().Add(time.Duration(-options.Options.GuestTemplateCheckInterval) * time.Hour)
q := gm.Query().Equals("status", computeapis.GT_READY)
q = q.Filter(sqlchemy.OR(sqlchemy.IsNull(q.Field("last_check_time")), sqlchemy.LE(q.Field("last_check_time"),
lastCheckTime)))
gts := make([]SGuestTemplate, 0, 10)
err := db.FetchModelObjects(gm, q, &gts)
if err != nil {
log.Errorf("Unable to fetch all guest templates that need to check: %s", err.Error())
return
}
for i := range gts {
_, err := GuestManager.validateCreateData(ctx, userCred, gts[i].GetOwnerId(), jsonutils.NewDict(),
gts[i].Content.(*jsonutils.JSONDict))
if err == nil {
gts[i].updateCheckTime()
continue
}
// invalid
gts[i].updateCheckTime()
reason := fmt.Sprintf("During the inspection, the guest template is not available: %s", err.Error())
gts[i].SetStatus(userCred, computeapis.GT_INVALID, reason)
}
}
+3
View File
@@ -134,6 +134,9 @@ type ComputeOptions struct {
FetchEtcdServiceInfoAndUseEtcdLock bool `default:"true" help:"fetch etcd service info and use etcd lock"`
GuestTemplateCheckInterval int `help:"The interval between two consecutive inspections about Guest Template,
unit: h" default:"12"`
SCapabilityOptions
SASControllerOptions
common_options.CommonOptions
+2
View File
@@ -142,6 +142,8 @@ func StartService() {
cron.AddJobEveryFewDays("SyncDBInstanceSkus", opts.SyncSkusDay, opts.SyncSkusHour, 0, 0, models.SyncDBInstanceSkus, true)
cron.AddJobEveryFewDays("SyncElasticCacheSkus", opts.SyncSkusDay, opts.SyncSkusHour, 0, 0, models.SyncElasticCacheSkus, true)
cron.AddJobEveryFewDays("StorageSnapshotsRecycle", 1, 2, 0, 0, models.StorageManager.StorageSnapshotsRecycle, false)
cron.AddJobEveryFewHour("InspectAllTemplate", 1, 0, 0, models.GuestTemplateManager.InspectAllTemplate, true)
go cron.Start2(ctx, electObj)
// init auto scaling controller