From 5f358b3fc4d11d2a3832b1b7a437efe39a9c0761 Mon Sep 17 00:00:00 2001 From: Yann Stepienik Date: Fri, 16 Jun 2023 11:52:37 +0100 Subject: [PATCH] Update to 0.7.0-unstable14 --- .../servapps/containers/docker-compose.jsx | 5 +- package.json | 2 +- src/docker/api_blueprint.go | 70 ++++++++++++++++++- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/client/src/pages/servapps/containers/docker-compose.jsx b/client/src/pages/servapps/containers/docker-compose.jsx index 2dfb8f5..8ff0056 100644 --- a/client/src/pages/servapps/containers/docker-compose.jsx +++ b/client/src/pages/servapps/containers/docker-compose.jsx @@ -81,13 +81,14 @@ const getHostnameFromName = (name) => { } const DockerComposeImport = ({ refresh, dockerComposeInit, installerInit, defaultName }) => { + const cleanDefaultName = defaultName && defaultName.replace(/\s/g, '-').replace(/[^a-zA-Z0-9-]/g, ''); const [step, setStep] = useState(0); const [isLoading, setIsLoading] = useState(false); const [openModal, setOpenModal] = useState(false); const [dockerCompose, setDockerCompose] = useState(''); const [service, setService] = useState({}); const [ymlError, setYmlError] = useState(''); - const [serviceName, setServiceName] = useState(defaultName || 'my-service'); + const [serviceName, setServiceName] = useState(cleanDefaultName || 'my-service'); const [hostnames, setHostnames] = useState({}); const [overrides, setOverrides] = useState({}); const [context, setContext] = useState({}); @@ -452,7 +453,7 @@ const DockerComposeImport = ({ refresh, dockerComposeInit, installerInit, defaul setContext({}); setDockerCompose(''); setInstaller(installerInit); - setServiceName(defaultName || 'default-name'); + setServiceName(cleanDefaultName || 'default-name'); } return <> diff --git a/package.json b/package.json index 34714d6..222fe61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cosmos-server", - "version": "0.7.0-unstable13", + "version": "0.7.0-unstable14", "description": "", "main": "test-server.js", "bugs": { diff --git a/src/docker/api_blueprint.go b/src/docker/api_blueprint.go index 719ac83..438d568 100644 --- a/src/docker/api_blueprint.go +++ b/src/docker/api_blueprint.go @@ -9,6 +9,7 @@ import ( "bufio" "strconv" "os" + "io/ioutil" "os/user" "errors" "github.com/docker/go-connections/nat" @@ -77,6 +78,8 @@ type ContainerCreateRequestContainer struct { CapAdd []string `json:"cap_add,omitempty"` CapDrop []string `json:"cap_drop,omitempty"` SysctlsMap map[string]string `json:"sysctls,omitempty"` + + PostInstall []string `json:"post_install,omitempty"` } type ContainerCreateRequestVolume struct { @@ -646,8 +649,71 @@ func CreateService(serviceRequest DockerServiceCreateRequest, OnLog func(string) } // Write a response to the client - utils.Log(fmt.Sprintf("Container %s started", container.Name)) - OnLog(fmt.Sprintf("Container %s started", container.Name)) + utils.Log(fmt.Sprintf("Container %s initiated", container.Name)) + OnLog(fmt.Sprintf("Container %s initiated", container.Name)) + + // if post install + if len(container.PostInstall) > 0 { + utils.Log(fmt.Sprintf("Waiting for container %s to start", container.Name)) + OnLog(fmt.Sprintf("Waiting for container %s to start", container.Name)) + + // wait for container to start + for { + time.Sleep(1 * time.Second) + inspect, _ := DockerClient.ContainerInspect(DockerContext, container.Name) + if inspect.State.Running { + break + } + } + time.Sleep(1 * time.Second) + + // run post install commands + for _, cmd := range container.PostInstall { + utils.Log(fmt.Sprintf("Running post install command: %s", cmd)) + OnLog(fmt.Sprintf("Running post install command: %s", cmd)) + + // setup the execution of command + execResponse, err := DockerClient.ContainerExecCreate(DockerContext, container.Name, doctype.ExecConfig{ + Cmd: []string{"/bin/sh", "-c", cmd}, + AttachStdout: true, + AttachStderr: true, + }) + + if err != nil { + utils.Error("CreateService: Post Install", err) + OnLog(fmt.Sprintf("[ERROR] Rolling back changes because of -- Post install error: "+err.Error())) + Rollback(rollbackActions, OnLog) + return err + } + + // attach to the exec instance + response, err := DockerClient.ContainerExecAttach(DockerContext, execResponse.ID, doctype.ExecStartCheck{}) + if err != nil { + utils.Error("CreateService: Post Install", err) + OnLog(fmt.Sprintf("[ERROR] Rolling back changes because of -- Post install error: "+err.Error())) + Rollback(rollbackActions, OnLog) + return err + } + defer response.Close() + + // run the command + err = DockerClient.ContainerExecStart(DockerContext, execResponse.ID, doctype.ExecStartCheck{}) + if err != nil { + utils.Error("CreateService: Post Install", err) + OnLog(fmt.Sprintf("[ERROR] Rolling back changes because of -- Post install error: "+err.Error())) + Rollback(rollbackActions, OnLog) + return err + } + + // read the output + out, _ := ioutil.ReadAll(response.Reader) + OnLog(fmt.Sprintf("----> %s", out)) + } + + // restart container + DockerClient.ContainerRestart(DockerContext, container.Name, conttype.StopOptions{}) + } + } // Save the route configs