From 1e4789285dea7a5dc7723174bfed5df445ed689c Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Fri, 28 Dec 2018 20:24:23 +0800 Subject: [PATCH] minor improvements --- pkg/cloudcommon/app.go | 20 ++++++++++++++- pkg/cloudcommon/options.go | 5 ++-- pkg/image/models/image_subs.go | 2 +- pkg/image/models/images.go | 20 +++++++++------ pkg/image/options/options.go | 2 ++ pkg/image/service/service.go | 2 ++ pkg/util/qemuimg/consts.go | 18 ++++++++++--- pkg/util/seclib2/certfile.go | 39 +++++++++++++++++++++++++++++ pkg/util/streamutils/streamutils.go | 2 -- 9 files changed, 94 insertions(+), 16 deletions(-) create mode 100644 pkg/util/seclib2/certfile.go diff --git a/pkg/cloudcommon/app.go b/pkg/cloudcommon/app.go index b38a02db05..6234305c93 100644 --- a/pkg/cloudcommon/app.go +++ b/pkg/cloudcommon/app.go @@ -2,10 +2,13 @@ package cloudcommon import ( "net" + "os" "strconv" "yunion.io/x/log" + "yunion.io/x/onecloud/pkg/appsrv" + "yunion.io/x/onecloud/pkg/util/seclib2" ) func InitApp(options *CommonOptions, dbAccess bool) *appsrv.Application { @@ -29,7 +32,22 @@ func ServeForever(app *appsrv.Application, options *CommonOptions) { } log.Infof("Start listen on %s://%s", proto, addr) if options.EnableSsl { - app.ListenAndServeTLS(addr, options.SslCertfile, options.SslKeyfile) + certfile := options.SslCertfile + if len(options.SslCafile) > 0 { + var err error + certfile, err = seclib2.MergeCaCertFiles(options.SslCafile, options.SslCertfile) + if err != nil { + log.Fatalf("fail to merge ca+cert content: %s", err) + } + defer os.Remove(certfile) + } + if len(certfile) == 0 { + log.Fatalf("Missing ssl-certfile") + } + if len(options.SslKeyfile) == 0 { + log.Fatalf("Missing ssl-keyfile") + } + app.ListenAndServeTLS(addr, certfile, options.SslKeyfile) } else { app.ListenAndServe(addr) } diff --git a/pkg/cloudcommon/options.go b/pkg/cloudcommon/options.go index e30b4e5c5d..e050237639 100644 --- a/pkg/cloudcommon/options.go +++ b/pkg/cloudcommon/options.go @@ -39,8 +39,9 @@ type CommonOptions struct { NotifyAdminUser string `default:"sysadmin" help:"System administrator user ID or name to notify"` EnableSsl bool `help:"Enable https"` - SslCertfile string `help:"ssl certification file"` - SslKeyfile string `help:"ssl certification key file"` + SslCafile string `help:"ssl certificate ca root file, separating ca and cert file is not encouraged" alias:"ca-file"` + SslCertfile string `help:"ssl certification file, normally combines all the certificates in the chain" alias:"cert-file"` + SslKeyfile string `help:"ssl certification private key file" alias:"key-file"` EnableRbac bool `help:"Switch on Role-based Access Control" default:"true"` RbacDebug bool `help:"turn on rbac debug log" default:"false"` diff --git a/pkg/image/models/image_subs.go b/pkg/image/models/image_subs.go index 351998dc0b..7e742d392b 100644 --- a/pkg/image/models/image_subs.go +++ b/pkg/image/models/image_subs.go @@ -92,7 +92,7 @@ func (self *SImageSubformat) DoConvert(image *SImage) error { log.Errorf("fail to seed torrent %s", err) return err } - log.Infof("Start seeding...") + // log.Infof("Start seeding...") return nil } diff --git a/pkg/image/models/images.go b/pkg/image/models/images.go index 6f36f73307..640bf90958 100644 --- a/pkg/image/models/images.go +++ b/pkg/image/models/images.go @@ -54,8 +54,6 @@ const ( ) var ( - candidateSubImageFormats = []qemuimg.TImageFormat{qemuimg.QCOW2, qemuimg.VMDK, qemuimg.VHD} - imageDeadStatus = []string{IMAGE_STATUS_DEACTIVATED, IMAGE_STATUS_KILLED, IMAGE_STATUS_DELETED, IMAGE_STATUS_PENDING_DELETE} ) @@ -761,6 +759,11 @@ func (self *SImage) newSubformat(format qemuimg.TImageFormat, migrate bool) erro } func (self *SImage) MigrateSubImage() error { + if !qemuimg.IsSupportedImageFormat(self.DiskFormat) { + log.Warningf("Unsupported image format %s, no need to migrate", self.DiskFormat) + return nil + } + subimg := ImageSubformatManager.FetchSubImage(self.Id, self.DiskFormat) if subimg != nil { return nil @@ -798,12 +801,15 @@ func (self *SImage) MakeSubImages() error { if self.GetImageType() == ImageTypeISO { return nil } - for _, format := range candidateSubImageFormats { - if string(format) != self.DiskFormat { + for _, format := range options.Options.TargetImageFormats { + if !qemuimg.IsSupportedImageFormat(format) { + continue + } + if format != self.DiskFormat { // need to create a record - subformat := ImageSubformatManager.FetchSubImage(self.Id, string(format)) + subformat := ImageSubformatManager.FetchSubImage(self.Id, format) if subformat == nil { - err := self.newSubformat(format, false) + err := self.newSubformat(qemuimg.String2ImageFormat(format), false) if err != nil { return err } @@ -984,7 +990,7 @@ func (self *SImage) DoCheckStatus(ctx context.Context, userCred mcclient.TokenCr } for i := 0; i < len(subimgs); i += 1 { subimgs[i].checkStatus(useFast) - if subimgs[i].Status != IMAGE_STATUS_ACTIVE || subimgs[i].Status != IMAGE_STATUS_ACTIVE { + if subimgs[i].Status != IMAGE_STATUS_ACTIVE { needConvert = true } } diff --git a/pkg/image/options/options.go b/pkg/image/options/options.go index 675c6f7a2c..7cf2ea593f 100644 --- a/pkg/image/options/options.go +++ b/pkg/image/options/options.go @@ -21,6 +21,8 @@ type SImageOptions struct { TorrentStoreDir string `help:"directory to store image torrent files"` EnableTorrentService bool `help:"Enable torrent service" default:"false"` + + TargetImageFormats []string `help:"target image formats that the system will automatically convert to" default:"qcow2,vmdk,vhd"` } var ( diff --git a/pkg/image/service/service.go b/pkg/image/service/service.go index 48cf2dc892..c50414e132 100644 --- a/pkg/image/service/service.go +++ b/pkg/image/service/service.go @@ -33,6 +33,8 @@ func StartService() { opts.Port = opts.PortV2 } + log.Infof("Target image formats %#v", opts.TargetImageFormats) + cloudcommon.InitAuth(commonOpts, func() { log.Infof("Auth complete!!") }) diff --git a/pkg/util/qemuimg/consts.go b/pkg/util/qemuimg/consts.go index 7ff2af46d1..c811ffdddf 100644 --- a/pkg/util/qemuimg/consts.go +++ b/pkg/util/qemuimg/consts.go @@ -2,8 +2,7 @@ package qemuimg import ( "strings" - - "yunion.io/x/log" + // "yunion.io/x/log" ) type TImageFormat string @@ -16,6 +15,19 @@ const ( RAW = TImageFormat("raw") ) +var supportedImageFormats = []TImageFormat{ + QCOW2, VMDK, VHD, ISO, RAW, +} + +func IsSupportedImageFormat(fmtStr string) bool { + for i := 0; i < len(supportedImageFormats); i += 1 { + if fmtStr == string(supportedImageFormats[i]) { + return true + } + } + return false +} + func (fmt TImageFormat) String() string { switch string(fmt) { case "vhd": @@ -38,6 +50,6 @@ func String2ImageFormat(fmt string) TImageFormat { case "raw": return RAW } - log.Fatalf("unknown image format!!! %s", fmt) + // log.Fatalf("unknown image format!!! %s", fmt) return TImageFormat(fmt) } diff --git a/pkg/util/seclib2/certfile.go b/pkg/util/seclib2/certfile.go new file mode 100644 index 0000000000..1f52d1acab --- /dev/null +++ b/pkg/util/seclib2/certfile.go @@ -0,0 +1,39 @@ +package seclib2 + +import ( + "fmt" + "io/ioutil" + "strings" +) + +const ( + certBeginString = "BEGIN CERTIFICATE" +) + +func MergeCaCertFiles(cafile string, certfile string) (string, error) { + tmpfile, err := ioutil.TempFile("", "cerfile.*.crt") + if err != nil { + return "", fmt.Errorf("fail to open tempfile for ca cerfile: %s", err) + } + defer tmpfile.Close() + + cont, err := ioutil.ReadFile(certfile) + if err != nil { + return "", fmt.Errorf("fail to read certfile %s", err) + } + offset := strings.Index(string(cont), certBeginString) + if offset < 0 { + return "", fmt.Errorf("invalid certfile, no BEGIN CERTIFICATE found") + } + for offset > 0 && cont[offset-1] == '-' { + offset -= 1 + } + tmpfile.Write(cont[offset:]) + cont, err = ioutil.ReadFile(cafile) + if err != nil { + return "", fmt.Errorf("fail to read cafile %s", err) + } + tmpfile.Write(cont) + + return tmpfile.Name(), nil +} diff --git a/pkg/util/streamutils/streamutils.go b/pkg/util/streamutils/streamutils.go index eb43412665..65b23ba62b 100644 --- a/pkg/util/streamutils/streamutils.go +++ b/pkg/util/streamutils/streamutils.go @@ -30,8 +30,6 @@ func StreamPipe(reader io.Reader, writer io.Writer) (*SStreamProperty, error) { } offset += m } - } else if n == 0 { - break } if err != nil { if err == io.EOF {