fix server create

This commit is contained in:
wanyaoqi
2018-08-11 18:34:21 +08:00
parent 4b28c2a201
commit 9474752c05
6 changed files with 62 additions and 12 deletions
+35 -8
View File
@@ -1,7 +1,9 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
@@ -36,9 +38,6 @@ type BaseOptions struct {
SUBCOMMAND string `help:"climc subcommand" subcommand:"true"`
}
var CacheToken mcclient.TokenCredential
var CacheTime time.Time
func getSubcommandsParser() (*structarg.ArgumentParser, error) {
parse, e := structarg.NewArgumentParser(&BaseOptions{},
"climc",
@@ -122,7 +121,26 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) {
options.Debug,
options.Secure)
if CacheToken == nil {
var cacheToken mcclient.TokenCredential
cacheFile, err := os.Open("/tmp/OS_AUTH_CACHE_TOKEN")
if err == nil && cacheFile != nil {
fileInfo, _ := cacheFile.Stat()
dur, err := time.ParseDuration("-24h")
if fileInfo != nil && err == nil && fileInfo.ModTime().After(time.Now().Add(dur)) {
bytesToken, err := ioutil.ReadAll(cacheFile)
if err == nil {
token := client.NewAuthTokenCredential()
err := json.Unmarshal(bytesToken, token)
if err != nil {
fmt.Printf("Unmarshal token error:%s", err)
} else {
cacheToken = token
}
}
cacheFile.Close()
}
}
if cacheToken == nil {
token, err := client.Authenticate(options.OsUsername,
options.OsPassword,
options.OsDomainName,
@@ -130,14 +148,23 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) {
if err != nil {
return nil, err
}
CacheToken = token
CacheTime = time.Now()
cacheToken = token
bytesCacheToken, err := json.Marshal(cacheToken)
if err != nil {
fmt.Printf("Marshal token error:%s", err)
} else {
fo, _ := os.Create("/tmp/OS_AUTH_CACHE_TOKEN")
fo.Write(bytesCacheToken)
fo.Close()
}
} else {
fmt.Println("******** Use Token Cache At /tmp/OS_AUTH_CACHE_TOKEN ********")
}
session := client.NewSession(options.OsRegionName,
options.OsZoneName,
options.OsEndpointType,
CacheToken,
cacheToken,
options.ApiVersion)
return session, nil
}
@@ -154,7 +181,7 @@ func main() {
fmt.Print(parser.HelpString())
} else if options.Version {
fmt.Printf("Yunion API client version:\n %s\n", version.GetJsonString())
} else if len(os.Args) <= 1 || (options.ApiVersion == "v2" && len(os.Args) <= 3) {
} else if len(options.SUBCOMMAND) == 0 {
session, e := newClientSession(options)
if e != nil {
showErrorAndExit(e)
+5
View File
@@ -55,6 +55,11 @@ func Executor(s string) {
e := parser.ParseArgs(strings.Split(s, " "), false)
subcmd := parser.GetSubcommand()
subparser := subcmd.GetSubParser()
if args[0] == "--debug" {
session.GetClient().SetDebug(true)
} else {
session.GetClient().SetDebug(false)
}
if e != nil {
if subparser != nil {
fmt.Print(subparser.Usage())
+5 -2
View File
@@ -593,6 +593,7 @@ type SDiskConfig struct {
Cache string //
Mountpoint string //
Backend string // stroageType
Medium string
ImageProperties map[string]string
}
@@ -608,9 +609,9 @@ func parseDiskInfo(ctx context.Context, userCred mcclient.TokenCredential, info
return &diskConfig, nil
}
// default backend
// default backend and medium type
diskConfig.Backend = STORAGE_LOCAL
// diskConfig.Medium = DISK_TYPE_HYBRID
diskConfig.Medium = DISK_TYPE_HYBRID
diskStr, err := info.GetString()
if err != nil {
@@ -629,6 +630,8 @@ func parseDiskInfo(ctx context.Context, userCred mcclient.TokenCredential, info
diskConfig.Driver = p
} else if utils.IsInStringArray(p, osprofile.DISK_CACHE_MODES) {
diskConfig.Cache = p
} else if utils.IsInStringArray(p, DISK_TYPES) {
diskConfig.Medium = p
} else if p[0] == '/' {
diskConfig.Mountpoint = p
} else if p == "autoextend" {
+2 -2
View File
@@ -27,11 +27,11 @@ func (self *DiskCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel,
self.SetStage("on_storage_cache_image_complete", nil)
storagecache.StartImageCacheTask(ctx, self.UserCred, imageId, false, self.GetTaskId())
} else {
self.OnStorageCacheImageComplete(ctx, disk)
self.OnStorageCacheImageComplete(ctx, disk, nil)
}
}
func (self *DiskCreateTask) OnStorageCacheImageComplete(ctx context.Context, disk *models.SDisk) {
func (self *DiskCreateTask) OnStorageCacheImageComplete(ctx context.Context, disk *models.SDisk, data jsonutils.JSONObject) {
rebuild, _ := self.GetParams().Bool("rebuild")
snapshot, _ := self.GetParams().GetString("snapshot")
if rebuild {
+11
View File
@@ -53,6 +53,10 @@ func NewClient(authUrl string, timeout int, debug bool, insecure bool) *Client {
return &client
}
func (this *Client) SetDebug(debug bool) {
this.debug = debug
}
func (this *Client) AuthVersion() string {
pos := strings.LastIndexByte(this.authUrl, '/')
if pos > 0 {
@@ -62,6 +66,13 @@ func (this *Client) AuthVersion() string {
}
}
func (this *Client) NewAuthTokenCredential() TokenCredential {
if this.AuthVersion() == "v3" {
return &TokenCredentialV3{}
}
return &TokenCredentialV2{}
}
func getDefaultHeader(header http.Header, token string) http.Header {
if len(token) > 0 {
if header == nil {
+4
View File
@@ -63,6 +63,10 @@ func SplitVersionedURL(url string) (string, string) {
return base
}*/
func (this *ClientSession) GetClient() *Client {
return this.client
}
func (this *ClientSession) GetServiceURL(service, endpointType string) (string, error) {
if len(this.endpointType) > 0 {
// session specific endpoint type should override the input endpointType, which is supplied by manager