client session add context

This commit is contained in:
wanyaoqi
2018-12-24 21:17:46 +08:00
parent f034ce6629
commit 742a67b0f8
29 changed files with 105 additions and 71 deletions
+5 -2
View File
@@ -1,6 +1,7 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
@@ -9,7 +10,7 @@ import (
"strings"
"time"
"github.com/c-bata/go-prompt"
prompt "github.com/c-bata/go-prompt"
"yunion.io/x/log"
"yunion.io/x/pkg/util/version"
@@ -183,7 +184,9 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) {
if options.ApiVersion != "" {
mcclient.DisableApiVersionByModule()
}
session := client.NewSession(options.OsRegionName,
session := client.NewSession(
context.Background(),
options.OsRegionName,
options.OsZoneName,
options.OsEndpointType,
cacheToken,
+1 -1
View File
@@ -54,7 +54,7 @@ func queryQuota(ctx context.Context, projectId string) (*jsonutils.JSONDict, err
return nil, err
}
usage := _manager.newQuota()
err = usage.FetchUsage(projectId)
err = usage.FetchUsage(ctx, projectId)
if err != nil {
return nil, err
}
+2 -2
View File
@@ -17,7 +17,7 @@ const (
type IQuota interface {
FetchSystemQuota()
FetchUsage(projectId string) error
FetchUsage(ctx context.Context, projectId string) error
Update(quota IQuota)
Add(quota IQuota)
Sub(quota IQuota)
@@ -116,7 +116,7 @@ func (manager *SQuotaManager) _checkQuota(ctx context.Context, userCred mcclient
return nil, err
}
used := manager.newQuota()
err = used.FetchUsage(projectId)
err = used.FetchUsage(ctx, projectId)
if err != nil {
log.Errorf("fail to get quota usage %s", err)
return nil, err
+1 -1
View File
@@ -78,7 +78,7 @@ func (manager *STenantCacheManager) FetchTenantByName(ctx context.Context, idStr
}
func (manager *STenantCacheManager) fetchTenantFromKeystone(ctx context.Context, idStr string) (*STenant, error) {
s := auth.GetAdminSession(consts.GetRegion(), "v1")
s := auth.GetAdminSession(ctx, consts.GetRegion(), "v1")
tenant, err := modules.Projects.Get(s, idStr, nil)
if err != nil {
log.Errorf("fetch project fail %s", err)
+2 -1
View File
@@ -1,6 +1,7 @@
package policy
import (
"context"
"fmt"
"sort"
"strings"
@@ -183,7 +184,7 @@ func parseJsonPolicy(obj jsonutils.JSONObject) (string, rbacutils.SRbacPolicy, e
}
func fetchPolicies() (map[string]rbacutils.SRbacPolicy, map[string]rbacutils.SRbacPolicy, error) {
s := auth.GetAdminSession(consts.GetRegion(), "v1")
s := auth.GetAdminSession(context.Background(), consts.GetRegion(), "v1")
policies := make(map[string]rbacutils.SRbacPolicy)
adminPolicies := make(map[string]rbacutils.SRbacPolicy)
+1 -1
View File
@@ -95,7 +95,7 @@ type ICloudStoragecache interface {
DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error)
UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error)
UploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error)
}
type ICloudStorage interface {
+1 -1
View File
@@ -46,7 +46,7 @@ func (self *SManagedVirtualizationHostDriver) CheckAndSetCacheImage(ctx context.
return nil, err
}
extImgId, err := iStorageCache.UploadImage(userCred, imageId, osArch, osType, osDist, osVersion, scimg.ExternalId, isForce)
extImgId, err := iStorageCache.UploadImage(ctx, userCred, imageId, osArch, osType, osDist, osVersion, scimg.ExternalId, isForce)
if err != nil {
return nil, err
+2 -2
View File
@@ -206,7 +206,7 @@ func (manager *SCachedimageManager) GetImageById(ctx context.Context, userCred m
}
}
}
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(ctx, options.Options.Region, "")
obj, err := modules.Images.Get(s, imageId, nil)
if err != nil {
log.Errorf("GetImageById %s error %s", imageId, err)
@@ -220,7 +220,7 @@ func (manager *SCachedimageManager) GetImageById(ctx context.Context, userCred m
}
func (manager *SCachedimageManager) getImageByName(ctx context.Context, userCred mcclient.TokenCredential, imageId string) (*SImage, error) {
s := auth.GetSession(userCred, options.Options.Region, "")
s := auth.GetSession(ctx, userCred, options.Options.Region, "")
obj, err := modules.Images.GetByName(s, imageId, nil)
if err != nil {
return nil, err
+1 -1
View File
@@ -219,7 +219,7 @@ func (self *SCloudprovider) syncProject(ctx context.Context) error {
var projectId string
if err == sql.ErrNoRows { // create one
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(ctx, options.Options.Region, "")
params := jsonutils.NewDict()
params.Add(jsonutils.NewString(self.Name), "name")
params.Add(jsonutils.NewString(fmt.Sprintf("auto create from cloud provider %s (%s)", self.Name, self.Id)), "description")
+1 -1
View File
@@ -703,7 +703,7 @@ func (self *SDisk) PrepareSaveImage(ctx context.Context, userCred mcclient.Token
}
data.Add(jsonutils.NewString(self.DiskFormat), "disk_format")
name, _ := data.GetString("name")
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(ctx, options.Options.Region, "")
if imageList, err := modules.Images.List(s, jsonutils.Marshal(map[string]string{"name": name, "admin": "true"})); err != nil {
return "", err
} else if imageList.Total > 0 {
+3 -3
View File
@@ -763,12 +763,12 @@ func (self *SHostManager) GetPropertyBmStartRegisterScript(ctx context.Context,
}
func (maanger *SHostManager) ClearAllSchedDescCache() error {
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(context.Background(), options.Options.Region, "")
return modules.SchedManager.CleanCache(s, "")
}
func (maanger *SHostManager) ClearSchedDescCache(hostId string) error {
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(context.Background(), options.Options.Region, "")
return modules.SchedManager.CleanCache(s, hostId)
}
@@ -2178,7 +2178,7 @@ func (manager *SHostManager) GetHostsByManagerAndRegion(managerId string, region
}*/
func (self *SHost) Request(userCred mcclient.TokenCredential, method string, url string, headers http.Header, body jsonutils.JSONObject) (jsonutils.JSONObject, error) {
s := auth.GetSession(userCred, "", "")
s := auth.GetSession(nil, userCred, "", "")
_, ret, err := s.JSONRequest(self.ManagerUri, "", method, url, headers, body)
return ret, err
}
+3 -2
View File
@@ -1,6 +1,7 @@
package models
import (
"context"
"errors"
"fmt"
@@ -72,7 +73,7 @@ func (self *SQuota) FetchSystemQuota() {
self.Snapshot = options.Options.DefaultSnapshotQuota
}
func (self *SQuota) FetchUsage(projectId string) error {
func (self *SQuota) FetchUsage(ctx context.Context, projectId string) error {
diskSize := totalDiskSize(projectId, tristate.None, tristate.None, false)
net := totalGuestNicCount(projectId, nil, false)
guest := totalGuestResourceCount(projectId, nil, nil, nil, false, false, nil, nil, nil)
@@ -91,7 +92,7 @@ func (self *SQuota) FetchUsage(projectId string) error {
self.Bw = net.InternalBandwidth
self.Ebw = net.ExternalBandwidth
self.Keypair = 0 // keypair
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(ctx, options.Options.Region, "")
self.Image, _ = modules.Images.GetPrivateImageCount(s, projectId, true)
self.Group = 0
self.Secgroup = totalSecurityGroupCount(projectId)
+1 -1
View File
@@ -101,7 +101,7 @@ func processSkuData(ndata jsonutils.JSONObject) jsonutils.JSONObject {
}
func (self *SkusZone) Init() error {
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(context.Background(), options.Options.Region, "")
p, r, z := self.getExternalZone()
limit := 1024
offset := 0
+1 -1
View File
@@ -102,7 +102,7 @@ func (self *DiskSaveTask) TaskFailed(ctx context.Context, resion string) {
self.SetStageFailed(ctx, resion)
if imageId, err := self.GetParams().GetString("image_id"); err != nil && len(imageId) > 0 {
log.Errorf("save disk task failed, set image %s killed", imageId)
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(ctx, options.Options.Region, "")
mc.Images.Update(s, imageId, jsonutils.Marshal(map[string]string{"status": "killed"}))
}
}
+1 -1
View File
@@ -104,7 +104,7 @@ func doScheduleObjects(
task.SetStage("OnScheduleComplete", schedtags)
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(ctx, options.Options.Region, "")
results, err := modules.SchedManager.DoSchedule(s, parmas, len(objs))
if err != nil {
onSchedulerRequestFail(ctx, task, objs, fmt.Sprintf("Scheduler fail: %s", err))
+2 -2
View File
@@ -106,8 +106,8 @@ func (r *SRegionDNS) initK8s() {
r.K8sManager.Start()
}
func (r *SRegionDNS) getAdminSession() *mcclient.ClientSession {
return auth.GetAdminSession(r.Region, "")
func (r *SRegionDNS) getAdminSession(ctx context.Context) *mcclient.ClientSession {
return auth.GetAdminSession(ctx, r.Region, "")
}
func (r *SRegionDNS) initAuth() {
+1 -1
View File
@@ -65,7 +65,7 @@ func (h *ApiHelper) Run(ctx context.Context) {
func (h *ApiHelper) adminClientSession(ctx context.Context) *mcclient.ClientSession {
region := h.opts.CommonOpts.Region
apiVersion := "v2"
s := auth.GetAdminSession(region, apiVersion)
s := auth.GetAdminSession(ctx, region, apiVersion)
return s
}
+8 -6
View File
@@ -1,6 +1,7 @@
package auth
import (
"context"
"fmt"
"time"
@@ -221,12 +222,12 @@ func AdminCredential() mcclient.TokenCredential {
return manager.adminCredential
}
func AdminSession(region, zone, endpointType, apiVersion string) *mcclient.ClientSession {
func AdminSession(ctx context.Context, region, zone, endpointType, apiVersion string) *mcclient.ClientSession {
cli := Client()
if cli == nil {
return nil
}
return cli.NewSession(region, zone, endpointType, AdminCredential(), apiVersion)
return cli.NewSession(ctx, region, zone, endpointType, AdminCredential(), apiVersion)
}
type AuthCompletedCallback func()
@@ -262,12 +263,13 @@ func Init(info *AuthInfo, debug, insecure bool, certFile, keyFile string) {
<-done
}
func GetAdminSession(region string, apiVersion string) *mcclient.ClientSession {
return GetSession(manager.adminCredential, region, apiVersion)
func GetAdminSession(ctx context.Context, region string,
apiVersion string) *mcclient.ClientSession {
return GetSession(ctx, manager.adminCredential, region, apiVersion)
}
func GetSession(token mcclient.TokenCredential, region string, apiVersion string) *mcclient.ClientSession {
return manager.client.NewSession(region, "", "internal", token, apiVersion)
func GetSession(ctx context.Context, token mcclient.TokenCredential, region string, apiVersion string) *mcclient.ClientSession {
return manager.client.NewSession(ctx, region, "", "internal", token, apiVersion)
}
// use for climc test only
+19 -12
View File
@@ -93,12 +93,11 @@ func joinUrl(baseUrl, path string) string {
return fmt.Sprintf("%s%s", baseUrl, path)
}
func (this *Client) rawRequest(endpoint string, token string, method string, url string, header http.Header, body io.Reader) (*http.Response, error) {
ctx := context.Background()
func (this *Client) rawRequest(ctx context.Context, endpoint string, token string, method string, url string, header http.Header, body io.Reader) (*http.Response, error) {
return httputils.Request(this.httpconn, ctx, method, joinUrl(endpoint, url), getDefaultHeader(header, token), body, this.debug)
}
func (this *Client) jsonRequest(endpoint string, token string, method string, url string, header http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) {
func (this *Client) jsonRequest(ctx context.Context, endpoint string, token string, method string, url string, header http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) {
/*bodystr := ""
if body != nil {
bodystr = body.String()
@@ -110,7 +109,6 @@ func (this *Client) jsonRequest(endpoint string, token string, method string, ur
header.Add("Content-Type", "application/json")
resp, err := this.rawRequest(endpoint, token, method, url, header, jbody)
return this.parseJSONResponse(resp, err)*/
ctx := context.Background()
return httputils.JSONRequest(this.httpconn, ctx, method, joinUrl(endpoint, url), getDefaultHeader(header, token), body, this.debug)
}
@@ -214,7 +212,7 @@ func (this *Client) _authV3(domainName, uname, passwd, projectId, projectName, t
body.Add(jsonutils.NewString("default"), "auth", "scope", "project", "domain", "id")
body.Add(jsonutils.NewString(projectName), "auth", "scope", "project", "name")
}
hdr, rbody, err := this.jsonRequest(this.authUrl, "", "POST", "/auth/tokens", nil, body)
hdr, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "POST", "/auth/tokens", nil, body)
if err != nil {
return nil, err
}
@@ -241,7 +239,7 @@ func (this *Client) _authV2(uname, passwd, tenantId, tenantName, token string) (
if len(token) > 0 {
body.Add(jsonutils.NewString(token), "auth", "token", "id")
}
_, rbody, err := this.jsonRequest(this.authUrl, "", "POST", "/tokens", nil, body)
_, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "POST", "/tokens", nil, body)
if err != nil {
return nil, err
}
@@ -292,7 +290,7 @@ func (this *Client) verifyV3(adminToken, token string) (TokenCredential, error)
header := http.Header{}
header.Add("X-Auth-Token", adminToken)
header.Add("X-Subject-Token", token)
_, rbody, err := this.jsonRequest(this.authUrl, "", "GET", "/auth/tokens", header, nil)
_, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "GET", "/auth/tokens", header, nil)
if err != nil {
return nil, err
}
@@ -303,7 +301,7 @@ func (this *Client) verifyV2(adminToken, token string) (TokenCredential, error)
header := http.Header{}
header.Add("X-Auth-Token", adminToken)
verifyUrl := fmt.Sprintf("/tokens/%s", token)
_, rbody, err := this.jsonRequest(this.authUrl, "", "GET", verifyUrl, header, nil)
_, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "GET", verifyUrl, header, nil)
if err != nil {
return nil, err
}
@@ -329,7 +327,7 @@ func (this *Client) SetProject(tenantId, tenantName string, token TokenCredentia
}
}
func (this *Client) NewSession(region, zone, endpointType string, token TokenCredential, apiVersion string) *ClientSession {
func (this *Client) NewSession(ctx context.Context, region, zone, endpointType string, token TokenCredential, apiVersion string) *ClientSession {
cata := token.GetServiceCatalog()
if this.serviceCatalog == nil {
if cata == nil {
@@ -337,10 +335,19 @@ func (this *Client) NewSession(region, zone, endpointType string, token TokenCre
}
this.serviceCatalog = cata
}
return &ClientSession{client: this, region: region, zone: zone,
endpointType: endpointType, token: token,
if ctx == nil {
ctx = context.Background()
}
return &ClientSession{
ctx: ctx,
client: this,
region: region,
zone: zone,
endpointType: endpointType,
token: token,
defaultApiVersion: apiVersion,
Header: http.Header{}}
Header: http.Header{},
}
}
/*
+14 -2
View File
@@ -1,6 +1,7 @@
package mcclient
import (
"context"
"fmt"
"io"
"io/ioutil"
@@ -9,6 +10,7 @@ import (
"regexp"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/util/httputils"
"yunion.io/x/pkg/utils"
@@ -38,6 +40,8 @@ func EnableApiVersionByModule() {
}
type ClientSession struct {
ctx context.Context
client *Client
region string
zone string
@@ -166,7 +170,11 @@ func (this *ClientSession) RawVersionRequest(
populateHeader(&tmpHeader, headers)
}
populateHeader(&tmpHeader, this.Header)
return this.client.rawRequest(baseurl,
ctx := this.ctx
if this.ctx == nil {
ctx = context.Background()
}
return this.client.rawRequest(ctx, baseurl,
this.token.GetTokenString(),
method, url, tmpHeader, body)
}
@@ -189,7 +197,11 @@ func (this *ClientSession) JSONVersionRequest(
populateHeader(&tmpHeader, headers)
}
populateHeader(&tmpHeader, this.Header)
return this.client.jsonRequest(baseUrl,
ctx := this.ctx
if this.ctx == nil {
ctx = context.Background()
}
return this.client.jsonRequest(ctx, baseUrl,
this.token.GetTokenString(),
method, url, tmpHeader, body)
}
+6 -5
View File
@@ -1,6 +1,7 @@
package aliyun
import (
"context"
"fmt"
"io/ioutil"
"os"
@@ -104,7 +105,7 @@ func (self *SStoragecache) GetPath() string {
return ""
}
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
if len(extId) > 0 {
log.Debugf("UploadImage: Image external ID exists %s", extId)
@@ -120,12 +121,12 @@ func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageI
log.Debugf("UploadImage: no external ID")
}
return self.uploadImage(userCred, imageId, osArch, osType, osDist, isForce)
return self.uploadImage(ctx, userCred, imageId, osArch, osType, osDist, isForce)
}
func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool) (string, error) {
func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool) (string, error) {
// first upload image to oss
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(ctx, options.Options.Region, "")
meta, reader, err := modules.Images.Download(s, imageId)
if err != nil {
@@ -342,7 +343,7 @@ func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imag
} else if err := bucket.DownloadFile(imageList.Objects[0].Key, tmpImageFile.Name(), 12*1024*1024, oss.Routines(3), oss.Progress(&OssProgressListener{})); err != nil {
return nil, err
} else {
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(context.Background(), options.Options.Region, "")
params := jsonutils.Marshal(map[string]string{"image_id": imageId, "disk-format": "raw"})
if file, err := os.Open(tmpImageFile.Name()); err != nil {
return nil, err
+8 -7
View File
@@ -1,6 +1,7 @@
package aws
import (
"context"
"fmt"
"strings"
"time"
@@ -109,7 +110,7 @@ func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imag
return self.downloadImage(userCred, imageId, extId)
}
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
if len(extId) > 0 {
log.Debugf("UploadImage: Image external ID exists %s", extId)
@@ -124,7 +125,7 @@ func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageI
log.Debugf("UploadImage: no external ID")
}
return self.uploadImage(userCred, imageId, osArch, osType, osDist, isForce)
return self.uploadImage(ctx, userCred, imageId, osArch, osType, osDist, isForce)
}
@@ -148,7 +149,7 @@ func (self *SStoragecache) fetchImages() error {
return nil
}
func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool) (string, error) {
func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool) (string, error) {
bucketName := GetBucketName(self.region.GetId(), imageId)
err := self.region.initVmimport(bucketName)
if err != nil {
@@ -161,10 +162,10 @@ func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageI
return "", err
}
defer s3client.DeleteBucket(&s3.DeleteBucketInput{Bucket: &bucketName}) // remove bucket
defer s3client.DeleteBucket(&s3.DeleteBucketInput{Bucket: &bucketName}) // remove bucket
var diskFormat string
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(ctx, options.Options.Region, "")
_, err = s3client.GetObject(&s3.GetObjectInput{Bucket: &bucketName, Key: &imageId})
if err != nil {
// first upload image to oss
@@ -196,7 +197,7 @@ func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageI
if err != nil {
return "", err
}
defer s3client.DeleteObject(&s3.DeleteObjectInput{Bucket: &bucketName, Key: &imageId}) // remove object
defer s3client.DeleteObject(&s3.DeleteObjectInput{Bucket: &bucketName, Key: &imageId}) // remove object
} else {
meta, _, err := modules.Images.Download(s, imageId)
if err != nil {
@@ -300,7 +301,7 @@ func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imag
return nil, err
}
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(context.Background(), options.Options.Region, "")
params := jsonutils.Marshal(map[string]string{"image_id": imageId, "disk-format": "raw"})
if result, err := modules.Images.Upload(s, params, ret.Body, IntVal(ret.ContentLength)); err != nil {
return nil, err
+6 -5
View File
@@ -1,6 +1,7 @@
package azure
import (
"context"
"fmt"
"io"
"io/ioutil"
@@ -98,7 +99,7 @@ func (self *SStoragecache) GetPath() string {
return ""
}
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
if len(extId) > 0 {
log.Debugf("UploadImage: Image external ID exists %s", extId)
status, err := self.region.GetImageStatus(extId)
@@ -111,7 +112,7 @@ func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageI
} else {
log.Debugf("UploadImage: no external ID")
}
return self.uploadImage(userCred, imageId, osArch, osType, osDist, isForce, options.Options.TempPath)
return self.uploadImage(ctx, userCred, imageId, osArch, osType, osDist, isForce, options.Options.TempPath)
}
func (self *SStoragecache) checkStorageAccount() (*SStorageAccount, error) {
@@ -146,8 +147,8 @@ func (self *SStoragecache) checkStorageAccount() (*SStorageAccount, error) {
return storageaccount, nil
}
func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool, tmpPath string) (string, error) {
s := auth.GetAdminSession(options.Options.Region, "")
func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool, tmpPath string) (string, error) {
s := auth.GetAdminSession(ctx, options.Options.Region, "")
meta, reader, err := modules.Images.Download(s, imageId)
if err != nil {
return "", err
@@ -278,7 +279,7 @@ func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imag
log.Debugf("download complate")
}
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(context.Background(), options.Options.Region, "")
params := jsonutils.Marshal(map[string]string{"image_id": imageId, "disk-format": "raw"})
if file, err := os.Open(tmpImageFile.Name()); err != nil {
return nil, err
+3 -1
View File
@@ -127,6 +127,8 @@ func (self *SDatastoreImageCache) DownloadImage(userCred mcclient.TokenCredentia
return nil, cloudprovider.ErrNotImplemented
}
func (self *SDatastoreImageCache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
func (self *SDatastoreImageCache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential,
imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool,
) (string, error) {
return "", cloudprovider.ErrNotImplemented
}
+2 -1
View File
@@ -1,6 +1,7 @@
package k8s
import (
"context"
"sync"
"time"
@@ -92,7 +93,7 @@ func (man *SKubeClusterManager) refreshKubeConfig() {
}
func (man *SKubeClusterManager) getKubeClusterConfig() (string, error) {
session := auth.GetAdminSession(man.region, "v1")
session := auth.GetAdminSession(context.Background(), man.region, "v1")
params := jsonutils.NewDict()
params.Add(jsonutils.JSONTrue, "directly")
ret, err := kubeserver.Clusters.PerformAction(session, "default", "generate-kubeconfig", params)
+3 -1
View File
@@ -1,6 +1,8 @@
package logclient
import (
"context"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/util/stringutils"
@@ -135,7 +137,7 @@ func addLog(model IObject, action string, iNotes interface{}, userCred mcclient.
logentry.Add(jsonutils.NewString(notes), "notes")
logclientWorkerMan.Run(func() {
s := auth.GetSession(userCred, "", "")
s := auth.GetSession(context.Background(), userCred, "", "")
_, err := api.Create(s, logentry)
if err != nil {
log.Errorf("create action log failed %s", err)
+4 -4
View File
@@ -126,7 +126,7 @@ func (self *SStoragecache) GetPath() string {
return ""
}
func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) {
if len(extId) > 0 {
log.Debugf("UploadImage: Image external ID exists %s", extId)
@@ -140,7 +140,7 @@ func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageI
} else {
log.Debugf("UploadImage: no external ID")
}
return self.uploadImage(userCred, imageId, osArch, osType, osDist, osVersion, isForce)
return self.uploadImage(ctx, userCred, imageId, osArch, osType, osDist, osVersion, isForce)
}
func (self *SRegion) getCosUrl(bucket, object string) string {
@@ -148,9 +148,9 @@ func (self *SRegion) getCosUrl(bucket, object string) string {
return fmt.Sprintf("http://%s-%s.cos.%s.myqcloud.com/%s", bucket, self.client.AppID, self.Region, object)
}
func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, isForce bool) (string, error) {
func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, isForce bool) (string, error) {
// first upload image to oss
s := auth.GetAdminSession(options.Options.Region, "")
s := auth.GetAdminSession(ctx, options.Options.Region, "")
meta, reader, err := modules.Images.Download(s, imageId)
if err != nil {
+1 -1
View File
@@ -30,7 +30,7 @@ type SSHtoolSol struct {
func getCommand(ctx context.Context, userCred mcclient.TokenCredential, ip string) (string, *BaseCommand, error) {
cmd := NewBaseCommand(o.Options.SshToolPath)
s := auth.GetAdminSession(o.Options.Region, "v2")
s := auth.GetAdminSession(ctx, o.Options.Region, "v2")
key, err := modules.Sshkeypairs.GetById(s, userCred.GetProjectId(), jsonutils.Marshal(map[string]bool{"admin": true}))
if err != nil {
return "", nil, err
+2 -2
View File
@@ -54,7 +54,7 @@ func fetchK8sEnv(ctx context.Context, w http.ResponseWriter, r *http.Request) (*
}
podName := params["<podName>"]
container, _ := body.GetString("container")
adminSession := auth.GetAdminSession(o.Options.Region, "")
adminSession := auth.GetAdminSession(ctx, o.Options.Region, "")
query := jsonutils.NewDict()
query.Add(jsonutils.NewString(namespace), "namespace")
@@ -107,7 +107,7 @@ func fetchCloudEnv(ctx context.Context, w http.ResponseWriter, r *http.Request)
if userCred == nil {
return nil, httperrors.NewUnauthorizedError("No token founded")
}
s := auth.Client().NewSession(o.Options.Region, "", "internal", userCred, "v2")
s := auth.Client().NewSession(ctx, o.Options.Region, "", "internal", userCred, "v2")
return &CloudEnv{
ClientSessin: s,
Params: params,