Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 24 additions & 48 deletions agent/utils/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker

import (
"context"
"github.com/docker/docker/api/types/network"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
Expand All @@ -14,6 +15,19 @@ import (
"github.com/docker/docker/client"
)

func NewDockerClient() (*client.Client, error) {
var settingItem model.Setting
_ = global.DB.Where("key = ?", "DockerSockPath").First(&settingItem).Error
if len(settingItem.Value) == 0 {
settingItem.Value = "unix:///var/run/docker.sock"
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithHost(settingItem.Value), client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
return cli, nil
}

type Client struct {
cli *client.Client
}
Expand All @@ -34,21 +48,14 @@ func NewClient() (Client, error) {
}, nil
}

func (c Client) Close() {
_ = c.cli.Close()
func NewClientWithCli(cli *client.Client) (Client, error) {
return Client{
cli: cli,
}, nil
}

func NewDockerClient() (*client.Client, error) {
var settingItem model.Setting
_ = global.DB.Where("key = ?", "DockerSockPath").First(&settingItem).Error
if len(settingItem.Value) == 0 {
settingItem.Value = "unix:///var/run/docker.sock"
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithHost(settingItem.Value), client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
return cli, nil
func (c Client) Close() {
_ = c.cli.Close()
}

func (c Client) ListContainersByName(names []string) ([]types.Container, error) {
Expand Down Expand Up @@ -90,8 +97,9 @@ func (c Client) ListAllContainers() ([]types.Container, error) {
}

func (c Client) CreateNetwork(name string) error {
_, err := c.cli.NetworkCreate(context.Background(), name, types.NetworkCreate{
Driver: "bridge",
_, err := c.cli.NetworkCreate(context.Background(), name, network.CreateOptions{
Driver: "bridge",
EnableIPv6: new(bool),
})
return err
}
Expand All @@ -103,26 +111,6 @@ func (c Client) DeleteImage(imageID string) error {
return nil
}

func (c Client) InspectContainer(containerID string) (types.ContainerJSON, error) {
return c.cli.ContainerInspect(context.Background(), containerID)
}

func (c Client) PullImage(imageName string, force bool) error {
if !force {
exist, err := c.CheckImageExist(imageName)
if err != nil {
return err
}
if exist {
return nil
}
}
if _, err := c.cli.ImagePull(context.Background(), imageName, image.PullOptions{}); err != nil {
return err
}
return nil
}

func (c Client) GetImageIDByName(imageName string) (string, error) {
filter := filters.NewArgs()
filter.Add("reference", imageName)
Expand All @@ -138,20 +126,8 @@ func (c Client) GetImageIDByName(imageName string) (string, error) {
return "", nil
}

func (c Client) CheckImageExist(imageName string) (bool, error) {
filter := filters.NewArgs()
filter.Add("reference", imageName)
list, err := c.cli.ImageList(context.Background(), image.ListOptions{
Filters: filter,
})
if err != nil {
return false, err
}
return len(list) > 0, nil
}

func (c Client) NetworkExist(name string) bool {
var options types.NetworkListOptions
var options network.ListOptions
options.Filters = filters.NewArgs(filters.Arg("name", name))
networks, err := c.cli.NetworkList(context.Background(), options)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the provided code snippets contain any errors, inconsistencies, or inefficiencies as they seem to be correct implementations of Docker's API using Go. It includes all necessary packages like container, network, and image. However, for a more precise check of differences, I would need access to actual input files rather than stub codes.

The NewClientWithCli function is used to create a new Docker client from a given CLI instance and does not return an error if passed an invalid argument. The method Close() should be removed because it doesn't make much sense within this context.

In summary:

No immediate issues were found with regard to coding style, implementation, or functionality discrepancies between versions.

Optimization/suggestions:

  • The function names can improve readability for beginners looking at their own examples.

Code review could recommend these changes depending on how familiar someone may already be with Docker APIs.
Note that further optimizations would depend upon specific requirements.

Expand Down
1 change: 0 additions & 1 deletion frontend/src/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,6 @@ export const encryptPassword = (password: string) => {
}
let rsaPublicKeyText = getCookie('panel_public_key');
if (!rsaPublicKeyText) {
console.log('RSA public key not found');
return password;
}
rsaPublicKeyText = urlDecode(rsaPublicKeyText);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code appears to be a function that takes an input string and returns it encrypted using RSA encryption with an already fetched public key from the web browser's cookie.

Here is a quick look at the changes between the old version and the current one:

  • The original code snippet has been commented out (export const encryptPassword = (password: string) => {). This should also be updated in this new release.

  • There's no significant change, as most of the existing functionality does not depend on the presence/absence of getCookie. It can still work even without checking cookies.

  • However, it might suggest the removal of 'window' as it's being deprecated, so make sure you consider this before updating anything here.

If there are specific improvements or optimizations desired regarding RSA encryption algorithm's performance or security checks, please clarify them and I would be happy to recommend ways to enhance its efficiency or compliance standards (e.g., adding proper salt and ensuring secure storage).

Expand Down
Loading