add doc and copyright

This commit is contained in:
Qiu Jian
2019-10-16 14:08:57 +08:00
parent 25d0d478e7
commit 7e02839d67
88 changed files with 2773 additions and 498 deletions
+86 -11
View File
@@ -21,9 +21,11 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/version"
"yunion.io/x/onecloud/pkg/cloudcommon/object"
"yunion.io/x/onecloud/pkg/hostman/storageman"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/modules"
)
@@ -55,6 +57,9 @@ type SBaseAgent struct {
AgentName string
Zone *SZoneInfo
CachePath string
CacheManager *storageman.SLocalImageCacheManager
stop bool
}
@@ -78,7 +83,7 @@ func (agent *SBaseAgent) IAgent() IAgent {
return agent.GetVirtualObject().(IAgent)
}
func (agent *SBaseAgent) Init(iagent IAgent, ifname string) error {
func (agent *SBaseAgent) Init(iagent IAgent, ifname string, cachePath string) error {
iface, err := net.InterfaceByName(ifname)
if err != nil {
return err
@@ -105,6 +110,7 @@ func (agent *SBaseAgent) Init(iagent IAgent, ifname string) error {
agent.SetVirtualObject(iagent)
agent.ListenInterface = iface
agent.ListenIPs = ips
agent.CachePath = cachePath
return nil
}
@@ -259,7 +265,7 @@ func (agent *SBaseAgent) createOrUpdateBaremetalAgent(session *mcclient.ClientSe
} else {
cloudBmAgent := ret.Data[0]
agentId, _ := cloudBmAgent.GetString("id")
cloudObj, err = agent.updateBaremetalAgent(session, agentId)
cloudObj, err = agent.updateBaremetalAgent(session, agentId, "")
if err != nil {
return err
}
@@ -276,6 +282,25 @@ func (agent *SBaseAgent) createOrUpdateBaremetalAgent(session *mcclient.ClientSe
agent.AgentId = agentId
agent.AgentName = agentName
storageCacheId, _ := cloudObj.GetString("storagecache_id")
if len(storageCacheId) == 0 {
storageCacheId, err = agent.createStorageCache(session)
if err != nil {
return err
}
_, err = agent.updateBaremetalAgent(session, agentId, storageCacheId)
if err != nil {
return err
}
} else {
err = agent.updateStorageCache(session, storageCacheId)
if err != nil {
return err
}
}
agent.CacheManager = storageman.NewLocalImageCacheManager(agent.IAgent(), agent.CachePath, storageCacheId)
return nil
}
@@ -298,13 +323,17 @@ func (agent *SBaseAgent) GetListenUri() string {
}
func (agent *SBaseAgent) getCreateUpdateInfo() (jsonutils.JSONObject, error) {
accessIP, err := agent.IAgent().GetAccessIP()
if err != nil {
return nil, err
}
params := jsonutils.NewDict()
if agent.AgentId == "" {
params.Add(jsonutils.NewString(fmt.Sprintf("%s_%s", agent.IAgent().GetAgentType(), accessIP)), "name")
agentName, err := agent.getName()
if err != nil {
return nil, errors.Wrap(err, "agent.getName")
}
params.Add(jsonutils.NewString(agentName), "name")
}
accessIP, err := agent.IAgent().GetAccessIP()
if err != nil {
return nil, errors.Wrap(err, "agent.IAgent().GetAccessIP()")
}
params.Add(jsonutils.NewString(accessIP.String()), "access_ip")
params.Add(jsonutils.NewString(agent.GetManagerUri()), "manager_uri")
@@ -315,6 +344,45 @@ func (agent *SBaseAgent) getCreateUpdateInfo() (jsonutils.JSONObject, error) {
return params, nil
}
func (agent *SBaseAgent) getName() (string, error) {
accessIP, err := agent.IAgent().GetAccessIP()
if err != nil {
return "", err
}
return fmt.Sprintf("%s-%s", agent.IAgent().GetAgentType(), accessIP), nil
}
func (agent *SBaseAgent) createStorageCache(session *mcclient.ClientSession) (string, error) {
body := jsonutils.NewDict()
agentName, err := agent.getName()
if err != nil {
return "", errors.Wrap(err, "agent.getName")
}
body.Set("name", jsonutils.NewString("imagecache-"+agentName))
body.Set("path", jsonutils.NewString(agent.CachePath))
body.Set("external_id", jsonutils.NewString(agent.AgentId))
sc, err := modules.Storagecaches.Create(session, body)
if err != nil {
return "", errors.Wrap(err, "modules.Storagecaches.Create")
}
storageCacheId, err := sc.GetString("id")
if err != nil {
return "", errors.Wrap(err, "sc.GetString id")
}
return storageCacheId, nil
}
func (agent *SBaseAgent) updateStorageCache(session *mcclient.ClientSession, storageCacheId string) error {
body := jsonutils.NewDict()
body.Set("path", jsonutils.NewString(agent.CachePath))
body.Set("external_id", jsonutils.NewString(agent.AgentId))
_, err := modules.Storagecaches.Update(session, storageCacheId, body)
if err != nil {
return errors.Wrap(err, "modules.Storagecaches.Update")
}
return nil
}
func (agent *SBaseAgent) createBaremetalAgent(session *mcclient.ClientSession) (jsonutils.JSONObject, error) {
params, err := agent.getCreateUpdateInfo()
if err != nil {
@@ -323,10 +391,17 @@ func (agent *SBaseAgent) createBaremetalAgent(session *mcclient.ClientSession) (
return modules.Baremetalagents.Create(session, params)
}
func (agent *SBaseAgent) updateBaremetalAgent(session *mcclient.ClientSession, id string) (jsonutils.JSONObject, error) {
params, err := agent.getCreateUpdateInfo()
if err != nil {
return nil, err
func (agent *SBaseAgent) updateBaremetalAgent(session *mcclient.ClientSession, id string, storageCacheId string) (jsonutils.JSONObject, error) {
var params jsonutils.JSONObject
var err error
if len(storageCacheId) > 0 {
params = jsonutils.NewDict()
params.(*jsonutils.JSONDict).Set("storagecache_id", jsonutils.NewString(storageCacheId))
} else {
params, err = agent.getCreateUpdateInfo()
if err != nil {
return nil, err
}
}
return modules.Baremetalagents.Update(session, id, params)
}
+59
View File
@@ -0,0 +1,59 @@
// 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 agent
import (
"context"
"fmt"
"net/http"
"yunion.io/x/onecloud/pkg/appsrv"
"yunion.io/x/onecloud/pkg/cloudcommon/workmanager"
"yunion.io/x/onecloud/pkg/hostman/hostutils"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient/auth"
)
func (agent *SBaseAgent) AddImageCacheHandler(prefix string, app *appsrv.Application) {
hostutils.InitWorkerManager()
app.AddHandler("POST",
fmt.Sprintf("%s/disks/image_cache", prefix),
auth.Authenticate(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
performImageCache(ctx, w, r, agent.CacheManager.PrefetchImageCache)
}))
app.AddHandler("DELETE",
fmt.Sprintf("%s/disks/image_cache", prefix),
auth.Authenticate(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
performImageCache(ctx, w, r, agent.CacheManager.DeleteImageCache)
}))
}
func performImageCache(
ctx context.Context,
w http.ResponseWriter,
r *http.Request,
performTask workmanager.DelayTaskFunc,
) {
_, _, body := appsrv.FetchEnv(ctx, w, r)
disk, err := body.Get("disk")
if err != nil {
httperrors.MissingParameterError(w, "disk")
return
}
hostutils.DelayTask(ctx, performTask, disk)
hostutils.ResponseOk(ctx, w)
}
+1 -1
View File
@@ -23,7 +23,7 @@ import (
)
type SExternalizedResourceBase struct {
ExternalId string `width:"256" charset:"utf8" index:"true" list:"user" create:"admin_optional"`
ExternalId string `width:"256" charset:"utf8" index:"true" list:"user" create:"admin_optional" update:"admin"`
}
func (model SExternalizedResourceBase) GetExternalId() string {
+2
View File
@@ -70,6 +70,8 @@ type BaseOptions struct {
NonDefaultDomainProjects bool `help:"allow projects in non-default domains" default:"false"`
TimeZone string `help:"time zone" default:"Asia/Shanghai"`
structarg.BaseOptions
}
+21 -5
View File
@@ -22,11 +22,15 @@ const (
)
type SIPMIInfo struct {
Username string `json:"username"`
Password string `json:"password"`
IpAddr string `json:"ip_addr"`
Present bool `json:"present"`
LanChannel int `json:"lan_channel"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
IpAddr string `json:"ip_addr,omitempty"`
Present bool `json:"present,omitempty"`
LanChannel int `json:"lan_channel,omitzero"`
Verified bool `json:"verified,omitfalse"`
RedfishApi bool `json:"redfish_api,omitfalse"`
CdromBoot bool `json:"cdrom_boot,omitfalse"`
PxeBoot bool `json:"pxe_boot,omitfalse"`
}
func (info SIPMIInfo) ToPrepareParams() jsonutils.JSONObject {
@@ -42,5 +46,17 @@ func (info SIPMIInfo) ToPrepareParams() jsonutils.JSONObject {
}
data.Add(jsonutils.NewBool(info.Present), "ipmi_present")
data.Add(jsonutils.NewInt(int64(info.LanChannel)), "ipmi_lan_channel")
if info.Verified {
data.Add(jsonutils.JSONTrue, "ipmi_verified")
}
if info.RedfishApi {
data.Add(jsonutils.JSONTrue, "ipmi_redfish_api")
}
if info.CdromBoot {
data.Add(jsonutils.JSONTrue, "ipmi_cdrom_boot")
}
if info.PxeBoot {
data.Add(jsonutils.JSONTrue, "ipmi_pxe_boot")
}
return data
}
+3
View File
@@ -93,6 +93,9 @@ type SIPMILanConfig struct {
Netmask string `json:"netmask"`
Mac net.HardwareAddr `json:"mac"`
Gateway string `json:"gateway"`
VlanId int `json:"vlan_id"`
SpeedMbps int `json:"speed_mbps"`
}
type SIPMIBootFlags struct {