From 00beda81371374c21f0c56d21e731d0dc73b98b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Sat, 14 Oct 2023 01:10:14 +0200 Subject: [PATCH] Add env --- apps/types.go | 2 ++ containers/docker.go | 9 +++++++-- containers/docker_test.go | 5 ++++- containers/types.go | 1 + glue/main.go | 4 ++++ 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/apps/types.go b/apps/types.go index a05f072..1ef3eb9 100644 --- a/apps/types.go +++ b/apps/types.go @@ -102,6 +102,8 @@ type App struct { // This is not store in the database but used in create request when the app suppose to be created from an existing snapshot Snapshot string `json:"snapshot" gorm:"-"` + Env map[string]string `json:"env"` + // Fields to setup during creating of the app, this is not stored in the database Setup struct { SSHKeys string `json:"ssh_keys"` diff --git a/containers/docker.go b/containers/docker.go index 6a9e888..666d06c 100644 --- a/containers/docker.go +++ b/containers/docker.go @@ -307,7 +307,7 @@ func (d *Driver) pullImage(image string) error { // cmd - string slice of command and its arguments // volumePath - host's directory to mount into the container // returns container ID -func (d *Driver) Create(name string, image string, volumePath string, HTTPPort int, SSHPort int, CPU int, memory int, cmd []string) (string, error) { +func (d *Driver) Create(name string, image string, volumePath string, HTTPPort int, SSHPort int, CPU int, memory int, cmd []string, env map[string]string) (string, error) { log.Println("Creating container " + name) cli, err := d.getClient() if err != nil { @@ -343,11 +343,16 @@ func (d *Driver) Create(name string, image string, volumePath string, HTTPPort i OOMKillDisable = true } + envList := []string{} + for key, value := range env { + envList = append(envList, key+"="+value) + } + createdContainer, err := cli.ContainerCreate( context.Background(), &container.Config{ Hostname: name, - Env: []string{}, + Env: envList, Image: image, Cmd: cmd, ExposedPorts: nat.PortSet{ diff --git a/containers/docker_test.go b/containers/docker_test.go index d7bd044..bfa55f0 100644 --- a/containers/docker_test.go +++ b/containers/docker_test.go @@ -25,7 +25,10 @@ func TestGetProcesses(t *testing.T) { driver.Remove("test") - _, err := driver.Create("test", "docker.io/library/busybox", "/tmp", 8990, 8922, 1, 128, []string{"sleep", "3600"}) + env := make(map[string]string) + env["TEST"] = "test" + + _, err := driver.Create("test", "docker.io/library/busybox", "/tmp", 8990, 8922, 1, 128, []string{"sleep", "3600"}, env) assert.Nil(t, err) err = driver.Start("test") diff --git a/containers/types.go b/containers/types.go index 04859dc..affdd7e 100644 --- a/containers/types.go +++ b/containers/types.go @@ -159,6 +159,7 @@ func (c *Container) Create() error { c.App.CPU, c.App.Memory, []string{}, + c.App.Env, ) return err diff --git a/glue/main.go b/glue/main.go index f6006a0..a291cf7 100644 --- a/glue/main.go +++ b/glue/main.go @@ -202,6 +202,10 @@ func (p *Processor) Get(noUpdate bool) (apps.App, error) { // Create creates a single app in the system func (p *Processor) Create(appTemplate apps.App) error { + if appTemplate.Env == nil { + appTemplate.Env = make(map[string]string) + } + err := p.Register(appTemplate) if err != nil { return err