-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(region,host): guest sync os info (#19100)
- Loading branch information
Showing
17 changed files
with
218 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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 tasks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"yunion.io/x/jsonutils" | ||
|
||
api "yunion.io/x/onecloud/pkg/apis/compute" | ||
"yunion.io/x/onecloud/pkg/cloudcommon/db" | ||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" | ||
"yunion.io/x/onecloud/pkg/compute/models" | ||
"yunion.io/x/onecloud/pkg/util/logclient" | ||
) | ||
|
||
func init() { | ||
taskman.RegisterTask(GuestQgaSyncOsInfoTask{}) | ||
} | ||
|
||
type GuestQgaSyncOsInfoTask struct { | ||
SGuestBaseTask | ||
} | ||
|
||
func (self *GuestQgaSyncOsInfoTask) guestPing(ctx context.Context, guest *models.SGuest) error { | ||
host, err := guest.GetHost() | ||
if err != nil { | ||
return err | ||
} | ||
return guest.GetDriver().QgaRequestGuestPing(ctx, self.GetTaskRequestHeader(), host, guest, true, nil) | ||
} | ||
|
||
func (self *GuestQgaSyncOsInfoTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { | ||
guest := obj.(*models.SGuest) | ||
host, _ := guest.GetHost() | ||
res, err := guest.GetDriver().QgaRequestGetOsInfo(ctx, self.UserCred, nil, host, guest) | ||
if err != nil { | ||
self.taskFailed(ctx, guest, err.Error()) | ||
return | ||
} | ||
self.updateOsInfo(ctx, guest, res) | ||
} | ||
|
||
type GuestOsInfo struct { | ||
Id string `json:"id"` | ||
KernelRelease string `json:"kernel-release"` | ||
KernelVersion string `json:"kernel-version"` | ||
Machine string `json:"machine"` | ||
Name string `json:"name"` | ||
PrettyName string `json:"pretty-name"` | ||
Version string `json:"version"` | ||
VersionId string `json:"version-id"` | ||
} | ||
|
||
func (self *GuestQgaSyncOsInfoTask) updateOsInfo(ctx context.Context, guest *models.SGuest, res jsonutils.JSONObject) { | ||
osInfo := new(GuestOsInfo) | ||
err := res.Unmarshal(osInfo) | ||
if err != nil { | ||
self.taskFailed(ctx, guest, fmt.Sprintf("failed unmarshal osinfo %s", err)) | ||
return | ||
} | ||
osType := "Linux" | ||
if osInfo.Id == "mswindows" { | ||
osType = "Windows" | ||
} | ||
osInput := api.ServerSetOSInfoInput{ | ||
Type: osType, | ||
Distribution: osInfo.PrettyName, | ||
Version: osInfo.Version, | ||
Arch: osInfo.Machine, | ||
} | ||
_, err = guest.PerformSetOsInfo(ctx, self.UserCred, nil, osInput) | ||
if err != nil { | ||
self.taskFailed(ctx, guest, fmt.Sprintf("failed set osinfo %s", err)) | ||
return | ||
} | ||
self.OnUpdateOsInfoComplete(ctx, guest, osInput) | ||
} | ||
|
||
func (self *GuestQgaSyncOsInfoTask) taskFailed(ctx context.Context, guest *models.SGuest, reason string) { | ||
guest.SetStatus(self.UserCred, api.VM_QGA_EXEC_COMMAND_FAILED, reason) | ||
guest.UpdateQgaStatus(api.QGA_STATUS_EXECUTE_FAILED) | ||
db.OpsLog.LogEvent(guest, db.ACT_SYNC_OS_INFO_FAIL, reason, self.UserCred) | ||
logclient.AddActionLogWithContext(ctx, guest, logclient.ACT_SET_USER_PASSWORD, reason, self.UserCred, false) | ||
self.SetStageFailed(ctx, jsonutils.NewString(reason)) | ||
} | ||
|
||
func (self *GuestQgaSyncOsInfoTask) OnUpdateOsInfoComplete(ctx context.Context, guest *models.SGuest, osInput api.ServerSetOSInfoInput) { | ||
guest.SetStatus(self.UserCred, api.VM_RUNNING, "on qga set user password success") | ||
guest.UpdateQgaStatus(api.QGA_STATUS_AVAILABLE) | ||
db.OpsLog.LogEvent(guest, db.ACT_SYNC_OS_INFO, jsonutils.Marshal(osInput), self.UserCred) | ||
logclient.AddActionLogWithContext(ctx, guest, logclient.ACT_SET_USER_PASSWORD, jsonutils.Marshal(osInput), self.UserCred, false) | ||
self.SetStageComplete(ctx, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.