From 2a34177ab8df43a72c3684d38f63e1f1728750f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Thu, 23 Dec 2021 14:01:27 +0100 Subject: [PATCH] Possibility to set technology version --- docker/types.go | 12 ++++++++++-- handlers.go | 14 +++++++------- handlers_nats.go | 20 +++++++++++++++++++- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/docker/types.go b/docker/types.go index 03969ec..08aaf1c 100644 --- a/docker/types.go +++ b/docker/types.go @@ -247,10 +247,18 @@ func (c *Container) SetFileContent(filename string, text string, mode string) er // SetTechnology prepares container for given technology (Python, PHP, Node.js, ...) // Where tech can be php, python or node and latest available version is used. -func (c *Container) SetTechnology(tech string) error { +// If version is empty string default version will be used. +func (c *Container) SetTechnology(tech string, version string) error { driver := c.getDriver() - _, err := driver.Exec(c.App.Name, []string{"su", "app", "-c", "rosti " + tech}, "", []string{}, false) + var err error + + // TODO: script injection here? + if version == "" { + _, err = driver.Exec(c.App.Name, []string{"su", "app", "-c", "rosti " + tech}, "", []string{}, false) + } else { + _, err = driver.Exec(c.App.Name, []string{"su", "app", "-c", "rosti " + tech + " " + version}, "", []string{}, false) + } return err } diff --git a/handlers.go b/handlers.go index 4e99349..3dce9df 100644 --- a/handlers.go +++ b/handlers.go @@ -320,49 +320,49 @@ func setServicesHandler(c echo.Context) error { } if quickServices.Python { - err = container.SetTechnology("python") + err = container.SetTechnology("python", "") if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } } if quickServices.PHP { - err = container.SetTechnology("php") + err = container.SetTechnology("php", "") if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } } if quickServices.Node { - err = container.SetTechnology("node") + err = container.SetTechnology("node", "") if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } } if quickServices.Ruby { - err = container.SetTechnology("ruby") + err = container.SetTechnology("ruby", "") if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } } if quickServices.Deno { - err = container.SetTechnology("deno") + err = container.SetTechnology("deno", "") if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } } if quickServices.Memcached { - err = container.SetTechnology("memcached") + err = container.SetTechnology("memcached", "") if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } } if quickServices.Redis { - err = container.SetTechnology("redis") + err = container.SetTechnology("redis", "") if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } diff --git a/handlers_nats.go b/handlers_nats.go index fb6da33..1033fe6 100644 --- a/handlers_nats.go +++ b/handlers_nats.go @@ -15,6 +15,7 @@ import ( "encoding/json" "fmt" "log" + "regexp" "strings" "github.com/nats-io/nats.go" @@ -612,8 +613,25 @@ func processesEventHandler(m *nats.Msg, message *RequestMessage) error { } // Enable one of the supported technologies or services (python, node, redis, ...) +// If payload contains only name of the tech the default version for given image is selected. +// Otherwise it can be passed in format tech:version. func enableTechEventHandler(m *nats.Msg, message *RequestMessage) error { + if !regexp.Match("[a-z0-9A-Z]*:?[0-9\.\-]*", []byte(message.Payload)) { + return errors.New("payload malformation, it has to be in format tech:version") + } + service := message.Payload + version := "" + + if strings.Contains(service, ":") { + parts := strings.SplitN(message.Payload, ":", 2) + if len(parts) != 2 { + return errors.New("service and version malformat") + } + + service = parts[0] + version = parts[1] + } err := waitForApp(message.AppName) if err != nil { @@ -636,7 +654,7 @@ func enableTechEventHandler(m *nats.Msg, message *RequestMessage) error { App: app, } - err = container.SetTechnology(service) + err = container.SetTechnology(service, version) if err != nil { log.Println("ERROR enable tech problem: " + err.Error()) publish(app.Name, "backend problem", true)