| @@ -2,15 +2,4 @@ package cmd | |||||
| import "github.com/spf13/cobra" | import "github.com/spf13/cobra" | ||||
| var RootCmd = &cobra.Command{ | |||||
| Use: "agent", | |||||
| } | |||||
| func init() { | |||||
| var configPath string | |||||
| RootCmd.Flags().StringVarP(&configPath, "config", "c", "", "path to config file") | |||||
| RootCmd.Run = func(cmd *cobra.Command, args []string) { | |||||
| serve(configPath) | |||||
| } | |||||
| } | |||||
| var RootCmd = &cobra.Command{} | |||||
| @@ -7,6 +7,7 @@ import ( | |||||
| "os" | "os" | ||||
| "github.com/go-co-op/gocron/v2" | "github.com/go-co-op/gocron/v2" | ||||
| "github.com/spf13/cobra" | |||||
| "gitlink.org.cn/cloudream/storage2/agent/internal/http" | "gitlink.org.cn/cloudream/storage2/agent/internal/http" | ||||
| "gitlink.org.cn/cloudream/storage2/common/pkgs/storage/pool" | "gitlink.org.cn/cloudream/storage2/common/pkgs/storage/pool" | ||||
| @@ -30,7 +31,21 @@ import ( | |||||
| cmdsvc "gitlink.org.cn/cloudream/storage2/agent/internal/mq" | cmdsvc "gitlink.org.cn/cloudream/storage2/agent/internal/mq" | ||||
| ) | ) | ||||
| func serve(configPath string) { | |||||
| func init() { | |||||
| var configPath string | |||||
| var httpAddr string | |||||
| cmd := &cobra.Command{ | |||||
| Run: func(cmd *cobra.Command, args []string) { | |||||
| serve(configPath, httpAddr) | |||||
| }, | |||||
| } | |||||
| cmd.Flags().StringVarP(&configPath, "config", "c", "", "path to config file") | |||||
| cmd.Flags().StringVarP(&httpAddr, "http", "", "127.0.0.1:8890", "http listen address") | |||||
| RootCmd.AddCommand(cmd) | |||||
| } | |||||
| func serve(configPath string, httpAddr string) { | |||||
| err := config.Init(configPath) | err := config.Init(configPath) | ||||
| if err != nil { | if err != nil { | ||||
| fmt.Printf("init config failed, err: %s", err.Error()) | fmt.Printf("init config failed, err: %s", err.Error()) | ||||
| @@ -58,7 +73,7 @@ func serve(configPath string) { | |||||
| worker := exec.NewWorker() | worker := exec.NewWorker() | ||||
| // 初始化HTTP服务 | // 初始化HTTP服务 | ||||
| httpSvr, err := http.NewServer(config.Cfg().ListenAddr, http.NewService(&worker, stgPool)) | |||||
| httpSvr, err := http.NewServer(httpAddr, http.NewService(&worker, stgPool)) | |||||
| if err != nil { | if err != nil { | ||||
| logger.Fatalf("new http server failed, err: %s", err.Error()) | logger.Fatalf("new http server failed, err: %s", err.Error()) | ||||
| } | } | ||||
| @@ -13,7 +13,6 @@ import ( | |||||
| type Config struct { | type Config struct { | ||||
| ID cdssdk.HubID `json:"id"` | ID cdssdk.HubID `json:"id"` | ||||
| ListenAddr string `json:"listenAddr"` | |||||
| Local stgglb.LocalMachineInfo `json:"local"` | Local stgglb.LocalMachineInfo `json:"local"` | ||||
| GRPC *grpc.Config `json:"grpc"` | GRPC *grpc.Config `json:"grpc"` | ||||
| Logger log.Config `json:"logger"` | Logger log.Config `json:"logger"` | ||||
| @@ -0,0 +1,61 @@ | |||||
| package cmdline | |||||
| import ( | |||||
| "fmt" | |||||
| "os" | |||||
| "github.com/spf13/cobra" | |||||
| "gitlink.org.cn/cloudream/storage2/client/internal/config" | |||||
| clitypes "gitlink.org.cn/cloudream/storage2/client/types" | |||||
| "gorm.io/driver/mysql" | |||||
| "gorm.io/gorm" | |||||
| ) | |||||
| func init() { | |||||
| var configPath string | |||||
| cmd := cobra.Command{ | |||||
| Use: "migrate", | |||||
| Short: "Run database migrations", | |||||
| Run: func(cmd *cobra.Command, args []string) { | |||||
| migrate(configPath) | |||||
| }, | |||||
| } | |||||
| cmd.Flags().StringVarP(&configPath, "config", "c", "", "Path to config file") | |||||
| RootCmd.AddCommand(&cmd) | |||||
| } | |||||
| func migrate(configPath string) { | |||||
| // TODO 将create_database.sql的内容逐渐移动到这里来 | |||||
| err := config.Init(configPath) | |||||
| if err != nil { | |||||
| fmt.Println(err) | |||||
| os.Exit(1) | |||||
| } | |||||
| db, err := gorm.Open(mysql.Open(config.Cfg().DB.MakeSourceString())) | |||||
| if err != nil { | |||||
| fmt.Println(err) | |||||
| os.Exit(1) | |||||
| } | |||||
| db = db.Set("gorm:table_options", "CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci") | |||||
| migrateOne(db, clitypes.Bucket{}) | |||||
| migrateOne(db, clitypes.ObjectAccessStat{}) | |||||
| migrateOne(db, clitypes.ObjectBlock{}) | |||||
| migrateOne(db, clitypes.Object{}) | |||||
| migrateOne(db, clitypes.PackageAccessStat{}) | |||||
| migrateOne(db, clitypes.Package{}) | |||||
| migrateOne(db, clitypes.PinnedObject{}) | |||||
| migrateOne(db, clitypes.UserSpace{}) | |||||
| fmt.Println("migrate success") | |||||
| } | |||||
| func migrateOne[T any](db *gorm.DB, model T) { | |||||
| err := db.AutoMigrate(model) | |||||
| if err != nil { | |||||
| fmt.Printf("migratting model %T: %v\n", model, err) | |||||
| os.Exit(1) | |||||
| } | |||||
| } | |||||
| @@ -18,6 +18,7 @@ type Config struct { | |||||
| Local stgglb.LocalMachineInfo `json:"local"` | Local stgglb.LocalMachineInfo `json:"local"` | ||||
| AgentGRPC agtrpc.PoolConfig `json:"agentGRPC"` | AgentGRPC agtrpc.PoolConfig `json:"agentGRPC"` | ||||
| Logger logger.Config `json:"logger"` | Logger logger.Config `json:"logger"` | ||||
| DB db.Config `json:"db"` | |||||
| RabbitMQ mq.Config `json:"rabbitMQ"` | RabbitMQ mq.Config `json:"rabbitMQ"` | ||||
| DistLock distlock.Config `json:"distlock"` | DistLock distlock.Config `json:"distlock"` | ||||
| Connectivity connectivity.Config `json:"connectivity"` | Connectivity connectivity.Config `json:"connectivity"` | ||||
| @@ -27,7 +28,6 @@ type Config struct { | |||||
| AuthAccessKey string `json:"authAccessKey"` // TODO 临时办法 | AuthAccessKey string `json:"authAccessKey"` // TODO 临时办法 | ||||
| AuthSecretKey string `json:"authSecretKey"` | AuthSecretKey string `json:"authSecretKey"` | ||||
| MaxHTTPBodySize int64 `json:"maxHttpBodySize"` | MaxHTTPBodySize int64 `json:"maxHttpBodySize"` | ||||
| DB db.Config `json:"db"` | |||||
| } | } | ||||
| var cfg Config | var cfg Config | ||||
| @@ -36,7 +36,7 @@ func (db *UserSpaceDB) GetAll(ctx SQLContext) ([]types.UserSpace, error) { | |||||
| return stgs, err | return stgs, err | ||||
| } | } | ||||
| func (db *UserSpaceDB) BatchGetAllUserSpaceIDs(ctx SQLContext, start int, count int) ([]types.UserSpaceID, error) { | |||||
| func (db *UserSpaceDB) BatchGetAllSpaceIDs(ctx SQLContext, start int, count int) ([]types.UserSpaceID, error) { | |||||
| var ret []types.UserSpaceID | var ret []types.UserSpaceID | ||||
| err := ctx.Table("UserSpace").Select("UserSpaceID").Find(&ret).Limit(count).Offset(start).Error | err := ctx.Table("UserSpace").Select("UserSpaceID").Find(&ret).Limit(count).Offset(start).Error | ||||
| return ret, err | return ret, err | ||||
| @@ -1,7 +1,6 @@ | |||||
| { | { | ||||
| "id": 1, | "id": 1, | ||||
| "local": { | "local": { | ||||
| "hubID": 1, | |||||
| "localIP": "127.0.0.1", | "localIP": "127.0.0.1", | ||||
| "externalIP": "127.0.0.1", | "externalIP": "127.0.0.1", | ||||
| "locationID": 1 | "locationID": 1 | ||||
| @@ -36,12 +35,5 @@ | |||||
| }, | }, | ||||
| "connectivity": { | "connectivity": { | ||||
| "testInterval": 300 | "testInterval": 300 | ||||
| }, | |||||
| "downloader": { | |||||
| "maxStripCacheCount": 100, | |||||
| "ecStripPrefetchCount": 1 | |||||
| }, | |||||
| "downloadStrategy": { | |||||
| "highLatencyHub": 35 | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,5 +1,6 @@ | |||||
| { | { | ||||
| "local": { | "local": { | ||||
| "userID": 1, | |||||
| "localIP": "127.0.0.1", | "localIP": "127.0.0.1", | ||||
| "externalIP": "127.0.0.1", | "externalIP": "127.0.0.1", | ||||
| "locationID": 1 | "locationID": 1 | ||||
| @@ -11,6 +12,12 @@ | |||||
| "output": "stdout", | "output": "stdout", | ||||
| "level": "debug" | "level": "debug" | ||||
| }, | }, | ||||
| "db": { | |||||
| "address": "127.0.0.1:3306", | |||||
| "account": "root", | |||||
| "password": "123456", | |||||
| "databaseName": "cloudream" | |||||
| }, | |||||
| "rabbitMQ": { | "rabbitMQ": { | ||||
| "address": "127.0.0.1:5672", | "address": "127.0.0.1:5672", | ||||
| "account": "cloudream", | "account": "cloudream", | ||||
| @@ -39,7 +46,7 @@ | |||||
| "downloadStrategy": { | "downloadStrategy": { | ||||
| "highLatencyHub": 35 | "highLatencyHub": 35 | ||||
| }, | }, | ||||
| "storageID": 0, | |||||
| "userSpaceID": 0, | |||||
| "authAccessKey": "", | "authAccessKey": "", | ||||
| "authSecretKey": "", | "authSecretKey": "", | ||||
| "maxHttpBodySize": 5242880 | "maxHttpBodySize": 5242880 | ||||