mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
glance重构 update 1
Conflicts: Gopkg.lock pkg/appsrv/appsrv.go pkg/cloudcommon/options.go pkg/compute/models/hosts.go pkg/compute/models/quotas.go pkg/compute/service/service.go pkg/mcclient/mcclient.go
This commit is contained in:
@@ -11,7 +11,7 @@ func init() {
|
||||
Limit int64 `help:"Limit, default 0, i.e. no limit" default:"20"`
|
||||
Offset int64 `help:"Offset, default 0, i.e. no offset"`
|
||||
Region string `help:"Search by region"`
|
||||
ServiceId string `help:"Search by service id"`
|
||||
Service string `help:"Search by service id or name"`
|
||||
Interface string `help:"Search by interface"`
|
||||
}
|
||||
R(&EndpointListOptions{}, "endpoint-list", "List service endpoints", func(s *mcclient.ClientSession, args *EndpointListOptions) error {
|
||||
@@ -29,8 +29,16 @@ func init() {
|
||||
if len(args.Region) > 0 {
|
||||
query.Add(jsonutils.NewString(args.Region), "region_id")
|
||||
}
|
||||
if len(args.ServiceId) > 0 {
|
||||
query.Add(jsonutils.NewString(args.ServiceId), "service_id")
|
||||
if len(args.Service) > 0 {
|
||||
srvMod, err := modules.GetModule(s, "services")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
srvId, err := srvMod.GetId(s, args.Service, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
query.Add(jsonutils.NewString(srvId), "service_id")
|
||||
}
|
||||
if len(args.Interface) > 0 {
|
||||
query.Add(jsonutils.NewString(args.Interface), "interface")
|
||||
|
||||
@@ -396,4 +396,35 @@ func init() {
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&ImageShowOptions{}, "image-private", "Make a image private", func(s *mcclient.ClientSession, args *ImageShowOptions) error {
|
||||
if len(args.ID) == 0 {
|
||||
return fmt.Errorf("No image ID provided")
|
||||
} else if len(args.ID) == 1 {
|
||||
result, err := modules.Images.PerformAction(s, args.ID[0], "private", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(result)
|
||||
} else {
|
||||
results := modules.Images.BatchPerformAction(s, args.ID, "private", nil)
|
||||
printBatchResults(results, modules.Images.GetColumns(s))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
R(&ImageShowOptions{}, "image-public", "Make a image public", func(s *mcclient.ClientSession, args *ImageShowOptions) error {
|
||||
if len(args.ID) == 0 {
|
||||
return fmt.Errorf("No image ID provided")
|
||||
} else if len(args.ID) == 1 {
|
||||
result, err := modules.Images.PerformAction(s, args.ID[0], "public", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(result)
|
||||
} else {
|
||||
results := modules.Images.BatchPerformAction(s, args.ID, "public", nil)
|
||||
printBatchResults(results, modules.Images.GetColumns(s))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
+54
-67
@@ -7,70 +7,28 @@ import (
|
||||
)
|
||||
|
||||
type QuotaBaseOptions struct {
|
||||
Cpu int64 `help:"CPU count"`
|
||||
Memory int64 `help:"Memory size in MB"`
|
||||
Storage int64 `help:"Storage size in MB"`
|
||||
Port int64 `help:"Internal NIC count"`
|
||||
Eport int64 `help:"External NIC count"`
|
||||
Eip int64 `help:"Elastic IP count"`
|
||||
Bw int64 `help:"Internal bandwidth in Mbps"`
|
||||
Ebw int64 `help:"External bandwidth in Mbps"`
|
||||
Image int64 `help:"Template count"`
|
||||
IsolatedDevice int64 `help:"Isolated device count"`
|
||||
Snapshot int64 `help:"Snapshot count"`
|
||||
Cpu int64 `help:"CPU count" json:"cpu:omitzero"`
|
||||
Memory int64 `help:"Memory size in MB" json:"memory,omitzero"`
|
||||
Storage int64 `help:"Storage size in MB" json:"storage,omitzero"`
|
||||
Port int64 `help:"Internal NIC count" json:"port,omitzero"`
|
||||
Eport int64 `help:"External NIC count" json:"eport,omitzero"`
|
||||
Eip int64 `help:"Elastic IP count" json:"eip,omitzero"`
|
||||
Bw int64 `help:"Internal bandwidth in Mbps" json:"bw,omitzero"`
|
||||
Ebw int64 `help:"External bandwidth in Mbps" json:"ebw,omitzero"`
|
||||
IsolatedDevice int64 `help:"Isolated device count" json:"isolated_device,omitzero"`
|
||||
Snapshot int64 `help:"Snapshot count" json:"snapshot,omitzero"`
|
||||
}
|
||||
|
||||
func quotaArgs2Params(args *QuotaBaseOptions) *jsonutils.JSONDict {
|
||||
params := jsonutils.NewDict()
|
||||
if args.Cpu > 0 {
|
||||
params.Add(jsonutils.NewInt(args.Cpu), "cpu")
|
||||
}
|
||||
if args.Memory > 0 {
|
||||
params.Add(jsonutils.NewInt(args.Memory), "memory")
|
||||
}
|
||||
if args.Storage > 0 {
|
||||
params.Add(jsonutils.NewInt(args.Storage), "storage")
|
||||
}
|
||||
if args.Image > 0 {
|
||||
params.Add(jsonutils.NewInt(args.Image), "image")
|
||||
}
|
||||
if args.Port > 0 {
|
||||
params.Add(jsonutils.NewInt(args.Port), "port")
|
||||
}
|
||||
if args.Eport > 0 {
|
||||
params.Add(jsonutils.NewInt(args.Eport), "eport")
|
||||
}
|
||||
if args.Eip > 0 {
|
||||
params.Add(jsonutils.NewInt(args.Eip), "eip")
|
||||
}
|
||||
if args.Bw > 0 {
|
||||
params.Add(jsonutils.NewInt(args.Bw), "bw")
|
||||
}
|
||||
if args.Ebw > 0 {
|
||||
params.Add(jsonutils.NewInt(args.Ebw), "ebw")
|
||||
}
|
||||
if args.IsolatedDevice > 0 {
|
||||
params.Add(jsonutils.NewInt(args.IsolatedDevice), "isolated_device")
|
||||
}
|
||||
if args.Snapshot > 0 {
|
||||
params.Add(jsonutils.NewInt(args.Snapshot), "snapshot")
|
||||
}
|
||||
return params
|
||||
type ImageQuotaBaseOptions struct {
|
||||
Image int64 `help:"Template count" json:"image,omitzero"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
type QuotaOptions struct {
|
||||
Tenant string `help:"Tenant name of ID"`
|
||||
User string `help:"User name of ID"`
|
||||
}
|
||||
R(&QuotaOptions{}, "quota", "Show quota for current user or tenant", func(s *mcclient.ClientSession, args *QuotaOptions) error {
|
||||
params := jsonutils.NewDict()
|
||||
if len(args.Tenant) > 0 {
|
||||
params.Add(jsonutils.NewString(args.Tenant), "tenant")
|
||||
}
|
||||
if len(args.User) > 0 {
|
||||
params.Add(jsonutils.NewString(args.User), "user")
|
||||
}
|
||||
params := jsonutils.Marshal(args)
|
||||
result, err := modules.Quotas.GetQuota(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -78,20 +36,22 @@ func init() {
|
||||
printObject(result)
|
||||
return nil
|
||||
})
|
||||
R(&QuotaOptions{}, "image-quota", "Show image quota for current user or tenant", func(s *mcclient.ClientSession, args *QuotaOptions) error {
|
||||
params := jsonutils.Marshal(args)
|
||||
result, err := modules.ImageQuotas.GetQuota(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(result)
|
||||
return nil
|
||||
})
|
||||
|
||||
type QuotaSetOptions struct {
|
||||
Tenant string `help:"Tenant name or ID to set quota"`
|
||||
User string `help:"User name of ID"`
|
||||
Tenant string `help:"Tenant name or ID to set quota" json:"tenant,omitempty"`
|
||||
QuotaBaseOptions
|
||||
}
|
||||
R(&QuotaSetOptions{}, "quota-set", "Set quota for tenant", func(s *mcclient.ClientSession, args *QuotaSetOptions) error {
|
||||
params := quotaArgs2Params(&args.QuotaBaseOptions)
|
||||
if len(args.Tenant) > 0 {
|
||||
params.Add(jsonutils.NewString(args.Tenant), "tenant")
|
||||
}
|
||||
if len(args.User) > 0 {
|
||||
params.Add(jsonutils.NewString(args.User), "user")
|
||||
}
|
||||
params := jsonutils.Marshal(args)
|
||||
result, e := modules.Quotas.DoQuotaSet(s, params)
|
||||
if e != nil {
|
||||
return e
|
||||
@@ -100,13 +60,26 @@ func init() {
|
||||
return nil
|
||||
})
|
||||
|
||||
type ImageQuotaSetOptions struct {
|
||||
Tenant string `help:"Tenant name or ID to set quota" json:"tenant,omitempty"`
|
||||
ImageQuotaBaseOptions
|
||||
}
|
||||
R(&ImageQuotaSetOptions{}, "image-quota-set", "Set image quota for tenant", func(s *mcclient.ClientSession, args *ImageQuotaSetOptions) error {
|
||||
params := jsonutils.Marshal(args)
|
||||
result, e := modules.ImageQuotas.DoQuotaSet(s, params)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
printObject(result)
|
||||
return nil
|
||||
})
|
||||
|
||||
type QuotaCheckOptions struct {
|
||||
TENANT string `help:"Tenant name or ID to check quota"`
|
||||
TENANT string `help:"Tenant name or ID to check quota" json:"tenant,omitempty"`
|
||||
QuotaBaseOptions
|
||||
}
|
||||
R(&QuotaCheckOptions{}, "quota-check", "Check quota for tenant", func(s *mcclient.ClientSession, args *QuotaCheckOptions) error {
|
||||
params := quotaArgs2Params(&args.QuotaBaseOptions)
|
||||
params.Add(jsonutils.NewString(args.TENANT), "tenant")
|
||||
params := jsonutils.Marshal(args)
|
||||
result, e := modules.Quotas.DoQuotaCheck(s, params)
|
||||
if e != nil {
|
||||
return e
|
||||
@@ -115,4 +88,18 @@ func init() {
|
||||
return nil
|
||||
})
|
||||
|
||||
type ImageQuotaCheckOptions struct {
|
||||
TENANT string `help:"Tenant name or ID to check quota" json:"tenant,omitempty"`
|
||||
ImageQuotaBaseOptions
|
||||
}
|
||||
R(&ImageQuotaCheckOptions{}, "image-quota-check", "Check quota for tenant", func(s *mcclient.ClientSession, args *ImageQuotaCheckOptions) error {
|
||||
params := jsonutils.Marshal(args)
|
||||
result, e := modules.ImageQuotas.DoQuotaCheck(s, params)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
printObject(result)
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@@ -112,4 +112,20 @@ func init() {
|
||||
printObject(result)
|
||||
return nil
|
||||
})
|
||||
|
||||
type ImageUsageOptions struct {
|
||||
Project string `help:"check image usage of a project"`
|
||||
}
|
||||
R(&ImageUsageOptions{}, "image-usage", "Show general usage of images", func(s *mcclient.ClientSession, args *ImageUsageOptions) error {
|
||||
params := jsonutils.NewDict()
|
||||
if args.Project != "" {
|
||||
params.Add(jsonutils.NewString(args.Project), "project")
|
||||
}
|
||||
result, err := modules.ImageUsages.GetUsage(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(result)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/image/service"
|
||||
)
|
||||
|
||||
func main() {
|
||||
service.StartService()
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"os/signal"
|
||||
"path"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/structarg"
|
||||
"yunion.io/x/pkg/util/version"
|
||||
"yunion.io/x/onecloud/pkg/util/torrentutils"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
structarg.BaseOptions
|
||||
|
||||
ROOT string `help:"Root directory to seed files"`
|
||||
TORRENT string `help:"path to torrent file"`
|
||||
|
||||
Tracker []string `help:"Tracker urls, e.g. http://10.168.222.252:6969/announce or udp://tracker.istole.it:6969"`
|
||||
}
|
||||
|
||||
func exitSignalHandlers(client *torrent.Client) {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||
for {
|
||||
log.Printf("close signal received: %+v", <-c)
|
||||
client.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
options := Options{}
|
||||
|
||||
parser, err := structarg.NewArgumentParser(&options, "torrent-srv", "bit-torrent server", "2018")
|
||||
if err != nil {
|
||||
log.Fatalf("%s", err)
|
||||
}
|
||||
|
||||
parser.ParseArgs(os.Args[1:], true)
|
||||
|
||||
if options.Help {
|
||||
fmt.Println(parser.HelpString())
|
||||
return
|
||||
}
|
||||
|
||||
if len(os.Args) <= 1 {
|
||||
fmt.Print(parser.Usage())
|
||||
return
|
||||
}
|
||||
|
||||
if options.Version {
|
||||
fmt.Println(version.GetJsonString())
|
||||
return
|
||||
}
|
||||
|
||||
root, err := filepath.Abs(options.ROOT)
|
||||
if err != nil {
|
||||
log.Fatalf("fail to get absolute path: %s", err)
|
||||
}
|
||||
|
||||
var mi *metainfo.MetaInfo
|
||||
|
||||
if len(options.Tracker) > 0 {
|
||||
// server mode
|
||||
mi, err = torrentutils.GenerateTorrent(root, options.Tracker, options.TORRENT)
|
||||
if err != nil {
|
||||
log.Fatalf("fail to save torrent file %s", err)
|
||||
}
|
||||
|
||||
} else {
|
||||
// client mode, load mi from torrent file
|
||||
mi, err = metainfo.LoadFromFile(options.TORRENT)
|
||||
if err != nil {
|
||||
log.Fatalf("fail to open torrent file %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
clientConfig := torrent.NewDefaultClientConfig()
|
||||
clientConfig.Debug = false
|
||||
clientConfig.Seed = true
|
||||
if len(options.Tracker) > 0 {
|
||||
// server mode
|
||||
clientConfig.DataDir = path.Dir(root)
|
||||
} else {
|
||||
// client mode
|
||||
clientConfig.DataDir = root
|
||||
}
|
||||
clientConfig.DisableTrackers = false
|
||||
clientConfig.DisablePEX = false
|
||||
clientConfig.NoDHT = true
|
||||
|
||||
client, err := torrent.NewClient(clientConfig)
|
||||
if err != nil {
|
||||
log.Fatalf("error creating client: %s", err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
go exitSignalHandlers(client)
|
||||
|
||||
t, err := client.AddTorrent(mi)
|
||||
if err != nil {
|
||||
log.Fatalf("%s", err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
<-t.GotInfo()
|
||||
|
||||
files := t.Info().Files
|
||||
log.Debugf("Got Info, start download %d files", len(files))
|
||||
for i := 0; i < len(files); i += 1 {
|
||||
log.Debugf("%d: %s", i, files[i].Path)
|
||||
}
|
||||
|
||||
t.DownloadAll()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
<-client.Closed()
|
||||
log.Debugf("client closed, exit!")
|
||||
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
for {
|
||||
if t.BytesCompleted() == t.Info().TotalLength() {
|
||||
fmt.Printf("\rSeeding.............")
|
||||
} else {
|
||||
fmt.Printf("\rDownload: %.1f%%", float64(t.BytesCompleted())*100.0/float64(t.Info().TotalLength()))
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user