minor improvements

This commit is contained in:
Qiu Jian
2018-12-28 20:24:23 +08:00
parent a3e802d803
commit 1e4789285d
9 changed files with 94 additions and 16 deletions
+19 -1
View File
@@ -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)
}
+3 -2
View File
@@ -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"`
+1 -1
View File
@@ -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
}
+13 -7
View File
@@ -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
}
}
+2
View File
@@ -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 (
+2
View File
@@ -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!!")
})
+15 -3
View File
@@ -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)
}
+39
View File
@@ -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
}
-2
View File
@@ -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 {