mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix Conflict
This commit is contained in:
Generated
+316
-33
File diff suppressed because it is too large
Load Diff
@@ -3,7 +3,7 @@
|
||||
REPO_PREFIX := yunion.io/x/onecloud
|
||||
VENDOR_PATH := $(REPO_PREFIX)/vendor
|
||||
VERSION_PKG := $(VENDOR_PATH)/yunion.io/x/pkg/util/version
|
||||
ROOT_DIR := $(shell readlink -f `pwd`)
|
||||
ROOT_DIR := $(CURDIR)
|
||||
BUILD_DIR := $(ROOT_DIR)/_output
|
||||
BIN_DIR := $(BUILD_DIR)/bin
|
||||
BUILD_SCRIPT := $(ROOT_DIR)/build/build.sh
|
||||
@@ -83,10 +83,6 @@ bin_dir: output_dir
|
||||
output_dir:
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
|
||||
|
||||
dep:
|
||||
cd $(ROOT_DIR) && dep ensure -v
|
||||
|
||||
dep_clean:
|
||||
rm -fr $(GOPATH)/pkg/dep/sources/*
|
||||
|
||||
@@ -105,5 +101,8 @@ fmt:
|
||||
find . -type f -name "*.go" -not -path "./_output/*" \
|
||||
-not -path "./vendor/*" | xargs gofmt -s -w
|
||||
|
||||
dep:
|
||||
cd $(ROOT_DIR) && dep ensure -v -update $(shell for p in $$(ls vendor/yunion.io/x/); do echo "yunion.io/x/$$p"; done | xargs)
|
||||
|
||||
%:
|
||||
@:
|
||||
|
||||
+17
-12
@@ -5,28 +5,32 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
prompt "github.com/c-bata/go-prompt"
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/util/version"
|
||||
"yunion.io/x/structarg"
|
||||
|
||||
"yunion.io/x/onecloud/cmd/climc/promputils"
|
||||
"yunion.io/x/onecloud/cmd/climc/shell"
|
||||
_ "yunion.io/x/onecloud/cmd/climc/shell/k8s"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
|
||||
_ "yunion.io/x/onecloud/cmd/climc/shell/k8s"
|
||||
)
|
||||
|
||||
type BaseOptions struct {
|
||||
Help bool `help:"Show help" short-token:"h"`
|
||||
Debug bool `help:"Show debug information"`
|
||||
Version bool `help:"Show version"`
|
||||
Timeout int `default:"600" help:"Number of seconds to wait for a response"`
|
||||
Insecure bool `default:"False" help:"Allow skip server cert verification if URL is https" short-token:"k"`
|
||||
OsUsername string `default:"$OS_USERNAME" help:"Username, defaults to env[OS_USERNAME]"`
|
||||
OsPassword string `default:"$OS_PASSWORD" help:"Password, defaults to env[OS_PASSWORD]"`
|
||||
Help bool `help:"Show help" short-token:"h"`
|
||||
Debug bool `help:"Show debug information"`
|
||||
Version bool `help:"Show version"`
|
||||
Timeout int `default:"600" help:"Number of seconds to wait for a response"`
|
||||
Insecure bool `default:"false" help:"Allow skip server cert verification if URL is https" short-token:"k"`
|
||||
NoCachedToken bool `default:"false" help:"Force not use cached token"`
|
||||
OsUsername string `default:"$OS_USERNAME" help:"Username, defaults to env[OS_USERNAME]"`
|
||||
OsPassword string `default:"$OS_PASSWORD" help:"Password, defaults to env[OS_PASSWORD]"`
|
||||
// OsProjectId string `default:"$OS_PROJECT_ID" help:"Proejct ID, defaults to env[OS_PROJECT_ID]"`
|
||||
OsProjectName string `default:"$OS_PROJECT_NAME" help:"Project name, defaults to env[OS_PROJECT_NAME]"`
|
||||
OsDomainName string `default:"$OS_DOMAIN_NAME" help:"Domain name, defaults to env[OS_DOMAIN_NAME]"`
|
||||
@@ -122,8 +126,9 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) {
|
||||
options.Insecure)
|
||||
|
||||
var cacheToken mcclient.TokenCredential
|
||||
cacheFile, err := os.Open("/tmp/OS_AUTH_CACHE_TOKEN")
|
||||
if err == nil && cacheFile != nil {
|
||||
tokenCachePath := filepath.Join(os.TempDir(), "OS_AUTH_CACHE_TOKEN")
|
||||
cacheFile, err := os.Open(tokenCachePath)
|
||||
if err == nil && cacheFile != nil && !options.NoCachedToken {
|
||||
fileInfo, _ := cacheFile.Stat()
|
||||
dur, err := time.ParseDuration("-24h")
|
||||
if fileInfo != nil && err == nil && fileInfo.ModTime().After(time.Now().Add(dur)) {
|
||||
@@ -153,12 +158,12 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) {
|
||||
if err != nil {
|
||||
fmt.Printf("Marshal token error:%s", err)
|
||||
} else {
|
||||
fo, _ := os.Create("/tmp/OS_AUTH_CACHE_TOKEN")
|
||||
fo, _ := os.Create(tokenCachePath)
|
||||
fo.Write(bytesCacheToken)
|
||||
fo.Close()
|
||||
}
|
||||
} else {
|
||||
fmt.Println("******** Use Token Cache At /tmp/OS_AUTH_CACHE_TOKEN ********")
|
||||
fmt.Printf("******** Use Token Cache At %s ********\n", tokenCachePath)
|
||||
}
|
||||
|
||||
session := client.NewSession(options.OsRegionName,
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/pkg/utils"
|
||||
"yunion.io/x/structarg"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
@@ -51,8 +52,8 @@ func Executor(s string) {
|
||||
return
|
||||
}
|
||||
s = escaper(s)
|
||||
args := strings.Split(s, " ")
|
||||
e := parser.ParseArgs(strings.Split(s, " "), false)
|
||||
args := utils.ArgsStringToArray(s)
|
||||
e := parser.ParseArgs(utils.ArgsStringToArray(s), false)
|
||||
subcmd := parser.GetSubcommand()
|
||||
subparser := subcmd.GetSubParser()
|
||||
if args[0] == "--debug" {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
json "yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
func initChart() {
|
||||
cmdN := func(suffix string) string {
|
||||
return resourceCmdN("chart", suffix)
|
||||
}
|
||||
|
||||
type listOpt struct {
|
||||
baseListOptions
|
||||
Name string `help:"Chart name"`
|
||||
Repo string `help:"Repository name"`
|
||||
RepoUrl string `help:"Repository url"`
|
||||
AllVersion bool `json:"Get Chart all history versions"`
|
||||
Keyword string `json:"Chart keyword"`
|
||||
}
|
||||
R(&listOpt{}, cmdN("list"), "List k8s helm global charts", func(s *mcclient.ClientSession, args *listOpt) error {
|
||||
params := fetchPagingParams(args.baseListOptions)
|
||||
if len(args.Name) != 0 {
|
||||
params.Add(json.NewString(args.Name), "name")
|
||||
}
|
||||
if len(args.Repo) != 0 {
|
||||
params.Add(json.NewString(args.Repo), "repo")
|
||||
}
|
||||
if len(args.RepoUrl) != 0 {
|
||||
params.Add(json.NewString(args.RepoUrl), "repo_url")
|
||||
}
|
||||
if args.AllVersion {
|
||||
params.Add(json.JSONTrue, "all_version")
|
||||
}
|
||||
if len(args.Keyword) != 0 {
|
||||
params.Add(json.NewString(args.Keyword), "keyword")
|
||||
}
|
||||
charts, err := k8s.Charts.List(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintHelmListResult(charts, ChartPrinter{})
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
type ChartPrinter struct{}
|
||||
|
||||
func (p ChartPrinter) Columns() []interface{} {
|
||||
return []interface{}{"REPO/NAME", "VERSION", "DESCRIPTION"}
|
||||
}
|
||||
|
||||
func (p ChartPrinter) RowKeys(obj *json.JSONDict) []interface{} {
|
||||
repo, _ := obj.GetString("repo")
|
||||
name, _ := obj.GetString("chart", "metadata", "name")
|
||||
version, _ := obj.GetString("chart", "metadata", "version")
|
||||
desc, _ := obj.GetString("chart", "metadata", "description")
|
||||
return []interface{}{fmt.Sprintf("%s/%s", repo, name), version, desc}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
func initConfigMap() {
|
||||
cmdN := func(suffix string) string {
|
||||
return resourceCmdN("configmap", suffix)
|
||||
}
|
||||
|
||||
type listOpt struct {
|
||||
namespaceListOptions
|
||||
baseListOptions
|
||||
}
|
||||
R(&listOpt{}, cmdN("list"), "List k8s configmap", func(s *mcclient.ClientSession, args *listOpt) error {
|
||||
params := fetchNamespaceParams(args.namespaceListOptions)
|
||||
params.Update(fetchPagingParams(args.baseListOptions))
|
||||
ret, err := k8s.ConfigMaps.ListInContexts(s, params, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(ret, k8s.ConfigMaps.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/util/sets"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
func initDeployment() {
|
||||
cmdN := func(suffix string) string {
|
||||
return resourceCmdN("deployment", suffix)
|
||||
}
|
||||
|
||||
type listOpt struct {
|
||||
namespaceListOptions
|
||||
baseListOptions
|
||||
}
|
||||
R(&listOpt{}, cmdN("list"), "List k8s deployment", func(s *mcclient.ClientSession, args *listOpt) error {
|
||||
params := fetchNamespaceParams(args.namespaceListOptions)
|
||||
params.Update(fetchPagingParams(args.baseListOptions))
|
||||
ret, err := k8s.Deployments.ListInContexts(s, params, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(ret, k8s.Deployments.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
type createOpt struct {
|
||||
namespaceOptions
|
||||
NAME string `help:"Name of deployment"`
|
||||
Image string `help:"The image for the container to run"`
|
||||
Replicas int64 `help:"Number of replicas for pods in this deployment"`
|
||||
RunAsPrivileged bool `help:"Whether to run the container as privileged user"`
|
||||
Labels string `help:"Comma separated labels to apply to the pod(s)"`
|
||||
Env []string `help:"Environment variables to set in container"`
|
||||
Port []string `help:"Port for the service that is created, format is <protocol>:<service_port>:<container_port> e.g. tcp:80:3000"`
|
||||
}
|
||||
R(&createOpt{}, cmdN("create"), "Create deployment resource", func(s *mcclient.ClientSession, args *createOpt) error {
|
||||
params := jsonutils.NewDict()
|
||||
if len(args.Image) == 0 {
|
||||
return fmt.Errorf("Image must provided")
|
||||
}
|
||||
params.Add(jsonutils.NewString(args.NAME), "name")
|
||||
params.Add(jsonutils.NewString(args.Image), "containerImage")
|
||||
if args.Namespace != "" {
|
||||
params.Add(jsonutils.NewString(args.Namespace), "namespace")
|
||||
}
|
||||
if args.Replicas > 1 {
|
||||
params.Add(jsonutils.NewInt(args.Replicas), "replicas")
|
||||
}
|
||||
if args.RunAsPrivileged {
|
||||
params.Add(jsonutils.JSONTrue, "runAsPrivileged")
|
||||
}
|
||||
if len(args.Port) != 0 {
|
||||
portMappings, err := parsePortMappings(args.Port)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params.Add(portMappings, "portMappings")
|
||||
}
|
||||
ret, err := k8s.Deployments.CreateInContexts(s, params, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(ret)
|
||||
return nil
|
||||
})
|
||||
|
||||
type getOpt struct {
|
||||
resourceGetOptions
|
||||
}
|
||||
R(&getOpt{}, cmdN("show"), "Get deployment details", func(s *mcclient.ClientSession, args *getOpt) error {
|
||||
id := args.NAME
|
||||
params := jsonutils.NewDict()
|
||||
if args.Namespace != "" {
|
||||
params.Add(jsonutils.NewString(args.Namespace), "namespace")
|
||||
}
|
||||
ret, err := k8s.Deployments.GetInContexts(s, id, params, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObjectYAML(ret)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
type portMapping struct {
|
||||
Port int32 `json:"port"`
|
||||
TargetPort int32 `json:"targetPort"`
|
||||
Protocol string `json:"protocol"`
|
||||
}
|
||||
|
||||
func parsePortMapping(port string) (*portMapping, error) {
|
||||
if len(port) == 0 {
|
||||
return nil, fmt.Errorf("empty port mapping desc string")
|
||||
}
|
||||
parts := strings.Split(port, ":")
|
||||
mapping := &portMapping{}
|
||||
for _, part := range parts {
|
||||
if sets.NewString("tcp", "udp").Has(strings.ToLower(part)) {
|
||||
mapping.Protocol = strings.ToUpper(part)
|
||||
}
|
||||
if port, err := strconv.Atoi(part); err != nil {
|
||||
continue
|
||||
} else {
|
||||
if mapping.Port == 0 {
|
||||
mapping.Port = int32(port)
|
||||
} else {
|
||||
mapping.TargetPort = int32(port)
|
||||
}
|
||||
}
|
||||
}
|
||||
if mapping.Protocol == "" {
|
||||
mapping.Protocol = "TCP"
|
||||
}
|
||||
if mapping.Port <= 0 {
|
||||
return nil, fmt.Errorf("Service port not provided")
|
||||
}
|
||||
if mapping.TargetPort < 0 {
|
||||
return nil, fmt.Errorf("Container invalid targetPort %d", mapping.TargetPort)
|
||||
}
|
||||
if mapping.TargetPort == 0 {
|
||||
mapping.TargetPort = mapping.Port
|
||||
}
|
||||
return mapping, nil
|
||||
}
|
||||
|
||||
func parsePortMappings(ports []string) (*jsonutils.JSONArray, error) {
|
||||
ret := jsonutils.NewArray()
|
||||
for _, port := range ports {
|
||||
mapping, err := parsePortMapping(port)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Port %q error: %v", port, err)
|
||||
}
|
||||
ret.Add(jsonutils.Marshal(mapping))
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gosuri/uitable"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
type ListPrinter interface {
|
||||
Columns() []interface{}
|
||||
RowKeys(obj *jsonutils.JSONDict) []interface{}
|
||||
}
|
||||
|
||||
func ListerTable(res *modules.ListResult, printer ListPrinter) *uitable.Table {
|
||||
min := func(x, y int) int {
|
||||
if x < y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
table := uitable.New()
|
||||
table.MaxColWidth = 80
|
||||
table.AddRow(printer.Columns()...)
|
||||
var idx int
|
||||
for ; idx < min(res.Limit, res.Total); idx++ {
|
||||
table.AddRow(printer.RowKeys(res.Data[idx].(*jsonutils.JSONDict))...)
|
||||
}
|
||||
return table
|
||||
}
|
||||
|
||||
func PrintHelmListResult(res *modules.ListResult, printer ListPrinter) {
|
||||
fmt.Println(ListerTable(res, printer))
|
||||
|
||||
table := uitable.New()
|
||||
total := res.Total
|
||||
offset := res.Offset
|
||||
limit := res.Limit
|
||||
page := (offset / limit) + 1
|
||||
pages := total / limit
|
||||
if pages*limit < total {
|
||||
pages += 1
|
||||
}
|
||||
table.AddRow("")
|
||||
table.AddRow("Total", "Pages", "Limit", "Offset", "Page")
|
||||
table.AddRow(total, pages, limit, offset, page)
|
||||
fmt.Println(table)
|
||||
}
|
||||
@@ -12,24 +12,84 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
// cluster resources
|
||||
initCluster()
|
||||
initNode()
|
||||
|
||||
// helm resources
|
||||
initTiller()
|
||||
initRepo()
|
||||
initChart()
|
||||
initRelease()
|
||||
|
||||
// kubernetes original resources
|
||||
initRaw()
|
||||
initConfigMap()
|
||||
initDeployment()
|
||||
initPod()
|
||||
initService()
|
||||
}
|
||||
|
||||
type BaseListOptions shell.BaseListOptions
|
||||
|
||||
type clusterBaseOptions struct {
|
||||
Cluster string `default:"$K8S_CLUSTER" help:"Kubernetes cluster name"`
|
||||
Cluster string `default:"$K8S_CLUSTER|default" help:"Kubernetes cluster name"`
|
||||
}
|
||||
|
||||
func (o clusterBaseOptions) ClusterContext() []modules.ManagerContext {
|
||||
return []modules.ManagerContext{clusterContext(o.Cluster)}
|
||||
}
|
||||
|
||||
type k8sBaseListOptions struct {
|
||||
type baseListOptions struct {
|
||||
Limit int `default:"20" help:"Page limit"`
|
||||
Offset int `default:"0" help:"page offset"`
|
||||
}
|
||||
|
||||
func fetchPagingParams(opt baseListOptions) *jsonutils.JSONDict {
|
||||
params := jsonutils.NewDict()
|
||||
if opt.Limit > 0 {
|
||||
params.Add(jsonutils.NewInt(int64(opt.Limit)), "limit")
|
||||
}
|
||||
if opt.Offset > 0 {
|
||||
params.Add(jsonutils.NewInt(int64(opt.Offset)), "offset")
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
type namespaceListOptions struct {
|
||||
namespaceOptions
|
||||
AllNamespace bool `help:"Show resource in all namespace"`
|
||||
}
|
||||
|
||||
type namespaceOptions struct {
|
||||
clusterBaseOptions
|
||||
Limit int `default:"20" help:"Page limit"`
|
||||
Namespace string `help:"Namespace of this resource"`
|
||||
}
|
||||
|
||||
type resourceGetOptions struct {
|
||||
clusterBaseOptions
|
||||
Namespace string `help:"Namespace of this resource"`
|
||||
NAME string `help:"Name ident of the resource"`
|
||||
}
|
||||
|
||||
func (o resourceGetOptions) ToJSON() *jsonutils.JSONDict {
|
||||
params := jsonutils.NewDict()
|
||||
if o.Namespace != "" {
|
||||
params.Add(jsonutils.NewString(o.Namespace), "namespace")
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
func fetchNamespaceParams(opt namespaceListOptions) *jsonutils.JSONDict {
|
||||
params := jsonutils.NewDict()
|
||||
if opt.AllNamespace {
|
||||
params.Add(jsonutils.JSONTrue, "all_namespace")
|
||||
return params
|
||||
}
|
||||
if opt.Namespace != "" {
|
||||
params.Add(jsonutils.NewString(opt.Namespace), "namespace")
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -53,3 +113,7 @@ func clusterContext(clusterId string) modules.ManagerContext {
|
||||
InstanceId: clusterId,
|
||||
}
|
||||
}
|
||||
|
||||
func printObjectYAML(obj jsonutils.JSONObject) {
|
||||
fmt.Println(obj.YAMLString())
|
||||
}
|
||||
|
||||
@@ -13,10 +13,26 @@ func initPod() {
|
||||
}
|
||||
|
||||
type listOpt struct {
|
||||
k8sBaseListOptions
|
||||
namespaceListOptions
|
||||
baseListOptions
|
||||
}
|
||||
R(&listOpt{}, cmdN("list"), "List k8s pod", func(s *mcclient.ClientSession, args *listOpt) error {
|
||||
ret, err := k8s.Pods.ListInContexts(s, nil, args.ClusterContext())
|
||||
params := fetchNamespaceParams(args.namespaceListOptions)
|
||||
params.Update(fetchPagingParams(args.baseListOptions))
|
||||
ret, err := k8s.Pods.ListInContexts(s, params, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(ret, k8s.Pods.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
type deleteOpt struct {
|
||||
resourceGetOptions
|
||||
}
|
||||
R(&deleteOpt{}, cmdN("delete"), "Delete pod", func(s *mcclient.ClientSession, args *deleteOpt) error {
|
||||
id := args.NAME
|
||||
ret, err := k8s.Pods.DeleteInContexts(s, id, args.ToJSON(), args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
type rawGetOpt struct {
|
||||
namespaceOptions
|
||||
KIND string `help:"resource kind"`
|
||||
NAME string `help:"instance name"`
|
||||
}
|
||||
|
||||
type rawDeleteOpt struct {
|
||||
rawGetOpt
|
||||
}
|
||||
|
||||
func initRaw() {
|
||||
R(&rawGetOpt{}, "k8s-get", "Get k8s resource instance raw info", func(s *mcclient.ClientSession, args *rawGetOpt) error {
|
||||
obj, err := k8s.RawResource.Get(s, args.KIND, args.Namespace, args.NAME, nil, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObjectYAML(obj)
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&rawDeleteOpt{}, "k8s-delete", "Delete k8s resource instance", func(s *mcclient.ClientSession, args *rawDeleteOpt) error {
|
||||
err := k8s.RawResource.Delete(s, args.KIND, args.Namespace, args.NAME, nil, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
json "yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
func initRelease() {
|
||||
cmdN := func(suffix string) string {
|
||||
return resourceCmdN("release", suffix)
|
||||
}
|
||||
type listOpt struct {
|
||||
namespaceListOptions
|
||||
baseListOptions
|
||||
Filter string `help:"Filter, split by space"`
|
||||
Admin bool `help:"Admin to show all namespace releases"`
|
||||
Deployed bool `help:"Show deployed status releases"`
|
||||
Deleted bool `help:"Show deleted status releases"`
|
||||
Deleting bool `help:"Show deleting status releases"`
|
||||
Failed bool `help:"Show failed status releases"`
|
||||
Superseded bool `help:"Show superseded status releases"`
|
||||
Pending bool `help:"Show pending status releases"`
|
||||
}
|
||||
R(&listOpt{}, cmdN("list"), "List k8s cluster helm releases", func(s *mcclient.ClientSession, args *listOpt) error {
|
||||
params := fetchNamespaceParams(args.namespaceListOptions)
|
||||
params.Update(fetchPagingParams(args.baseListOptions))
|
||||
if args.Filter != "" {
|
||||
params.Add(json.NewString(args.Filter), "filter")
|
||||
}
|
||||
if args.Namespace != "" {
|
||||
params.Add(json.NewString(args.Namespace), "namespace")
|
||||
}
|
||||
params.Add(json.JSONTrue, "all")
|
||||
if args.Admin {
|
||||
params.Add(json.JSONTrue, "admin")
|
||||
}
|
||||
if args.Deployed {
|
||||
params.Add(json.JSONTrue, "deployed")
|
||||
}
|
||||
if args.Deleted {
|
||||
params.Add(json.JSONTrue, "deleted")
|
||||
}
|
||||
if args.Deleting {
|
||||
params.Add(json.JSONTrue, "deleting")
|
||||
}
|
||||
if args.Failed {
|
||||
params.Add(json.JSONTrue, "failed")
|
||||
}
|
||||
if args.Superseded {
|
||||
params.Add(json.JSONTrue, "superseded")
|
||||
}
|
||||
if args.Pending {
|
||||
params.Add(json.JSONTrue, "pending")
|
||||
}
|
||||
ret, err := k8s.Releases.ListInContexts(s, params, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(ret, k8s.Releases.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
type showOpt struct {
|
||||
clusterBaseOptions
|
||||
NAME string `help:"Release instance name"`
|
||||
}
|
||||
R(&showOpt{}, cmdN("show"), "Get helm release details", func(s *mcclient.ClientSession, args *showOpt) error {
|
||||
ret, err := k8s.Releases.GetInContexts(s, args.NAME, nil, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resources, err := ret.GetString("info", "status", "resources")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(resources)
|
||||
return nil
|
||||
})
|
||||
|
||||
type releaseCUOpts struct {
|
||||
Values string `help:"Specify values in a YAML file (can specify multiple)" short-token:"f"`
|
||||
Version string `help:"Specify the exact chart version to install. If not specified, latest version installed"`
|
||||
//Set []string `help:"set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)"`
|
||||
DryRun bool `help:"Simulate an install"`
|
||||
Details bool `help:"Show release deploy details, include kubernetes created resources"`
|
||||
Timeout int64 `help:"Time in seconds to wait for any individual kubernetes operation (like Jobs for hooks)" default:"600"`
|
||||
}
|
||||
releaseCUDict := func(args releaseCUOpts) (*json.JSONDict, error) {
|
||||
params := json.NewDict()
|
||||
if args.Version != "" {
|
||||
params.Add(json.NewString(args.Version), "version")
|
||||
}
|
||||
if args.DryRun {
|
||||
params.Add(json.JSONTrue, "dry_run")
|
||||
}
|
||||
params.Add(json.NewInt(args.Timeout), "timeout")
|
||||
if args.Values != "" {
|
||||
//vals, err := helm.MergeValuesF(args.Values, args.Set, []string{})
|
||||
vals, err := ioutil.ReadFile(args.Values)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
params.Add(json.NewString(string(vals)), "values")
|
||||
}
|
||||
return params, nil
|
||||
}
|
||||
|
||||
type releaseCreateOpts struct {
|
||||
namespaceOptions
|
||||
releaseCUOpts
|
||||
Name string `help:"Release name, If unspecified, it will autogenerate one for you"`
|
||||
CHARTNAME string `help:"Helm chart name, e.g stable/etcd"`
|
||||
}
|
||||
R(&releaseCreateOpts{}, cmdN("create"), "Create release with specified helm chart", func(s *mcclient.ClientSession, args *releaseCreateOpts) error {
|
||||
params, err := releaseCUDict(args.releaseCUOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params.Add(json.NewString(args.CHARTNAME), "chart_name")
|
||||
if args.Namespace != "" {
|
||||
params.Add(json.NewString(args.Namespace), "namespace")
|
||||
}
|
||||
if args.Name != "" {
|
||||
params.Add(json.NewString(args.Name), "release_name")
|
||||
}
|
||||
ret, err := k8s.Releases.CreateInContexts(s, params, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(ret)
|
||||
return nil
|
||||
})
|
||||
|
||||
type releaseUpgradeOpts struct {
|
||||
clusterBaseOptions
|
||||
releaseCUOpts
|
||||
NAME string `help:"Release instance name"`
|
||||
CHARTNAME string `help:"Helm chart name, e.g stable/etcd"`
|
||||
ReuseValues bool `help:"When upgrading, reuse the last release's values, and merge in any new values. If '--reset-values' is specified, this is ignored"`
|
||||
ResetValues bool `help:"When upgrading, reset the values to the ones built into the chart"`
|
||||
}
|
||||
R(&releaseUpgradeOpts{}, cmdN("upgrade"), "Upgrade release", func(s *mcclient.ClientSession, args *releaseUpgradeOpts) error {
|
||||
params, err := releaseCUDict(args.releaseCUOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params.Add(json.NewString(args.CHARTNAME), "chart_name")
|
||||
params.Add(json.NewString(args.NAME), "release_name")
|
||||
if args.ReuseValues {
|
||||
params.Add(json.JSONTrue, "reuse_values")
|
||||
}
|
||||
if args.ResetValues {
|
||||
params.Add(json.JSONTrue, "reset_values")
|
||||
}
|
||||
|
||||
res, err := k8s.Releases.PutInContexts(s, args.NAME, params, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(res)
|
||||
return nil
|
||||
})
|
||||
|
||||
type deleteOpt struct {
|
||||
clusterBaseOptions
|
||||
NAME string `help:"Release instance name"`
|
||||
}
|
||||
R(&deleteOpt{}, cmdN("delete"), "Delete release", func(s *mcclient.ClientSession, args *deleteOpt) error {
|
||||
_, err := k8s.Releases.DeleteInContexts(s, args.NAME, nil, args.ClusterContext())
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
func initRepo() {
|
||||
cmdN := func(suffix string) string {
|
||||
return resourceCmdN("repo", suffix)
|
||||
}
|
||||
type listOpt struct {
|
||||
BaseListOptions
|
||||
}
|
||||
R(&listOpt{}, cmdN("list"), "List k8s global helm repos", func(s *mcclient.ClientSession, args *listOpt) error {
|
||||
params := FetchPagingParams(args.BaseListOptions)
|
||||
result, err := k8s.Repos.List(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(result, k8s.Repos.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
type getOpt struct {
|
||||
NAME string `help:"ID or name of the repo"`
|
||||
}
|
||||
R(&getOpt{}, cmdN("show"), "Show details fo a repo", func(s *mcclient.ClientSession, args *getOpt) error {
|
||||
repo, err := k8s.Repos.Get(s, args.NAME, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(repo)
|
||||
return nil
|
||||
})
|
||||
|
||||
type createOpt struct {
|
||||
getOpt
|
||||
URL string `help:"Repository url"`
|
||||
Public bool `help:"Make repository public"`
|
||||
}
|
||||
R(&createOpt{}, cmdN("create"), "Add repository", func(s *mcclient.ClientSession, args *createOpt) error {
|
||||
params := jsonutils.NewDict()
|
||||
params.Add(jsonutils.NewString(args.NAME), "name")
|
||||
params.Add(jsonutils.NewString(args.URL), "url")
|
||||
if args.Public {
|
||||
params.Add(jsonutils.JSONTrue, "is_public")
|
||||
}
|
||||
repo, err := k8s.Repos.Create(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(repo)
|
||||
return nil
|
||||
})
|
||||
|
||||
type updateOpt struct {
|
||||
getOpt
|
||||
Name string `help:"Repository name to change"`
|
||||
Url string `help:"Repository url to change"`
|
||||
}
|
||||
R(&updateOpt{}, cmdN("update"), "Update helm repository", func(s *mcclient.ClientSession, args *updateOpt) error {
|
||||
params := jsonutils.NewDict()
|
||||
if args.Name != "" {
|
||||
params.Add(jsonutils.NewString(args.Name), "name")
|
||||
}
|
||||
if args.Url != "" {
|
||||
params.Add(jsonutils.NewString(args.Url), "url")
|
||||
}
|
||||
repo, err := k8s.Repos.Update(s, args.NAME, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(repo)
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&getOpt{}, cmdN("private"), "Make repository private", func(s *mcclient.ClientSession, args *getOpt) error {
|
||||
repo, err := k8s.Repos.PerformAction(s, args.NAME, "private", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(repo)
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&getOpt{}, cmdN("public"), "Make repository public", func(s *mcclient.ClientSession, args *getOpt) error {
|
||||
repo, err := k8s.Repos.PerformAction(s, args.NAME, "public", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(repo)
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&getOpt{}, cmdN("delete"), "Delete a repository", func(s *mcclient.ClientSession, args *getOpt) error {
|
||||
repo, err := k8s.Repos.Delete(s, args.NAME, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(repo)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
func initService() {
|
||||
cmdN := func(suffix string) string {
|
||||
return resourceCmdN("service", suffix)
|
||||
}
|
||||
|
||||
type listOpt struct {
|
||||
namespaceListOptions
|
||||
baseListOptions
|
||||
}
|
||||
R(&listOpt{}, cmdN("list"), "List k8s service", func(s *mcclient.ClientSession, args *listOpt) error {
|
||||
params := fetchNamespaceParams(args.namespaceListOptions)
|
||||
params.Update(fetchPagingParams(args.baseListOptions))
|
||||
ret, err := k8s.Services.ListInContexts(s, params, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(ret, k8s.Services.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
json "yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
|
||||
)
|
||||
|
||||
func initTiller() {
|
||||
cmdN := func(suffix string) string {
|
||||
return resourceCmdN("tiller", suffix)
|
||||
}
|
||||
type createOpt struct {
|
||||
clusterBaseOptions
|
||||
KubeContext string `json:"kube_context"`
|
||||
Namespace string `json:"namespace" default:"kube-system"`
|
||||
// Upgrade if Tiller is already installed
|
||||
Upgrade bool `json:"upgrade"`
|
||||
// Name of service account
|
||||
ServiceAccount string `json:"service_account" default:"tiller"`
|
||||
// Use the canary Tiller image
|
||||
Canary bool `json:"canary_image"`
|
||||
|
||||
// Override Tiller image
|
||||
Image string `json:"tiller_image" default:"yunion/tiller:v2.9.0"`
|
||||
// Limit the maximum number of revisions saved per release. Use 0 for no limit.
|
||||
MaxHistory int `json:"history_max"`
|
||||
}
|
||||
R(&createOpt{}, cmdN("create"), "Install helm tiller server to Kubernetes cluster", func(s *mcclient.ClientSession, args *createOpt) error {
|
||||
params := json.NewDict()
|
||||
if len(args.KubeContext) > 0 {
|
||||
params.Add(json.NewString(args.KubeContext), "kube_context")
|
||||
}
|
||||
params.Add(json.NewString(args.Namespace), "namespace")
|
||||
params.Add(json.NewString(args.ServiceAccount), "service_account")
|
||||
if args.Canary {
|
||||
params.Add(json.JSONTrue, "canary_image")
|
||||
}
|
||||
if args.Upgrade {
|
||||
params.Add(json.JSONTrue, "upgrade")
|
||||
}
|
||||
if len(args.Image) > 0 {
|
||||
params.Add(json.NewString(args.Image), "tiller_image")
|
||||
}
|
||||
if args.MaxHistory > 0 {
|
||||
params.Add(json.NewInt(int64(args.MaxHistory)), "history_max")
|
||||
}
|
||||
ret, err := k8s.Tiller.CreateInContexts(s, params, args.ClusterContext())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(ret)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -64,6 +64,7 @@ func init() {
|
||||
Secgroup string `help:"Secgroup ID or Name"`
|
||||
AdminSecgroup string `help:"AdminSecgroup ID or Name"`
|
||||
Hypervisor string `help:"Show server of hypervisor" choices:"kvm|esxi|container|baremetal|aliyun"`
|
||||
Manager string `help:"Show servers imported from manager"`
|
||||
BaseListOptions
|
||||
}
|
||||
R(&ServerListOptions{}, "server-list", "List virtual servers", func(s *mcclient.ClientSession, args *ServerListOptions) error {
|
||||
@@ -98,6 +99,9 @@ func init() {
|
||||
if len(args.Hypervisor) > 0 {
|
||||
params.Add(jsonutils.NewString(args.Hypervisor), "hypervisor")
|
||||
}
|
||||
if len(args.Manager) > 0 {
|
||||
params.Add(jsonutils.NewString(args.Manager), "manager")
|
||||
}
|
||||
result, err := modules.Servers.List(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -209,8 +209,7 @@ func performClassActionHandler(ctx context.Context, w http.ResponseWriter, r *ht
|
||||
if data == nil {
|
||||
data = body.(*jsonutils.JSONDict)
|
||||
}
|
||||
}
|
||||
if data == nil {
|
||||
} else {
|
||||
data = jsonutils.NewDict()
|
||||
}
|
||||
results, err := manager.PerformClassAction(ctx, params["<action>"], query, data)
|
||||
|
||||
@@ -524,7 +524,7 @@ func (dispatcher *DBModelDispatcher) tryGetModelProperty(ctx context.Context, pr
|
||||
}
|
||||
|
||||
func (dispatcher *DBModelDispatcher) Get(ctx context.Context, idStr string, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
log.Debugf("Get %s", idStr)
|
||||
// log.Debugf("Get %s", idStr)
|
||||
userCred := fetchUserCredential(ctx)
|
||||
|
||||
data, err := dispatcher.tryGetModelProperty(ctx, idStr, query)
|
||||
@@ -541,7 +541,7 @@ func (dispatcher *DBModelDispatcher) Get(ctx context.Context, idStr string, quer
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debugf("Get found %s", model)
|
||||
// log.Debugf("Get found %s", model)
|
||||
if !model.AllowGetDetails(ctx, userCred, query) {
|
||||
return nil, httperrors.NewForbiddenError("Not allow to get details")
|
||||
}
|
||||
|
||||
@@ -289,20 +289,21 @@ func (manager *STaskManager) execTask(taskId string, data jsonutils.JSONObject)
|
||||
}
|
||||
}
|
||||
|
||||
func execITask(taskValue reflect.Value, task *STask, data jsonutils.JSONObject, isMulti bool) {
|
||||
func execITask(taskValue reflect.Value, task *STask, odata jsonutils.JSONObject, isMulti bool) {
|
||||
var err error
|
||||
ctxData := task.GetRequestContext()
|
||||
ctx := ctxData.GetContext()
|
||||
|
||||
taskFailed := false
|
||||
|
||||
data := odata
|
||||
if data != nil {
|
||||
taskStatus, _ := data.GetString("__status__")
|
||||
if len(taskStatus) > 0 && taskStatus != "OK" {
|
||||
taskFailed = true
|
||||
data, err = data.Get("reason")
|
||||
data, err = data.Get("__reason__")
|
||||
if err != nil {
|
||||
data = jsonutils.NewString("Task failed due to unknown remote errors!")
|
||||
data = jsonutils.NewString(fmt.Sprintf("Task failed due to unknown remote errors! %s", odata))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -328,7 +329,13 @@ func execITask(taskValue reflect.Value, task *STask, data jsonutils.JSONObject,
|
||||
|
||||
if !funcValue.IsValid() || funcValue.IsNil() {
|
||||
msg := fmt.Sprintf("Stage %s not found", stageName)
|
||||
log.Errorf(msg)
|
||||
if taskFailed {
|
||||
// failed handler is optional, ignore the error
|
||||
log.Warningf(msg)
|
||||
msg, _ = data.GetString()
|
||||
} else {
|
||||
log.Errorf(msg)
|
||||
}
|
||||
task.SetStageFailed(ctx, msg)
|
||||
task.SaveRequestContext(&ctxData)
|
||||
return
|
||||
@@ -548,7 +555,7 @@ func (self *STask) NotifyParentTaskFailure(ctx context.Context, reason string) {
|
||||
if len(reason) > 100 {
|
||||
reason = reason[:100] + "..."
|
||||
}
|
||||
body.Add(jsonutils.NewString(fmt.Sprintf("Subtask %s failed: %s", self.TaskName, reason)))
|
||||
body.Add(jsonutils.NewString(fmt.Sprintf("Subtask %s failed: %s", self.TaskName, reason)), "__reason__")
|
||||
self.NotifyParentTaskComplete(ctx, body, true)
|
||||
}
|
||||
|
||||
|
||||
@@ -161,16 +161,16 @@ func (model *SVirtualResourceBase) AllowPerformMetadata(ctx context.Context, use
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) GetTenantCache(ctx context.Context) (*STenant, error) {
|
||||
log.Debugf("Get tenant by Id %s", model.ProjectId)
|
||||
// log.Debugf("Get tenant by Id %s", model.ProjectId)
|
||||
return TenantCacheManager.FetchTenantById(ctx, model.ProjectId)
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) getMoreDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, extra *jsonutils.JSONDict) *jsonutils.JSONDict {
|
||||
if userCred.IsSystemAdmin() {
|
||||
log.Debugf("GetCustomizeColumns")
|
||||
// log.Debugf("GetCustomizeColumns")
|
||||
tobj, err := model.GetTenantCache(ctx)
|
||||
if err == nil {
|
||||
log.Debugf("GetTenantFromCache %s", jsonutils.Marshal(tobj))
|
||||
// log.Debugf("GetTenantFromCache %s", jsonutils.Marshal(tobj))
|
||||
extra.Add(jsonutils.NewString(tobj.GetName()), "tenant")
|
||||
} else {
|
||||
log.Errorf("GetTenantCache fail %s", err)
|
||||
|
||||
@@ -123,10 +123,11 @@ type SDiskInfo struct {
|
||||
func (self *SAliyunGuestDriver) RequestDeployGuestOnHost(ctx context.Context, guest *models.SGuest, host *models.SHost, task taskman.ITask) error {
|
||||
config := guest.GetDeployConfigOnHost(ctx, host, task.GetParams())
|
||||
|
||||
onfinish, err := config.GetString("on_finish")
|
||||
/* onfinish, err := config.GetString("on_finish")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} */
|
||||
|
||||
action, err := config.GetString("action")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -172,12 +173,12 @@ func (self *SAliyunGuestDriver) RequestDeployGuestOnHost(ctx context.Context, gu
|
||||
}
|
||||
}
|
||||
|
||||
if onfinish == "none" {
|
||||
/*if onfinish == "none" {
|
||||
err = iVM.StartVM()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
encpasswd, err := utils.EncryptAESBase64(guest.Id, passwd)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package models
|
||||
|
||||
const (
|
||||
BAREMETAL_INIT = "init"
|
||||
BAREMETAL_PREPARE = "prepare"
|
||||
BAREMETAL_PREPARE_FAIL = "prepare_fail"
|
||||
BAREMETAL_READY = "ready"
|
||||
BAREMETAL_RUNNING = "running"
|
||||
BAREMETAL_MAINTAINING = "maintaining"
|
||||
BAREMETAL_START_MAINTAIN = "start_maintain"
|
||||
BAREMETAL_DELETING = "deleting"
|
||||
BAREMETAL_DELETE = "delete"
|
||||
BAREMETAL_DELETE_FAIL = "delete_fail"
|
||||
BAREMETAL_UNKNOWN = "unknown"
|
||||
BAREMETAL_SYNCING_STATUS = "syncing_status"
|
||||
BAREMETAL_SYNC = "sync"
|
||||
BAREMETAL_SYNC_FAIL = "sync_fail"
|
||||
BAREMETAL_START_CONVERT = "start_convert"
|
||||
BAREMETAL_CONVERTING = "converting"
|
||||
BAREMETAL_START_FAIL = "start_fail"
|
||||
BAREMETAL_STOP_FAIL = "stop_fail"
|
||||
)
|
||||
@@ -145,9 +145,9 @@ func (manager *SCloudregionManager) SyncRegions(ctx context.Context, userCred mc
|
||||
_, err = removed[i].PerformDisable(ctx, userCred, nil, nil)
|
||||
}
|
||||
if err != nil {
|
||||
syncResult.Delete()
|
||||
} else {
|
||||
syncResult.DeleteError(err)
|
||||
} else {
|
||||
syncResult.Delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -701,6 +701,27 @@ func parseIsoInfo(ctx context.Context, userCred mcclient.TokenCredential, info s
|
||||
return image.Id, nil
|
||||
}
|
||||
|
||||
// def get_disk_spec_v2(conf):
|
||||
// def _get_spec(storages):
|
||||
// spec = {}
|
||||
// for adapter, ss in group_by_adapter(storages).items():
|
||||
// if len(ss) == 0:
|
||||
// continue
|
||||
// spec[adapter] = get_disk_spec(ss)
|
||||
// return spec
|
||||
|
||||
// spec = {}
|
||||
// for driver in DISK_DRIVERS:
|
||||
// storages = [s for s in conf if s['driver'] == driver]
|
||||
// if len(storages) != 0:
|
||||
// spec[driver] = _get_spec(storages)
|
||||
// return spec
|
||||
|
||||
func GetDiskSpecV2(storageInfo jsonutils.JSONObject) *jsonutils.JSONDict {
|
||||
// ToDo
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SDisk) fetchDiskInfo(diskConfig *SDiskConfig) {
|
||||
if len(diskConfig.ImageId) > 0 {
|
||||
self.TemplateId = diskConfig.ImageId
|
||||
@@ -832,9 +853,8 @@ func (self *SDisk) GetAttachedGuests() []SGuest {
|
||||
q = q.Filter(sqlchemy.Equals(guestdisks.Field("disk_id"), self.Id))
|
||||
|
||||
ret := make([]SGuest, 0)
|
||||
err := q.All(&ret)
|
||||
if err != nil {
|
||||
log.Errorf("%s", err)
|
||||
if err := db.FetchModelObjects(GuestManager, q, &ret); err != nil {
|
||||
log.Errorf("Fetch Geusts Objects %v", err)
|
||||
return nil
|
||||
}
|
||||
return ret
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/util/regutils"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
@@ -205,7 +204,6 @@ func (man *SDnsRecordManager) QueryDns(projectId, name string) *SDnsRecord {
|
||||
|
||||
err := q.First(rec)
|
||||
if err != nil {
|
||||
log.Errorf("QueryDns %q fail: %s", name, err)
|
||||
return nil
|
||||
}
|
||||
return rec
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
|
||||
"yunion.io/x/onecloud/pkg/compute/options"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -263,17 +264,18 @@ func (self *SGuestnetwork) GetJsonDescAtHost(host *SHost) jsonutils.JSONObject {
|
||||
}
|
||||
|
||||
func (manager *SGuestnetworkManager) GetGuestByAddress(address string) *SGuest {
|
||||
gn := SGuestnetwork{}
|
||||
gn.SetModelManager(GuestnetworkManager)
|
||||
|
||||
err := manager.Query().Equals("ip_addr", address).First(&gn)
|
||||
if err != nil {
|
||||
log.Errorf("GetGuestByAddress query fail: %s", err)
|
||||
return nil
|
||||
}
|
||||
guest, _ := GuestManager.FetchById(gn.GuestId)
|
||||
if guest != nil {
|
||||
return guest.(*SGuest)
|
||||
networks := manager.TableSpec().Instance()
|
||||
guests := GuestManager.Query()
|
||||
q := guests.Join(networks, sqlchemy.AND(
|
||||
sqlchemy.IsFalse(networks.Field("deleted")),
|
||||
sqlchemy.Equals(networks.Field("ip_addr"), address),
|
||||
sqlchemy.Equals(networks.Field("guest_id"), guests.Field("id")),
|
||||
))
|
||||
guest := &SGuest{}
|
||||
guest.SetModelManager(GuestManager)
|
||||
err := q.First(guest)
|
||||
if err == nil {
|
||||
return guest
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -333,6 +335,22 @@ func (manager *SGuestnetworkManager) DeleteGuestNics(ctx context.Context, guest
|
||||
return nil
|
||||
}
|
||||
|
||||
func (manager *SGuestnetworkManager) getGuestNicByIP(ip string) (*SGuestnetwork, error) {
|
||||
gn := SGuestnetwork{}
|
||||
q := manager.Query()
|
||||
q = q.Equals("ip_addr", ip)
|
||||
err := q.First(&gn)
|
||||
if err != nil {
|
||||
if err != sql.ErrNoRows {
|
||||
log.Errorf("getGuestNicByIP fail %s", err)
|
||||
return nil, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
gn.SetModelManager(manager)
|
||||
return &gn, nil
|
||||
}
|
||||
|
||||
func (self *SGuestnetwork) LogDetachEvent(userCred mcclient.TokenCredential, guest *SGuest, network *SNetwork) {
|
||||
if network == nil {
|
||||
netTmp, _ := NetworkManager.FetchById(self.NetworkId)
|
||||
|
||||
+180
-35
@@ -205,49 +205,60 @@ func (manager *SGuestManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQ
|
||||
if len(hypervisor) > 0 {
|
||||
q = q.Equals("hypervisor", hypervisor)
|
||||
}
|
||||
|
||||
hostFilter, _ := queryDict.GetString("host")
|
||||
zoneFilter, _ := queryDict.GetString("zone")
|
||||
wireFilter, _ := queryDict.GetString("wire")
|
||||
networkFilter, _ := queryDict.GetString("network")
|
||||
diskFilter, _ := queryDict.GetString("disk")
|
||||
var sq *sqlchemy.SSubQuery
|
||||
if len(hostFilter) > 0 {
|
||||
host, _ := HostManager.FetchByIdOrName("", hostFilter)
|
||||
if host == nil {
|
||||
return nil, httperrors.NewResourceNotFoundError(fmt.Sprintf("host %s not found", hostFilter))
|
||||
return nil, httperrors.NewResourceNotFoundError("host %s not found", hostFilter)
|
||||
}
|
||||
sq = HostManager.Query("id").Equals("id", host.GetId()).SubQuery()
|
||||
} else if len(zoneFilter) > 0 {
|
||||
q = q.Equals("host_id", host.GetId())
|
||||
}
|
||||
|
||||
zoneFilter, _ := queryDict.GetString("zone")
|
||||
if len(zoneFilter) > 0 {
|
||||
zone, _ := ZoneManager.FetchByIdOrName("", zoneFilter)
|
||||
if zone == nil {
|
||||
return nil, httperrors.NewResourceNotFoundError(fmt.Sprintf("zone %s not found", zoneFilter))
|
||||
return nil, httperrors.NewResourceNotFoundError("zone %s not found", zoneFilter)
|
||||
}
|
||||
hostTable := HostManager.Query().SubQuery()
|
||||
zoneTable := ZoneManager.Query().SubQuery()
|
||||
sq = hostTable.Query(hostTable.Field("id")).Join(zoneTable,
|
||||
sq := hostTable.Query(hostTable.Field("id")).Join(zoneTable,
|
||||
sqlchemy.Equals(zoneTable.Field("id"), hostTable.Field("zone_id"))).Filter(sqlchemy.Equals(zoneTable.Field("id"), zone.GetId())).SubQuery()
|
||||
} else if len(wireFilter) > 0 {
|
||||
q = q.In("host_id", sq)
|
||||
}
|
||||
|
||||
wireFilter, _ := queryDict.GetString("wire")
|
||||
if len(wireFilter) > 0 {
|
||||
wire, _ := WireManager.FetchByIdOrName("", wireFilter)
|
||||
if wire == nil {
|
||||
return nil, httperrors.NewResourceNotFoundError(fmt.Sprintf("wire %s not found", wireFilter))
|
||||
return nil, httperrors.NewResourceNotFoundError("wire %s not found", wireFilter)
|
||||
}
|
||||
hostTable := HostManager.Query().SubQuery()
|
||||
hostWire := HostwireManager.Query().SubQuery()
|
||||
sq = hostTable.Query(hostTable.Field("id")).Join(hostWire, sqlchemy.Equals(hostWire.Field("host_id"), hostTable.Field("id"))).Filter(sqlchemy.Equals(hostWire.Field("wire_id"), wire.GetId())).SubQuery()
|
||||
} else if len(networkFilter) > 0 {
|
||||
sq := hostTable.Query(hostTable.Field("id")).Join(hostWire, sqlchemy.Equals(hostWire.Field("host_id"), hostTable.Field("id"))).Filter(sqlchemy.Equals(hostWire.Field("wire_id"), wire.GetId())).SubQuery()
|
||||
q = q.In("host_id", sq)
|
||||
}
|
||||
|
||||
networkFilter, _ := queryDict.GetString("network")
|
||||
if len(networkFilter) > 0 {
|
||||
netI, _ := NetworkManager.FetchByIdOrName(userCred.GetProjectId(), networkFilter)
|
||||
if netI == nil {
|
||||
return nil, httperrors.NewResourceNotFoundError(fmt.Sprintf("network %s not found", networkFilter))
|
||||
return nil, httperrors.NewResourceNotFoundError("network %s not found", networkFilter)
|
||||
}
|
||||
net := netI.(*SNetwork)
|
||||
hostTable := HostManager.Query().SubQuery()
|
||||
hostWire := HostwireManager.Query().SubQuery()
|
||||
sq = hostTable.Query(hostTable.Field("id")).Join(hostWire,
|
||||
sq := hostTable.Query(hostTable.Field("id")).Join(hostWire,
|
||||
sqlchemy.Equals(hostWire.Field("host_id"), hostTable.Field("id"))).Filter(sqlchemy.Equals(hostWire.Field("wire_id"), net.WireId)).SubQuery()
|
||||
} else if len(diskFilter) > 0 {
|
||||
q = q.In("host_id", sq)
|
||||
}
|
||||
|
||||
diskFilter, _ := queryDict.GetString("disk")
|
||||
if len(diskFilter) > 0 {
|
||||
diskI, _ := DiskManager.FetchByIdOrName(userCred.GetProjectId(), diskFilter)
|
||||
if diskI == nil {
|
||||
return nil, httperrors.NewResourceNotFoundError(fmt.Sprintf("disk %s not found", diskFilter))
|
||||
return nil, httperrors.NewResourceNotFoundError("disk %s not found", diskFilter)
|
||||
}
|
||||
disk := diskI.(*SDisk)
|
||||
guestdisks := GuestdiskManager.Query().SubQuery()
|
||||
@@ -264,7 +275,7 @@ func (manager *SGuestManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQ
|
||||
hosts := HostManager.Query().SubQuery()
|
||||
hoststorages := HoststorageManager.Query().SubQuery()
|
||||
storages := StorageManager.Query().SubQuery()
|
||||
sq = hosts.Query(hosts.Field("id")).
|
||||
sq := hosts.Query(hosts.Field("id")).
|
||||
Join(hoststorages, sqlchemy.AND(
|
||||
sqlchemy.Equals(hoststorages.Field("host_id"), hosts.Field("id")),
|
||||
sqlchemy.IsFalse(hoststorages.Field("deleted")))).
|
||||
@@ -272,11 +283,21 @@ func (manager *SGuestManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQ
|
||||
sqlchemy.Equals(storages.Field("id"), hoststorages.Field("storage_id")),
|
||||
sqlchemy.IsFalse(storages.Field("deleted")))).
|
||||
Filter(sqlchemy.Equals(storages.Field("id"), disk.StorageId)).SubQuery()
|
||||
q = q.In("host_id", sq)
|
||||
}
|
||||
}
|
||||
if sq != nil {
|
||||
|
||||
managerFilter, _ := queryDict.GetString("manager")
|
||||
if len(managerFilter) > 0 {
|
||||
managerI, _ := CloudproviderManager.FetchByIdOrName(userCred.GetProjectId(), managerFilter)
|
||||
if managerI == nil {
|
||||
return nil, httperrors.NewResourceNotFoundError("cloud provider %s not found", managerFilter)
|
||||
}
|
||||
hosts := HostManager.Query().SubQuery()
|
||||
sq := hosts.Query(hosts.Field("id")).Equals("manager_id", managerI.GetId()).SubQuery()
|
||||
q = q.In("host_id", sq)
|
||||
}
|
||||
|
||||
gpu, _ := queryDict.GetString("gpu")
|
||||
if len(gpu) != 0 {
|
||||
isodev := IsolatedDeviceManager.Query().SubQuery()
|
||||
@@ -908,11 +929,7 @@ func (self *SGuest) GetCustomizeColumns(ctx context.Context, userCred mcclient.T
|
||||
// extra.Add(jsonutils.NewString(self.getFlavorName()), "flavor")
|
||||
extra.Add(jsonutils.NewString(self.getKeypairName()), "keypair")
|
||||
extra.Add(jsonutils.NewInt(int64(self.getExtBandwidth())), "ext_bw")
|
||||
zone := self.getZone()
|
||||
if zone != nil {
|
||||
extra.Add(jsonutils.NewString(zone.Id), "zone_id")
|
||||
extra.Add(jsonutils.NewString(zone.Name), "zone")
|
||||
}
|
||||
|
||||
extra.Add(jsonutils.NewString(self.GetSecgroupName()), "secgroup")
|
||||
|
||||
if self.PendingDeleted {
|
||||
@@ -920,6 +937,28 @@ func (self *SGuest) GetCustomizeColumns(ctx context.Context, userCred mcclient.T
|
||||
extra.Add(jsonutils.NewString(timeutils.FullIsoTime(pendingDeletedAt)), "auto_delete_at")
|
||||
}
|
||||
|
||||
return self.moreExtraInfo(extra)
|
||||
}
|
||||
|
||||
func (self *SGuest) moreExtraInfo(extra *jsonutils.JSONDict) *jsonutils.JSONDict {
|
||||
zone := self.getZone()
|
||||
if zone != nil {
|
||||
extra.Add(jsonutils.NewString(zone.GetId()), "zone_id")
|
||||
extra.Add(jsonutils.NewString(zone.GetName()), "zone")
|
||||
if len(zone.ExternalId) > 0 {
|
||||
extra.Add(jsonutils.NewString(zone.ExternalId), "zone_external_id")
|
||||
}
|
||||
|
||||
region := zone.GetRegion()
|
||||
if region != nil {
|
||||
extra.Add(jsonutils.NewString(region.Id), "region_id")
|
||||
extra.Add(jsonutils.NewString(region.Name), "region")
|
||||
|
||||
if len(region.ExternalId) > 0 {
|
||||
extra.Add(jsonutils.NewString(region.ExternalId), "region_external_id")
|
||||
}
|
||||
}
|
||||
}
|
||||
return extra
|
||||
}
|
||||
|
||||
@@ -950,14 +989,7 @@ func (self *SGuest) GetExtraDetails(ctx context.Context, userCred mcclient.Token
|
||||
}
|
||||
extra.Add(jsonutils.NewString(self.getAdminSecurityRules()), "admin_security_rules")
|
||||
}
|
||||
zone := self.getZone()
|
||||
if zone != nil {
|
||||
extra.Add(jsonutils.NewString(zone.GetId()), "zone_id")
|
||||
extra.Add(jsonutils.NewString(zone.GetName()), "zone")
|
||||
extra.Add(jsonutils.NewString(zone.GetRegion().GetName()), "region")
|
||||
extra.Add(jsonutils.NewString(zone.GetRegion().GetId()), "region_id")
|
||||
}
|
||||
return extra
|
||||
return self.moreExtraInfo(extra)
|
||||
}
|
||||
|
||||
func (self *SGuest) getNetworksDetails() string {
|
||||
@@ -1445,7 +1477,20 @@ func (self *SGuest) SyncVMNics(ctx context.Context, userCred mcclient.TokenCrede
|
||||
if add.net == nil {
|
||||
continue // cannot determine which network it attached to
|
||||
}
|
||||
err := self.Attach2Network(ctx, userCred, add.net, nil, add.nic.GetIP(),
|
||||
// check if the IP has been occupied, if yes, release the IP
|
||||
gn, err := GuestnetworkManager.getGuestNicByIP(add.nic.GetIP())
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
continue
|
||||
}
|
||||
if gn != nil {
|
||||
err = gn.Detach(ctx, userCred)
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
err = self.Attach2Network(ctx, userCred, add.net, nil, add.nic.GetIP(),
|
||||
add.nic.GetMAC(), add.nic.GetDriver(), 0, false, -1, add.reserve, IPAllocationDefault, true)
|
||||
if err != nil {
|
||||
result.AddError(err)
|
||||
@@ -2310,7 +2355,7 @@ func (self *SGuest) PerformDetachdisk(ctx context.Context, userCred mcclient.Tok
|
||||
disk.SetStatus(userCred, DISK_DETACHING, "")
|
||||
}
|
||||
taskData := jsonutils.NewDict()
|
||||
taskData.Add(jsonutils.NewString(diskId), "disk_id")
|
||||
taskData.Add(jsonutils.NewString(disk.Id), "disk_id")
|
||||
taskData.Add(jsonutils.NewBool(keepDisk), "keep_disk")
|
||||
self.GetDriver().StartGuestDetachdiskTask(ctx, userCred, self, taskData, "")
|
||||
return nil, nil
|
||||
@@ -3011,6 +3056,106 @@ func (manager *SGuestManager) FetchGuestById(guestId string) *SGuest {
|
||||
return guest.(*SGuest)
|
||||
}
|
||||
|
||||
func (self *SGuest) GetSpec(checkStatus bool) *jsonutils.JSONDict {
|
||||
if checkStatus {
|
||||
if !utils.IsInStringArray(self.Status, []string{VM_SCHEDULE_FAILED}) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
spec := jsonutils.NewDict()
|
||||
spec.Set("cpu", jsonutils.NewInt(int64(self.VcpuCount)))
|
||||
spec.Set("mem", jsonutils.NewInt(int64(self.VmemSize)))
|
||||
|
||||
// get disk spec
|
||||
guestdisks := self.GetDisks()
|
||||
diskSpecs := jsonutils.NewArray()
|
||||
for _, guestdisk := range guestdisks {
|
||||
disk := guestdisk.GetDisk()
|
||||
diskSpec := jsonutils.NewDict()
|
||||
diskSpec.Set("size", jsonutils.NewInt(int64(disk.DiskSize)))
|
||||
s := disk.GetStorage()
|
||||
diskSpec.Set("backend", jsonutils.NewString(s.StorageType))
|
||||
diskSpec.Set("medium_type", jsonutils.NewString(s.MediumType))
|
||||
diskSpecs.Add(diskSpec)
|
||||
}
|
||||
spec.Set("disk", diskSpecs)
|
||||
|
||||
// get nic spec
|
||||
guestnics := self.GetNetworks()
|
||||
nicSpecs := jsonutils.NewArray()
|
||||
for _, guestnic := range guestnics {
|
||||
nicSpec := jsonutils.NewDict()
|
||||
nicSpec.Set("bandwidth", jsonutils.NewInt(int64(guestnic.getBandwidth())))
|
||||
t := "int"
|
||||
if guestnic.IsExit() {
|
||||
t = "ext"
|
||||
}
|
||||
nicSpec.Set("type", jsonutils.NewString(t))
|
||||
nicSpecs.Add(nicSpec)
|
||||
}
|
||||
spec.Set("nic", nicSpecs)
|
||||
|
||||
// get isolate device spec
|
||||
guestgpus := self.GetIsolatedDevices()
|
||||
gpuSpecs := jsonutils.NewArray()
|
||||
for _, guestgpu := range guestgpus {
|
||||
if strings.HasPrefix(guestgpu.DevType, "GPU") {
|
||||
gs := guestgpu.GetSpec(false)
|
||||
if gs != nil {
|
||||
gpuSpecs.Add(gs)
|
||||
}
|
||||
}
|
||||
}
|
||||
spec.Set("gpu", gpuSpecs)
|
||||
return spec
|
||||
}
|
||||
|
||||
func (self *SGuest) GetTemplateId() string {
|
||||
guestdisks := self.GetDisks()
|
||||
for _, guestdisk := range guestdisks {
|
||||
disk := guestdisk.GetDisk()
|
||||
if disk != nil {
|
||||
templateId := disk.GetTemplateId()
|
||||
if len(templateId) > 0 {
|
||||
return templateId
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGuest) GetShortDesc() *jsonutils.JSONDict {
|
||||
desc := self.SStandaloneResourceBase.GetShortDesc()
|
||||
desc.Set("mem", jsonutils.NewInt(int64(self.VmemSize)))
|
||||
desc.Set("cpu", jsonutils.NewInt(int64(self.VcpuCount)))
|
||||
templateId := self.GetTemplateId()
|
||||
if len(templateId) > 0 {
|
||||
desc.Set("cpu", jsonutils.NewString(templateId))
|
||||
}
|
||||
extBw := self.getBandwidth(true)
|
||||
intBw := self.getBandwidth(false)
|
||||
if extBw > 0 {
|
||||
desc.Set("ext_bandwidth", jsonutils.NewInt(int64(extBw)))
|
||||
}
|
||||
if intBw > 0 {
|
||||
desc.Set("int_bandwidth", jsonutils.NewInt(int64(intBw)))
|
||||
}
|
||||
desc.Set("hypervisor", jsonutils.NewString(self.GetHypervisor()))
|
||||
spec := self.GetSpec(false)
|
||||
if self.GetHypervisor() == HYPERVISOR_BAREMETAL {
|
||||
host := self.GetHost()
|
||||
if host != nil {
|
||||
hostSpec := host.GetSpec(false)
|
||||
hostSpecIdent := host.GetSpecIdent(hostSpec)
|
||||
spec.Set("host_spec", jsonutils.NewString(strings.Join(hostSpecIdent, "/")))
|
||||
}
|
||||
}
|
||||
if spec != nil {
|
||||
desc.Update(spec)
|
||||
}
|
||||
return desc
|
||||
}
|
||||
|
||||
func (self *SGuest) saveOsType(osType string) error {
|
||||
_, err := self.GetModelManager().TableSpec().Update(self, func() error {
|
||||
self.OsType = osType
|
||||
|
||||
@@ -92,7 +92,7 @@ func (man *SHostnetworkManager) QueryByAddress(addr string) *sqlchemy.SQuery {
|
||||
func (man *SHostnetworkManager) GetHostNetworkByAddress(addr string) *SHostnetwork {
|
||||
network := SHostnetwork{}
|
||||
err := man.QueryByAddress(addr).First(&network)
|
||||
if err != nil {
|
||||
if err == nil {
|
||||
return &network
|
||||
}
|
||||
return nil
|
||||
@@ -107,9 +107,18 @@ func (man *SHostnetworkManager) GetNetworkByAddress(addr string) *SNetwork {
|
||||
}
|
||||
|
||||
func (man *SHostnetworkManager) GetHostByAddress(addr string) *SHost {
|
||||
net := man.GetHostNetworkByAddress(addr)
|
||||
if net == nil {
|
||||
return nil
|
||||
networks := man.TableSpec().Instance()
|
||||
hosts := HostManager.Query()
|
||||
q := hosts.Join(networks, sqlchemy.AND(
|
||||
sqlchemy.IsFalse(networks.Field("deleted")),
|
||||
sqlchemy.Equals(networks.Field("ip_addr"), addr),
|
||||
sqlchemy.Equals(networks.Field("baremetal_id"), hosts.Field("id")),
|
||||
))
|
||||
host := &SHost{}
|
||||
host.SetModelManager(HostManager)
|
||||
err := q.First(host)
|
||||
if err == nil {
|
||||
return host
|
||||
}
|
||||
return net.GetHost()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -506,6 +506,58 @@ func (self *SHost) ClearSchedDescCache() error {
|
||||
return HostManager.ClearSchedDescCache(self.Id)
|
||||
}
|
||||
|
||||
func (self *SHost) GetSpec(statusCheck bool) *jsonutils.JSONDict {
|
||||
if statusCheck {
|
||||
if utils.IsInStringArray(self.Status, []string{BAREMETAL_INIT, BAREMETAL_PREPARE_FAIL, BAREMETAL_PREPARE}) ||
|
||||
self.getBaremetalServer() != nil {
|
||||
return nil
|
||||
}
|
||||
if self.MemSize == 0 || self.CpuCount == 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
spec := self.GetHardwareSpecification()
|
||||
spec.Remove("storage_info")
|
||||
netInfo := jsonutils.NewArray()
|
||||
nifs := self.GetNetInterfaces()
|
||||
for _, nif := range nifs {
|
||||
netDesc := nif.getBaremetalJsonDesc()
|
||||
nicType, err := netDesc.GetString("nic_type")
|
||||
if err != nil && nicType != NIC_TYPE_IPMI {
|
||||
netInfo.Add(netDesc)
|
||||
}
|
||||
}
|
||||
spec.Set("nic_count", jsonutils.NewInt(int64(netInfo.Length())))
|
||||
manufacture, err := self.SysInfo.Get("manufacture")
|
||||
if err != nil {
|
||||
manufacture = jsonutils.NewString("Unknown")
|
||||
}
|
||||
spec.Set("manufacture", manufacture)
|
||||
model, err := self.SysInfo.Get("model")
|
||||
if err != nil {
|
||||
model = jsonutils.NewString("Unknown")
|
||||
}
|
||||
spec.Set("model", model)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SHost) GetSpecIdent(spec *jsonutils.JSONDict) []string {
|
||||
// Todo
|
||||
return []string{}
|
||||
}
|
||||
|
||||
func (self *SHost) GetHardwareSpecification() *jsonutils.JSONDict {
|
||||
spec := jsonutils.NewDict()
|
||||
spec.Set("cpu", jsonutils.NewInt(int64(self.CpuCount)))
|
||||
spec.Set("mem", jsonutils.NewInt(int64(self.MemSize)))
|
||||
if self.StorageInfo != nil {
|
||||
spec.Set("disk", GetDiskSpecV2(self.StorageInfo))
|
||||
spec.Set("driver", jsonutils.NewString(self.StorageDriver))
|
||||
spec.Set("storage_info", self.StorageInfo)
|
||||
}
|
||||
return spec
|
||||
}
|
||||
|
||||
type SStorageCapacity struct {
|
||||
Capacity int
|
||||
Used int
|
||||
|
||||
@@ -380,6 +380,24 @@ func (self *SIsolatedDevice) getDesc() *jsonutils.JSONDict {
|
||||
return desc
|
||||
}
|
||||
|
||||
func (self *SIsolatedDevice) GetSpec(checkStatus bool) *jsonutils.JSONDict {
|
||||
if checkStatus {
|
||||
if len(self.GuestId) > 0 {
|
||||
return nil
|
||||
}
|
||||
host := self.getHost()
|
||||
if host.Status != BAREMETAL_RUNNING || !host.Enabled {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
spec := jsonutils.NewDict()
|
||||
spec.Set("dev_type", jsonutils.NewString(self.DevType))
|
||||
spec.Set("model", jsonutils.NewString(self.Model))
|
||||
spec.Set("pci_id", jsonutils.NewString(self.VendorDeviceId))
|
||||
spec.Set("vendor", jsonutils.NewString(self.getVendor()))
|
||||
return spec
|
||||
}
|
||||
|
||||
func (self *SIsolatedDevice) GetShortDesc() *jsonutils.JSONDict {
|
||||
desc := self.getDesc()
|
||||
desc.Add(jsonutils.NewString(self.Keyword()), "res_name")
|
||||
|
||||
@@ -328,7 +328,7 @@ func (manager *SSecurityGroupRuleManager) SyncRules(ctx context.Context, userCre
|
||||
cmp := strings.Compare(dbStr, ruleStr)
|
||||
if cmp == 0 {
|
||||
if dbRules[j].Description != rules[i].Description {
|
||||
if _, err := manager.TableSpec().Update(dbRules[j], func() error {
|
||||
if _, err := manager.TableSpec().Update(&dbRules[j], func() error {
|
||||
dbRules[j].Description = rules[i].Description
|
||||
return nil
|
||||
}); err != nil {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/pkg/tristate"
|
||||
"yunion.io/x/pkg/util/compare"
|
||||
"yunion.io/x/pkg/util/sysutils"
|
||||
"yunion.io/x/sqlchemy"
|
||||
)
|
||||
|
||||
@@ -654,6 +655,26 @@ func (manager *SStorageManager) ListItemFilter(ctx context.Context, q *sqlchemy.
|
||||
q = q.Filter(sqlchemy.In(q.Field("zone_id"), sq.SubQuery()))
|
||||
}
|
||||
|
||||
if jsonutils.QueryBoolean(query, "share", false) {
|
||||
q = q.Filter(sqlchemy.NotIn(q.Field("storage_type"), sysutils.LOCAL_STORAGE_TYPES))
|
||||
}
|
||||
|
||||
if jsonutils.QueryBoolean(query, "local", false) {
|
||||
q = q.Filter(sqlchemy.In(q.Field("storage_type"), sysutils.LOCAL_STORAGE_TYPES))
|
||||
}
|
||||
|
||||
if jsonutils.QueryBoolean(query, "usable", false) {
|
||||
hostStorageTable := HoststorageManager.Query().SubQuery()
|
||||
hostTable := HostManager.Query().SubQuery()
|
||||
sq := hostStorageTable.Query(hostStorageTable.Field("storage_id")).Join(hostTable,
|
||||
sqlchemy.Equals(hostTable.Field("id"), hostStorageTable.Field("host_id"))).
|
||||
Filter(sqlchemy.Equals(hostTable.Field("host_status"), HOST_ONLINE))
|
||||
|
||||
q = q.Filter(sqlchemy.In(q.Field("id"), sq)).
|
||||
Filter(sqlchemy.In(q.Field("status"), []string{STORAGE_ENABLED, STORAGE_ONLINE})).
|
||||
Filter(sqlchemy.IsTrue(q.Field("enabled")))
|
||||
}
|
||||
|
||||
managerStr := jsonutils.GetAnyString(query, []string{"manager", "provider", "manager_id", "provider_id"})
|
||||
if len(managerStr) > 0 {
|
||||
provider := CloudproviderManager.FetchCloudproviderByIdOrName(managerStr)
|
||||
|
||||
@@ -347,6 +347,7 @@ func (self *SWire) getNetworks() ([]SNetwork, error) {
|
||||
func (self *SWire) getGatewayNetworkQuery() *sqlchemy.SQuery {
|
||||
q := self.getNetworkQuery()
|
||||
q = q.IsNotNull("guest_gateway").IsNotEmpty("guest_gateway")
|
||||
q = q.Equals("status", NETWORK_STATUS_AVAILABLE)
|
||||
return q
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/compute/options"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
)
|
||||
|
||||
type DiskDeleteTask struct {
|
||||
@@ -51,7 +52,9 @@ func (self *DiskDeleteTask) startDeleteDisk(ctx context.Context, disk *models.SD
|
||||
self.OnGuestDiskDeleteSucc(ctx, disk, nil)
|
||||
} else {
|
||||
self.SetStage("on_guest_disk_delete_succ", nil)
|
||||
if err := host.GetHostDriver().RequestDeallocateDiskOnHost(host, storage, disk, self); err != nil {
|
||||
if host == nil {
|
||||
self.OnGuestDiskDeleteFailed(ctx, disk, httperrors.NewNotFoundError("fail to find master host"))
|
||||
} else if err := host.GetHostDriver().RequestDeallocateDiskOnHost(host, storage, disk, self); err != nil {
|
||||
self.OnGuestDiskDeleteFailed(ctx, disk, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func (self *DiskResizeTask) OnInit(ctx context.Context, obj db.IStandaloneModel,
|
||||
}
|
||||
resion := "Cannot find host for disk"
|
||||
if host == nil || host.HostStatus != models.HOST_ONLINE {
|
||||
disk.SetStatus(self.GetUserCred(), models.DISK_READY, resion)
|
||||
disk.SetDiskReady(ctx, self.GetUserCred(), resion)
|
||||
self.SetStageFailed(ctx, resion)
|
||||
db.OpsLog.LogEvent(disk, db.ACT_RESIZE_FAIL, resion, self.GetUserCred())
|
||||
} else {
|
||||
@@ -65,7 +65,7 @@ func (self *DiskResizeTask) OnStartResizeDiskSucc(ctx context.Context, disk *mod
|
||||
}
|
||||
|
||||
func (self *DiskResizeTask) OnStartResizeDiskFailed(ctx context.Context, disk *models.SDisk, resion error) {
|
||||
disk.SetStatus(self.GetUserCred(), models.DISK_READY, resion.Error())
|
||||
disk.SetDiskReady(ctx, self.GetUserCred(), resion.Error())
|
||||
self.SetStageFailed(ctx, resion.Error())
|
||||
db.OpsLog.LogEvent(disk, db.ACT_RESIZE_FAIL, resion.Error(), self.GetUserCred())
|
||||
}
|
||||
@@ -94,6 +94,7 @@ func (self *DiskResizeTask) OnDiskResizeComplete(ctx context.Context, disk *mode
|
||||
self.OnStartResizeDiskFailed(ctx, disk, err)
|
||||
return
|
||||
}
|
||||
disk.SetDiskReady(ctx, self.GetUserCred(), "")
|
||||
notes := fmt.Sprintf("%s=>%s", oldStatus, disk.Status)
|
||||
db.OpsLog.LogEvent(disk, db.ACT_UPDATE_STATUS, notes, self.UserCred)
|
||||
self.CleanHostSchedCache(disk)
|
||||
@@ -103,6 +104,6 @@ func (self *DiskResizeTask) OnDiskResizeComplete(ctx context.Context, disk *mode
|
||||
}
|
||||
|
||||
func (self *DiskResizeTask) OnDiskResizeCompleteFailed(ctx context.Context, disk *models.SDisk, resion error) {
|
||||
disk.SetStatus(self.UserCred, models.DISK_READY, resion.Error())
|
||||
disk.SetDiskReady(ctx, self.GetUserCred(), resion.Error())
|
||||
db.OpsLog.LogEvent(disk, db.ACT_RESIZE_FAIL, disk.GetShortDesc(), self.UserCred)
|
||||
}
|
||||
|
||||
@@ -4,12 +4,14 @@ import (
|
||||
"context"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/utils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/compute/options"
|
||||
"yunion.io/x/pkg/utils"
|
||||
)
|
||||
|
||||
type GuestDeleteTask struct {
|
||||
@@ -28,15 +30,18 @@ func (self *GuestDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel
|
||||
|
||||
func (self *GuestDeleteTask) OnGuestStopComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
guest := obj.(*models.SGuest)
|
||||
guestStatus, _ := self.Params.GetString("guest_status")
|
||||
if options.Options.EnablePendingDelete && !guest.PendingDeleted &&
|
||||
!jsonutils.QueryBoolean(self.Params, "purge", false) &&
|
||||
!jsonutils.QueryBoolean(self.Params, "override_pending_delete", false) &&
|
||||
!utils.IsInStringArray(guestStatus, []string{models.VM_SCHEDULE_FAILED, models.VM_NETWORK_FAILED, models.VM_DISK_FAILED,
|
||||
!jsonutils.QueryBoolean(self.Params, "override_pending_delete", false) {
|
||||
log.Debugf("XXXXXXX Do guest pending delete... XXXXXXX")
|
||||
guestStatus, _ := self.Params.GetString("guest_status")
|
||||
if !utils.IsInStringArray(guestStatus, []string{models.VM_SCHEDULE_FAILED, models.VM_NETWORK_FAILED, models.VM_DISK_FAILED,
|
||||
models.VM_CREATE_FAILED, models.VM_DEVICE_FAILED}) {
|
||||
self.StartPendingDeleteGuest(ctx, guest)
|
||||
return
|
||||
self.StartPendingDeleteGuest(ctx, guest)
|
||||
return
|
||||
}
|
||||
}
|
||||
log.Debugf("XXXXXXX Do real delete on guest ... XXXXXXX")
|
||||
self.OnGuestStopCompleteFailed(ctx, guest, data)
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ func (self *GuestStartTask) OnStartCompleteFailed(ctx context.Context, obj db.IS
|
||||
}
|
||||
|
||||
func (self *GuestStartTask) onStartGuestFailed(ctx context.Context, guest *models.SGuest, err error) {
|
||||
guest.SetStatus(self.UserCred, models.VM_START_FAILED, err.Error())
|
||||
self.SetStageFailed(ctx, err.Error())
|
||||
self.OnStartCompleteFailed(ctx, guest, jsonutils.NewString(err.Error()))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
# 行为约定
|
||||
|
||||
A
|
||||
|
||||
```sh
|
||||
names=''
|
||||
names="$names titan" #ok, guest PlainName
|
||||
names="$names titan.hq.cloud.yunionyun.com" #ok, guest CloudZoneFQDN
|
||||
names="$names kubenode" #ok, host PlainName
|
||||
names="$names kubenode.hq.cloud.yunionyun.com" #ok, host CloudZoneFQDN
|
||||
names="$names whoever-the-ether" #NXDOMAIN, nonexistent PlainName
|
||||
names="$names whoever-the-ether.titan.hq.cloud.yunionyun.com" #NXDOMAIN, nonexistent CloudZoneFQDN
|
||||
names="$names mail.google.com" #ok, dnsrecords
|
||||
names="$names www.douban.com" #ok, pub
|
||||
names="$names app" #ok, k8s svc PlainName in "default" ns
|
||||
names="$names app.default" #NXDOMAIN, k8s svc name.namespace
|
||||
names="$names app.default.hq.cloud.yunionyun.com" #ok, k8s name.ns CloudZoneFQDN
|
||||
names="$names mon-kafka.system" #NXDOMAIN, k8s svc name.namespace
|
||||
names="$names mon-kafka.system.hq.cloud.yunionyun.com" #ok, k8s name.ns CloudZoneFQDN
|
||||
|
||||
# TODO source IP
|
||||
# TODO other TYPEs
|
||||
for name in $names; do
|
||||
echo "############### $name"
|
||||
#dig @192.168.222.171 $name
|
||||
#dig -p 54 @10.168.222.136 $name
|
||||
dig -p 54 @192.168.222.171 $name
|
||||
done
|
||||
```
|
||||
|
||||
PTR
|
||||
|
||||
```sh
|
||||
names=''
|
||||
names="$names " #ok, guest ip
|
||||
names="$names " #ok, host ip
|
||||
names="$names " #ok, ptr records in db
|
||||
names="$names " #NXDOMAIN, others
|
||||
|
||||
for name in $names; do
|
||||
echo "############### $name"
|
||||
dig -p 54 @192.168.222.171 -x $name
|
||||
done
|
||||
```
|
||||
|
||||
# 配置
|
||||
|
||||
log {
|
||||
# note that apart from rcode like NXDOMAIN, SERVFAIL, coredns will also
|
||||
# log NOERROR response when it's NoData as defined by coredns itself
|
||||
#
|
||||
# > NoData indicates name found, but not the type: NOERROR in header, SOA in auth.
|
||||
#
|
||||
class denial
|
||||
class error
|
||||
}
|
||||
+80
-87
@@ -18,25 +18,27 @@ import (
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/miekg/dns"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/fields"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
|
||||
ylog "yunion.io/x/log"
|
||||
"yunion.io/x/pkg/utils"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/onecloud/pkg/util/k8s"
|
||||
"yunion.io/x/pkg/utils"
|
||||
"yunion.io/x/sqlchemy"
|
||||
)
|
||||
|
||||
const (
|
||||
PluginName string = "yunion"
|
||||
|
||||
// defaultTTL to apply to all answers
|
||||
defaultTTL = 10
|
||||
defaultTTL = 10
|
||||
defaultDbMaxOpenConn = 32
|
||||
defaultDbMaxIdleConn = 32
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -74,15 +76,17 @@ func (r *SRegionDNS) initDB(c *caddy.Controller) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dbConn, err := sql.Open(dialect, sqlStr)
|
||||
sqlDb, err := sql.Open(dialect, sqlStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sqlchemy.SetDB(dbConn)
|
||||
sqlDb.SetMaxOpenConns(defaultDbMaxOpenConn)
|
||||
sqlDb.SetMaxIdleConns(defaultDbMaxIdleConn)
|
||||
sqlchemy.SetDB(sqlDb)
|
||||
db.InitAllManagers()
|
||||
|
||||
c.OnShutdown(func() error {
|
||||
r.CloseDB()
|
||||
sqlchemy.CloseDB()
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
@@ -103,10 +107,6 @@ func (r *SRegionDNS) initK8s(c *caddy.Controller) {
|
||||
ylog.Infof("Init k8s client success, %d pods in the cluster", len(pods.Items))
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) CloseDB() {
|
||||
sqlchemy.CloseDB()
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) ServeDNS(ctx context.Context, w dns.ResponseWriter, rmsg *dns.Msg) (int, error) {
|
||||
var (
|
||||
records []dns.RR
|
||||
@@ -116,36 +116,26 @@ func (r *SRegionDNS) ServeDNS(ctx context.Context, w dns.ResponseWriter, rmsg *d
|
||||
|
||||
opt := plugin.Options{}
|
||||
state := request.Request{W: w, Req: rmsg, Context: ctx}
|
||||
|
||||
zone := plugin.Zones(r.Zones).Matches(state.Name())
|
||||
|
||||
switch state.QType() {
|
||||
case dns.TypeA:
|
||||
ylog.Debugf("A question: %#v", state)
|
||||
records, err = plugin.A(r, zone, state, nil, opt)
|
||||
case dns.TypeAAAA:
|
||||
ylog.Debugf("AAAA question: %#v", state)
|
||||
// TODO fallthrough to next
|
||||
records, err = plugin.AAAA(r, zone, state, nil, opt)
|
||||
case dns.TypeTXT:
|
||||
ylog.Debugf("TXT question: %#v", state)
|
||||
records, err = plugin.TXT(r, zone, state, opt)
|
||||
case dns.TypeCNAME:
|
||||
ylog.Debugf("CNAME question: %#v", state)
|
||||
records, err = plugin.CNAME(r, zone, state, opt)
|
||||
case dns.TypePTR:
|
||||
ylog.Debugf("PTR question: %#v", state)
|
||||
records, err = plugin.PTR(r, zone, state, opt)
|
||||
case dns.TypeMX:
|
||||
ylog.Debugf("MX question: %#v", state)
|
||||
records, extra, err = plugin.MX(r, zone, state, opt)
|
||||
case dns.TypeSRV:
|
||||
ylog.Debugf("SRV question: %#v", state)
|
||||
records, extra, err = plugin.SRV(r, zone, state, opt)
|
||||
case dns.TypeSOA:
|
||||
ylog.Debugf("SOA question: %#v", state)
|
||||
records, err = plugin.SOA(r, zone, state, opt)
|
||||
case dns.TypeNS:
|
||||
ylog.Debugf("NS question: %#v", state)
|
||||
if state.Name() == zone {
|
||||
records, extra, err = plugin.NS(r, zone, state, opt)
|
||||
break
|
||||
@@ -157,18 +147,19 @@ func (r *SRegionDNS) ServeDNS(ctx context.Context, w dns.ResponseWriter, rmsg *d
|
||||
_, err = plugin.A(r, zone, state, nil, opt)
|
||||
}
|
||||
|
||||
if r.IsNameError(err) {
|
||||
if err == errCallNext {
|
||||
if r.Fall.Through(state.Name()) {
|
||||
return plugin.NextOrFailure(r.Name(), r.Next, ctx, w, rmsg)
|
||||
}
|
||||
return plugin.BackendError(r, zone, dns.RcodeNameError, state, nil /* err */, opt)
|
||||
}
|
||||
if err != nil {
|
||||
return plugin.BackendError(r, zone, dns.RcodeServerFailure, state, err, opt)
|
||||
} else if err == errRefused {
|
||||
return plugin.BackendError(r, zone, dns.RcodeRefused, state, err, opt)
|
||||
} else if err == errNotFound {
|
||||
return plugin.BackendError(r, zone, dns.RcodeNameError, state, err, opt)
|
||||
}
|
||||
|
||||
if len(records) == 0 {
|
||||
return plugin.BackendError(r, zone, dns.RcodeSuccess, state, err, opt)
|
||||
return plugin.BackendError(r, zone, dns.RcodeNameError, state, err, opt)
|
||||
}
|
||||
|
||||
m := new(dns.Msg)
|
||||
@@ -184,13 +175,14 @@ func (r *SRegionDNS) ServeDNS(ctx context.Context, w dns.ResponseWriter, rmsg *d
|
||||
}
|
||||
|
||||
var (
|
||||
errNoItems = errors.New("no items found")
|
||||
errRefused = errors.New("refused the query")
|
||||
errNotFound = errors.New("not found")
|
||||
errCallNext = errors.New("continue to next")
|
||||
)
|
||||
|
||||
// Services implements the ServiceBackend interface
|
||||
func (r *SRegionDNS) Services(state request.Request, exact bool, opt plugin.Options) (services []msg.Service, err error) {
|
||||
switch state.QType() {
|
||||
|
||||
case dns.TypeTXT:
|
||||
t, _ := dnsutil.TrimZone(state.Name(), state.Zone)
|
||||
|
||||
@@ -203,7 +195,6 @@ func (r *SRegionDNS) Services(state request.Request, exact bool, opt plugin.Opti
|
||||
}
|
||||
svc := msg.Service{Text: "0.0.1", TTL: 28800, Key: msg.Path(state.QName(), "coredns")}
|
||||
return []msg.Service{svc}, nil
|
||||
|
||||
case dns.TypeNS:
|
||||
ns := r.nsAddr()
|
||||
svc := msg.Service{Host: ns.A.String(), Key: msg.Path(state.QName(), "coredns")}
|
||||
@@ -219,7 +210,6 @@ func (r *SRegionDNS) Services(state request.Request, exact bool, opt plugin.Opti
|
||||
}
|
||||
|
||||
services, err = r.Records(state, false)
|
||||
ylog.Debugf("Get records: %#v, error: %v", services, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -230,7 +220,7 @@ func (r *SRegionDNS) Lookup(state request.Request, name string, typ uint16) (*dn
|
||||
|
||||
// IsNameError implements the ServiceBackend interface
|
||||
func (r *SRegionDNS) IsNameError(err error) bool {
|
||||
return err == errNoItems
|
||||
return err == errCallNext
|
||||
}
|
||||
|
||||
// Records looks up records in region mysql
|
||||
@@ -242,25 +232,22 @@ func (r *SRegionDNS) Records(state request.Request, exact bool) ([]msg.Service,
|
||||
return r.findRecords(req)
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) getHostIpWithName(req *recordRequest) []string {
|
||||
func (r *SRegionDNS) getHostIpWithName(req *recordRequest) string {
|
||||
name := req.QueryName()
|
||||
host, _ := models.HostManager.FetchByName("", name)
|
||||
if host == nil {
|
||||
return nil
|
||||
return ""
|
||||
}
|
||||
ip := host.(*models.SHost).AccessIp
|
||||
if len(ip) == 0 {
|
||||
return nil
|
||||
}
|
||||
return []string{ip}
|
||||
return ip
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) getGuestIpWithName(req *recordRequest) []string {
|
||||
ips := []string{}
|
||||
name := req.QueryName()
|
||||
projectId := req.ProjectId()
|
||||
isExitOnly := req.IsExitOnly()
|
||||
ips = models.GuestManager.GetIpInProjectWithName(projectId, name, isExitOnly)
|
||||
wantOnlyExit := false
|
||||
ips = models.GuestManager.GetIpInProjectWithName(projectId, name, wantOnlyExit)
|
||||
return ips
|
||||
}
|
||||
|
||||
@@ -268,6 +255,9 @@ func (r *SRegionDNS) getK8sServiceBackends(req *recordRequest) ([]string, error)
|
||||
queryInfo := req.GetK8sQueryInfo()
|
||||
pods, err := r.getK8sServicePods(queryInfo.Namespace, queryInfo.ServiceName)
|
||||
if err != nil {
|
||||
if k8serrors.IsNotFound(err) {
|
||||
err = nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
ips := make([]string, 0)
|
||||
@@ -281,7 +271,11 @@ func (r *SRegionDNS) getK8sServiceBackends(req *recordRequest) ([]string, error)
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) getK8sServicePods(namespace, name string) ([]v1.Pod, error) {
|
||||
cli := r.K8sClient
|
||||
cli, err := k8s.NewClientByFile(r.K8sConfigFile, nil)
|
||||
if err != nil {
|
||||
ylog.Errorf("Init kubernetes client error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
svc, err := cli.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -301,10 +295,9 @@ func (r *SRegionDNS) Name() string {
|
||||
return PluginName
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) queryLocalDnsRecords(req *recordRequest) (recs []msg.Service, err error) {
|
||||
func (r *SRegionDNS) queryLocalDnsRecords(req *recordRequest) (recs []msg.Service) {
|
||||
ips := models.DnsRecordManager.QueryDnsIps(req.ProjectId(), req.Name(), req.Type())
|
||||
if len(ips) == 0 {
|
||||
err = errNoItems
|
||||
return
|
||||
}
|
||||
|
||||
@@ -317,12 +310,12 @@ func (r *SRegionDNS) queryLocalDnsRecords(req *recordRequest) (recs []msg.Servic
|
||||
if req.IsSRV() {
|
||||
parts := strings.SplitN(ip.Addr, ":", 2)
|
||||
if len(parts) != 2 {
|
||||
err = fmt.Errorf("Invalid SRV records: %q", ip.Addr)
|
||||
ylog.Errorf("Invalid SRV records: %q", ip.Addr)
|
||||
return
|
||||
}
|
||||
port, e := strconv.Atoi(parts[1])
|
||||
if e != nil {
|
||||
err = e
|
||||
ylog.Errorf("Invalid SRV records: %q", ip.Addr)
|
||||
return
|
||||
}
|
||||
s = msg.Service{Host: parts[0], Port: port, TTL: ttl}
|
||||
@@ -334,17 +327,6 @@ func (r *SRegionDNS) queryLocalDnsRecords(req *recordRequest) (recs []msg.Servic
|
||||
return
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) IsCloudNetworkIp(req *recordRequest) bool {
|
||||
if req.network != nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) IsK8sClientReady() bool {
|
||||
return r.K8sClient != nil
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) isMyDomain(req *recordRequest) bool {
|
||||
zones := []string{fmt.Sprintf("%s.", r.PrimaryZone)}
|
||||
zone := plugin.Zones(zones).Matches(req.state.Name())
|
||||
@@ -354,54 +336,65 @@ func (r *SRegionDNS) isMyDomain(req *recordRequest) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) findRecords(req *recordRequest) (recs []msg.Service, err error) {
|
||||
func (r *SRegionDNS) findRecords(req *recordRequest) ([]msg.Service, error) {
|
||||
// 1. try local dns records table
|
||||
recs, err = r.queryLocalDnsRecords(req)
|
||||
if len(recs) != 0 {
|
||||
return
|
||||
rrs := r.queryLocalDnsRecords(req)
|
||||
if len(rrs) > 0 {
|
||||
return rrs, nil
|
||||
}
|
||||
|
||||
isPlainName := req.IsPlainName()
|
||||
isMyDomain := r.isMyDomain(req)
|
||||
|
||||
isCloudIp := r.IsCloudNetworkIp(req)
|
||||
|
||||
// 2. not my domain and src ip not in cloud network table
|
||||
// query from upstream
|
||||
if !isMyDomain && !isCloudIp {
|
||||
err = errNoItems
|
||||
return
|
||||
if isPlainName {
|
||||
isCloudIp := req.SrcInCloud()
|
||||
if isCloudIp {
|
||||
ips := r.findInternalRecordIps(req)
|
||||
if len(ips) > 0 {
|
||||
return ips2DnsRecords(ips), nil
|
||||
} else {
|
||||
return nil, errNotFound
|
||||
}
|
||||
} else {
|
||||
return nil, errRefused
|
||||
}
|
||||
} else if isMyDomain {
|
||||
ips := r.findInternalRecordIps(req)
|
||||
if len(ips) > 0 {
|
||||
return ips2DnsRecords(ips), nil
|
||||
} else {
|
||||
return nil, errNotFound
|
||||
}
|
||||
} else {
|
||||
return nil, errCallNext
|
||||
}
|
||||
|
||||
// 3. internal query
|
||||
ips, err := r.findInternalRecordIps(req)
|
||||
return ips2DnsRecords(ips), err
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) findInternalRecordIps(req *recordRequest) ([]string, error) {
|
||||
// 1. try host table
|
||||
ip := r.getHostIpWithName(req)
|
||||
if len(ip) != 0 {
|
||||
return ip, nil
|
||||
func (r *SRegionDNS) findInternalRecordIps(req *recordRequest) []string {
|
||||
{
|
||||
// 1. try host table
|
||||
ip := r.getHostIpWithName(req)
|
||||
if len(ip) > 0 {
|
||||
return []string{ip}
|
||||
}
|
||||
}
|
||||
// 2. try guest table
|
||||
ip = r.getGuestIpWithName(req)
|
||||
if len(ip) != 0 {
|
||||
return ip, nil
|
||||
{
|
||||
// 2. try guest table
|
||||
ips := r.getGuestIpWithName(req)
|
||||
if len(ips) > 0 {
|
||||
return ips
|
||||
}
|
||||
}
|
||||
|
||||
if !r.IsK8sClientReady() {
|
||||
if r.K8sClient == nil {
|
||||
ylog.Warningf("K8s client not ready, skip it.")
|
||||
return nil, errNoItems
|
||||
return nil
|
||||
}
|
||||
// 3. try k8s service backends
|
||||
ips, err := r.getK8sServiceBackends(req)
|
||||
if len(ips) != 0 {
|
||||
return ips, nil
|
||||
}
|
||||
if err != nil {
|
||||
ylog.Errorf("Get k8s service backends error: %v", err)
|
||||
}
|
||||
return nil, errNoItems
|
||||
return ips
|
||||
}
|
||||
|
||||
func ips2DnsRecords(ips []string) []msg.Service {
|
||||
|
||||
+27
-22
@@ -7,17 +7,16 @@ import (
|
||||
"github.com/coredns/coredns/request"
|
||||
"github.com/miekg/dns"
|
||||
|
||||
"yunion.io/x/pkg/tristate"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
"yunion.io/x/pkg/tristate"
|
||||
)
|
||||
|
||||
type recordRequest struct {
|
||||
state request.Request
|
||||
domainSegs []string
|
||||
guest *models.SGuest
|
||||
host *models.SHost
|
||||
network *models.SNetwork
|
||||
state request.Request
|
||||
domainSegs []string
|
||||
srcProjectId string
|
||||
srcInCloud bool
|
||||
network *models.SNetwork
|
||||
}
|
||||
|
||||
func parseRequest(state request.Request) (r *recordRequest, err error) {
|
||||
@@ -28,9 +27,19 @@ func parseRequest(state request.Request) (r *recordRequest, err error) {
|
||||
domainSegs: segs,
|
||||
}
|
||||
srcIP := r.SrcIP4()
|
||||
r.guest = models.GuestnetworkManager.GetGuestByAddress(srcIP)
|
||||
r.host = models.HostnetworkManager.GetHostByAddress(srcIP)
|
||||
r.network, _ = models.NetworkManager.GetNetworkOfIP(srcIP, "", tristate.None)
|
||||
// NOTE the check on networks_tbl is a hack, we should be more specific
|
||||
// by querying only guest networks, host networks, and others like
|
||||
// loadbalancer network the to come.
|
||||
//
|
||||
// Order matters here, we want to find the srcIP project as accurately
|
||||
// as possible
|
||||
if guest := models.GuestnetworkManager.GetGuestByAddress(srcIP); guest != nil {
|
||||
r.srcProjectId = guest.ProjectId
|
||||
r.srcInCloud = true
|
||||
} else if network, _ := models.NetworkManager.GetNetworkOfIP(srcIP, "", tristate.None); network != nil {
|
||||
r.srcProjectId = network.ProjectId
|
||||
r.srcInCloud = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -41,6 +50,11 @@ func (r recordRequest) Name() string {
|
||||
return name
|
||||
}
|
||||
|
||||
func (r recordRequest) IsPlainName() bool {
|
||||
nl := dns.CountLabel(r.Name())
|
||||
return nl == 1
|
||||
}
|
||||
|
||||
func (r recordRequest) QueryName() string {
|
||||
seps := strings.Split(r.Name(), ".")
|
||||
if len(seps) == 0 {
|
||||
@@ -63,20 +77,11 @@ func (r recordRequest) SrcIP4() string {
|
||||
}
|
||||
|
||||
func (r recordRequest) ProjectId() string {
|
||||
if r.guest != nil {
|
||||
return r.guest.ProjectId
|
||||
}
|
||||
if r.network != nil {
|
||||
return r.network.ProjectId
|
||||
}
|
||||
return ""
|
||||
return r.srcProjectId
|
||||
}
|
||||
|
||||
func (r recordRequest) IsExitOnly() bool {
|
||||
if r.guest == nil {
|
||||
return false
|
||||
}
|
||||
return r.guest.IsExitOnly()
|
||||
func (r recordRequest) SrcInCloud() bool {
|
||||
return r.srcInCloud
|
||||
}
|
||||
|
||||
type K8sQueryInfo struct {
|
||||
|
||||
+2
-10
@@ -8,8 +8,6 @@ import (
|
||||
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"yunion.io/x/log"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
)
|
||||
|
||||
@@ -21,13 +19,7 @@ func (r *SRegionDNS) Reverse(state request.Request, exact bool, opt plugin.Optio
|
||||
return nil, e
|
||||
}
|
||||
records, err := r.getNameForIp(ip, state)
|
||||
if err != nil {
|
||||
log.Errorf("Reverse get name for ip: %v", err)
|
||||
}
|
||||
if len(records) == 0 {
|
||||
return records, errNoItems
|
||||
}
|
||||
return records, nil
|
||||
return records, err
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) getNameForIp(ip string, state request.Request) ([]msg.Service, error) {
|
||||
@@ -53,7 +45,7 @@ func (r *SRegionDNS) getNameForIp(ip string, state request.Request) ([]msg.Servi
|
||||
if guest != nil {
|
||||
return []msg.Service{{Host: r.joinDomain(guest.Name), TTL: defaultTTL}}, nil
|
||||
}
|
||||
return nil, errNoItems
|
||||
return nil, errNotFound
|
||||
}
|
||||
|
||||
func (r *SRegionDNS) joinDomain(name string) string {
|
||||
|
||||
+3
-6
@@ -2,7 +2,6 @@ package dns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
@@ -20,18 +19,16 @@ func init() {
|
||||
}
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
os.Stderr = os.Stdout
|
||||
|
||||
rDNS, err := regionDNSParse(c)
|
||||
if err != nil {
|
||||
return plugin.Error(PluginName, err)
|
||||
}
|
||||
|
||||
if rDNS.PrimaryZone == "" {
|
||||
return fmt.Errorf("dns_domain must provided")
|
||||
if len(rDNS.PrimaryZone) == 0 {
|
||||
return fmt.Errorf("dns_domain missing")
|
||||
}
|
||||
if !regutils.MatchDomainName(rDNS.PrimaryZone) {
|
||||
return fmt.Errorf("dns_domain %q not match domain format", rDNS.PrimaryZone)
|
||||
return fmt.Errorf("dns_domain %q invalid", rDNS.PrimaryZone)
|
||||
}
|
||||
|
||||
err = rDNS.initDB(c)
|
||||
|
||||
@@ -103,7 +103,7 @@ func (c *TokenCacheVerify) Verify(cli *mcclient.Client, adminToken, token string
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Add %s credential to cache: %#v", cred.GetTokenString(), err)
|
||||
}
|
||||
log.Infof("Add token: %s", cred)
|
||||
// log.Debugf("Add token: %s", cred)
|
||||
return cred, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package mcclient
|
||||
|
||||
type IServiceCatalog interface {
|
||||
GetServiceURL(service, region, zone, endpointType string) (string, error)
|
||||
GetServiceURLs(service, region, zone, endpointType string) ([]string, error)
|
||||
}
|
||||
+25
-28
@@ -10,36 +10,16 @@ import (
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
"yunion.io/x/log"
|
||||
)
|
||||
|
||||
/*const (
|
||||
USER_AGENT = "yunioncloud-go/201708"
|
||||
)
|
||||
|
||||
var (
|
||||
red = color.New(color.FgRed, color.Bold).PrintlnFunc()
|
||||
green = color.New(color.FgGreen, color.Bold).PrintlnFunc()
|
||||
yellow = color.New(color.FgYellow, color.Bold).PrintlnFunc()
|
||||
cyan = color.New(color.FgHiCyan, color.Bold).PrintlnFunc()
|
||||
)*/
|
||||
|
||||
/*type JSONClientError struct {
|
||||
Code int
|
||||
Class string
|
||||
Details string
|
||||
}
|
||||
|
||||
func (e *JSONClientError) Error() string {
|
||||
return fmt.Sprintf("JSONClientError: %s %d %s", e.Details, e.Code, e.Class)
|
||||
}*/
|
||||
|
||||
type Client struct {
|
||||
authUrl string
|
||||
timeout int
|
||||
debug bool
|
||||
|
||||
httpconn *http.Client
|
||||
// serviceCatalog *KeystoneServiceCatalog
|
||||
serviceCatalog IServiceCatalog
|
||||
}
|
||||
|
||||
func NewClient(authUrl string, timeout int, debug bool, insecure bool) *Client {
|
||||
@@ -222,7 +202,7 @@ func (this *Client) _authV3(domainName, uname, passwd, projectId, projectName, t
|
||||
if len(tokenId) == 0 {
|
||||
return nil, fmt.Errorf("No X-Subject-Token in header")
|
||||
}
|
||||
ret, err := UnmarshalV3Token(rbody, tokenId)
|
||||
ret, err := this.unmarshalV3Token(rbody, tokenId)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
@@ -245,7 +225,7 @@ func (this *Client) _authV2(uname, passwd, tenantId, tenantName, token string) (
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnmarshalV2Token(rbody)
|
||||
return this.unmarshalV2Token(rbody)
|
||||
}
|
||||
|
||||
func (this *Client) Authenticate(uname, passwd, domainName, tenantName string) (TokenCredential, error) {
|
||||
@@ -255,16 +235,21 @@ func (this *Client) Authenticate(uname, passwd, domainName, tenantName string) (
|
||||
return this._authV2(uname, passwd, "", tenantName, "")
|
||||
}
|
||||
|
||||
func UnmarshalV3Token(rbody jsonutils.JSONObject, tokenId string) (cred TokenCredential, err error) {
|
||||
func (this *Client) unmarshalV3Token(rbody jsonutils.JSONObject, tokenId string) (cred TokenCredential, err error) {
|
||||
cred = &TokenCredentialV3{Id: tokenId}
|
||||
err = rbody.Unmarshal(cred)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Invalid response when unmarshal V3 Token: %v", err)
|
||||
}
|
||||
cata := cred.GetServiceCatalog()
|
||||
if cata == nil {
|
||||
log.Fatalf("No srvice catalog avaiable")
|
||||
}
|
||||
this.serviceCatalog = cata
|
||||
return
|
||||
}
|
||||
|
||||
func UnmarshalV2Token(rbody jsonutils.JSONObject) (cred TokenCredential, err error) {
|
||||
func (this *Client) unmarshalV2Token(rbody jsonutils.JSONObject) (cred TokenCredential, err error) {
|
||||
access, err := rbody.Get("access")
|
||||
if err == nil {
|
||||
cred = &TokenCredentialV2{}
|
||||
@@ -272,6 +257,11 @@ func UnmarshalV2Token(rbody jsonutils.JSONObject) (cred TokenCredential, err err
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Invalid response when unmarshal V2 Token: %s", err)
|
||||
}
|
||||
cata := cred.GetServiceCatalog()
|
||||
if cata == nil {
|
||||
log.Fatalf("No srvice catalog avaiable")
|
||||
}
|
||||
this.serviceCatalog = cata
|
||||
return
|
||||
}
|
||||
err = fmt.Errorf("Invalid response: no access object")
|
||||
@@ -286,7 +276,7 @@ func (this *Client) verifyV3(adminToken, token string) (TokenCredential, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnmarshalV3Token(rbody, token)
|
||||
return this.unmarshalV3Token(rbody, token)
|
||||
}
|
||||
|
||||
func (this *Client) verifyV2(adminToken, token string) (TokenCredential, error) {
|
||||
@@ -297,7 +287,7 @@ func (this *Client) verifyV2(adminToken, token string) (TokenCredential, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnmarshalV2Token(rbody)
|
||||
return this.unmarshalV2Token(rbody)
|
||||
}
|
||||
|
||||
func (this *Client) Verify(adminToken, token string) (cred TokenCredential, err error) {
|
||||
@@ -320,6 +310,13 @@ func (this *Client) SetProject(tenantId, tenantName string, token TokenCredentia
|
||||
}
|
||||
|
||||
func (this *Client) NewSession(region, zone, endpointType string, token TokenCredential, apiVersion string) *ClientSession {
|
||||
cata := token.GetServiceCatalog()
|
||||
if this.serviceCatalog == nil {
|
||||
if cata == nil {
|
||||
log.Fatalf("Missing service catalog in token")
|
||||
}
|
||||
this.serviceCatalog = cata
|
||||
}
|
||||
return &ClientSession{client: this, region: region, zone: zone,
|
||||
endpointType: endpointType, token: token,
|
||||
apiVersion: apiVersion,
|
||||
|
||||
@@ -1,13 +1,56 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/pkg/util/sets"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
func NewManager(keyword, keywordPlural string, columns, adminColumns []string) *modules.ResourceManager {
|
||||
func NewManager(keyword, keywordPlural string, columns, adminColumns *Columns) *modules.ResourceManager {
|
||||
return &modules.ResourceManager{
|
||||
BaseManager: *modules.NewBaseManager("k8s", "", "", columns, adminColumns),
|
||||
BaseManager: *modules.NewBaseManager("k8s", "", "", columns.Array(), adminColumns.Array()),
|
||||
Keyword: keyword,
|
||||
KeywordPlural: keywordPlural,
|
||||
}
|
||||
}
|
||||
|
||||
type Columns struct {
|
||||
cols []string
|
||||
}
|
||||
|
||||
func NewColumns(cols ...string) *Columns {
|
||||
c := &Columns{cols: make([]string, 0)}
|
||||
return c.Add(cols...)
|
||||
}
|
||||
|
||||
func (c *Columns) Add(cols ...string) *Columns {
|
||||
for _, col := range cols {
|
||||
c.add(col)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Columns) add(col string) *Columns {
|
||||
src := sets.NewString(c.Array()...)
|
||||
if src.Has(col) {
|
||||
return c
|
||||
}
|
||||
c.cols = append(c.cols, col)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c Columns) Array() []string {
|
||||
return c.cols
|
||||
}
|
||||
|
||||
func NewNamespaceCols(col ...string) *Columns {
|
||||
return NewColumns("name", "namespace").Add(col...)
|
||||
}
|
||||
|
||||
func NewClusterCols(col ...string) *Columns {
|
||||
return NewColumns("cluster").Add(col...)
|
||||
}
|
||||
|
||||
func NewResourceCols(col ...string) *Columns {
|
||||
return NewColumns("name", "id").Add(col...)
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
Clusters = NewManager("cluster", "clusters",
|
||||
[]string{"id", "name", "mode", "k8s_version", "status", "api_endpoint"},
|
||||
[]string{})
|
||||
Clusters = NewManager("kube_cluster", "kube_clusters",
|
||||
NewResourceCols("mode", "k8s_version", "status", "api_endpoint"),
|
||||
NewColumns())
|
||||
modules.Register(Clusters)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var ConfigMaps *ConfigMapManager
|
||||
|
||||
type ConfigMapManager struct {
|
||||
modules.ResourceManager
|
||||
}
|
||||
|
||||
func init() {
|
||||
ConfigMaps = &ConfigMapManager{
|
||||
ResourceManager: *NewManager(
|
||||
"configmap", "configmaps",
|
||||
NewNamespaceCols(),
|
||||
NewClusterCols())}
|
||||
modules.Register(ConfigMaps)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var Deployments *DeploymentManager
|
||||
|
||||
type DeploymentManager struct {
|
||||
modules.ResourceManager
|
||||
}
|
||||
|
||||
func init() {
|
||||
Deployments = &DeploymentManager{
|
||||
ResourceManager: *NewManager(
|
||||
"deployment", "deployments",
|
||||
NewNamespaceCols("labels"),
|
||||
NewClusterCols())}
|
||||
modules.Register(Deployments)
|
||||
}
|
||||
@@ -10,7 +10,7 @@ var (
|
||||
|
||||
func init() {
|
||||
Logs = NewManager("kube_event", "kube_events",
|
||||
[]string{"id", "ops_time", "obj_id", "obj_type", "obj_name", "user", "user_id", "tenant", "tenant_id", "owner_tenant_id", "action", "notes"},
|
||||
[]string{})
|
||||
NewColumns("id", "ops_time", "obj_id", "obj_type", "obj_name", "user", "user_id", "tenant", "tenant_id", "owner_tenant_id", "action", "notes"),
|
||||
NewColumns())
|
||||
modules.Register(Logs)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var (
|
||||
Repos *modules.ResourceManager
|
||||
)
|
||||
|
||||
func init() {
|
||||
Repos = NewManager("repo", "repos",
|
||||
NewResourceCols("url", "is_public", "source"),
|
||||
NewColumns(),
|
||||
)
|
||||
modules.Register(Repos)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var (
|
||||
Charts *modules.ResourceManager
|
||||
)
|
||||
|
||||
func init() {
|
||||
Charts = NewManager("chart", "charts",
|
||||
NewResourceCols(),
|
||||
NewColumns())
|
||||
modules.Register(Charts)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var (
|
||||
Releases *ReleaseManager
|
||||
)
|
||||
|
||||
type ReleaseManager struct {
|
||||
modules.ResourceManager
|
||||
}
|
||||
|
||||
func init() {
|
||||
Releases = &ReleaseManager{
|
||||
ResourceManager: *NewManager(
|
||||
"release", "releases",
|
||||
NewNamespaceCols(""),
|
||||
NewColumns(),
|
||||
)}
|
||||
modules.Register(Releases)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var (
|
||||
Tiller *TillerManager
|
||||
)
|
||||
|
||||
type TillerManager struct {
|
||||
modules.ResourceManager
|
||||
}
|
||||
|
||||
func init() {
|
||||
Tiller = &TillerManager{
|
||||
ResourceManager: *NewManager(
|
||||
"tiller", "tiller",
|
||||
NewColumns(),
|
||||
NewColumns(),
|
||||
)}
|
||||
modules.Register(Tiller)
|
||||
}
|
||||
@@ -9,6 +9,6 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
Nodes = NewManager("node", "nodes", []string{"id", "name", "cluster", "roles", "address", "status"}, []string{})
|
||||
Nodes = NewManager("kube_node", "kube_nodes", NewResourceCols("cluster", "roles", "address", "status"), NewColumns())
|
||||
modules.Register(Nodes)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ func init() {
|
||||
Pods = &PodManager{
|
||||
ResourceManager: *NewManager(
|
||||
"pod", "pods",
|
||||
[]string{"id", "name", "cluster", "roles", "address", "status"},
|
||||
[]string{})}
|
||||
NewNamespaceCols("podIP", "status", "restartCount", "labels"),
|
||||
NewClusterCols("nodeName"))}
|
||||
modules.Register(Pods)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var RawResource *RawResourceManager
|
||||
|
||||
func init() {
|
||||
RawResource = &RawResourceManager{
|
||||
serviceType: "k8s",
|
||||
}
|
||||
}
|
||||
|
||||
type rawResourceContext struct {
|
||||
kind string
|
||||
name string
|
||||
query jsonutils.JSONObject
|
||||
ctxs []modules.ManagerContext
|
||||
}
|
||||
|
||||
func newRawResourceContext(kind, namespace, name string, query jsonutils.JSONObject, ctxs []modules.ManagerContext) *rawResourceContext {
|
||||
nsQuery := getNamespaceQuery(namespace)
|
||||
if query != nil {
|
||||
nsQuery.Update(query)
|
||||
}
|
||||
ctx := &rawResourceContext{kind: kind, name: name, query: nsQuery, ctxs: ctxs}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (ctx rawResourceContext) contextPath() string {
|
||||
segs := make([]string, 0)
|
||||
ctxs := ctx.ctxs
|
||||
if ctxs != nil && len(ctxs) > 0 {
|
||||
for _, c := range ctxs {
|
||||
segs = append(segs, c.InstanceManager.KeyString())
|
||||
if len(c.InstanceId) > 0 {
|
||||
segs = append(segs, url.PathEscape(c.InstanceId))
|
||||
}
|
||||
}
|
||||
}
|
||||
segs = append(segs, "_raw", ctx.kind, ctx.name)
|
||||
path := fmt.Sprintf("/%s", strings.Join(segs, "/"))
|
||||
if ctx.query != nil {
|
||||
qs := ctx.query.QueryString()
|
||||
if len(qs) > 0 {
|
||||
path = fmt.Sprintf("%s?%s", path, qs)
|
||||
}
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
type RawResourceManager struct {
|
||||
serviceType string
|
||||
}
|
||||
|
||||
func (m *RawResourceManager) request(s *mcclient.ClientSession, method string, path string, body jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
_, ret, err := s.JSONRequest(m.serviceType, "", method, path, nil, body)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func getNamespaceQuery(namespace string) *jsonutils.JSONDict {
|
||||
query := jsonutils.NewDict()
|
||||
if namespace != "" {
|
||||
query.Set("namespace", jsonutils.NewString(namespace))
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func (m *RawResourceManager) Get(s *mcclient.ClientSession, kind string, namespace string, name string, query jsonutils.JSONObject, ctxs []modules.ManagerContext) (jsonutils.JSONObject, error) {
|
||||
ctx := newRawResourceContext(kind, namespace, name, query, ctxs)
|
||||
return m.request(s, "GET", ctx.contextPath(), nil)
|
||||
}
|
||||
|
||||
func (m *RawResourceManager) Put(s *mcclient.ClientSession, kind string, namespace string, name string, body jsonutils.JSONObject, ctxs []modules.ManagerContext) error {
|
||||
rawBytes := body.String()
|
||||
newBody := jsonutils.NewDict()
|
||||
newBody.Add(jsonutils.NewString(rawBytes), "raw")
|
||||
ctx := newRawResourceContext(kind, namespace, name, nil, ctxs)
|
||||
_, err := m.request(s, "PUT", ctx.contextPath(), newBody)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *RawResourceManager) Delete(s *mcclient.ClientSession, kind string, namespace string, name string, query jsonutils.JSONObject, ctxs []modules.ManagerContext) error {
|
||||
ctx := newRawResourceContext(kind, namespace, name, query, ctxs)
|
||||
_, err := m.request(s, "DELETE", ctx.contextPath(), nil)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var Services *ServiceManager
|
||||
|
||||
type ServiceManager struct {
|
||||
modules.ResourceManager
|
||||
}
|
||||
|
||||
func init() {
|
||||
Services = &ServiceManager{
|
||||
ResourceManager: *NewManager(
|
||||
"k8s_service", "k8s_services",
|
||||
NewNamespaceCols("clusterIP", "selector", "internalEndpoint", "externalEndpoints"),
|
||||
NewClusterCols())}
|
||||
modules.Register(Services)
|
||||
}
|
||||
@@ -240,12 +240,7 @@ func (this *DomainManager) DoDomainConfigDelete(s *mcclient.ClientSession, param
|
||||
return ret, httperrors.NewResourceNotFoundError("找不到该认证域")
|
||||
}
|
||||
|
||||
driver, err := detail.GetString("driver")
|
||||
if err != nil {
|
||||
log.Errorf("got driver from domain detail error: %v", err)
|
||||
return ret, httperrors.NewInternalServerError("服务器错误,获取认证协议失败,不允许删除")
|
||||
}
|
||||
|
||||
driver, _ := detail.GetString("driver")
|
||||
if driver != "ldap" {
|
||||
if result, err := UsersV3.List(s, params); err != nil {
|
||||
log.Errorf("user list got error: %v", err)
|
||||
|
||||
@@ -2,10 +2,10 @@ package modules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
@@ -142,6 +142,17 @@ func _getJointKey(mod1 Manager, mod2 Manager) string {
|
||||
return fmt.Sprintf("%s-%s", mod1.KeyString(), mod2.KeyString())
|
||||
}
|
||||
|
||||
func ensureModuleNotRegistered(mod, newMod BaseManagerInterface) {
|
||||
modSvcType := mod.ServiceType()
|
||||
newModSvcType := newMod.ServiceType()
|
||||
if mod == newMod {
|
||||
log.Fatalf("Module %#v duplicate registered, service type: %q", mod, modSvcType)
|
||||
}
|
||||
if modSvcType != newModSvcType {
|
||||
log.Fatalf("Module %#v already registered, service type is %q.\nSo new module %#v can't be registered, service type is %q", mod, modSvcType, newMod, newModSvcType)
|
||||
}
|
||||
}
|
||||
|
||||
func _register(version string, mod BaseManagerInterface) {
|
||||
if modules == nil {
|
||||
modules = make(map[string]map[string][]BaseManagerInterface)
|
||||
@@ -156,9 +167,7 @@ func _register(version string, mod BaseManagerInterface) {
|
||||
mods = make([]BaseManagerInterface, 0)
|
||||
}
|
||||
for _, m := range mods {
|
||||
if m == mod {
|
||||
log.Fatal("Module already registered", mod)
|
||||
}
|
||||
ensureModuleNotRegistered(m, mod)
|
||||
}
|
||||
modtable[mod.KeyString()] = append(mods, mod)
|
||||
}
|
||||
@@ -179,7 +188,7 @@ func _registerJointModule(version string, mod BaseManagerInterface) {
|
||||
}
|
||||
for _, m := range jointMods {
|
||||
if m == jointMod {
|
||||
log.Fatal("Module already registered", jointMod)
|
||||
ensureModuleNotRegistered(m, jointMod)
|
||||
}
|
||||
}
|
||||
modtable[jointKey] = append(jointMods, jointMod)
|
||||
|
||||
+20
-1
@@ -75,7 +75,26 @@ func (this *ClientSession) GetServiceURL(service, endpointType string) (string,
|
||||
if len(this.apiVersion) > 0 && this.apiVersion != DEFAULT_API_VERSION {
|
||||
service = fmt.Sprintf("%s_%s", service, this.apiVersion)
|
||||
}
|
||||
return this.token.GetServiceURL(service, this.region, this.zone, endpointType)
|
||||
url, err := this.token.GetServiceURL(service, this.region, this.zone, endpointType)
|
||||
if err != nil {
|
||||
url, err = this.client.serviceCatalog.GetServiceURL(service, this.region, this.zone, endpointType)
|
||||
}
|
||||
return url, err
|
||||
}
|
||||
|
||||
func (this *ClientSession) GetServiceURLs(service, endpointType string) ([]string, error) {
|
||||
if len(this.endpointType) > 0 {
|
||||
// session specific endpoint type should override the input endpointType, which is supplied by manager
|
||||
endpointType = this.endpointType
|
||||
}
|
||||
if len(this.apiVersion) > 0 && this.apiVersion != DEFAULT_API_VERSION {
|
||||
service = fmt.Sprintf("%s_%s", service, this.apiVersion)
|
||||
}
|
||||
urls, err := this.token.GetServiceURLs(service, this.region, this.zone, endpointType)
|
||||
if err != nil {
|
||||
urls, err = this.client.serviceCatalog.GetServiceURLs(service, this.region, this.zone, endpointType)
|
||||
}
|
||||
return urls, err
|
||||
}
|
||||
|
||||
func (this *ClientSession) getBaseUrl(service, endpointType string) (string, error) {
|
||||
|
||||
@@ -23,6 +23,8 @@ type Endpoint struct {
|
||||
type TokenCredential interface {
|
||||
gotypes.ISerializable
|
||||
|
||||
IServiceCatalog
|
||||
|
||||
GetTokenString() string
|
||||
GetDomainId() string
|
||||
GetDomainName() string
|
||||
@@ -39,7 +41,9 @@ type TokenCredential interface {
|
||||
IsAdmin() bool
|
||||
IsSystemAdmin() bool
|
||||
GetRegions() []string
|
||||
GetServiceURL(service, region, zone, endpointType string) (string, error)
|
||||
|
||||
GetServiceCatalog() IServiceCatalog
|
||||
|
||||
GetInternalServices(region string) []string
|
||||
GetExternalServices(region string) []ExternalService
|
||||
GetEndpoints(region string, endpointType string) []Endpoint
|
||||
|
||||
+18
-2
@@ -133,7 +133,11 @@ func (this *TokenCredentialV2) IsSystemAdmin() bool {
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
return this.ServiceCatalog.getServiceURL(service, region, zone, endpointType)
|
||||
return this.ServiceCatalog.GetServiceURL(service, region, zone, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) {
|
||||
return this.ServiceCatalog.GetServiceURLs(service, region, zone, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetInternalServices(region string) []string {
|
||||
@@ -148,6 +152,10 @@ func (this *TokenCredentialV2) GetEndpoints(region string, endpointType string)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetServiceCatalog() IServiceCatalog {
|
||||
return this.ServiceCatalog
|
||||
}
|
||||
|
||||
func stringArrayContains(arr []string, needle string) bool {
|
||||
for i := 0; i < len(arr); i++ {
|
||||
if arr[i] == needle {
|
||||
@@ -211,7 +219,7 @@ func (catalog KeystoneServiceCatalogV2) getServiceEndpoint(service, region, zone
|
||||
return selected, fmt.Errorf("No such service %s", service)
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV2) getServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
func (catalog KeystoneServiceCatalogV2) GetServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
ep, err := catalog.getServiceEndpoint(service, region, zone)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -219,6 +227,14 @@ func (catalog KeystoneServiceCatalogV2) getServiceURL(service, region, zone, end
|
||||
return ep.getURL(endpointType), nil
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV2) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) {
|
||||
url, err := catalog.GetServiceURL(service, region, zone, endpointType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []string{url}, nil
|
||||
}
|
||||
|
||||
func (ep KeystoneEndpointV2) getURL(epType string) string {
|
||||
switch epType {
|
||||
case "publicURL":
|
||||
|
||||
+22
-6
@@ -141,7 +141,11 @@ func (this *TokenCredentialV3) GetRegions() []string {
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
return this.Token.Catalog.getServiceURL(service, region, zone, endpointType)
|
||||
return this.Token.Catalog.GetServiceURL(service, region, zone, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) {
|
||||
return this.Token.Catalog.GetServiceURLs(service, region, zone, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetInternalServices(region string) []string {
|
||||
@@ -156,6 +160,10 @@ func (this *TokenCredentialV3) GetEndpoints(region string, endpointType string)
|
||||
return this.Token.Catalog.getEndpoints(region, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetServiceCatalog() IServiceCatalog {
|
||||
return this.Token.Catalog
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV3) getInternalServices(region string) []string {
|
||||
services := make([]string, 0)
|
||||
for i := 0; i < len(catalog); i++ {
|
||||
@@ -242,7 +250,15 @@ func Id2RegionZone(id string) (string, string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV3) getServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
func (catalog KeystoneServiceCatalogV3) GetServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
urls, err := catalog.GetServiceURLs(service, region, zone, endpointType)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return urls[rand.Intn(len(urls))], nil
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV3) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) {
|
||||
if endpointType == "" {
|
||||
endpointType = "internalURL"
|
||||
}
|
||||
@@ -273,7 +289,7 @@ func (catalog KeystoneServiceCatalogV3) getServiceURL(service, region, zone, end
|
||||
break
|
||||
}
|
||||
} else {
|
||||
return "", fmt.Errorf("No default region")
|
||||
return nil, fmt.Errorf("No default region")
|
||||
}
|
||||
} else {
|
||||
_, ok := regeps[regionzone]
|
||||
@@ -282,14 +298,14 @@ func (catalog KeystoneServiceCatalogV3) getServiceURL(service, region, zone, end
|
||||
} else {
|
||||
selected, ok = regeps[region]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("No valid %s endpoints for %s in region %s", endpointType, service, RegionID(region, zone))
|
||||
return nil, fmt.Errorf("No valid %s endpoints for %s in region %s", endpointType, service, RegionID(region, zone))
|
||||
}
|
||||
}
|
||||
}
|
||||
return selected[rand.Intn(len(selected))], nil
|
||||
return selected, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("No such service %s", service)
|
||||
return nil, fmt.Errorf("No such service %s", service)
|
||||
}
|
||||
|
||||
func (self *TokenCredentialV3) String() string {
|
||||
|
||||
@@ -96,6 +96,10 @@ func (self *SSimpleToken) GetServiceURL(service, region, zone, endpointType stri
|
||||
return "", fmt.Errorf("Not available")
|
||||
}
|
||||
|
||||
func (self *SSimpleToken) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) {
|
||||
return nil, fmt.Errorf("Not available")
|
||||
}
|
||||
|
||||
func (self *SSimpleToken) GetInternalServices(region string) []string {
|
||||
return nil
|
||||
}
|
||||
@@ -108,6 +112,10 @@ func (this *SSimpleToken) GetEndpoints(region string, endpointType string) []End
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *SSimpleToken) GetServiceCatalog() IServiceCatalog {
|
||||
return nil
|
||||
}
|
||||
|
||||
func SimplifyToken(token TokenCredential) TokenCredential {
|
||||
simToken, ok := token.(*SSimpleToken)
|
||||
if ok {
|
||||
|
||||
@@ -551,7 +551,15 @@ func newBaremetalDiskConfigFromSimpleJson(sjson *simplejson.Json) (*BaremetalDis
|
||||
rangeArray := make([]int64, 0)
|
||||
ranges := sjson.Get("range").MustArray()
|
||||
for _, size := range ranges {
|
||||
rangeArray = append(rangeArray, size.(int64))
|
||||
sizeNum, ok := size.(json.Number)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Invalid range size, not json.Number: %v", size)
|
||||
}
|
||||
rangeSize, err := sizeNum.Int64()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rangeArray = append(rangeArray, rangeSize)
|
||||
}
|
||||
baremetalDiskConfig.Range = rangeArray
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package aliyun
|
||||
import (
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package aliyun
|
||||
|
||||
import (
|
||||
aliyunerrors "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
|
||||
)
|
||||
|
||||
func isError(err error, code string) bool {
|
||||
aliyunErr, ok := err.(aliyunerrors.Error)
|
||||
if ! ok {
|
||||
return false
|
||||
}
|
||||
return aliyunErr.ErrorCode() == code
|
||||
}
|
||||
+43
-21
@@ -340,14 +340,6 @@ func (self *SInstance) StopVM(isForce bool) error {
|
||||
return cloudprovider.WaitStatus(self, models.VM_READY, 10*time.Second, 300*time.Second) // 5mintues
|
||||
}
|
||||
|
||||
func (self *SInstance) DeleteVM() error {
|
||||
err := self.host.zone.region.DeleteVM(self.InstanceId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return cloudprovider.WaitDeleted(self, 10*time.Second, 300*time.Second) // 5minutes
|
||||
}
|
||||
|
||||
func (self *SInstance) GetVNCInfo() (jsonutils.JSONObject, error) {
|
||||
url, err := self.host.zone.region.GetInstanceVNCUrl(self.InstanceId)
|
||||
if err != nil {
|
||||
@@ -479,7 +471,10 @@ func (self *SRegion) doStopVM(instanceId string, isForce bool) error {
|
||||
}
|
||||
|
||||
func (self *SRegion) doDeleteVM(instanceId string) error {
|
||||
return self.instanceOperation(instanceId, "DeleteInstance", nil)
|
||||
params := make(map[string]string)
|
||||
params["TerminateSubscription"] = "false"
|
||||
params["Force"] = "true"
|
||||
return self.instanceOperation(instanceId, "DeleteInstance", params)
|
||||
}
|
||||
|
||||
/*func (self *SRegion) waitInstanceStatus(instanceId string, target string, interval time.Duration, timeout time.Duration) error {
|
||||
@@ -502,8 +497,13 @@ func (self *SInstance) waitStatus(target string, interval time.Duration, timeout
|
||||
}*/
|
||||
|
||||
func (self *SRegion) StartVM(instanceId string) error {
|
||||
status, _ := self.GetInstanceStatus(instanceId)
|
||||
status, err := self.GetInstanceStatus(instanceId)
|
||||
if err != nil {
|
||||
log.Errorf("Fail to get instance status on StartVM: %s", err)
|
||||
return err
|
||||
}
|
||||
if status != InstanceStatusStopped {
|
||||
log.Errorf("StartVM: vm status is %s expect %s", status, InstanceStatusStopped)
|
||||
return cloudprovider.ErrInvalidStatus
|
||||
}
|
||||
return self.doStartVM(instanceId)
|
||||
@@ -514,8 +514,13 @@ func (self *SRegion) StartVM(instanceId string) error {
|
||||
}
|
||||
|
||||
func (self *SRegion) StopVM(instanceId string, isForce bool) error {
|
||||
status, _ := self.GetInstanceStatus(instanceId)
|
||||
status, err := self.GetInstanceStatus(instanceId)
|
||||
if err != nil {
|
||||
log.Errorf("Fail to get instance status on StopVM: %s", err)
|
||||
return err
|
||||
}
|
||||
if status != InstanceStatusRunning {
|
||||
log.Errorf("StopVM: vm status is %s expect %s", status, InstanceStatusRunning)
|
||||
return cloudprovider.ErrInvalidStatus
|
||||
}
|
||||
return self.doStopVM(instanceId, isForce)
|
||||
@@ -527,13 +532,13 @@ func (self *SRegion) StopVM(instanceId string, isForce bool) error {
|
||||
|
||||
func (self *SRegion) DeleteVM(instanceId string) error {
|
||||
status, err := self.GetInstanceStatus(instanceId)
|
||||
if status == InstanceStatusRunning {
|
||||
err = self.StopVM(instanceId, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if status != InstanceStatusStopped {
|
||||
return cloudprovider.ErrInvalidStatus
|
||||
if err != nil {
|
||||
log.Errorf("Fail to get instance status on DeleteVM: %s", err)
|
||||
return err
|
||||
}
|
||||
log.Debugf("Instance status on delete is %s", status)
|
||||
if status != InstanceStatusStopped {
|
||||
log.Warningf("DeleteVM: vm status is %s expect %s", status, InstanceStatusStopped)
|
||||
}
|
||||
return self.doDeleteVM(instanceId)
|
||||
// if err != nil {
|
||||
@@ -589,6 +594,23 @@ func (self *SRegion) DeployVM(instanceId string, name string, password string, k
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SInstance) DeleteVM() error {
|
||||
for {
|
||||
err := self.host.zone.region.DeleteVM(self.InstanceId)
|
||||
if err != nil {
|
||||
if isError(err, "IncorrectInstanceStatus.Initializing") {
|
||||
log.Infof("The instance is initializing, try later ...")
|
||||
time.Sleep(10 * time.Second)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return cloudprovider.WaitDeleted(self, 10*time.Second, 300*time.Second) // 5minutes
|
||||
}
|
||||
|
||||
func (self *SRegion) UpdateVM(instanceId string, hostname string) error {
|
||||
/*
|
||||
api: ModifyInstanceAttribute
|
||||
@@ -610,7 +632,7 @@ func (self *SRegion) ReplaceSystemDisk(instanceId string, image string) error {
|
||||
}
|
||||
|
||||
func (self *SRegion) ChangeVMConfig(zoneId string, instanceId string, ncpu int, vmem int, disks []*SDisk) error {
|
||||
// todo: support change disk config?
|
||||
// todo: support change disk config?
|
||||
params := make(map[string]string)
|
||||
instanceTypes, e := self.GetMatchInstanceTypes(ncpu, vmem, 0, zoneId)
|
||||
if e != nil {
|
||||
@@ -630,7 +652,7 @@ func (self *SRegion) ChangeVMConfig(zoneId string, instanceId string, ncpu int,
|
||||
return fmt.Errorf("Failed to change vm config, specification not supported")
|
||||
}
|
||||
|
||||
func (self *SRegion) AttachDisk(instanceId string, diskId string) error {
|
||||
func (self *SRegion) AttachDisk(instanceId string, diskId string) error {
|
||||
params := make(map[string]string)
|
||||
params["InstanceId"] = instanceId
|
||||
params["DiskId"] = diskId
|
||||
@@ -667,4 +689,4 @@ func (self *SInstance) SyncSecurityGroup(secgroupId string, name string, rules [
|
||||
self.SecurityGroupIds.SecurityGroupId = []string{secgrpId}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -574,3 +574,15 @@ func (self *SRegion) leaveSecurityGroup(secgroupId, instanceId string) error {
|
||||
_, err := self.ecsRequest("LeaveSecurityGroup", params)
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *SRegion) deleteSecurityGroup(secGrpId string) error {
|
||||
params := make(map[string]string)
|
||||
params["SecurityGroupId"] = secGrpId
|
||||
|
||||
_, err := self.ecsRequest("DeleteSecurityGroup", params)
|
||||
if err != nil {
|
||||
log.Errorf("Delete security group fail %s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -184,6 +184,19 @@ func (self *SVpc) GetManagerId() string {
|
||||
}
|
||||
|
||||
func (self *SVpc) Delete() error {
|
||||
err := self.fetchSecurityGroups()
|
||||
if err != nil {
|
||||
log.Errorf("fetchSecurityGroup for VPC delete fail %s", err)
|
||||
return err
|
||||
}
|
||||
for i := 0; i < len(self.secgroups); i += 1 {
|
||||
secgroup := self.secgroups[i].(*SSecurityGroup)
|
||||
err := self.region.deleteSecurityGroup(secgroup.SecurityGroupId)
|
||||
if err != nil {
|
||||
log.Errorf("deleteSecurityGroup for VPC delete fail %s", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return self.region.DeleteVpc(self.VpcId)
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ func PrintJSONList(list *modules.ListResult, columns []string) {
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
fmt.Println(pt.GetString(rows))
|
||||
fmt.Print(pt.GetString(rows))
|
||||
title := fmt.Sprintf("Total: %d", list.Total)
|
||||
if list.Limit == 0 && list.Total > len(list.Data) {
|
||||
list.Limit = len(list.Data)
|
||||
@@ -95,7 +95,7 @@ func PrintJSONObject(obj jsonutils.JSONObject) {
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
fmt.Println(pt.GetString(rows))
|
||||
fmt.Print(pt.GetString(rows))
|
||||
}
|
||||
|
||||
func PrintJSONBatchResults(results []modules.SubmitResult, columns []string) {
|
||||
|
||||
Generated
Vendored
-110
@@ -1,110 +0,0 @@
|
||||
package push
|
||||
|
||||
//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.
|
||||
//
|
||||
// Code generated by Alibaba Cloud SDK Code Generator.
|
||||
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
|
||||
|
||||
import (
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
|
||||
)
|
||||
|
||||
// PushNoticeToAndroid invokes the push.PushNoticeToAndroid API synchronously
|
||||
// api document: https://help.aliyun.com/api/push/pushnoticetoandroid.html
|
||||
func (client *Client) PushNoticeToAndroid(request *PushNoticeToAndroidRequest) (response *PushNoticeToAndroidResponse, err error) {
|
||||
response = CreatePushNoticeToAndroidResponse()
|
||||
err = client.DoAction(request, response)
|
||||
return
|
||||
}
|
||||
|
||||
// PushNoticeToAndroidWithChan invokes the push.PushNoticeToAndroid API asynchronously
|
||||
// api document: https://help.aliyun.com/api/push/pushnoticetoandroid.html
|
||||
// asynchronous document: https://help.aliyun.com/document_detail/66220.html
|
||||
func (client *Client) PushNoticeToAndroidWithChan(request *PushNoticeToAndroidRequest) (<-chan *PushNoticeToAndroidResponse, <-chan error) {
|
||||
responseChan := make(chan *PushNoticeToAndroidResponse, 1)
|
||||
errChan := make(chan error, 1)
|
||||
err := client.AddAsyncTask(func() {
|
||||
defer close(responseChan)
|
||||
defer close(errChan)
|
||||
response, err := client.PushNoticeToAndroid(request)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
} else {
|
||||
responseChan <- response
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
close(responseChan)
|
||||
close(errChan)
|
||||
}
|
||||
return responseChan, errChan
|
||||
}
|
||||
|
||||
// PushNoticeToAndroidWithCallback invokes the push.PushNoticeToAndroid API asynchronously
|
||||
// api document: https://help.aliyun.com/api/push/pushnoticetoandroid.html
|
||||
// asynchronous document: https://help.aliyun.com/document_detail/66220.html
|
||||
func (client *Client) PushNoticeToAndroidWithCallback(request *PushNoticeToAndroidRequest, callback func(response *PushNoticeToAndroidResponse, err error)) <-chan int {
|
||||
result := make(chan int, 1)
|
||||
err := client.AddAsyncTask(func() {
|
||||
var response *PushNoticeToAndroidResponse
|
||||
var err error
|
||||
defer close(result)
|
||||
response, err = client.PushNoticeToAndroid(request)
|
||||
callback(response, err)
|
||||
result <- 1
|
||||
})
|
||||
if err != nil {
|
||||
defer close(result)
|
||||
callback(nil, err)
|
||||
result <- 0
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// PushNoticeToAndroidRequest is the request struct for api PushNoticeToAndroid
|
||||
type PushNoticeToAndroidRequest struct {
|
||||
*requests.RpcRequest
|
||||
AppKey requests.Integer `position:"Query" name:"AppKey"`
|
||||
Target string `position:"Query" name:"Target"`
|
||||
TargetValue string `position:"Query" name:"TargetValue"`
|
||||
Title string `position:"Query" name:"Title"`
|
||||
Body string `position:"Query" name:"Body"`
|
||||
JobKey string `position:"Query" name:"JobKey"`
|
||||
ExtParameters string `position:"Query" name:"ExtParameters"`
|
||||
}
|
||||
|
||||
// PushNoticeToAndroidResponse is the response struct for api PushNoticeToAndroid
|
||||
type PushNoticeToAndroidResponse struct {
|
||||
*responses.BaseResponse
|
||||
RequestId string `json:"RequestId" xml:"RequestId"`
|
||||
MessageId string `json:"MessageId" xml:"MessageId"`
|
||||
}
|
||||
|
||||
// CreatePushNoticeToAndroidRequest creates a request to invoke PushNoticeToAndroid API
|
||||
func CreatePushNoticeToAndroidRequest() (request *PushNoticeToAndroidRequest) {
|
||||
request = &PushNoticeToAndroidRequest{
|
||||
RpcRequest: &requests.RpcRequest{},
|
||||
}
|
||||
request.InitWithApiInfo("Push", "2016-08-01", "PushNoticeToAndroid", "", "")
|
||||
return
|
||||
}
|
||||
|
||||
// CreatePushNoticeToAndroidResponse creates a response to parse from PushNoticeToAndroid response
|
||||
func CreatePushNoticeToAndroidResponse() (response *PushNoticeToAndroidResponse) {
|
||||
response = &PushNoticeToAndroidResponse{
|
||||
BaseResponse: &responses.BaseResponse{},
|
||||
}
|
||||
return
|
||||
}
|
||||
Generated
Vendored
-111
@@ -1,111 +0,0 @@
|
||||
package push
|
||||
|
||||
//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.
|
||||
//
|
||||
// Code generated by Alibaba Cloud SDK Code Generator.
|
||||
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
|
||||
|
||||
import (
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
|
||||
)
|
||||
|
||||
// PushNoticeToiOS invokes the push.PushNoticeToiOS API synchronously
|
||||
// api document: https://help.aliyun.com/api/push/pushnoticetoios.html
|
||||
func (client *Client) PushNoticeToiOS(request *PushNoticeToiOSRequest) (response *PushNoticeToiOSResponse, err error) {
|
||||
response = CreatePushNoticeToiOSResponse()
|
||||
err = client.DoAction(request, response)
|
||||
return
|
||||
}
|
||||
|
||||
// PushNoticeToiOSWithChan invokes the push.PushNoticeToiOS API asynchronously
|
||||
// api document: https://help.aliyun.com/api/push/pushnoticetoios.html
|
||||
// asynchronous document: https://help.aliyun.com/document_detail/66220.html
|
||||
func (client *Client) PushNoticeToiOSWithChan(request *PushNoticeToiOSRequest) (<-chan *PushNoticeToiOSResponse, <-chan error) {
|
||||
responseChan := make(chan *PushNoticeToiOSResponse, 1)
|
||||
errChan := make(chan error, 1)
|
||||
err := client.AddAsyncTask(func() {
|
||||
defer close(responseChan)
|
||||
defer close(errChan)
|
||||
response, err := client.PushNoticeToiOS(request)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
} else {
|
||||
responseChan <- response
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
close(responseChan)
|
||||
close(errChan)
|
||||
}
|
||||
return responseChan, errChan
|
||||
}
|
||||
|
||||
// PushNoticeToiOSWithCallback invokes the push.PushNoticeToiOS API asynchronously
|
||||
// api document: https://help.aliyun.com/api/push/pushnoticetoios.html
|
||||
// asynchronous document: https://help.aliyun.com/document_detail/66220.html
|
||||
func (client *Client) PushNoticeToiOSWithCallback(request *PushNoticeToiOSRequest, callback func(response *PushNoticeToiOSResponse, err error)) <-chan int {
|
||||
result := make(chan int, 1)
|
||||
err := client.AddAsyncTask(func() {
|
||||
var response *PushNoticeToiOSResponse
|
||||
var err error
|
||||
defer close(result)
|
||||
response, err = client.PushNoticeToiOS(request)
|
||||
callback(response, err)
|
||||
result <- 1
|
||||
})
|
||||
if err != nil {
|
||||
defer close(result)
|
||||
callback(nil, err)
|
||||
result <- 0
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// PushNoticeToiOSRequest is the request struct for api PushNoticeToiOS
|
||||
type PushNoticeToiOSRequest struct {
|
||||
*requests.RpcRequest
|
||||
AppKey requests.Integer `position:"Query" name:"AppKey"`
|
||||
Target string `position:"Query" name:"Target"`
|
||||
TargetValue string `position:"Query" name:"TargetValue"`
|
||||
ApnsEnv string `position:"Query" name:"ApnsEnv"`
|
||||
Title string `position:"Query" name:"Title"`
|
||||
Body string `position:"Query" name:"Body"`
|
||||
JobKey string `position:"Query" name:"JobKey"`
|
||||
ExtParameters string `position:"Query" name:"ExtParameters"`
|
||||
}
|
||||
|
||||
// PushNoticeToiOSResponse is the response struct for api PushNoticeToiOS
|
||||
type PushNoticeToiOSResponse struct {
|
||||
*responses.BaseResponse
|
||||
RequestId string `json:"RequestId" xml:"RequestId"`
|
||||
MessageId string `json:"MessageId" xml:"MessageId"`
|
||||
}
|
||||
|
||||
// CreatePushNoticeToiOSRequest creates a request to invoke PushNoticeToiOS API
|
||||
func CreatePushNoticeToiOSRequest() (request *PushNoticeToiOSRequest) {
|
||||
request = &PushNoticeToiOSRequest{
|
||||
RpcRequest: &requests.RpcRequest{},
|
||||
}
|
||||
request.InitWithApiInfo("Push", "2016-08-01", "PushNoticeToiOS", "", "")
|
||||
return
|
||||
}
|
||||
|
||||
// CreatePushNoticeToiOSResponse creates a response to parse from PushNoticeToiOS response
|
||||
func CreatePushNoticeToiOSResponse() (response *PushNoticeToiOSResponse) {
|
||||
response = &PushNoticeToiOSResponse{
|
||||
BaseResponse: &responses.BaseResponse{},
|
||||
}
|
||||
return
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
language: go
|
||||
sudo: false
|
||||
install:
|
||||
- go get ./...
|
||||
go:
|
||||
- 1.4
|
||||
- 1.5.2
|
||||
- tip
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
MIT License
|
||||
===========
|
||||
|
||||
Copyright (c) 2015, Greg Osuri
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
test:
|
||||
@go test ./...
|
||||
|
||||
.PHONY: test
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
# uitable [](https://godoc.org/github.com/gosuri/uitable) [](https://travis-ci.org/gosuri/uitable)
|
||||
|
||||
uitable is a go library for representing data as tables for terminal applications. It provides primitives for sizing and wrapping columns to improve readability.
|
||||
|
||||
## Example Usage
|
||||
|
||||
Full source code for the example is available at [example/main.go](example/main.go)
|
||||
|
||||
```go
|
||||
table := uitable.New()
|
||||
table.MaxColWidth = 50
|
||||
|
||||
table.AddRow("NAME", "BIRTHDAY", "BIO")
|
||||
for _, hacker := range hackers {
|
||||
table.AddRow(hacker.Name, hacker.Birthday, hacker.Bio)
|
||||
}
|
||||
fmt.Println(table)
|
||||
```
|
||||
|
||||
Will render the data as:
|
||||
|
||||
```sh
|
||||
NAME BIRTHDAY BIO
|
||||
Ada Lovelace December 10, 1815 Ada was a British mathematician and writer, chi...
|
||||
Alan Turing June 23, 1912 Alan was a British pioneering computer scientis...
|
||||
```
|
||||
|
||||
For wrapping in two columns:
|
||||
|
||||
```go
|
||||
table = uitable.New()
|
||||
table.MaxColWidth = 80
|
||||
table.Wrap = true // wrap columns
|
||||
|
||||
for _, hacker := range hackers {
|
||||
table.AddRow("Name:", hacker.Name)
|
||||
table.AddRow("Birthday:", hacker.Birthday)
|
||||
table.AddRow("Bio:", hacker.Bio)
|
||||
table.AddRow("") // blank
|
||||
}
|
||||
fmt.Println(table)
|
||||
```
|
||||
|
||||
Will render the data as:
|
||||
|
||||
```
|
||||
Name: Ada Lovelace
|
||||
Birthday: December 10, 1815
|
||||
Bio: Ada was a British mathematician and writer, chiefly known for her work on
|
||||
Charles Babbage's early mechanical general-purpose computer, the Analytical
|
||||
Engine
|
||||
|
||||
Name: Alan Turing
|
||||
Birthday: June 23, 1912
|
||||
Bio: Alan was a British pioneering computer scientist, mathematician, logician,
|
||||
cryptanalyst and theoretical biologist
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
$ go get -v github.com/gosuri/uitable
|
||||
```
|
||||
|
||||
|
||||
[](https://bitdeli.com/free "Bitdeli Badge")
|
||||
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
// Package uitable provides a decorator for formating data as a table
|
||||
package uitable
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/gosuri/uitable/util/strutil"
|
||||
"github.com/gosuri/uitable/util/wordwrap"
|
||||
"github.com/mattn/go-runewidth"
|
||||
)
|
||||
|
||||
// Separator is the default column seperator
|
||||
var Separator = "\t"
|
||||
|
||||
// Table represents a decorator that renders the data in formatted in a table
|
||||
type Table struct {
|
||||
// Rows is the collection of rows in the table
|
||||
Rows []*Row
|
||||
|
||||
// MaxColWidth is the maximum allowed width for cells in the table
|
||||
MaxColWidth uint
|
||||
|
||||
// Wrap when set to true wraps the contents of the columns when the length exceeds the MaxColWidth
|
||||
Wrap bool
|
||||
|
||||
// Separator is the seperator for columns in the table. Default is "\t"
|
||||
Separator string
|
||||
|
||||
mtx *sync.RWMutex
|
||||
rightAlign map[int]bool
|
||||
}
|
||||
|
||||
// New returns a new Table with default values
|
||||
func New() *Table {
|
||||
return &Table{
|
||||
Separator: Separator,
|
||||
mtx: new(sync.RWMutex),
|
||||
rightAlign: map[int]bool{},
|
||||
}
|
||||
}
|
||||
|
||||
// AddRow adds a new row to the table
|
||||
func (t *Table) AddRow(data ...interface{}) *Table {
|
||||
t.mtx.Lock()
|
||||
defer t.mtx.Unlock()
|
||||
r := NewRow(data...)
|
||||
t.Rows = append(t.Rows, r)
|
||||
return t
|
||||
}
|
||||
|
||||
// Bytes returns the []byte value of table
|
||||
func (t *Table) Bytes() []byte {
|
||||
return []byte(t.String())
|
||||
}
|
||||
|
||||
func (t *Table) RightAlign(col int) {
|
||||
t.mtx.Lock()
|
||||
t.rightAlign[col] = true
|
||||
t.mtx.Unlock()
|
||||
}
|
||||
|
||||
// String returns the string value of table
|
||||
func (t *Table) String() string {
|
||||
t.mtx.RLock()
|
||||
defer t.mtx.RUnlock()
|
||||
|
||||
if len(t.Rows) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// determine the width for each column (cell in a row)
|
||||
var colwidths []uint
|
||||
for _, row := range t.Rows {
|
||||
for i, cell := range row.Cells {
|
||||
// resize colwidth array
|
||||
if i+1 > len(colwidths) {
|
||||
colwidths = append(colwidths, 0)
|
||||
}
|
||||
cellwidth := cell.LineWidth()
|
||||
if t.MaxColWidth != 0 && cellwidth > t.MaxColWidth {
|
||||
cellwidth = t.MaxColWidth
|
||||
}
|
||||
|
||||
if cellwidth > colwidths[i] {
|
||||
colwidths[i] = cellwidth
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var lines []string
|
||||
for _, row := range t.Rows {
|
||||
row.Separator = t.Separator
|
||||
for i, cell := range row.Cells {
|
||||
cell.Width = colwidths[i]
|
||||
cell.Wrap = t.Wrap
|
||||
cell.RightAlign = t.rightAlign[i]
|
||||
}
|
||||
lines = append(lines, row.String())
|
||||
}
|
||||
return strutil.Join(lines, "\n")
|
||||
}
|
||||
|
||||
// Row represents a row in a table
|
||||
type Row struct {
|
||||
// Cells is the group of cell for the row
|
||||
Cells []*Cell
|
||||
|
||||
// Separator for tabular columns
|
||||
Separator string
|
||||
}
|
||||
|
||||
// NewRow returns a new Row and adds the data to the row
|
||||
func NewRow(data ...interface{}) *Row {
|
||||
r := &Row{Cells: make([]*Cell, len(data))}
|
||||
for i, d := range data {
|
||||
r.Cells[i] = &Cell{Data: d}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// String returns the string representation of the row
|
||||
func (r *Row) String() string {
|
||||
// get the max number of lines for each cell
|
||||
var lc int // line count
|
||||
for _, cell := range r.Cells {
|
||||
if clc := len(strings.Split(cell.String(), "\n")); clc > lc {
|
||||
lc = clc
|
||||
}
|
||||
}
|
||||
|
||||
// allocate a two-dimentional array of cells for each line and add size them
|
||||
cells := make([][]*Cell, lc)
|
||||
for x := 0; x < lc; x++ {
|
||||
cells[x] = make([]*Cell, len(r.Cells))
|
||||
for y := 0; y < len(r.Cells); y++ {
|
||||
cells[x][y] = &Cell{Width: r.Cells[y].Width}
|
||||
}
|
||||
}
|
||||
|
||||
// insert each line in a cell as new cell in the cells array
|
||||
for y, cell := range r.Cells {
|
||||
lines := strings.Split(cell.String(), "\n")
|
||||
for x, line := range lines {
|
||||
cells[x][y].Data = line
|
||||
}
|
||||
}
|
||||
|
||||
// format each line
|
||||
lines := make([]string, lc)
|
||||
for x := range lines {
|
||||
line := make([]string, len(cells[x]))
|
||||
for y := range cells[x] {
|
||||
line[y] = cells[x][y].String()
|
||||
}
|
||||
lines[x] = strutil.Join(line, r.Separator)
|
||||
}
|
||||
return strutil.Join(lines, "\n")
|
||||
}
|
||||
|
||||
// Cell represents a column in a row
|
||||
type Cell struct {
|
||||
// Width is the width of the cell
|
||||
Width uint
|
||||
|
||||
// Wrap when true wraps the contents of the cell when the lenght exceeds the width
|
||||
Wrap bool
|
||||
|
||||
// RightAlign when true aligns contents to the right
|
||||
RightAlign bool
|
||||
|
||||
// Data is the cell data
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
// LineWidth returns the max width of all the lines in a cell
|
||||
func (c *Cell) LineWidth() uint {
|
||||
width := 0
|
||||
for _, s := range strings.Split(c.String(), "\n") {
|
||||
w := runewidth.StringWidth(s)
|
||||
if w > width {
|
||||
width = w
|
||||
}
|
||||
}
|
||||
return uint(width)
|
||||
}
|
||||
|
||||
// String returns the string formated representation of the cell
|
||||
func (c *Cell) String() string {
|
||||
if c.Data == nil {
|
||||
return strutil.PadLeft(" ", int(c.Width), ' ')
|
||||
}
|
||||
s := fmt.Sprintf("%v", c.Data)
|
||||
if c.Width > 0 {
|
||||
if c.Wrap && uint(len(s)) > c.Width {
|
||||
return wordwrap.WrapString(s, c.Width)
|
||||
} else {
|
||||
return strutil.Resize(s, c.Width, c.RightAlign)
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
// Package strutil provides various utilities for manipulating strings
|
||||
package strutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/mattn/go-runewidth"
|
||||
)
|
||||
|
||||
// PadRight returns a new string of a specified length in which the end of the current string is padded with spaces or with a specified Unicode character.
|
||||
func PadRight(str string, length int, pad byte) string {
|
||||
slen := runewidth.StringWidth(str)
|
||||
if slen >= length {
|
||||
return str
|
||||
}
|
||||
buf := bytes.NewBufferString(str)
|
||||
for i := 0; i < length-slen; i++ {
|
||||
buf.WriteByte(pad)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// PadLeft returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character.
|
||||
func PadLeft(str string, length int, pad byte) string {
|
||||
slen := runewidth.StringWidth(str)
|
||||
if slen >= length {
|
||||
return str
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
for i := 0; i < length-slen; i++ {
|
||||
buf.WriteByte(pad)
|
||||
}
|
||||
buf.WriteString(str)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// Resize resizes the string with the given length. It ellipses with '...' when the string's length exceeds
|
||||
// the desired length or pads spaces to the right of the string when length is smaller than desired
|
||||
func Resize(s string, length uint, rightAlign bool) string {
|
||||
slen := runewidth.StringWidth(s)
|
||||
n := int(length)
|
||||
if slen == n {
|
||||
return s
|
||||
}
|
||||
// Pads only when length of the string smaller than len needed
|
||||
if rightAlign {
|
||||
s = PadLeft(s, n, ' ')
|
||||
} else {
|
||||
s = PadRight(s, n, ' ')
|
||||
}
|
||||
if slen > n {
|
||||
rs := []rune(s)
|
||||
var buf bytes.Buffer
|
||||
w := 0
|
||||
for _, r := range rs {
|
||||
buf.WriteRune(r)
|
||||
rw := runewidth.RuneWidth(r)
|
||||
if w+rw >= n-3 {
|
||||
break
|
||||
}
|
||||
w += rw
|
||||
}
|
||||
buf.WriteString("...")
|
||||
s = buf.String()
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Join joins the list of the string with the delim provided
|
||||
func Join(list []string, delim string) string {
|
||||
var buf bytes.Buffer
|
||||
for i := 0; i < len(list)-1; i++ {
|
||||
buf.WriteString(list[i] + delim)
|
||||
}
|
||||
buf.WriteString(list[len(list)-1])
|
||||
return buf.String()
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Mitchell Hashimoto
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
# go-wordwrap
|
||||
|
||||
`go-wordwrap` (Golang package: `wordwrap`) is a package for Go that
|
||||
automatically wraps words into multiple lines. The primary use case for this
|
||||
is in formatting CLI output, but of course word wrapping is a generally useful
|
||||
thing to do.
|
||||
|
||||
## Installation and Usage
|
||||
|
||||
Install using `go get github.com/mitchellh/go-wordwrap`.
|
||||
|
||||
Full documentation is available at
|
||||
http://godoc.org/github.com/mitchellh/go-wordwrap
|
||||
|
||||
Below is an example of its usage ignoring errors:
|
||||
|
||||
```go
|
||||
wrapped := wordwrap.WrapString("foo bar baz", 3)
|
||||
fmt.Println(wrapped)
|
||||
```
|
||||
|
||||
Would output:
|
||||
|
||||
```
|
||||
foo
|
||||
bar
|
||||
baz
|
||||
```
|
||||
|
||||
## Word Wrap Algorithm
|
||||
|
||||
This library doesn't use any clever algorithm for word wrapping. The wrapping
|
||||
is actually very naive: whenever there is whitespace or an explicit linebreak.
|
||||
The goal of this library is for word wrapping CLI output, so the input is
|
||||
typically pretty well controlled human language. Because of this, the naive
|
||||
approach typically works just fine.
|
||||
|
||||
In the future, we'd like to make the algorithm more advanced. We would do
|
||||
so without breaking the API.
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
// Package wordwrap provides methods for wrapping the contents of a string
|
||||
package wordwrap
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/mattn/go-runewidth"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// WrapString wraps the given string within lim width in characters.
|
||||
//
|
||||
// Wrapping is currently naive and only happens at white-space. A future
|
||||
// version of the library will implement smarter wrapping. This means that
|
||||
// pathological cases can dramatically reach past the limit, such as a very
|
||||
// long word.
|
||||
func WrapString(s string, lim uint) string {
|
||||
// Initialize a buffer with a slightly larger size to account for breaks
|
||||
init := make([]byte, 0, len(s))
|
||||
buf := bytes.NewBuffer(init)
|
||||
|
||||
var current uint
|
||||
var wordBuf, spaceBuf bytes.Buffer
|
||||
var wordWidth, spaceWidth int
|
||||
|
||||
for _, char := range s {
|
||||
if char == '\n' {
|
||||
if wordBuf.Len() == 0 {
|
||||
if current+uint(spaceWidth) > lim {
|
||||
current = 0
|
||||
} else {
|
||||
current += uint(spaceWidth)
|
||||
spaceBuf.WriteTo(buf)
|
||||
spaceWidth += runewidth.StringWidth(buf.String())
|
||||
}
|
||||
spaceBuf.Reset()
|
||||
spaceWidth = 0
|
||||
} else {
|
||||
current += uint(spaceWidth + wordWidth)
|
||||
spaceBuf.WriteTo(buf)
|
||||
spaceBuf.Reset()
|
||||
wordBuf.WriteTo(buf)
|
||||
wordBuf.Reset()
|
||||
spaceWidth = 0
|
||||
wordWidth = 0
|
||||
}
|
||||
buf.WriteRune(char)
|
||||
current = 0
|
||||
} else if unicode.IsSpace(char) {
|
||||
if spaceBuf.Len() == 0 || wordBuf.Len() > 0 {
|
||||
current += uint(spaceWidth + wordWidth)
|
||||
spaceBuf.WriteTo(buf)
|
||||
spaceBuf.Reset()
|
||||
wordBuf.WriteTo(buf)
|
||||
wordBuf.Reset()
|
||||
spaceWidth = 0
|
||||
wordWidth = 0
|
||||
}
|
||||
|
||||
spaceBuf.WriteRune(char)
|
||||
spaceWidth += runewidth.RuneWidth(char)
|
||||
} else {
|
||||
wordBuf.WriteRune(char)
|
||||
wordWidth += runewidth.RuneWidth(char)
|
||||
|
||||
if current+uint(spaceWidth+wordWidth) > lim && uint(wordWidth) < lim {
|
||||
buf.WriteRune('\n')
|
||||
current = 0
|
||||
spaceBuf.Reset()
|
||||
spaceWidth = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if wordBuf.Len() == 0 {
|
||||
if current+uint(spaceWidth) <= lim {
|
||||
spaceBuf.WriteTo(buf)
|
||||
}
|
||||
} else {
|
||||
spaceBuf.WriteTo(buf)
|
||||
wordBuf.WriteTo(buf)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
-156
@@ -1,156 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
||||
|
||||
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.
|
||||
*/
|
||||
// Copyright 2017 VMware, Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 simulator
|
||||
|
||||
import (
|
||||
"github.com/vmware/govmomi/object"
|
||||
"github.com/vmware/govmomi/vim25/methods"
|
||||
"github.com/vmware/govmomi/vim25/mo"
|
||||
"github.com/vmware/govmomi/vim25/soap"
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
// EvalLicense is the default license
|
||||
var EvalLicense = types.LicenseManagerLicenseInfo{
|
||||
LicenseKey: "00000-00000-00000-00000-00000",
|
||||
EditionKey: "eval",
|
||||
Name: "Evaluation Mode",
|
||||
Properties: []types.KeyAnyValue{
|
||||
{
|
||||
Key: "feature",
|
||||
Value: types.KeyValue{
|
||||
Key: "serialuri:2",
|
||||
Value: "Remote virtual Serial Port Concentrator",
|
||||
},
|
||||
},
|
||||
{
|
||||
Key: "feature",
|
||||
Value: types.KeyValue{
|
||||
Key: "dvs",
|
||||
Value: "vSphere Distributed Switch",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
type LicenseManager struct {
|
||||
mo.LicenseManager
|
||||
}
|
||||
|
||||
func NewLicenseManager(ref types.ManagedObjectReference) object.Reference {
|
||||
m := &LicenseManager{}
|
||||
m.Self = ref
|
||||
m.Licenses = []types.LicenseManagerLicenseInfo{EvalLicense}
|
||||
|
||||
if Map.IsVPX() {
|
||||
am := Map.Put(&LicenseAssignmentManager{}).Reference()
|
||||
m.LicenseAssignmentManager = &am
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *LicenseManager) AddLicense(req *types.AddLicense) soap.HasFault {
|
||||
body := &methods.AddLicenseBody{
|
||||
Res: &types.AddLicenseResponse{},
|
||||
}
|
||||
|
||||
for _, license := range m.Licenses {
|
||||
if license.LicenseKey == req.LicenseKey {
|
||||
body.Res.Returnval = licenseInfo(license.LicenseKey, license.Labels)
|
||||
return body
|
||||
}
|
||||
}
|
||||
|
||||
m.Licenses = append(m.Licenses, types.LicenseManagerLicenseInfo{
|
||||
LicenseKey: req.LicenseKey,
|
||||
Labels: req.Labels,
|
||||
})
|
||||
|
||||
body.Res.Returnval = licenseInfo(req.LicenseKey, req.Labels)
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
func (m *LicenseManager) RemoveLicense(req *types.RemoveLicense) soap.HasFault {
|
||||
body := &methods.RemoveLicenseBody{
|
||||
Res: &types.RemoveLicenseResponse{},
|
||||
}
|
||||
|
||||
for i, license := range m.Licenses {
|
||||
if req.LicenseKey == license.LicenseKey {
|
||||
m.Licenses = append(m.Licenses[:i], m.Licenses[i+1:]...)
|
||||
return body
|
||||
}
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
type LicenseAssignmentManager struct {
|
||||
mo.LicenseAssignmentManager
|
||||
}
|
||||
|
||||
func (m *LicenseAssignmentManager) QueryAssignedLicenses(req *types.QueryAssignedLicenses) soap.HasFault {
|
||||
body := &methods.QueryAssignedLicensesBody{
|
||||
Res: &types.QueryAssignedLicensesResponse{},
|
||||
}
|
||||
|
||||
// EntityId can be a HostSystem or the vCenter InstanceUuid
|
||||
if req.EntityId != "" {
|
||||
if req.EntityId != Map.content().About.InstanceUuid {
|
||||
id := types.ManagedObjectReference{
|
||||
Type: "HostSystem",
|
||||
Value: req.EntityId,
|
||||
}
|
||||
|
||||
if Map.Get(id) == nil {
|
||||
return body
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
body.Res.Returnval = []types.LicenseAssignmentManagerLicenseAssignment{
|
||||
{
|
||||
EntityId: req.EntityId,
|
||||
AssignedLicense: EvalLicense,
|
||||
},
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
func licenseInfo(key string, labels []types.KeyValue) types.LicenseManagerLicenseInfo {
|
||||
info := EvalLicense
|
||||
|
||||
info.LicenseKey = key
|
||||
info.Labels = labels
|
||||
|
||||
return info
|
||||
}
|
||||
+16
-13
@@ -358,12 +358,12 @@ func (this *JSONArray) unmarshalValue(val reflect.Value) error {
|
||||
val.SetString(this.String())
|
||||
return nil
|
||||
case reflect.Ptr:
|
||||
if val.IsNil() {
|
||||
kind := val.Type().Elem().Kind()
|
||||
if kind == reflect.Array || kind == reflect.Slice {
|
||||
kind := val.Type().Elem().Kind()
|
||||
if kind == reflect.Array || kind == reflect.Slice {
|
||||
if val.IsNil() {
|
||||
val.Set(reflect.New(val.Type().Elem()))
|
||||
return this.unmarshalValue(val.Elem())
|
||||
}
|
||||
return this.unmarshalValue(val.Elem())
|
||||
}
|
||||
return fmt.Errorf("JSONArray type mismatch %s", val.Type())
|
||||
case reflect.Slice, reflect.Array:
|
||||
@@ -373,9 +373,12 @@ func (this *JSONArray) unmarshalValue(val reflect.Value) error {
|
||||
val.Type(), val.Len(), len(this.data))
|
||||
}
|
||||
} else if val.Kind() == reflect.Slice {
|
||||
if val.Len() < len(this.data) {
|
||||
newVal := reflect.MakeSlice(val.Type(), len(this.data), len(this.data))
|
||||
dataLen := len(this.data)
|
||||
if val.Cap() < dataLen {
|
||||
newVal := reflect.MakeSlice(val.Type(), dataLen, dataLen)
|
||||
val.Set(newVal)
|
||||
} else if val.Len() != dataLen {
|
||||
val.SetLen(dataLen)
|
||||
}
|
||||
}
|
||||
for i, json := range this.data {
|
||||
@@ -412,15 +415,15 @@ func (this *JSONDict) unmarshalValue(val reflect.Value) error {
|
||||
case reflect.Struct:
|
||||
return this.unmarshalStruct(val)
|
||||
case reflect.Ptr:
|
||||
if val.IsNil() {
|
||||
kind := val.Type().Elem().Kind()
|
||||
if kind != reflect.Struct && kind != reflect.Map {
|
||||
return fmt.Errorf("JSONDict type mismatch: %s", val.Type())
|
||||
kind := val.Type().Elem().Kind()
|
||||
if kind == reflect.Struct || kind == reflect.Map {
|
||||
if val.IsNil() {
|
||||
newVal := reflect.New(val.Type().Elem())
|
||||
val.Set(newVal)
|
||||
}
|
||||
newVal := reflect.New(val.Type().Elem())
|
||||
val.Set(newVal)
|
||||
return this.unmarshalValue(val.Elem())
|
||||
}
|
||||
return this.unmarshalValue(val.Elem())
|
||||
fallthrough
|
||||
default:
|
||||
return fmt.Errorf("JSONDict type mismatch: %s", val.Type())
|
||||
}
|
||||
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package gotypes
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type DeepCopyFlags uintptr
|
||||
|
||||
func DeepCopy(v interface{}) interface{} {
|
||||
rv := reflect.ValueOf(v)
|
||||
cpRv := DeepCopyRv(rv)
|
||||
cpV := cpRv.Interface()
|
||||
return cpV
|
||||
}
|
||||
|
||||
func DeepCopyRv(rv reflect.Value) reflect.Value {
|
||||
kind := rv.Kind()
|
||||
if kind == reflect.Invalid {
|
||||
return reflect.Value{}
|
||||
}
|
||||
typ := rv.Type()
|
||||
switch kind {
|
||||
case reflect.Ptr:
|
||||
if rv.IsNil() {
|
||||
return reflect.New(typ).Elem()
|
||||
}
|
||||
elemRv := rv.Elem()
|
||||
cpElemRv := DeepCopyRv(elemRv)
|
||||
cpRv := reflect.New(typ.Elem())
|
||||
cpRv.Elem().Set(cpElemRv)
|
||||
return cpRv
|
||||
case reflect.Slice, reflect.Array:
|
||||
var cpRv reflect.Value
|
||||
switch kind {
|
||||
case reflect.Slice:
|
||||
if rv.IsNil() {
|
||||
return reflect.New(typ).Elem()
|
||||
}
|
||||
cpRv = reflect.MakeSlice(typ, rv.Len(), rv.Cap())
|
||||
case reflect.Array:
|
||||
cpRv = reflect.New(typ).Elem()
|
||||
}
|
||||
n := rv.Len()
|
||||
for i := 0; i < n; i++ {
|
||||
elem := rv.Index(i)
|
||||
cpElem := DeepCopyRv(elem)
|
||||
cpRvElem := cpRv.Index(i)
|
||||
cpRvElem.Set(cpElem)
|
||||
}
|
||||
return cpRv
|
||||
case reflect.Struct:
|
||||
cpRv := reflect.New(typ).Elem()
|
||||
if typ == TimeType {
|
||||
cpRv.Set(rv)
|
||||
}
|
||||
n := rv.NumField()
|
||||
for i := 0; i < n; i++ {
|
||||
f := rv.Field(i)
|
||||
if f.CanInterface() {
|
||||
cpRvF := cpRv.Field(i)
|
||||
cpF := DeepCopyRv(f)
|
||||
cpRvF.Set(cpF)
|
||||
}
|
||||
}
|
||||
return cpRv
|
||||
case reflect.Map:
|
||||
if rv.IsNil() {
|
||||
return reflect.New(typ).Elem()
|
||||
}
|
||||
cpRv := reflect.MakeMap(typ)
|
||||
mk := rv.MapKeys()
|
||||
for _, k := range mk {
|
||||
v := rv.MapIndex(k)
|
||||
cpK := DeepCopyRv(k)
|
||||
cpV := DeepCopyRv(v)
|
||||
cpRv.SetMapIndex(cpK, cpV)
|
||||
}
|
||||
return cpRv
|
||||
case reflect.Interface:
|
||||
cpRv := reflect.New(typ).Elem()
|
||||
if !rv.IsNil() {
|
||||
elemRv := rv.Elem()
|
||||
cpElemRv := DeepCopyRv(elemRv)
|
||||
cpRv.Set(cpElemRv)
|
||||
}
|
||||
return cpRv
|
||||
case reflect.Chan:
|
||||
if rv.IsNil() {
|
||||
return reflect.New(typ).Elem()
|
||||
}
|
||||
return reflect.MakeChan(typ.Elem(), rv.Cap())
|
||||
default:
|
||||
// Invalid, Bool, Int*, Uint*, Float*, Complex*, Func, String
|
||||
// TODO UnsafePointer
|
||||
cpRv := reflect.New(typ).Elem()
|
||||
cpRv.Set(rv)
|
||||
return cpRv
|
||||
}
|
||||
panic("unhandled kind " + kind.String())
|
||||
}
|
||||
+44
-15
@@ -2,6 +2,7 @@ package prettytable
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AlignmentType uint8
|
||||
@@ -40,6 +41,7 @@ func rowLine(buf *bytes.Buffer, widths []int) {
|
||||
buf.WriteByte('-')
|
||||
buf.WriteByte('+')
|
||||
}
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
|
||||
func textCell(buf *bytes.Buffer, col ptColumn, width int) {
|
||||
@@ -65,13 +67,45 @@ func textCell(buf *bytes.Buffer, col ptColumn, width int) {
|
||||
}
|
||||
|
||||
func textLine(buf *bytes.Buffer, columns []ptColumn, widths []int) {
|
||||
buf.WriteByte('|')
|
||||
for i, w := range widths {
|
||||
buf.WriteByte(' ')
|
||||
textCell(buf, columns[i], w)
|
||||
buf.WriteByte(' ')
|
||||
buf.WriteByte('|')
|
||||
nlines := 0
|
||||
splitted := make([][]string, len(columns))
|
||||
for i, column := range columns {
|
||||
title := strings.TrimRight(column.Title, "\n")
|
||||
s := strings.Split(title, "\n")
|
||||
splitted[i] = s
|
||||
if nlines < len(s) {
|
||||
nlines = len(s)
|
||||
}
|
||||
}
|
||||
for j := 0; j < nlines; j++ {
|
||||
buf.WriteByte('|')
|
||||
for i, w := range widths {
|
||||
buf.WriteByte(' ')
|
||||
var text string
|
||||
if j < len(splitted[i]) {
|
||||
text = splitted[i][j]
|
||||
}
|
||||
c := ptColumn{
|
||||
Align: columns[i].Align,
|
||||
Title: text,
|
||||
}
|
||||
textCell(buf, c, w)
|
||||
buf.WriteByte(' ')
|
||||
buf.WriteByte('|')
|
||||
}
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
}
|
||||
|
||||
func cellWidth(cell string) int {
|
||||
width := 0
|
||||
lines := strings.Split(cell, "\n")
|
||||
for _, line := range lines {
|
||||
if width < len(line) {
|
||||
width = len(line)
|
||||
}
|
||||
}
|
||||
return width
|
||||
}
|
||||
|
||||
func (this *PrettyTable) GetString(fields [][]string) string {
|
||||
@@ -80,37 +114,32 @@ func (this *PrettyTable) GetString(fields [][]string) string {
|
||||
}
|
||||
var widths = make([]int, len(this.columns))
|
||||
for i, c := range this.columns {
|
||||
if widths[i] < len(c.Title) {
|
||||
widths[i] = len(c.Title)
|
||||
}
|
||||
widths[i] = cellWidth(c.Title)
|
||||
}
|
||||
for _, line := range fields {
|
||||
for i, cell := range line {
|
||||
if widths[i] < len(cell) {
|
||||
widths[i] = len(cell)
|
||||
cw := cellWidth(cell)
|
||||
if widths[i] < cw {
|
||||
widths[i] = cw
|
||||
}
|
||||
}
|
||||
}
|
||||
var columns = make([]ptColumn, len(this.columns))
|
||||
var buf bytes.Buffer
|
||||
rowLine(&buf, widths)
|
||||
buf.WriteByte('\n')
|
||||
for i := 0; i < len(columns); i++ {
|
||||
columns[i].Title = this.columns[i].Title
|
||||
columns[i].Align = AlignCenter
|
||||
}
|
||||
textLine(&buf, columns, widths)
|
||||
buf.WriteByte('\n')
|
||||
rowLine(&buf, widths)
|
||||
for _, line := range fields {
|
||||
for i := 0; i < len(line); i++ {
|
||||
columns[i].Title = line[i]
|
||||
columns[i].Align = this.columns[i].Align
|
||||
}
|
||||
buf.WriteByte('\n')
|
||||
textLine(&buf, columns, widths)
|
||||
}
|
||||
buf.WriteByte('\n')
|
||||
rowLine(&buf, widths)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
+68
@@ -149,3 +149,71 @@ func FloatRound(num float64, precision int) float64 {
|
||||
num = temp
|
||||
return num
|
||||
}
|
||||
|
||||
func getStringInQuote(s string, start, length int, quote byte, backIndex int) (string, int) {
|
||||
var i = start + 1
|
||||
if quote != '\'' && quote != '"' {
|
||||
for i < length {
|
||||
if s[i] == ' ' || s[i] == '\'' || s[i] == '"' {
|
||||
return s[backIndex:i], i
|
||||
}
|
||||
i++
|
||||
}
|
||||
return s[backIndex:], i
|
||||
} else {
|
||||
for i < length {
|
||||
if s[i] == quote {
|
||||
return s[backIndex+1 : i], i + 1
|
||||
}
|
||||
i++
|
||||
}
|
||||
return s[backIndex:], i
|
||||
}
|
||||
}
|
||||
|
||||
func ArgsStringToArray(s string) []string {
|
||||
var args, i, j = make([]string, 0), 0, 0
|
||||
var length = len(s)
|
||||
for i < length {
|
||||
switch s[i] {
|
||||
case ' ':
|
||||
if i > j {
|
||||
args = append(args, s[j:i])
|
||||
j = i
|
||||
}
|
||||
i++
|
||||
j = i
|
||||
case '"':
|
||||
var s1, s2 string
|
||||
var oldStr = s[j:i]
|
||||
s1, i = getStringInQuote(s, i, length, '"', i)
|
||||
s1 = oldStr + s1
|
||||
for i < length && s[i] != ' ' {
|
||||
s2, i = getStringInQuote(s, i, length, s[i], i)
|
||||
s1 += s2
|
||||
fmt.Println(s2)
|
||||
}
|
||||
args = append(args, s1)
|
||||
i++
|
||||
j = i
|
||||
case '\'':
|
||||
var s1, s2 string
|
||||
var oldStr = s[j:i]
|
||||
s1, i = getStringInQuote(s, i, length, '\'', i)
|
||||
s1 = oldStr + s1
|
||||
for i < length && s[i] != ' ' {
|
||||
s2, i = getStringInQuote(s, i, length, s[i], i)
|
||||
s1 += s2
|
||||
}
|
||||
args = append(args, s1)
|
||||
i++
|
||||
j = i
|
||||
default:
|
||||
i++
|
||||
}
|
||||
}
|
||||
if j < length {
|
||||
args = append(args, s[j:])
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
+1
-1
@@ -102,7 +102,7 @@ func (ts *STableSpec) updateFields(dt interface{}, fields map[string]interface{}
|
||||
buf.WriteString(fmt.Sprintf(", `%s` = `%s` + 1", versionField, versionField))
|
||||
}
|
||||
for _, updatedField := range updatedFields {
|
||||
buf.WriteString(fmt.Sprintf(", `%s` = NOW()", updatedField))
|
||||
buf.WriteString(fmt.Sprintf(", `%s` = UTC_TIMESTAMP()", updatedField))
|
||||
}
|
||||
buf.WriteString(" WHERE ")
|
||||
first = true
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ func (t *STableSpec) insertSqlPrep(dataFields map[string]interface{}) (string, [
|
||||
if ok && (dtc.IsCreatedAt || dtc.IsUpdatedAt) {
|
||||
createdAtFields = append(createdAtFields, k)
|
||||
names = append(names, fmt.Sprintf("`%s`", k))
|
||||
format = append(format, "NOW()")
|
||||
format = append(format, "UTC_TIMESTAMP()")
|
||||
} else if ov != nil && !c.IsZero(ov) && !isAutoInc {
|
||||
v := c.ConvertFromValue(ov)
|
||||
values = append(values, v)
|
||||
|
||||
+3
-4
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/gotypes"
|
||||
"yunion.io/x/pkg/util/reflectutils"
|
||||
"yunion.io/x/pkg/utils"
|
||||
)
|
||||
@@ -20,7 +21,6 @@ func (ts *STableSpec) prepareUpdate(dt interface{}) (*SUpdateSession, error) {
|
||||
if reflect.ValueOf(dt).Kind() != reflect.Ptr {
|
||||
return nil, fmt.Errorf("Update input must be a Pointer")
|
||||
}
|
||||
dataType := reflect.TypeOf(dt).Elem()
|
||||
dataValue := reflect.ValueOf(dt).Elem()
|
||||
fields := reflectutils.FetchStructFieldNameValueInterfaces(dataValue) // fetchStructFieldNameValue(dataType, dataValue)
|
||||
|
||||
@@ -41,8 +41,7 @@ func (ts *STableSpec) prepareUpdate(dt interface{}) (*SUpdateSession, error) {
|
||||
strings.Join(zeroPrimary, ","), strings.Join(zeroKeyIndex, ","))
|
||||
}
|
||||
|
||||
originValue := reflect.Indirect(reflect.New(dataType))
|
||||
originValue.Set(dataValue)
|
||||
originValue := gotypes.DeepCopyRv(dataValue)
|
||||
us := SUpdateSession{oValue: originValue, tableSpec: ts}
|
||||
return &us, nil
|
||||
}
|
||||
@@ -127,7 +126,7 @@ func (us *SUpdateSession) saveUpdate(dt interface{}) (map[string]SUpdateDiff, er
|
||||
buf.WriteString(fmt.Sprintf(", `%s` = `%s` + 1", versionField, versionField))
|
||||
}
|
||||
for _, updatedField := range updatedFields {
|
||||
buf.WriteString(fmt.Sprintf(", `%s` = NOW()", updatedField))
|
||||
buf.WriteString(fmt.Sprintf(", `%s` = UTC_TIMESTAMP()", updatedField))
|
||||
}
|
||||
buf.WriteString(" WHERE ")
|
||||
first = true
|
||||
|
||||
+3
@@ -305,6 +305,9 @@ func (this *ArgumentParser) addArgument(f reflect.StructField, v reflect.Value)
|
||||
return fmt.Errorf("positional %s must not have default value", token)
|
||||
}
|
||||
}
|
||||
if !positional && use_default && required {
|
||||
return fmt.Errorf("non-positional argument with default value should not have required:true set")
|
||||
}
|
||||
subcommand, err := strconv.ParseBool(tagMap[TAG_SUBCOMMAND])
|
||||
if err != nil {
|
||||
subcommand = false
|
||||
|
||||
Reference in New Issue
Block a user