added possibility to create multiple ports

This commit is contained in:
2021-04-07 20:55:37 +04:00
parent 82f5e921b5
commit 8d417b84f5
4 changed files with 54 additions and 49 deletions

View File

@@ -25,6 +25,7 @@ type DockerVM struct {
ID string `json:"ID"`
TagName string `json:"TagName"`
ImagePath string `json:"ImagePath"`
Ports []int `json:"OpenPorts"`
}
type ErrorLine struct {
@@ -38,16 +39,23 @@ type ErrorDetail struct {
var dockerRegistryUserID = ""
func BuildRunContainer() (*DockerVM,error) {
func BuildRunContainer(NumPorts int) (*DockerVM,error) {
//Docker Struct Variable
var RespDocker *DockerVM = new(DockerVM)
// Count: 2 allocated ports for VNC and SSH + other ports
// to open
count := NumPorts + 2
// Gets 2 TCP ports empty
Ports, err := freeport.GetFreePorts(2)
Ports, err := freeport.GetFreePorts(count)
if err != nil {
return nil,err
}
for i := 2; i < count; i++ {
RespDocker.Ports = append(RespDocker.Ports, Ports[i])
}
//fmt.Println(Ports[0])
// Sets Free port to Struct
@@ -58,6 +66,7 @@ func BuildRunContainer() (*DockerVM,error) {
RespDocker.SSHUsername = "master"
RespDocker.SSHPassword = "password"
RespDocker.VNCPassword = "vncpassword"
RespDocker.Ports = Ports
//Default parameters
RespDocker.TagName = "p2p-ubuntu"
@@ -117,7 +126,7 @@ func (d *DockerVM)imageBuild(dockerClient *client.Client) error {
return nil
}
// Starts container and assigns IP addresses
// Starts container and assigns port numbers
func (d *DockerVM)runContainer(dockerClient *client.Client) error{
ctx, _ := context.WithTimeout(context.Background(), time.Second*2000)
@@ -128,21 +137,40 @@ func (d *DockerVM)runContainer(dockerClient *client.Client) error{
Volumes: map[string]struct{}{"/opt/data:/data":{}},
}
hostConfig := &container.HostConfig{
PortBindings: nat.PortMap{
"22/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: fmt.Sprint(d.SSHPort),
},
},
"6901/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: fmt.Sprint(d.VNCPort),
},
// Port forwarding for VNC and SSH ports
PortForwarding := nat.PortMap{
"22/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: fmt.Sprint(d.SSHPort),
},
},
"6901/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: fmt.Sprint(d.VNCPort),
},
},
}
// Opening other ports (At the current moment only TCP ports can be
// opened
for i := range d.Ports {
Port, err := nat.NewPort("tcp",fmt.Sprint(d.Ports[i]))
if err != nil {
return err
}
PortForwarding[Port] = []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: fmt.Sprint(d.Ports[i]),
},
}
}
hostConfig := &container.HostConfig{
PortBindings: PortForwarding,
}
res, err := dockerClient.ContainerCreate(ctx,config,hostConfig,

View File

@@ -1,31 +0,0 @@
#!/usr/bin/env bash
#Implementation from (https://gist.github.com/Kubuxu/0cafd6dc71114349875827c2c379fa1f)
REMOTE_HOST=${1-"YOUR REMOTE HOST HERE"}
DEFAULT_API_FILE="$HOME/.ipfs/api"
API_FILE="${IPFS_PATH-$DEFAULT_API_FILE}"
if [ -e "$API_FILE" ]; then
echo "IPFS API is already running"
exit 1
fi
PORT=5001
ssh -N -L ${PORT}:localhost:5001 $REMOTE_HOST &
SSH_PID=$!
function cleanupAndExit() {
rm -f "$API_FILE"
echo
echo "Killing ssh."
kill "$SSH_PID"
exit
}
trap cleanupAndExit INT
printf "/ip4/127.0.0.1/tcp/$PORT" > "$API_FILE"
echo "Linked local API to $REMOTE_HOST."
wait $SSH_PID
rm -f "$API_FILE"

View File

@@ -20,7 +20,7 @@ type Docker struct {
// Starts container using RPC calls
func (l *Listener) StartContainer( reply *Docker) error {
fmt.Print("here")
vm, err := docker.BuildRunContainer()
vm, err := docker.BuildRunContainer(3)
if err != nil {
return err
}

View File

@@ -77,8 +77,16 @@ func Server() error{
// Starts docker container in server
r.GET("/startcontainer", func(c *gin.Context) {
// Get Number of ports to open
Ports := c.DefaultQuery("ports","0")
var PortsInt int
resp, err := docker.BuildRunContainer()
// Convert Get Request value to int
fmt.Sscanf(Ports, "%d", &PortsInt)
// Creates container and returns back result to
// access container
resp, err := docker.BuildRunContainer(PortsInt)
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))