/
containers.go
85 lines (73 loc) · 2.57 KB
/
containers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2018 The Liman Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cmd
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/yinwoods/anchor/anchor/util"
"golang.org/x/net/context"
)
const (
dockerContainerURL = "http://localhost:8089/api/container"
)
// ContainerCreateConfig wraps container config 、host config and networking config
type ContainerCreateConfig struct {
ContainerConfig container.Config
HostConfig container.HostConfig
NetworkingConfig network.NetworkingConfig
}
// ContainersListOutput used to interact with template
type ContainersListOutput struct {
ID string `json:"Id"`
Name string `json:"Name"`
CreatedTime string `json:"CreatedTime"`
State string `json:"State"`
}
// ContainersList used to list containers
func ContainersList() ([]ContainersListOutput, error) {
resp, _ := util.HTTPGet(dockerContainerURL)
var containers []types.Container
json.Unmarshal(resp, &containers)
containersListOutput := []ContainersListOutput{}
for _, container := range containers {
containerName := container.Names[0]
// 截取k8s_
if strings.HasPrefix(container.Names[0], "/k8s_") {
containerName = container.Names[0][5:]
}
containersListOutput = append(containersListOutput, ContainersListOutput{
ID: container.ID,
Name: containerName,
CreatedTime: time.Unix(container.Created, 0).Format("2006-01-02 15:04"),
State: container.State,
})
}
return containersListOutput, nil
}
// ContainerGet returns docker inspect information
func ContainerGet(id string) (types.ContainerJSON, error) {
resp, _ := util.HTTPGet(fmt.Sprintf("%s/%s/json", dockerContainerURL, id))
var container types.ContainerJSON
json.Unmarshal(resp, &container)
return container, nil
}
// ContainerCreate create a container
func ContainerCreate(config ContainerCreateConfig) (container.ContainerCreateCreatedBody, error) {
return DockerClient.ContainerCreate(context.Background(), &config.ContainerConfig, &config.HostConfig, &config.NetworkingConfig, "")
}
// ContainerUpdate updates an container
func ContainerUpdate(id, config string) error {
_, err := util.HTTPPost(fmt.Sprintf("%s/%s", dockerContainerURL, id), config)
return err
}
// ContainerDelete delete a container
func ContainerDelete(id string) error {
_, err := util.HTTPDelete(fmt.Sprintf("%s/%s", dockerContainerURL, id))
return err
}