refactor: remove all calls to common.NewShellCmdWithArgs
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package appjson
|
package appjson
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -332,13 +331,16 @@ func executeScript(appName string, image string, imageTag string, phase string)
|
|||||||
fmt.Sprintf("LABEL com.dokku.%s-phase=true", phase),
|
fmt.Sprintf("LABEL com.dokku.%s-phase=true", phase),
|
||||||
}...)
|
}...)
|
||||||
commitArgs = append(commitArgs, containerID, image)
|
commitArgs = append(commitArgs, containerID, image)
|
||||||
containerCommitCmd := common.NewShellCmdWithArgs(
|
result, err := common.CallExecCommand(common.ExecCommandInput{
|
||||||
common.DockerBin(),
|
Command: common.DockerBin(),
|
||||||
commitArgs...,
|
Args: commitArgs,
|
||||||
)
|
StreamStderr: true,
|
||||||
containerCommitCmd.ShowOutput = false
|
})
|
||||||
containerCommitCmd.Command.Stderr = os.Stderr
|
if err != nil {
|
||||||
if !containerCommitCmd.Execute() {
|
return fmt.Errorf("Commiting of '%s' to image failed: %w", phase, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result.ExitCode != 0 {
|
||||||
return fmt.Errorf("Commiting of '%s' to image failed: %s", phase, command)
|
return fmt.Errorf("Commiting of '%s' to image failed: %s", phase, command)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,34 +392,11 @@ func getCommandFromImage(image string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func waitForExecution(containerID string) bool {
|
func waitForExecution(containerID string) bool {
|
||||||
containerStartCmd := common.NewShellCmdWithArgs(
|
if !common.ContainerStart(containerID) {
|
||||||
common.DockerBin(),
|
|
||||||
"container",
|
|
||||||
"start",
|
|
||||||
containerID,
|
|
||||||
)
|
|
||||||
containerStartCmd.ShowOutput = false
|
|
||||||
containerStartCmd.Command.Stderr = os.Stderr
|
|
||||||
if !containerStartCmd.Execute() {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
containerWaitCmd := common.NewShellCmdWithArgs(
|
return common.ContainerWait(containerID)
|
||||||
common.DockerBin(),
|
|
||||||
"container",
|
|
||||||
"wait",
|
|
||||||
containerID,
|
|
||||||
)
|
|
||||||
|
|
||||||
containerWaitCmd.ShowOutput = false
|
|
||||||
containerWaitCmd.Command.Stderr = os.Stderr
|
|
||||||
b, err := containerWaitCmd.Output()
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
containerExitCode := strings.TrimSpace(string(b[:]))
|
|
||||||
return containerExitCode == "0"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createdContainerID(appName string, dockerArgs []string, image string, command []string, phase string) (string, error) {
|
func createdContainerID(appName string, dockerArgs []string, image string, command []string, phase string) (string, error) {
|
||||||
@@ -440,21 +419,19 @@ func createdContainerID(appName string, dockerArgs []string, image string, comma
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
containerCreateCmd := common.NewShellCmdWithArgs(
|
result, err := common.CallExecCommand(common.ExecCommandInput{
|
||||||
common.DockerBin(),
|
Command: common.DockerBin(),
|
||||||
arguments...,
|
Args: arguments,
|
||||||
)
|
Env: env,
|
||||||
var stderr bytes.Buffer
|
})
|
||||||
containerCreateCmd.Env = env
|
|
||||||
containerCreateCmd.ShowOutput = false
|
|
||||||
containerCreateCmd.Command.Stderr = &stderr
|
|
||||||
|
|
||||||
b, err = containerCreateCmd.Output()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New(stderr.String())
|
return "", err
|
||||||
|
}
|
||||||
|
if result.ExitCode != 0 {
|
||||||
|
return "", errors.New(result.StderrContents())
|
||||||
}
|
}
|
||||||
|
|
||||||
containerID := strings.TrimSpace(string(b))
|
containerID := result.StdoutContents()
|
||||||
err = common.PlugnTrigger("post-container-create", []string{"app", containerID, appName, phase}...)
|
err = common.PlugnTrigger("post-container-create", []string{"app", containerID, appName, phase}...)
|
||||||
return containerID, err
|
return containerID, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,19 @@ func ContainerIsRunning(containerID string) bool {
|
|||||||
return strings.TrimSpace(string(b[:])) == "true"
|
return strings.TrimSpace(string(b[:])) == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ContainerStart runs 'docker container start' against an existing container
|
||||||
|
func ContainerStart(containerID string) bool {
|
||||||
|
result, err := CallExecCommand(ExecCommandInput{
|
||||||
|
Command: DockerBin(),
|
||||||
|
Args: []string{"container", "start", containerID},
|
||||||
|
StreamStderr: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return result.ExitCode == 0
|
||||||
|
}
|
||||||
|
|
||||||
// ContainerRemove runs 'docker container remove' against an existing container
|
// ContainerRemove runs 'docker container remove' against an existing container
|
||||||
func ContainerRemove(containerID string) bool {
|
func ContainerRemove(containerID string) bool {
|
||||||
result, err := CallExecCommand(ExecCommandInput{
|
result, err := CallExecCommand(ExecCommandInput{
|
||||||
@@ -43,6 +56,19 @@ func ContainerExists(containerID string) bool {
|
|||||||
return result.ExitCode == 0
|
return result.ExitCode == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ContainerWait runs 'docker container wait' against an existing container
|
||||||
|
func ContainerWait(containerID string) bool {
|
||||||
|
result, err := CallExecCommand(ExecCommandInput{
|
||||||
|
Command: DockerBin(),
|
||||||
|
Args: []string{"container", "wait", containerID},
|
||||||
|
StreamStderr: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return result.ExitCode == 0
|
||||||
|
}
|
||||||
|
|
||||||
// ContainerWaitTilReady will wait timeout seconds and then check if a container is running
|
// ContainerWaitTilReady will wait timeout seconds and then check if a container is running
|
||||||
// returning an error if it is not running at the end of the timeout
|
// returning an error if it is not running at the end of the timeout
|
||||||
func ContainerWaitTilReady(containerID string, timeout time.Duration) error {
|
func ContainerWaitTilReady(containerID string, timeout time.Duration) error {
|
||||||
|
|||||||
@@ -42,6 +42,12 @@ type ExecCommandInput struct {
|
|||||||
// StreamStderr prints stderr directly to os.Stderr as the command runs.
|
// StreamStderr prints stderr directly to os.Stderr as the command runs.
|
||||||
StreamStderr bool
|
StreamStderr bool
|
||||||
|
|
||||||
|
// StdoutWriter is the writer to write stdout to
|
||||||
|
StdoutWriter io.Writer
|
||||||
|
|
||||||
|
// StderrWriter is the writer to write stderr to
|
||||||
|
StderrWriter io.Writer
|
||||||
|
|
||||||
// Sudo runs the command with sudo -n -u root
|
// Sudo runs the command with sudo -n -u root
|
||||||
Sudo bool
|
Sudo bool
|
||||||
}
|
}
|
||||||
@@ -137,6 +143,12 @@ func CallExecCommandWithContext(ctx context.Context, input ExecCommandInput) (Ex
|
|||||||
if input.StreamStderr {
|
if input.StreamStderr {
|
||||||
cmd.StdErrWriter = os.Stderr
|
cmd.StdErrWriter = os.Stderr
|
||||||
}
|
}
|
||||||
|
if input.StdoutWriter != nil {
|
||||||
|
cmd.StdOutWriter = input.StdoutWriter
|
||||||
|
}
|
||||||
|
if input.StderrWriter != nil {
|
||||||
|
cmd.StdErrWriter = input.StderrWriter
|
||||||
|
}
|
||||||
|
|
||||||
res, err := cmd.Execute(ctx)
|
res, err := cmd.Execute(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -174,22 +174,27 @@ func LogVerboseQuietContainerLogsTail(containerID string, lines int, tail bool)
|
|||||||
args = append(args, "--follow")
|
args = append(args, "--follow")
|
||||||
}
|
}
|
||||||
|
|
||||||
sc := NewShellCmdWithArgs(DockerBin(), args...)
|
|
||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
sc.Command.Stdout = &writer{
|
result, err := CallExecCommand(ExecCommandInput{
|
||||||
mu: &mu,
|
Command: DockerBin(),
|
||||||
source: "stdout",
|
Args: args,
|
||||||
}
|
DisableStdioBuffer: true,
|
||||||
sc.Command.Stderr = &writer{
|
StdoutWriter: &writer{
|
||||||
mu: &mu,
|
mu: &mu,
|
||||||
source: "stderr",
|
source: "stdout",
|
||||||
}
|
},
|
||||||
|
StderrWriter: &writer{
|
||||||
|
mu: &mu,
|
||||||
|
source: "stderr",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
if err := sc.Command.Start(); err != nil {
|
if err != nil {
|
||||||
LogExclaim(fmt.Sprintf("Failed to fetch container logs: %s", containerID))
|
LogExclaim(fmt.Sprintf("Failed to fetch container logs: %s", containerID))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := sc.Command.Wait(); err != nil {
|
if !tail && result.ExitCode != 0 {
|
||||||
LogExclaim(fmt.Sprintf("Failed to fetch container logs: %s", containerID))
|
LogExclaim(fmt.Sprintf("Failed to fetch container logs: %s", containerID))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user