-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
2,194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package queue | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
|
||
"github.com/daocloud/crproxy/internal/server" | ||
"github.com/daocloud/crproxy/queue" | ||
"github.com/emicklei/go-restful/v3" | ||
"github.com/gorilla/handlers" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type flagpole struct { | ||
Behind bool | ||
Address string | ||
AcmeHosts []string | ||
AcmeCacheDir string | ||
CertFile string | ||
PrivateKeyFile string | ||
|
||
TokenPublicKeyFile string | ||
|
||
SimpleAuthUserpass map[string]string | ||
|
||
AdminToken string | ||
|
||
DBURL string | ||
} | ||
|
||
func NewCommand() *cobra.Command { | ||
flags := &flagpole{ | ||
Address: ":18010", | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "queue", | ||
Short: "Queue", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runE(cmd.Context(), flags) | ||
}, | ||
} | ||
|
||
cmd.Flags().BoolVar(&flags.Behind, "behind", flags.Behind, "Behind") | ||
cmd.Flags().StringVar(&flags.Address, "address", flags.Address, "Address") | ||
cmd.Flags().StringSliceVar(&flags.AcmeHosts, "acme-hosts", flags.AcmeHosts, "Acme hosts") | ||
cmd.Flags().StringVar(&flags.AcmeCacheDir, "acme-cache-dir", flags.AcmeCacheDir, "Acme cache dir") | ||
cmd.Flags().StringVar(&flags.CertFile, "cert-file", flags.CertFile, "Cert file") | ||
cmd.Flags().StringVar(&flags.PrivateKeyFile, "private-key-file", flags.PrivateKeyFile, "Private key file") | ||
|
||
cmd.Flags().StringVar(&flags.TokenPublicKeyFile, "token-public-key-file", "", "public key file") | ||
|
||
cmd.Flags().StringVar(&flags.AdminToken, "admin-token", flags.AdminToken, "Admin token") | ||
|
||
cmd.Flags().StringVar(&flags.DBURL, "db-url", flags.DBURL, "Database URL") | ||
|
||
return cmd | ||
} | ||
|
||
func runE(ctx context.Context, flags *flagpole) error { | ||
logger := slog.New(slog.NewJSONHandler(os.Stderr, nil)) | ||
|
||
container := restful.NewContainer() | ||
|
||
var mgr *queue.QueueManager | ||
if flags.DBURL != "" { | ||
dburl := flags.DBURL | ||
db, err := sql.Open("mysql", dburl) | ||
if err != nil { | ||
return fmt.Errorf("failed to connect to database: %w", err) | ||
} | ||
defer db.Close() | ||
|
||
if err = db.Ping(); err != nil { | ||
return fmt.Errorf("failed to ping database: %w", err) | ||
} | ||
|
||
logger.Info("Connected to DB") | ||
|
||
mgr = queue.NewQueueManager(flags.AdminToken, db) | ||
|
||
mgr.Register(container) | ||
|
||
mgr.InitTable(ctx) | ||
|
||
mgr.Schedule(ctx, logger) | ||
} | ||
|
||
var handler http.Handler = container | ||
|
||
handler = handlers.LoggingHandler(os.Stderr, handler) | ||
|
||
if flags.Behind { | ||
handler = handlers.ProxyHeaders(handler) | ||
} | ||
|
||
handler = handlers.CORS( | ||
handlers.AllowedMethods([]string{http.MethodHead, http.MethodGet, http.MethodPost, http.MethodPatch, http.MethodPut, http.MethodDelete}), | ||
handlers.AllowedHeaders([]string{"Authorization", "Accept", "Content-Type", "Origin"}), | ||
handlers.AllowedOrigins([]string{"*"}), | ||
)(handler) | ||
|
||
err := server.Run(ctx, flags.Address, handler, flags.AcmeHosts, flags.AcmeCacheDir, flags.CertFile, flags.PrivateKeyFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to run server: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/daocloud/crproxy/cache" | ||
"github.com/daocloud/crproxy/runner" | ||
"github.com/daocloud/crproxy/storage" | ||
csync "github.com/daocloud/crproxy/sync" | ||
"github.com/daocloud/crproxy/transport" | ||
"github.com/docker/distribution/manifest/manifestlist" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type flagpole struct { | ||
QueueURL string | ||
|
||
AdminToken string | ||
|
||
StorageURL []string | ||
Deep bool | ||
Quick bool | ||
Platform []string | ||
Userpass []string | ||
|
||
Duration time.Duration | ||
} | ||
|
||
func NewCommand() *cobra.Command { | ||
flags := &flagpole{ | ||
Platform: []string{ | ||
"linux/amd64", | ||
"linux/arm64", | ||
}, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "runner", | ||
Short: "Runner", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runE(cmd.Context(), flags) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&flags.AdminToken, "admin-token", flags.AdminToken, "Admin token") | ||
|
||
cmd.Flags().StringVar(&flags.QueueURL, "queue-url", flags.QueueURL, "Queue URL") | ||
|
||
cmd.Flags().StringArrayVar(&flags.StorageURL, "storage-url", flags.StorageURL, "Storage driver url") | ||
cmd.Flags().BoolVar(&flags.Deep, "deep", flags.Deep, "Deep sync with blob") | ||
cmd.Flags().BoolVar(&flags.Quick, "quick", flags.Quick, "Quick sync with tags") | ||
cmd.Flags().StringSliceVar(&flags.Platform, "platform", flags.Platform, "Platform") | ||
cmd.Flags().StringArrayVarP(&flags.Userpass, "user", "u", flags.Userpass, "host and username and password -u user:pwd@host") | ||
|
||
cmd.Flags().DurationVar(&flags.Duration, "duration", flags.Duration, "Duration of the running") | ||
|
||
return cmd | ||
} | ||
|
||
func runE(ctx context.Context, flags *flagpole) error { | ||
logger := slog.New(slog.NewJSONHandler(os.Stderr, nil)) | ||
|
||
opts := []csync.Option{} | ||
|
||
var caches []*cache.Cache | ||
for _, s := range flags.StorageURL { | ||
sd, err := storage.NewStorage(s) | ||
if err != nil { | ||
return fmt.Errorf("create storage driver failed: %w", err) | ||
} | ||
|
||
cache, err := cache.NewCache(cache.WithStorageDriver(sd)) | ||
if err != nil { | ||
return fmt.Errorf("create cache failed: %w", err) | ||
} | ||
|
||
caches = append(caches, cache) | ||
} | ||
|
||
transportOpts := []transport.Option{ | ||
transport.WithLogger(logger), | ||
} | ||
|
||
if len(flags.Userpass) != 0 { | ||
transportOpts = append(transportOpts, transport.WithUserAndPass(flags.Userpass)) | ||
} | ||
|
||
tp, err := transport.NewTransport(transportOpts...) | ||
if err != nil { | ||
return fmt.Errorf("create transport failed: %w", err) | ||
} | ||
|
||
opts = append(opts, | ||
csync.WithCaches(caches...), | ||
csync.WithDeep(flags.Deep), | ||
csync.WithQuick(flags.Quick), | ||
csync.WithTransport(tp), | ||
csync.WithLogger(logger), | ||
csync.WithFilterPlatform(filterPlatform(flags.Platform)), | ||
) | ||
|
||
sm, err := csync.NewSyncManager(opts...) | ||
if err != nil { | ||
return fmt.Errorf("create sync manager failed: %w", err) | ||
} | ||
|
||
runner, err := runner.NewRunner(http.DefaultClient, flags.QueueURL, flags.AdminToken, sm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if flags.Duration > 0 { | ||
ctx, _ = context.WithTimeout(ctx, flags.Duration) | ||
} | ||
|
||
return runner.Run(ctx, logger) | ||
} | ||
|
||
func filterPlatform(ps []string) func(pf manifestlist.PlatformSpec) bool { | ||
platforms := map[string]struct{}{} | ||
for _, p := range ps { | ||
platforms[p] = struct{}{} | ||
} | ||
return func(pf manifestlist.PlatformSpec) bool { | ||
p := fmt.Sprintf("%s/%s", pf.OS, pf.Architecture) | ||
|
||
if _, ok := platforms[p]; ok { | ||
return true | ||
} | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.