Пример #1
0
/**
 * Start a container as binary
 */
func Run(name, currentPath string, arguments []string, ports, environments map[string]string) {
	runFunc := reflect.ValueOf(exec.Command)
	rawArgs := []string{getDockerBinaryPath(), "run", "-v=" + currentPath + ":" + currentPath, "-w=" + currentPath}

	// Add environments
	util.Debug(environments)
	for envName, envValue := range environments {
		rawArgs = append(rawArgs, "-e="+envName+"="+envValue)
	}

	// Add ports
	for portIn, portOut := range ports {
		rawArgs = append(rawArgs, "-p="+string(portIn)+":"+string(portOut))
	}

	rawArgs = append(rawArgs, name)

	// Add user arguments
	for _, argument := range arguments {
		rawArgs = append(rawArgs, argument)
	}

	runCmd := runFunc.Call(util.BuildReflectArguments(rawArgs))[0].Interface().(*exec.Cmd)
	runCmd.Stdout = os.Stdout
	runCmd.Stdin = os.Stdin
	runCmd.Stderr = os.Stderr

	util.Debug("Run command:", runCmd.Args)

	if err := runCmd.Start(); err != nil {
		util.LogError(err)
	}
}
Пример #2
0
func Build(name, path string) {
	buildFunc := reflect.ValueOf(exec.Command)
	rawArgs := []string{getDockerBinaryPath(), "build"}

	if *noCache {
		rawArgs = append(rawArgs, "--no-cache")
	}

	rawArgs = append(rawArgs, "-t", name, path)

	util.Debug(rawArgs)

	buildCmd := buildFunc.Call(util.BuildReflectArguments(rawArgs))[0].Interface().(*exec.Cmd)
	buildCmd.Stderr = os.Stderr

	if !*quiet {
		buildCmd.Stdout = os.Stdout
	}

	if err := buildCmd.Run(); err != nil {
		util.LogError(err)
	}

	buildCmd.Wait()

	time.Sleep(1 * time.Second)
}
Пример #3
0
func Build(name, path string) {
	buildFunc := reflect.ValueOf(exec.Command)
	rawArgs := []string{getDockerBinaryPath(), "build"}

	if *noCache {
		rawArgs = append(rawArgs, "--no-cache")
	}

	rawArgs = append(rawArgs, "-t", name, path)

	util.Debug(rawArgs)

	buildCmd := buildFunc.Call(util.BuildReflectArguments(rawArgs))[0].Interface().(*exec.Cmd)
	buildCmd.Stdin = os.Stdin

	out, err := buildCmd.CombinedOutput()
	if err != nil {
		util.Print(string(out))
		util.LogError("Error while starting container '" + name + "'")
	}

	buildCmd.Wait()

	time.Sleep(1 * time.Second)
}
Пример #4
0
/**
 * Start a container as a server
 */
func Start(name, image string, links []string, ports, volumes, environments map[string]string) string {
	runFunc := reflect.ValueOf(exec.Command)
	rawArgs := []string{getDockerBinaryPath(), "run", "-d", "-i", "-t", "--privileged", "--name=" + name}

	// Add environments
	util.Debug(environments)
	for envName, envValue := range environments {
		rawArgs = append(rawArgs, "-e="+envName+"="+envValue)
	}

	// Add links
	for _, link := range links {
		rawArgs = append(rawArgs, "--link="+link+":"+link)
	}

	// Add ports
	for portIn, portOut := range ports {
		rawArgs = append(rawArgs, "-p="+string(portIn)+":"+string(portOut))
	}

	// Add volumes
	for volumeHost, volumeContainer := range volumes {
		rawArgs = append(rawArgs, "-v="+volumeHost+":"+volumeContainer)
	}

	rawArgs = append(rawArgs, image)

	// Initiate the command with several arguments
	runCmd := runFunc.Call(util.BuildReflectArguments(rawArgs))[0].Interface().(*exec.Cmd)
	util.Debug("Start command:", runCmd.Args)

	out, err := runCmd.CombinedOutput()
	if err != nil {
		util.LogError(string(out))
	}

	// Inspect container to check status code
	exitCodeBuff, _ := Inspect(name, "--format", "{{.State.ExitCode}}")
	exitCode, _ := strconv.Atoi(strings.TrimSpace(string(exitCodeBuff)))

	if exitCode != 0 {
		error, _ := Logs(name)
		util.LogError("Error while starting container '" + name + "' : " + error)
	}

	return string(out)
}
Пример #5
0
func Exec(args []string, hasStdout bool) {
	execFunc := reflect.ValueOf(exec.Command)
	execCmd := execFunc.Call(util.BuildReflectArguments(args))[0].Interface().(*exec.Cmd)

	if hasStdout {
		execCmd.Stdout = os.Stdout
	}

	execCmd.Stdin = os.Stdin
	execCmd.Stderr = os.Stderr

	util.Debug("Exec command:", execCmd.Args)

	if err := execCmd.Start(); err != nil {
		util.LogError(err)
	}
}
Пример #6
0
func Inspect(params ...string) ([]byte, error) {
	inspectFunc := reflect.ValueOf(exec.Command)
	rawArgs := []string{getDockerBinaryPath(), "inspect"}

	// Add extra arguments
	if len(params) > 1 {
		for _, arg := range params[1:] {
			rawArgs = append(rawArgs, arg)
		}
	}

	// Add the id at the end
	rawArgs = append(rawArgs, params[0])

	inspectCmd := inspectFunc.Call(util.BuildReflectArguments(rawArgs))[0].Interface().(*exec.Cmd)
	out, err := inspectCmd.CombinedOutput()
	if err != nil {
		return nil, err
	}

	return out, nil
}
Пример #7
0
/**
 * Start a container as a server
 */
func Start(name, image string, links []string, ports, volumes, environments map[string]string) string {
	runFunc := reflect.ValueOf(exec.Command)
	rawArgs := []string{getDockerBinaryPath(), "run", "-d", "-i", "-t", "--privileged", "--name=" + name}

	// Add environments
	util.Debug(environments)
	for envName, envValue := range environments {
		rawArgs = append(rawArgs, "-e="+envName+"="+envValue)
	}

	// Add links
	for _, link := range links {
		rawArgs = append(rawArgs, "--link="+link+":"+link)
	}

	// Add ports
	for portIn, portOut := range ports {
		rawArgs = append(rawArgs, "-p="+string(portIn)+":"+string(portOut))
	}

	// Add volumes
	for volumeHost, volumeContainer := range volumes {
		rawArgs = append(rawArgs, "-v="+volumeHost+":"+volumeContainer)
	}

	rawArgs = append(rawArgs, image)

	// Initiate the command with several arguments
	runCmd := runFunc.Call(util.BuildReflectArguments(rawArgs))[0].Interface().(*exec.Cmd)
	util.Debug("Start command:", runCmd.Args)

	out, err := runCmd.CombinedOutput()
	if err != nil {
		util.LogError(string(out))
	}

	return string(out)
}