Possibility to enable techs and services via API

* Environment variables for Exec
This commit is contained in:
Adam Štrauch 2020-08-21 01:00:24 +02:00
parent fb69cd2a81
commit ba748f1134
Signed by: cx
GPG key ID: 018304FFA8988F8D
4 changed files with 82 additions and 8 deletions

View file

@ -364,7 +364,7 @@ func (d *Driver) Create(name string, image string, volumePath string, HTTPPort i
}
// Exec runs command cmd with stdin if it's not empty.
func (d *Driver) Exec(name string, cmd []string, stdin string) error {
func (d *Driver) Exec(name string, cmd []string, stdin string, env []string) error {
if len(cmd) == 0 {
return errors.New("cmd needs at least one string in the slice")
}

View file

@ -171,12 +171,12 @@ func (c *Container) Delete() error {
func (c *Container) SetPassword(password string) error {
driver := c.getDriver()
err := driver.Exec(c.App.Name, []string{"chpasswd"}, appUsername+":"+password)
err := driver.Exec(c.App.Name, []string{"chpasswd"}, appUsername+":"+password, []string{})
if err != nil {
return err
}
err = driver.Exec(c.App.Name, []string{"tee", passwordFile}, password)
err = driver.Exec(c.App.Name, []string{"tee", passwordFile}, password, []string{})
if err != nil {
return err
}
@ -191,26 +191,35 @@ func (c *Container) SetFileContent(filename string, text string, mode string) er
directory := path.Dir(filename)
err := driver.Exec(c.App.Name, []string{"mkdir", "-p", directory}, "")
err := driver.Exec(c.App.Name, []string{"mkdir", "-p", directory}, "", []string{})
if err != nil {
return err
}
err = driver.Exec(c.App.Name, []string{"tee", filename}, text)
err = driver.Exec(c.App.Name, []string{"tee", filename}, text, []string{})
if err != nil {
return err
}
err = driver.Exec(c.App.Name, []string{"chown", directory, "app:app"}, "")
err = driver.Exec(c.App.Name, []string{"chown", directory, "app:app"}, "", []string{})
if err != nil {
return err
}
err = driver.Exec(c.App.Name, []string{"chown", filename, "app:app"}, "")
err = driver.Exec(c.App.Name, []string{"chown", filename, "app:app"}, "", []string{})
if err != nil {
return err
}
err = driver.Exec(c.App.Name, []string{"chmod", mode, filename}, "")
err = driver.Exec(c.App.Name, []string{"chmod", mode, filename}, "", []string{})
return err
}
// 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 {
driver := c.getDriver()
err := driver.Exec(c.App.Name, []string{"su", "app", "-c", "rosti " + tech}, "", []string{})
return err
}

56
main.go
View file

@ -317,6 +317,62 @@ func main() {
return c.JSON(http.StatusOK, Message{Message: "ok"})
})
e.PUT("/v1/apps/:name/set-services", func(c echo.Context) error {
name := c.Param("name")
quickServices := &QuickServices{}
err := c.Bind(quickServices)
if err != nil {
return c.JSONPretty(http.StatusBadRequest, Message{Message: err.Error()}, JSONIndent)
}
app, err := apps.Get(name)
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
container := docker.Container{
App: app,
}
if quickServices.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")
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
}
if quickServices.Node {
err = container.SetTechnology("node")
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
}
if quickServices.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")
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
}
return c.JSON(http.StatusOK, Message{Message: "ok"})
})
// Rebuilds existing app, it keeps the data but created the container again
e.PUT("/v1/apps/:name/rebuild", func(c echo.Context) error {
name := c.Param("name")

View file

@ -23,3 +23,12 @@ type templateData struct {
type Password struct {
Password string `json:"password"`
}
// QuickServices comes from client and say what technology or service enable in the container
type QuickServices struct {
Python bool `json:"python"`
Node bool `json:"node"`
PHP bool `json:"php"`
Memcached bool `json:"memcached"`
Redis bool `json:"redis"`
}