init commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package containers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Create struct {
|
||||
Image string `json:"image,required"`
|
||||
Name string `json:"name,omitempty"`
|
||||
}
|
||||
type CreateJson struct {
|
||||
Cause string `json:"cause"`
|
||||
Message string `json:"message"`
|
||||
Response int `json:"response"`
|
||||
Id string `json:"Id,omitempty"`
|
||||
Warning []string `json:"Warnings,omitempty"`
|
||||
}
|
||||
|
||||
type create struct {
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
func (c *Create) Create() (string, error) {
|
||||
var container create
|
||||
container.Image = c.Image
|
||||
|
||||
str, err := json.Marshal(container)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return "", err
|
||||
}
|
||||
log.Println(string(str))
|
||||
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v2.1.2/libpod/containers/create", bytes.NewBuffer(str))
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
log.Println(string(b))
|
||||
var createJson CreateJson
|
||||
err = json.NewDecoder(resp.Body).Decode(&createJson)
|
||||
if err != nil {
|
||||
log.Println(createJson)
|
||||
log.Println(err)
|
||||
return "", err
|
||||
}
|
||||
log.Println(createJson)
|
||||
startContainer(createJson.Id)
|
||||
return createJson.Id, nil
|
||||
}
|
||||
func startContainer(id string) error {
|
||||
post, err := http.Post(fmt.Sprintf("/%s/libpod/containers/%s/start", config.Entrypoint, id), "application/json", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bytes, err := io.ReadAll(post.Body)
|
||||
log.Println(string(bytes))
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package containers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ListContainers struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Image string `json:"image"`
|
||||
Created string `json:"created"`
|
||||
Ports []interface{} `json:"ports"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
type Containers struct {
|
||||
Id string `json:"Id"`
|
||||
Names []string `json:"Names"`
|
||||
Image string `json:"Image"`
|
||||
ImageID string `json:"ImageID"`
|
||||
Command string `json:"Command"`
|
||||
Created int `json:"Created"`
|
||||
//Ports []interface{} `json:"Ports"`
|
||||
State string `json:"State"`
|
||||
Status string `json:"Status"`
|
||||
}
|
||||
|
||||
func List() []ListContainers {
|
||||
var containers []Containers
|
||||
req, err := http.NewRequest(http.MethodGet, config.Entrypoint+"/containers/json?all=true", nil)
|
||||
if err != nil {
|
||||
log.Println("request for containers list error:", err)
|
||||
return []ListContainers{}
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println("request for containers list error:", err)
|
||||
return []ListContainers{}
|
||||
}
|
||||
err = json.NewDecoder(res.Body).Decode(&containers)
|
||||
if err != nil {
|
||||
log.Println("json decode container list error:", err)
|
||||
}
|
||||
var list []ListContainers
|
||||
for _, container := range containers {
|
||||
list = append(list, ListContainers{
|
||||
Id: container.Id,
|
||||
Name: strings.TrimPrefix(container.Names[0], "/"),
|
||||
Image: container.Image,
|
||||
Created: time.Unix(int64(container.Created), 0).Format(time.DateTime),
|
||||
State: container.State,
|
||||
})
|
||||
}
|
||||
return list
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type DeleteRsp struct {
|
||||
Cause string `json:"cause"` //cause image is in use by a container
|
||||
Message string `json:"message"`
|
||||
Response int `json:"response"`
|
||||
}
|
||||
type DeleteRsult struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func Delete(id string) DeleteRsult {
|
||||
req, _ := http.NewRequestWithContext(context.Background(), http.MethodDelete, config.Entrypoint+fmt.Sprintf("/images/%s", id), nil)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return DeleteRsult{
|
||||
Error: err.Error(),
|
||||
}
|
||||
}
|
||||
var deleteRsult DeleteRsp
|
||||
var response DeleteRsult
|
||||
response.Code = resp.StatusCode
|
||||
_ = json.NewDecoder(resp.Body).Decode(&deleteRsult)
|
||||
log.Println(deleteRsult)
|
||||
if deleteRsult.Cause != "" {
|
||||
return DeleteRsult{
|
||||
Error: deleteRsult.Cause,
|
||||
}
|
||||
}
|
||||
return response
|
||||
}
|
||||
@@ -1 +1,40 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Tag string `json:"tag"`
|
||||
Digest string `json:"digest"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Size string `json:"size"`
|
||||
}
|
||||
|
||||
func List() []Image {
|
||||
var images []model.Image
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, config.Entrypoint+"/images/json", nil)
|
||||
resp, _ := http.DefaultClient.Do(req)
|
||||
defer resp.Body.Close()
|
||||
json.NewDecoder(resp.Body).Decode(&images)
|
||||
var imagesList []Image
|
||||
for _, v := range images {
|
||||
imagesList = append(imagesList, Image{
|
||||
Id: v.Id[7:],
|
||||
Name: v.Names[0],
|
||||
Tag: v.RepoTags[0],
|
||||
Digest: v.Digest,
|
||||
CreatedAt: time.Unix(int64(v.Created), 0).Format(time.DateTime),
|
||||
Size: strconv.Itoa(v.Size/1024/1024) + " MB",
|
||||
})
|
||||
}
|
||||
return imagesList
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type PullRsp struct {
|
||||
Images []string `json:"images"`
|
||||
Id string `json:"id"`
|
||||
Error string `json:"errr,omitempty"`
|
||||
Info string `json:"info,omitempty"`
|
||||
}
|
||||
|
||||
func Pull(ref string) PullRsp {
|
||||
var pull PullRsp
|
||||
req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, config.Entrypoint+fmt.Sprintf("/v2.2.2/libpod/images/pull?reference=%s&quiet=true", ref), nil)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return PullRsp{
|
||||
Error: err.Error(),
|
||||
}
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err = Body.Close()
|
||||
if err != nil {
|
||||
log.Printf("Failed to close response body: %v", err)
|
||||
}
|
||||
}(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return PullRsp{
|
||||
Error: "status code " + resp.Status,
|
||||
}
|
||||
}
|
||||
_ = json.NewDecoder(resp.Body).Decode(&pull)
|
||||
|
||||
return pull
|
||||
|
||||
}
|
||||
@@ -2,13 +2,15 @@ package network
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ListJsons() []model.NetWorkJson {
|
||||
req, _ := http.NewRequest("GET", "http://127.0.0.1:8888/networks", nil)
|
||||
req, _ := http.NewRequest("GET", config.Entrypoint+"/networks", nil)
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println("request for network list error:", err)
|
||||
@@ -18,3 +20,23 @@ func ListJsons() []model.NetWorkJson {
|
||||
err = json.NewDecoder(res.Body).Decode(&result)
|
||||
return result
|
||||
}
|
||||
func ListNetwork() []model.NetWork {
|
||||
var result []model.NetWork
|
||||
for _, v := range ListJsons() {
|
||||
conf := v.IPAM.Config
|
||||
var ipam []model.Config
|
||||
for _, val := range conf {
|
||||
ipam = append(ipam, model.Config{
|
||||
Subnet: val.Subnet, Gateway: val.Gateway,
|
||||
})
|
||||
}
|
||||
result = append(result, model.NetWork{
|
||||
Name: v.Name,
|
||||
Id: v.Id,
|
||||
Created: v.Created.Format(time.DateTime),
|
||||
Driver: v.Driver,
|
||||
IPAM: ipam,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@ package system
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Version() model.Version {
|
||||
req, _ := http.NewRequest("GET", "http://127.0.0.1:8888/version", nil)
|
||||
req, _ := http.NewRequest("GET", config.Entrypoint+"/version", nil)
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println("request for version list error:", err)
|
||||
|
||||
@@ -2,13 +2,14 @@ package volume
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ListJsons() model.VolumesJson {
|
||||
req, _ := http.NewRequest("GET", "http://127.0.0.1:8888/volumes", nil)
|
||||
req, _ := http.NewRequest("GET", config.Entrypoint+"/volumes", nil)
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println("request for volume list error:", err)
|
||||
|
||||
Reference in New Issue
Block a user