From 3eea07f2791c9251da1bb504bb3d07de5216b718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Thu, 20 Sep 2018 10:57:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=BF=E5=85=8D/tmp=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E5=AE=B9=E9=87=8F=E4=B8=8D=E5=A4=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/cloudcommon/options.go | 1 + pkg/cloudprovider/resources.go | 2 +- pkg/compute/hostdrivers/aliyun.go | 12 ++++++++--- pkg/util/aliyun/aliyun.go | 33 ++++++++++++++++--------------- pkg/util/aliyun/storagecache.go | 19 +++++++++++------- 5 files changed, 40 insertions(+), 27 deletions(-) diff --git a/pkg/cloudcommon/options.go b/pkg/cloudcommon/options.go index a6e4fa7336..5d97bf2f0f 100644 --- a/pkg/cloudcommon/options.go +++ b/pkg/cloudcommon/options.go @@ -26,6 +26,7 @@ type Options struct { AdminProject string `help:"Admin project" default:"system" alias:"admin-tenant-name"` CorsHosts []string `help:"List of hostname that allow CORS"` AuthTokenCacheSize uint32 `help:"Auth token Cache Size" default:"2048"` + TempPath string `help:"Path for store temp file, at least 40G space" default:"/opt/yunion/tmp"` ApplicationID string `help:"Application ID"` RequestWorkerCount int `default:"4" help:"Request worker thread count, default is 4"` diff --git a/pkg/cloudprovider/resources.go b/pkg/cloudprovider/resources.go index f4d95664b9..25b17d1387 100644 --- a/pkg/cloudprovider/resources.go +++ b/pkg/cloudprovider/resources.go @@ -79,7 +79,7 @@ type ICloudStoragecache interface { CreateIImage(snapshotId, imageName, imageDesc string) (ICloudImage, error) - DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string) (jsonutils.JSONObject, error) + DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, extId string, isForce bool) (string, error) } diff --git a/pkg/compute/hostdrivers/aliyun.go b/pkg/compute/hostdrivers/aliyun.go index 15842e7a9b..f5e00d3775 100644 --- a/pkg/compute/hostdrivers/aliyun.go +++ b/pkg/compute/hostdrivers/aliyun.go @@ -3,14 +3,16 @@ package hostdrivers import ( "context" "fmt" + "os" "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/compute/models" + "yunion.io/x/onecloud/pkg/compute/options" "yunion.io/x/onecloud/pkg/httperrors" - "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" ) type SAliyunHostDriver struct { @@ -36,7 +38,6 @@ func (self *SAliyunHostDriver) CheckAndSetCacheImage(ctx context.Context, host * osType, _ := params.GetString("os_type") osDist, _ := params.GetString("os_distribution") - isForce := jsonutils.QueryBoolean(params, "is_force", false) userCred := task.GetUserCred() taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { @@ -93,7 +94,12 @@ func (self *SAliyunHostDriver) RequestSaveUploadImageOnHost(ctx context.Context, return nil, err } else { scimg.SetExternalId(iImage.GetId()) - if result, err := iStoragecache.DownloadImage(task.GetUserCred(), imageId, iImage.GetId()); err != nil { + if _, err := os.Stat(options.Options.TempPath); os.IsNotExist(err) { + if err = os.MkdirAll(options.Options.TempPath, 0755); err != nil { + return nil, err + } + } + if result, err := iStoragecache.DownloadImage(task.GetUserCred(), imageId, iImage.GetId(), options.Options.TempPath); err != nil { scimg.SetStatus(task.GetUserCred(), models.CACHED_IMAGE_STATUS_CACHE_FAILED, err.Error()) return nil, err } else { diff --git a/pkg/util/aliyun/aliyun.go b/pkg/util/aliyun/aliyun.go index c8a334a57c..b41cefe251 100644 --- a/pkg/util/aliyun/aliyun.go +++ b/pkg/util/aliyun/aliyun.go @@ -4,11 +4,12 @@ import ( "github.com/aliyun/alibaba-cloud-sdk-go/sdk" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" + "time" + "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/compute/models" - "time" ) const ( @@ -205,11 +206,11 @@ func (self *SAliyunClient) GetIStoragecacheById(id string) (cloudprovider.ICloud } type SAccountBalance struct { - AvailableAmount float64 + AvailableAmount float64 AvailableCashAmount float64 - CreditAmount float64 - MybankCreditAmount float64 - Currency string + CreditAmount float64 + MybankCreditAmount float64 + Currency string } type SCashCoupon struct { @@ -226,15 +227,15 @@ type SCashCoupon struct { } type SPrepaidCard struct { - PrepaidCardId string - PrepaidCardNo string - GrantedTime time.Time - EffectiveTime time.Time - ExpiryTime time.Time - NominalValue float64 - Balance float64 - ApplicableProducts string - ApplicableScenarios string + PrepaidCardId string + PrepaidCardNo string + GrantedTime time.Time + EffectiveTime time.Time + ExpiryTime time.Time + NominalValue float64 + Balance float64 + ApplicableProducts string + ApplicableScenarios string } func (self *SAliyunClient) QueryAccountBalance() (*SAccountBalance, error) { @@ -272,7 +273,7 @@ func (self *SAliyunClient) QueryCashCoupons() ([]SCashCoupon, error) { func (self *SAliyunClient) QueryPrepaidCards() ([]SPrepaidCard, error) { params := make(map[string]string) params["EffectiveOrNot"] = "True" - body, err := self.businessRequest("QueryPrepaidCards", params) + body, err := self.businessRequest("QueryPrepaidCards", params) if err != nil { log.Errorf("QueryPrepaidCards fail %s", err) return nil, err @@ -284,4 +285,4 @@ func (self *SAliyunClient) QueryPrepaidCards() ([]SPrepaidCard, error) { return nil, err } return cards, nil -} \ No newline at end of file +} diff --git a/pkg/util/aliyun/storagecache.go b/pkg/util/aliyun/storagecache.go index 5f8bd8e41c..1bf66aa958 100644 --- a/pkg/util/aliyun/storagecache.go +++ b/pkg/util/aliyun/storagecache.go @@ -2,9 +2,11 @@ package aliyun import ( "fmt" + "io/ioutil" "os" "strings" "time" + "github.com/aliyun/aliyun-oss-go-sdk/oss" "yunion.io/x/jsonutils" "yunion.io/x/log" @@ -252,8 +254,8 @@ func (self *SRegion) createIImage(snapshoutId, imageName, imageDesc string) (str } } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string) (jsonutils.JSONObject, error) { - return self.downloadImage(userCred, imageId, extId) +func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { + return self.downloadImage(userCred, imageId, extId, path) } // 定义进度条监听器。 @@ -279,8 +281,12 @@ func (listener *OssProgressListener) ProgressChanged(event *oss.ProgressEvent) { } } -func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imageId string, extId string) (jsonutils.JSONObject, error) { - tmpImageFile := fmt.Sprintf("/tmp/%s", extId) +func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { + tmpImageFile, err := ioutil.TempFile(path, extId) + if err != nil { + return nil, err + } + defer os.Remove(tmpImageFile.Name()) bucketName := strings.ToLower(fmt.Sprintf("imgcache-%s", self.region.GetId())) if bucket, err := self.region.checkBucket(bucketName); err != nil { return nil, err @@ -294,17 +300,16 @@ func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imag return nil, err } else if len(imageList.Objects) != 1 { return nil, httperrors.NewResourceNotFoundError("exported image not find") - } else if err := bucket.DownloadFile(imageList.Objects[0].Key, tmpImageFile, 12*1024*1024, oss.Routines(3), oss.Progress(&OssProgressListener{})); err != nil { + } 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, "") params := jsonutils.Marshal(map[string]string{"image_id": imageId, "disk-format": "raw"}) - if file, err := os.Open(tmpImageFile); err != nil { + if file, err := os.Open(tmpImageFile.Name()); err != nil { return nil, err } else if result, err := modules.Images.Upload(s, params, file, imageList.Objects[0].Size); err != nil { return nil, err } else { - os.Remove(tmpImageFile) return result, nil } }