Files
ai/pkgdocker/docker_container.go
zeekay ce4219faba chore: OSS attribution — restore casibase/OpenAgent per-file copyright (Apache-2.0 §4c)
Casibase's upstream '// Copyright <yr> The OpenAgent Authors. All Rights
Reserved.' (and legacy 'The Casibase Authors' on older files) was stripped
from every derived source file and replaced with a Hanzo-only line. This
re-adds it as a '// Portions Copyright ...' line beneath the Hanzo line on
the 573 casibase-derived files, using each file's real upstream year/holder.
Net-new Hanzo files are untouched. Nothing deleted. LICENSE + NOTICE were
already correct.
2026-07-02 13:02:21 -07:00

134 lines
4.2 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2025 The OpenAgent Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pkgdocker
import (
"context"
"fmt"
"strconv"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/hanzoai/ai/i18n"
)
type Container struct {
Owner string `db:"pk" json:"owner"`
Name string `db:"pk" json:"name"`
DisplayName string `json:"displayName"`
Provider string `json:"provider"`
CreatedTime string `json:"createdTime"`
Image string `json:"image"`
ImageId string `json:"imageId"`
Command string `json:"command"`
SizeRw int64 `json:"sizeRw,omitempty"`
SizeRootFs int64 `json:"sizeRootFs,omitempty"`
Labels map[string]string
State string `json:"state"`
Status string `json:"status"`
Ports string `json:"ports"`
HostConfig struct {
NetworkMode string `json:",omitempty"`
Annotations map[string]string `json:",omitempty"`
}
}
type ContainerClient struct {
Client *client.Client
}
// Docker remote access service needs to be enabled and configured in advance.
func NewContainerClient(username string, hostname string, region string) (ContainerClient, error) {
remoteAddr := "tcp://" + hostname + ":2375"
containerClient, err := client.NewClientWithOpts(client.WithHost(remoteAddr), client.WithAPIVersionNegotiation())
if err != nil {
return ContainerClient{}, err
}
return ContainerClient{Client: containerClient}, nil
}
func getContainerFromSummary(summary container.Summary) *Container {
var ports string
for _, port := range summary.Ports {
ports += port.IP + ":" + strconv.Itoa(int(port.PublicPort)) + ":" + strconv.Itoa(int(port.PrivatePort)) + " "
}
myContainer := &Container{
Name: summary.ID,
DisplayName: summary.Names[0],
CreatedTime: time.Unix(summary.Created, 0).Format(time.RFC3339),
Image: summary.Image,
ImageId: summary.ImageID,
Command: summary.Command,
SizeRw: summary.SizeRw,
SizeRootFs: summary.SizeRootFs,
Labels: summary.Labels,
State: summary.State,
Status: summary.Status,
Ports: ports,
HostConfig: summary.HostConfig,
}
return myContainer
}
func (client ContainerClient) GetContainers() ([]*Container, error) {
containers := []*Container{}
sum, err := client.Client.ContainerList(context.Background(), container.ListOptions{All: true})
if err != nil {
return nil, err
}
for _, summary := range sum {
containers = append(containers, getContainerFromSummary(summary))
}
return containers, nil
}
func (client ContainerClient) GetContainer(name string, lang string) (*Container, error) {
sum, err := client.Client.ContainerList(context.Background(), container.ListOptions{All: true})
if err != nil {
return nil, err
}
for _, summary := range sum {
if summary.ID == name {
return getContainerFromSummary(summary), nil
}
}
return nil, fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "pkgdocker:Container %s not found"), name))
}
func (client ContainerClient) UpdateContainerState(name string, state string, lang string) (bool, string, error) {
myContainer, err := client.GetContainer(name, lang)
if err != nil {
return false, "", err
}
id := myContainer.Name
switch state {
case "Running":
err = client.Client.ContainerStart(context.Background(), id, container.StartOptions{})
case "Stopped":
err = client.Client.ContainerStop(context.Background(), id, container.StopOptions{})
default:
return false, fmt.Sprintf("Unsupported state: %s", state), nil
}
if err != nil {
return false, "", err
}
return true, fmt.Sprintf("Container: [%s]'s state has been successfully updated to: [%s]", id, state), nil
}