finish work
This commit is contained in:
@@ -16,6 +16,8 @@ type Create struct {
|
||||
Network string `json:"network,required"`
|
||||
Port string `json:"port,omitempty"`
|
||||
Start bool `json:"start,omitempty"`
|
||||
Volume string `json:"volume,omitzero"`
|
||||
Source string `json:"source,omitzero"`
|
||||
}
|
||||
type CreateJson struct {
|
||||
Cause string `json:"cause"`
|
||||
@@ -33,6 +35,11 @@ type create struct {
|
||||
} `json:"netns"`
|
||||
Port []PortMapping `json:"portmappings"`
|
||||
Networks map[string]NetSettings `json:"networks"`
|
||||
Volumes []VolumeMap `json:"volumes,omitzero"`
|
||||
}
|
||||
type VolumeMap struct {
|
||||
Name string `json:"name"`
|
||||
Dest string `json:"dest"`
|
||||
}
|
||||
type NetSettings struct {
|
||||
Aliases []string `json:"aliases,omitempty"`
|
||||
@@ -60,10 +67,14 @@ func (c *Create) Create() (string, error) {
|
||||
ContainerPort: port2,
|
||||
})
|
||||
}
|
||||
container.Networks = make(map[string]NetSettings)
|
||||
container.Networks["podman"] = NetSettings{
|
||||
InterfaceName: "ens0",
|
||||
if c.Source != c.Volume {
|
||||
container.Volumes = append(container.Volumes, VolumeMap{
|
||||
Name: c.Volume,
|
||||
Dest: c.Source,
|
||||
})
|
||||
}
|
||||
container.Networks = make(map[string]NetSettings)
|
||||
container.Networks["podman"] = NetSettings{}
|
||||
for k, v := range strings.Split(c.Network, ",") {
|
||||
log.Println(v)
|
||||
container.Networks[v] = NetSettings{
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"fmt"
|
||||
"github.com/go-jose/go-jose/v3/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -29,11 +32,26 @@ func DisConnect(ns string, id string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func Connect(ns string, id string) (err error) {
|
||||
reader := strings.NewReader(fmt.Sprintf(`{
|
||||
"container":"%s"
|
||||
}`, id))
|
||||
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/"+ns+"/connect", reader)
|
||||
type ConnectInfo struct {
|
||||
ContainerID string `json:"container"`
|
||||
Aliases []string `json:"aliases,aliases"`
|
||||
StaticIPs []string `json:"static_ips,omitempty"`
|
||||
StaticMac string `json:"static_mac,omitempty"`
|
||||
}
|
||||
|
||||
func Connect(ns string, r model.ConnectRequest) (err error) {
|
||||
var con ConnectInfo
|
||||
con.ContainerID = r.Id
|
||||
if r.Ip != "" {
|
||||
con.StaticIPs = strings.Split(r.Ip, ",")
|
||||
}
|
||||
if r.Alias != "" {
|
||||
con.Aliases = strings.Split(r.Alias, ",")
|
||||
}
|
||||
con.StaticMac = r.Mac
|
||||
b, _ := json.Marshal(&con)
|
||||
fmt.Println(string(b))
|
||||
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/"+ns+"/connect", bytes.NewReader(b))
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println("request for network list error:", err)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Create(r model.CreateNetworkRequest) error {
|
||||
var create CreatNet
|
||||
create.DnsEnabled = r.EnableDns
|
||||
create.Driver = "bridge"
|
||||
create.Name = r.Name
|
||||
if r.Subnet != "" {
|
||||
create.Subnets = []SubNet{
|
||||
{
|
||||
Subnet: r.Subnet,
|
||||
},
|
||||
}
|
||||
}
|
||||
b, _ := json.Marshal(&create)
|
||||
log.Println(string(b))
|
||||
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/create", bytes.NewReader(b))
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
var e model.ErrInfo
|
||||
json.NewDecoder(res.Body).Decode(&e)
|
||||
if e.Response > 400 {
|
||||
return errors.New(e.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CreatNet struct {
|
||||
Name string `json:"name,omitzero"`
|
||||
Id string `json:"id,omitzero"`
|
||||
Driver string `json:"driver,omitzero"`
|
||||
NetworkInterface string `json:"network_interface,omitzero"`
|
||||
Created time.Time `json:"created,omitzero"`
|
||||
Subnets []SubNet `json:"subnets,omitzero"`
|
||||
Ipv6Enabled bool `json:"ipv6_enabled,omitzero"`
|
||||
Internal bool `json:"internal,omitzero"`
|
||||
DnsEnabled bool `json:"dns_enabled,omitzero"`
|
||||
IpamOptions struct {
|
||||
Driver string `json:"driver,omitzero"`
|
||||
} `json:"ipam_options,omitzero"`
|
||||
}
|
||||
type SubNet struct {
|
||||
Subnet string `json:"subnet"`
|
||||
Gateway string `json:"gateway"`
|
||||
}
|
||||
|
||||
func MakerNet(name string, id []string) (err error) {
|
||||
var create CreatNet
|
||||
create.DnsEnabled = true
|
||||
create.Driver = "bridge"
|
||||
create.Name = name
|
||||
b, _ := json.Marshal(&create)
|
||||
log.Println(string(b))
|
||||
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/create", bytes.NewReader(b))
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println("request for network list error:", err)
|
||||
return
|
||||
}
|
||||
if res.StatusCode != 200 {
|
||||
str, _ := io.ReadAll(res.Body)
|
||||
log.Println(string(str))
|
||||
}
|
||||
log.Println(id)
|
||||
var c ConnectInfo
|
||||
for _, v := range id {
|
||||
c.ContainerID = v
|
||||
b, _ = json.Marshal(&c)
|
||||
req, _ = http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/"+name+"/connect", bytes.NewReader(b))
|
||||
_, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Delete(ns string) error {
|
||||
req, _ := http.NewRequest(http.MethodDelete, config.Entrypoint+"/v4.0.0/libpod/networks/"+ns, nil)
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
}
|
||||
defer res.Body.Close()
|
||||
var e model.ErrInfo
|
||||
json.NewDecoder(res.Body).Decode(&e)
|
||||
if e.Response > 400 {
|
||||
return errors.New(e.Message)
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package volume
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Create(r model.CreateVolume) error {
|
||||
b, _ := json.Marshal(&r)
|
||||
req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/volumes/create", bytes.NewReader(b))
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var e model.ErrInfo
|
||||
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
|
||||
return err
|
||||
}
|
||||
if e.Cause != "" {
|
||||
return errors.New(e.Cause)
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
func Delete(name string) error {
|
||||
req, _ := http.NewRequest(http.MethodDelete, config.Entrypoint+"/v4.0.0/libpod/volumes/"+name, nil)
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res.StatusCode >= 300 {
|
||||
return errors.New("delete failed")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package libpod
|
||||
|
||||
const (
|
||||
Defaultruntime = "youki"
|
||||
)
|
||||
@@ -0,0 +1,74 @@
|
||||
package libpod
|
||||
|
||||
import (
|
||||
"github.com/containers/podman/v5/pkg/parallel"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/containers/podman/v5/cmd/podman/registry"
|
||||
"github.com/containers/podman/v5/pkg/domain/entities"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultLogLevel = "warn"
|
||||
logLevel = defaultLogLevel
|
||||
dockerConfig = ""
|
||||
debug bool
|
||||
|
||||
noStdout = false
|
||||
useStdout = ""
|
||||
)
|
||||
|
||||
func persistentPreRunE(cmd *cobra.Command, args []string) error {
|
||||
logrus.Infof("Called %s.PersistentPreRunE(%s)", cmd.Name(), strings.Join(os.Args, " "))
|
||||
podmanConfig := registry.PodmanConfig()
|
||||
c := &cobra.Command{}
|
||||
if _, err := registry.NewImageEngine(c, args); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := registry.NewContainerEngine(c, args); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, ok := os.LookupEnv("TMPDIR"); !ok {
|
||||
if tmpdir, err := podmanConfig.ContainersConfDefaultsRO.ImageCopyTmpDir(); err != nil {
|
||||
logrus.Warnf("Failed to retrieve default tmp dir: %s", err.Error())
|
||||
} else {
|
||||
os.Setenv("TMPDIR", tmpdir)
|
||||
}
|
||||
}
|
||||
|
||||
_, found := c.Annotations[registry.ParentNSRequired]
|
||||
if !registry.IsRemote() && !found {
|
||||
cgroupMode := ""
|
||||
_, noMoveProcess := c.Annotations[registry.NoMoveProcess]
|
||||
if flag := c.LocalFlags().Lookup("cgroups"); flag != nil {
|
||||
cgroupMode = flag.Value.String()
|
||||
}
|
||||
err := registry.ContainerEngine().SetupRootless(registry.Context(), noMoveProcess, cgroupMode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setupRuntime(podmanConfig *entities.PodmanConfig) {
|
||||
podmanConfig.ContainersConf.Engine.DBBackend = podmanConfig.ContainersConfDefaultsRO.Engine.DBBackend
|
||||
podmanConfig.ContainersConf.Engine.CgroupManager = podmanConfig.ContainersConfDefaultsRO.Engine.CgroupManager
|
||||
podmanConfig.ContainersConf.Engine.NetworkCmdPath = podmanConfig.ContainersConfDefaultsRO.Engine.NetworkCmdPath
|
||||
podmanConfig.ContainersConf.Network.NetworkConfigDir = podmanConfig.ContainersConfDefaultsRO.Network.NetworkConfigDir
|
||||
podmanConfig.ContainersConf.Containers.DefaultMountsFile = podmanConfig.ContainersConfDefaultsRO.Containers.DefaultMountsFile
|
||||
podmanConfig.ContainersConf.Engine.EventsLogger = podmanConfig.ContainersConfDefaultsRO.Engine.EventsLogger
|
||||
podmanConfig.MaxWorks = (runtime.NumCPU() * 3) + 1
|
||||
podmanConfig.ContainersConf.Engine.Namespace = podmanConfig.ContainersConfDefaultsRO.Engine.Namespace
|
||||
podmanConfig.ContainersConf.Network.NetworkBackend = podmanConfig.ContainersConfDefaultsRO.Network.NetworkBackend
|
||||
podmanConfig.RuntimePath = "youki"
|
||||
podmanConfig.ContainersConf.Engine.TmpDir = podmanConfig.ContainersConfDefaultsRO.Engine.TmpDir
|
||||
podmanConfig.Trace = true
|
||||
podmanConfig.ContainersConf.Engine.VolumePath = podmanConfig.ContainersConfDefaultsRO.Engine.VolumePath
|
||||
parallel.SetMaxThreads(uint(podmanConfig.MaxWorks))
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package libpod
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/containers/podman/v5/cmd/podman/registry"
|
||||
api "github.com/containers/podman/v5/pkg/api/server"
|
||||
"github.com/containers/podman/v5/pkg/domain/entities"
|
||||
"github.com/containers/podman/v5/pkg/domain/infra"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewServer() {
|
||||
cmd := &cobra.Command{}
|
||||
cfg := registry.PodmanConfig()
|
||||
defaultConf := cfg.ContainersConfDefaultsRO
|
||||
defaultConf.Engine.OCIRuntime = Defaultruntime
|
||||
setupRuntime(cfg)
|
||||
persistentPreRunE(cmd, nil)
|
||||
libpodRuntime, err := infra.GetRuntime(registry.Context(), cmd.PersistentFlags(), cfg)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:8888")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
libpodRuntime.SetRemoteURI("tcp://localhost:8888")
|
||||
infra.StartWatcher(libpodRuntime)
|
||||
|
||||
server, err := api.NewServerWithSettings(libpodRuntime, listener, entities.ServiceOptions{
|
||||
URI: "tcp://localhost:8888",
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
go server.Serve()
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package model
|
||||
|
||||
type ErrInfo struct {
|
||||
Cause string `json:"cause"`
|
||||
Message string `json:"message"`
|
||||
Response int `json:"response"`
|
||||
}
|
||||
@@ -62,3 +62,14 @@ type NetConnect struct {
|
||||
Ip string `json:"ip"`
|
||||
Mac string `json:"mac"`
|
||||
}
|
||||
type ConnectRequest struct {
|
||||
Mac string `json:"mac"`
|
||||
Ip string `json:"ip"`
|
||||
Alias string `json:"alias"`
|
||||
Id string `json:"id"`
|
||||
}
|
||||
type CreateNetworkRequest struct {
|
||||
Name string `json:"name"`
|
||||
EnableDns bool `json:"enable_dns"`
|
||||
Subnet string `json:"subnet"`
|
||||
}
|
||||
|
||||
@@ -11,3 +11,7 @@ type Volume struct {
|
||||
Name string `json:"name"`
|
||||
Scope string `json:"scope"`
|
||||
}
|
||||
type CreateVolume struct {
|
||||
Name string
|
||||
Type string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user