diff --git a/cmd/climc/climc.go b/cmd/climc/climc.go index ef9b1bee40..e5983068a6 100644 --- a/cmd/climc/climc.go +++ b/cmd/climc/climc.go @@ -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) diff --git a/cmd/climc/promputils/executor.go b/cmd/climc/promputils/executor.go index 7ef8e7dc89..9047dd12ce 100644 --- a/cmd/climc/promputils/executor.go +++ b/cmd/climc/promputils/executor.go @@ -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()) diff --git a/pkg/compute/models/disks.go b/pkg/compute/models/disks.go index a47fdf3f0b..b9c027c43a 100644 --- a/pkg/compute/models/disks.go +++ b/pkg/compute/models/disks.go @@ -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" { diff --git a/pkg/compute/tasks/disk_create_task.go b/pkg/compute/tasks/disk_create_task.go index c035d9e256..35b6c035f1 100644 --- a/pkg/compute/tasks/disk_create_task.go +++ b/pkg/compute/tasks/disk_create_task.go @@ -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 { diff --git a/pkg/mcclient/mcclient.go b/pkg/mcclient/mcclient.go index 92bf5b4bb9..4df0561160 100644 --- a/pkg/mcclient/mcclient.go +++ b/pkg/mcclient/mcclient.go @@ -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 { diff --git a/pkg/mcclient/session.go b/pkg/mcclient/session.go index d458d65038..3526f6eea4 100644 --- a/pkg/mcclient/session.go +++ b/pkg/mcclient/session.go @@ -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