Skip to content

Commit

Permalink
feature: support baremetal profile (#20687)
Browse files Browse the repository at this point in the history
Co-authored-by: Qiu Jian <[email protected]>
  • Loading branch information
swordqiu and Qiu Jian authored Jun 30, 2024
1 parent c333a80 commit 44183fb
Show file tree
Hide file tree
Showing 43 changed files with 995 additions and 328 deletions.
50 changes: 50 additions & 0 deletions cmd/climc/shell/compute/baremetal_profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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

import (
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/printutils"

"yunion.io/x/onecloud/cmd/climc/shell"
"yunion.io/x/onecloud/pkg/mcclient"
baremetalmodules "yunion.io/x/onecloud/pkg/mcclient/modules/compute/baremetal"
baremetaloptions "yunion.io/x/onecloud/pkg/mcclient/options/compute/baremetal"
)

func init() {
cmd := shell.NewResourceCmd(&baremetalmodules.BaremetalProfiles)
cmd.List(new(baremetaloptions.BaremetalProfileListOptions))
cmd.Show(new(baremetaloptions.BaremetalProfileIdOptions))
cmd.Delete(new(baremetaloptions.BaremetalProfileIdOptions))

type BaremetalProfileMatchOpts struct {
OEMNAME string
MODEL string
}
R(&BaremetalProfileMatchOpts{}, "baremetal-profile-match", "find match baremetal profile by oemname and model", func(s *mcclient.ClientSession, args *BaremetalProfileMatchOpts) error {
specs, err := baremetalmodules.BaremetalProfiles.GetMatchProfiles(s, args.OEMNAME, args.MODEL)
if err != nil {
return errors.Wrap(err, "GetMatchProfiles")
}
listResults := printutils.ListResult{}
for _, spec := range specs {
listResults.Data = append(listResults.Data, jsonutils.Marshal(spec))
}
printList(&listResults, nil)
return nil
})
}
15 changes: 15 additions & 0 deletions pkg/apis/compute/baremetal/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 baremetal // import "yunion.io/x/onecloud/pkg/apis/compute/baremetal"
165 changes: 165 additions & 0 deletions pkg/apis/compute/baremetal/profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// 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 baremetal

import (
"strings"

"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/cloudcommon/types"
)

const (
DefaultBaremetalProfileId = "default"
)

type BaremetalProfileListInput struct {
apis.StandaloneAnonResourceListInput

OemName []string `json:"oem_name"`
Model []string `json:"model"`
}

func (input *BaremetalProfileListInput) Normalize() {
for i := range input.OemName {
input.OemName[i] = strings.TrimSpace(input.OemName[i])
}
for i := range input.Model {
input.Model[i] = strings.TrimSpace(input.Model[i])
}
}

type BaremetalProfileCreateInput struct {
apis.StandaloneAnonResourceCreateInput

OemName string
Model string
LanChannel uint8
RootId int
RootName string
StrongPass bool
}

type BaremetalProfileUpdateInput struct {
apis.StandaloneAnonResourceBaseUpdateInput

LanChannel uint8
RootId *int
RootName string
StrongPass *bool
}

type BaremetalProfileDetails struct {
SBaremetalProfile
}

func (detail BaremetalProfileDetails) ToSpec() BaremetalProfileSpec {
channels := make([]uint8, 0)
if detail.LanChannel > 0 {
channels = append(channels, detail.LanChannel)
}
if detail.LanChannel2 > 0 {
channels = append(channels, detail.LanChannel2)
}
if detail.LanChannel3 > 0 {
channels = append(channels, detail.LanChannel3)
}
return BaremetalProfileSpec{
OemName: detail.OemName,
Model: detail.Model,
LanChannels: channels,
RootName: detail.RootName,
RootId: detail.RootId,
StrongPass: detail.StrongPass,
}
}

type BaremetalProfileSpec struct {
OemName string
Model string
LanChannels []uint8
RootName string
RootId int
StrongPass bool
}

type BaremetalProfileSpecs []BaremetalProfileSpec

func (a BaremetalProfileSpecs) Len() int { return len(a) }
func (a BaremetalProfileSpecs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a BaremetalProfileSpecs) Less(i, j int) bool {
if a[i].OemName != a[j].OemName {
return a[i].OemName < a[j].OemName
}
if a[i].Model != a[j].Model {
return a[i].Model < a[j].Model
}
return false
}

var PredefinedProfiles = []BaremetalProfileSpec{
{
OemName: "",
LanChannels: []uint8{1, 2, 8},
RootName: "root",
RootId: 2,
},
{
OemName: types.OEM_NAME_INSPUR,
LanChannels: []uint8{8, 1},
RootName: "admin",
RootId: 2,
},
{
OemName: types.OEM_NAME_LENOVO,
LanChannels: []uint8{1, 8},
RootName: "root",
RootId: 2,
},
{
OemName: types.OEM_NAME_HP,
LanChannels: []uint8{1, 2},
RootName: "root",
RootId: 1,
},
{
OemName: types.OEM_NAME_HUAWEI,
LanChannels: []uint8{1},
RootName: "root",
RootId: 2,
StrongPass: true,
},
{
OemName: types.OEM_NAME_FOXCONN,
LanChannels: []uint8{1},
RootName: "root",
RootId: 2,
StrongPass: true,
},
{
OemName: types.OEM_NAME_QEMU,
LanChannels: []uint8{8, 1},
RootName: "root",
RootId: 2,
StrongPass: true,
},
{
OemName: types.OEM_NAME_H3C,
LanChannels: []uint8{1},
RootName: "root",
RootId: 2,
StrongPass: true,
},
}
36 changes: 36 additions & 0 deletions pkg/apis/compute/baremetal/zz_generated.model.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/apis/compute/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ type HostIpmiAttributes struct {
// presence
IpmiPresent *bool `json:"ipmi_present"`
// lan channel
IpmiLanChannel *int `json:"ipmi_lan_channel"`
IpmiLanChannel *uint8 `json:"ipmi_lan_channel"`
// verified
IpmiVerified *bool `json:"ipmi_verified"`
// Redfish API support
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/compute/zz_generated.model.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/baremetal/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ func (agent *SBaremetalAgent) StartService() error {
if err != nil {
return fmt.Errorf("Baremetal manager load config error: %v", err)
}
if err := manager.initBaremetals(files); err != nil {

ctx := context.Background()
if err := manager.initBaremetals(ctx, files); err != nil {
log.Warningf("init baremetals by files err: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/baremetal/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (job *SStatusProbeJob) Do(ctx context.Context, now time.Time) error {
pps := PowerStatusToBaremetalStatus(ps)
if pps != bStatus {
log.Debugf("Detected baremetal status change!")
job.baremetal.SyncAllStatus(ps)
job.baremetal.SyncAllStatus(ctx, ps)
return nil
}
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/baremetal/handler/delay.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package handler

import (
"context"
"fmt"

"yunion.io/x/jsonutils"
Expand All @@ -30,17 +31,18 @@ func init() {
delayTaskWorkerMan = appsrv.NewWorkerManager("DelayTaskWorkerManager", 8, 1024, false)
}

type ProcessFunc func(data jsonutils.JSONObject) (jsonutils.JSONObject, error)
type ProcessFunc func(ctx context.Context, data jsonutils.JSONObject) (jsonutils.JSONObject, error)

type delayTask struct {
ctx context.Context
process ProcessFunc
taskId string
session *mcclient.ClientSession
data jsonutils.JSONObject
}

func (t *delayTask) Run() {
ret, err := t.process(t.data)
ret, err := t.process(t.ctx, t.data)
if err != nil {
modules.ComputeTasks.TaskFailed(t.session, t.taskId, err)
return
Expand Down
8 changes: 4 additions & 4 deletions pkg/baremetal/handler/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func handleBaremetalNotify(ctx *Context, bm *baremetal.SBaremetalInstance) {
task := bm.GetTask()
if task == nil {
task = tasks.NewBaremetalServerPrepareTask(bm)
bm.SyncStatus(baremetalstatus.PREPARE, "")
bm.SyncStatus(ctx, baremetalstatus.PREPARE, "")
}
log.Infof("Get notify from pxe rom os, start exec task: %s", task.GetName())
task.SSHExecute(remoteAddr, key, nil)
Expand Down Expand Up @@ -157,7 +157,7 @@ func handleBaremetalResetBMC(ctx *Context, bm *baremetal.SBaremetalInstance) {
}

func handleBaremetalIpmiProbe(ctx *Context, bm *baremetal.SBaremetalInstance) {
bm.StartBaremetalIpmiProbeTask(ctx.UserCred(), ctx.TaskId(), ctx.Data())
bm.StartBaremetalIpmiProbeTask(ctx, ctx.UserCred(), ctx.TaskId(), ctx.Data())
ctx.ResponseOk()
}

Expand All @@ -178,7 +178,7 @@ func handleBaremetalJnlpTask(ctx *Context, bm *baremetal.SBaremetalInstance) {
}

func handleServerCreate(ctx *Context, bm *baremetal.SBaremetalInstance) {
err := bm.StartServerCreateTask(ctx.UserCred(), ctx.TaskId(), ctx.Data())
err := bm.StartServerCreateTask(ctx, ctx.UserCred(), ctx.TaskId(), ctx.Data())
if err != nil {
ctx.ResponseError(httperrors.NewGeneralError(err))
return
Expand Down Expand Up @@ -238,7 +238,7 @@ func handleServerStatus(ctx *Context, bm *baremetal.SBaremetalInstance, _ bareme
}

func handleBaremetalRegister(ctx *Context, input *baremetal.BmRegisterInput) {
ctx.DelayProcess(func(data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
ctx.DelayProcess(func(_ context.Context, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
baremetal.GetBaremetalManager().RegisterBaremetal(ctx, ctx.userCred, input)
return nil, nil
}, nil)
Expand Down
2 changes: 1 addition & 1 deletion pkg/baremetal/handler/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func bmObjMiddlewareWithFetch(h bmObjHandlerFunc, fetch bool) appsrv.FilterHandl
baremetal := newCtx.GetBaremetalManager().GetBaremetalById(bmId)
if baremetal == nil {
if fetch {
err := newCtx.GetBaremetalManager().InitBaremetal(bmId, false)
err := newCtx.GetBaremetalManager().InitBaremetal(newCtx, bmId, false)
if err != nil {
newCtx.ResponseError(err)
return
Expand Down
Loading

0 comments on commit 44183fb

Please sign in to comment.