Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
amuluze committed Jul 7, 2024
2 parents d54ec55 + 7b3d9e5 commit 2aa0392
Show file tree
Hide file tree
Showing 188 changed files with 7,711 additions and 4,578 deletions.
1 change: 1 addition & 0 deletions amprobe/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
.DS_Store
*/dist/*
amprobe
2 changes: 1 addition & 1 deletion amprobe/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN apt update && \
chmod +x /app/amprobe

COPY ./configs /app/configs
COPY ./dist /usr/share/nginx/html
COPY ./dist /usr/share/nginx/html/app
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
COPY ./supervisor /etc/supervisor/conf.d

Expand Down
2 changes: 1 addition & 1 deletion amprobe/Dockerfile.amd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN apt update && \
chmod +x /app/amprobe

COPY ./configs /app/configs
COPY ./dist /usr/share/nginx/html
COPY ./dist /usr/share/nginx/html/app
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
COPY ./supervisor /etc/supervisor/conf.d

Expand Down
2 changes: 1 addition & 1 deletion amprobe/Dockerfile.arm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN apt update && \
chmod +x /app/amprobe

COPY ./configs /app/configs
COPY ./dist /usr/share/nginx/html
COPY ./dist /usr/share/nginx/html/app
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
COPY ./supervisor /etc/supervisor/conf.d

Expand Down
4 changes: 2 additions & 2 deletions amprobe/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ http {
server_name localhost; # 服务器地址或绑定域名

location /app {
root /usr/share/nginx/html/app;
index index.html index.htm;
alias /usr/share/nginx/html/app;
index index.html;
try_files $uri $uri/ /app/index.html;
}

Expand Down
21 changes: 19 additions & 2 deletions amprobe/pkg/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ package rpc

import (
"context"
"fmt"
"os"

"github.com/smallnest/rpcx/client"
"github.com/smallnest/rpcx/share"
)

type Client struct {
client client.XClient
client client.XClient
fileClient client.XClient
}

func NewClient(addr string) (*Client, error) {
Expand All @@ -19,15 +24,27 @@ func NewClient(addr string) (*Client, error) {
return nil, err
}
xclient := client.NewXClient("Service", client.Failtry, client.RandomSelect, sf, client.DefaultOption)
fileClient := client.NewXClient(share.SendFileServiceName, client.Failtry, client.RandomSelect, sf, client.DefaultOption)
return &Client{
client: xclient,
client: xclient,
fileClient: fileClient,
}, nil
}

func (c *Client) Call(ctx context.Context, method string, args interface{}, reply interface{}) error {
return c.client.Call(ctx, method, args, reply)
}

func (c *Client) SendFile(ctx context.Context, filename string, prefix string) error {
return c.fileClient.SendFile(ctx, filename, 0, map[string]string{"path": prefix})
}

func (c *Client) DownloadFile(ctx context.Context, downlaodFilepath string, filename string) error {
file, _ := os.Create(fmt.Sprintf("/tmp/%s", filename))
defer file.Close()
return c.fileClient.DownloadFile(ctx, downlaodFilepath, file, map[string]string{})
}

func (c *Client) Close() error {
return c.client.Close()
}
1 change: 1 addition & 0 deletions amprobe/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewFiberApp(config *Config, r IRouter) *fiber.App {
Prefork: config.Fiber.Prefork,
AppName: config.Fiber.AppName,
ServerHeader: config.Fiber.SeverHeader,
BodyLimit: 1000 * 1024 * 1024,
}

app := fiber.New(fiberConfig)
Expand Down
187 changes: 169 additions & 18 deletions amprobe/service/container/api/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
package api

import (
"github.com/amuluze/amprobe/service/container/rpc"
"fmt"
"log/slog"

"github.com/amuluze/amprobe/service/container/rpc"

"github.com/amuluze/amprobe/pkg/fiberx"
"github.com/amuluze/amprobe/pkg/validatex"
"github.com/amuluze/amprobe/service/schema"
Expand All @@ -24,49 +26,49 @@ func NewContainerAPI(containerService rpc.IContainerService) *ContainerAPI {
}
}

func (a *ContainerAPI) ContainerList(ctx *fiber.Ctx) error {
func (a *ContainerAPI) Version(ctx *fiber.Ctx) error {
c := ctx.UserContext()
version, err := a.ContainerService.Version(c)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.Success(ctx, version)
}

var args schema.ContainerQueryArgs
if err := fiberx.ParseQuery(ctx, &args); err != nil {
func (a *ContainerAPI) ContainerCreate(ctx *fiber.Ctx) error {
c := ctx.UserContext()
var args schema.ContainerCreateArgs
if err := fiberx.ParseBody(ctx, &args); err != nil {
return fiberx.Failure(ctx, err)
}
slog.Info("args", "args", args)
if err := validatex.ValidateStruct(&args); err != nil {
return fiberx.Failure(ctx, err)
}

container, err := a.ContainerService.ContainerList(c, args)
container, err := a.ContainerService.ContainerCreate(c, args)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.Success(ctx, container)
}

func (a *ContainerAPI) ImageList(ctx *fiber.Ctx) error {
func (a *ContainerAPI) ContainerList(ctx *fiber.Ctx) error {
c := ctx.UserContext()

var args schema.ImageQueryArgs
var args schema.ContainerQueryArgs
if err := fiberx.ParseQuery(ctx, &args); err != nil {
return fiberx.Failure(ctx, err)
}
slog.Info("args", "args", args)
if err := validatex.ValidateStruct(&args); err != nil {
return fiberx.Failure(ctx, err)
}
images, err := a.ContainerService.ImageList(c, args)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.Success(ctx, images)
}

func (a *ContainerAPI) Version(ctx *fiber.Ctx) error {
c := ctx.UserContext()
version, err := a.ContainerService.Version(c)
container, err := a.ContainerService.ContainerList(c, args)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.Success(ctx, version)
return fiberx.Success(ctx, container)
}

func (a *ContainerAPI) ContainerStart(ctx *fiber.Ctx) error {
Expand Down Expand Up @@ -139,6 +141,79 @@ func (a *ContainerAPI) ContainerRestart(ctx *fiber.Ctx) error {
return fiberx.NoContent(ctx)
}

func (a *ContainerAPI) ImageList(ctx *fiber.Ctx) error {
c := ctx.UserContext()

var args schema.ImageQueryArgs
if err := fiberx.ParseQuery(ctx, &args); err != nil {
return fiberx.Failure(ctx, err)
}
if err := validatex.ValidateStruct(&args); err != nil {
return fiberx.Failure(ctx, err)
}
images, err := a.ContainerService.ImageList(c, args)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.Success(ctx, images)
}

func (a *ContainerAPI) ImagePull(ctx *fiber.Ctx) error {
c := ctx.UserContext()
var args schema.ImagePullArgs
if err := fiberx.ParseBody(ctx, &args); err != nil {
return fiberx.Failure(ctx, err)
}
slog.Info("args", "args", args)
if err := validatex.ValidateStruct(&args); err != nil {
return fiberx.Failure(ctx, err)
}
err := a.ContainerService.ImagePull(c, args)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.NoContent(ctx)
}

// ImageImport 包含两部分,文件上传和加载
func (a *ContainerAPI) ImageImport(ctx *fiber.Ctx) error {
c := ctx.UserContext()
file, err := ctx.FormFile("file")
if err != nil {
return fiberx.Failure(ctx, err)
}
// save file
slog.Info("image import", "file nameage name", file.Filename)
if err := ctx.SaveFile(file, fmt.Sprintf("/tmp/%s", file.Filename)); err != nil {
slog.Error("save file error", "err", err, "filepath", fmt.Sprintf("/tmp/%s", file.Filename))
return fiberx.Failure(ctx, err)
}
args := schema.ImageImportArgs{
SourceFile: fmt.Sprintf("/tmp/%s", file.Filename),
}
if err := a.ContainerService.ImageImport(c, args); err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.NoContent(ctx)
}

// ImageExport 包含两步,将镜像导出为压缩文件,并提供下载
func (a *ContainerAPI) ImageExport(ctx *fiber.Ctx) error {
c := ctx.UserContext()
var args schema.ImageExportArgs
if err := fiberx.ParseQuery(ctx, &args); err != nil {
return fiberx.Failure(ctx, err)
}
slog.Info("args", "args", args)
if err := validatex.ValidateStruct(&args); err != nil {
return fiberx.Failure(ctx, err)
}
if err := a.ContainerService.ImageExport(c, args); err != nil {
return fiberx.Failure(ctx, err)
}
return ctx.Download(fmt.Sprintf("/tmp/%s.tar", args.ImageName))
}

func (a *ContainerAPI) ImageRemove(ctx *fiber.Ctx) error {
c := ctx.UserContext()
var args schema.ImageRemoveArgs
Expand All @@ -165,3 +240,79 @@ func (a *ContainerAPI) ImagesPrune(ctx *fiber.Ctx) error {
}
return fiberx.NoContent(ctx)
}

func (a *ContainerAPI) NetworkList(ctx *fiber.Ctx) error {
c := ctx.UserContext()
var args schema.NetworkListArgs
if err := fiberx.ParseQuery(ctx, &args); err != nil {
return fiberx.Failure(ctx, err)
}
slog.Info("network list args", "args", args)
if err := validatex.ValidateStruct(&args); err != nil {
return fiberx.Failure(ctx, err)
}
networks, err := a.ContainerService.NetworkList(c, args)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.Success(ctx, networks)
}

func (a *ContainerAPI) NetworkDelete(ctx *fiber.Ctx) error {
c := ctx.UserContext()
var args schema.NetworkDeleteArgs
if err := fiberx.ParseBody(ctx, &args); err != nil {
return fiberx.Failure(ctx, err)
}
if err := validatex.ValidateStruct(&args); err != nil {
return fiberx.Failure(ctx, err)
}
err := a.ContainerService.NetworkDelete(c, args)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.NoContent(ctx)
}

func (a *ContainerAPI) NetworkCreate(ctx *fiber.Ctx) error {
c := ctx.UserContext()
var args schema.NetworkCreateArgs
if err := fiberx.ParseBody(ctx, &args); err != nil {
return fiberx.Failure(ctx, err)
}
if err := validatex.ValidateStruct(&args); err != nil {
return fiberx.Failure(ctx, err)
}
reply, err := a.ContainerService.NetworkCreate(c, args)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.Success(ctx, reply)
}

func (a *ContainerAPI) GetDockerRegistryMirrors(ctx *fiber.Ctx) error {
c := ctx.UserContext()
args := schema.GetDockerRegistryMirrorsArgs{}
res, err := a.ContainerService.GetDockerRegistryMirrors(c, args)
slog.Info("get docker registry mirrors", "res", res, "err", err)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.Success(ctx, res)
}

func (a *ContainerAPI) SetDockerRegistryMirrors(ctx *fiber.Ctx) error {
c := ctx.UserContext()
args := schema.SetDockerRegistryMirrorsArgs{}
if err := fiberx.ParseBody(ctx, &args); err != nil {
return fiberx.Failure(ctx, err)
}
if err := validatex.ValidateStruct(&args); err != nil {
return fiberx.Failure(ctx, err)
}
err := a.ContainerService.SetDockerRegistryMirrors(c, args)
if err != nil {
return fiberx.Failure(ctx, err)
}
return fiberx.NoContent(ctx)
}
Loading

0 comments on commit 2aa0392

Please sign in to comment.