示例#1
0
文件: build.go 项目: tomologic/wrench
func build() {
	image_name := config.GetProjectImage()

	if !flag_rebuild && utils.DockerImageExists(image_name) {
		fmt.Printf("INFO: Docker image %s already exists\n", image_name)

		// Build test image if missing
		if !utils.DockerImageExists(fmt.Sprintf("%s-test", image_name)) {
			buildTest()
		}

		os.Exit(0)
	}

	if utils.FileExists("./Dockerfile.builder") {
		buildBuilder()
		buildTest()
	} else if utils.FileExists("./Dockerfile") {
		buildSimple()
		buildTest()
	} else {
		fmt.Printf("ERROR: %s\n", "No Dockerfile found.")
		os.Exit(1)
	}
}
示例#2
0
文件: build.go 项目: tomologic/wrench
func buildTest() {
	image_name := config.GetProjectImage()

	test_image_name := fmt.Sprintf("%s-test", image_name)

	if !utils.FileExists("./Dockerfile.test") {
		return
	}

	fmt.Printf("INFO: %s %s\n\n",
		"Found Dockerfile.test, building test image",
		test_image_name)

	dockerfile := utils.GetFileContent("./Dockerfile.test")

	dockerfile_lines := strings.Split(dockerfile, "\n")

	if !strings.HasPrefix(dockerfile_lines[0], "FROM") {
		fmt.Println("ERROR: Missing FROM on first line in Dockerfile.test")
		os.Exit(1)
	}

	// if FROM string subfix with builder then base on builder image
	if strings.HasSuffix(dockerfile_lines[0], "builder") {
		dockerfile_lines[0] = fmt.Sprintf("FROM %s-builder", image_name)
	} else {
		dockerfile_lines[0] = fmt.Sprintf("FROM %s", image_name)
	}

	dockerfile = strings.Join(dockerfile_lines, "\n")

	utils.WriteFileContent("./Dockerfile.test", dockerfile)

	cmd_string := fmt.Sprintf("docker build -f Dockerfile.test -t '%s' .", test_image_name)
	cmd := exec.Command("sh", "-c", cmd_string)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
示例#3
0
}

var getTmplContextEnviron = func() *map[string]string {
	Environ := make(map[string]string)

	// Get all environment variables
	for _, item := range getEnviron() {
		splits := strings.Split(item, "=")
		Environ[splits[0]] = strings.Join(splits[1:], "=")
	}

	return &Environ
}

var getConfigContent = func() (string, error) {
	if !utils.FileExists("./wrench.yml") {
		return "", nil
	}

	content, err := ioutil.ReadFile("./wrench.yml")
	if err != nil {
		return "", err
	}

	return strings.TrimSpace(string(content)), nil
}

var getRenderedConfigContent = func(content string) (string, error) {
	// Create template from wrench file
	var config_rendered bytes.Buffer
	tmpl, err := template.New("config").Parse(content)
示例#4
0
文件: run.go 项目: tomologic/wrench
func run(name string) {
	image_name := config.GetProjectImage()

	run, ok := config.GetRun(name)
	if ok == false {
		fmt.Printf("ERROR: %s not found in wrench.yml\n", name)
		os.Exit(1)
	}

	if utils.FileExists("./Dockerfile.test") {
		// If test dockerfile exists then use test image
		image_name = fmt.Sprintf("%s-test", image_name)
	}

	if !utils.DockerImageExists(image_name) {
		fmt.Printf("ERROR: Image %s does not exist, run wrench build\n", image_name)
		os.Exit(1)
	}

	fmt.Printf("INFO: running %s in image %s\n", name, image_name)

	// Tempdir for building temporary run image
	dir, err := os.Getwd()
	if err != nil {
		fmt.Printf("ERROR: %s\n", err)
		os.Exit(1)
	}

	tempdir, err := ioutil.TempDir(dir, ".wrench_run_")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Create temp Dockerfile
	dockerfile_content := "" +
		fmt.Sprintf("FROM %s\n", image_name) +
		"ADD wrench_run.sh /tmp/\n" +
		"ENTRYPOINT [\"/bin/sh\"]\n" +
		"CMD [\"/tmp/wrench_run.sh\"]\n"

	dockerfile := fmt.Sprintf("%s/Dockerfile", tempdir)
	utils.WriteFileContent(dockerfile, dockerfile_content)

	// Create wrench run shell script
	runfile := fmt.Sprintf("%s/wrench_run.sh", tempdir)
	utils.WriteFileContent(runfile, run.Cmd)

	tempdir_base := string(filepath.Base(tempdir))
	run_image_name := fmt.Sprintf("%s-%s", image_name, tempdir_base)

	cmd_string := fmt.Sprintf("docker build -t '%s' .", run_image_name)
	cmd := exec.Command("sh", "-c", cmd_string)
	cmd.Dir = tempdir
	out, err := cmd.Output()

	// Remove tempdir
	if err != nil {
		os.RemoveAll(tempdir)

		fmt.Printf("ERROR: %s\n", string(out))
		os.Exit(1)
	}

	// Defer so all defered cleanup is done
	defer func() {
		if err != nil {
			fmt.Printf("ERROR: %s\n", err)
			os.Exit(utils.GetCommandExitCode(err))
		}
	}()
	defer utils.DockerRemoveImage(run_image_name)
	defer os.RemoveAll(tempdir)

	if len(run.Env) > 0 {
		envfile := fmt.Sprintf("./%s-env", tempdir_base)
		utils.WriteFileContent(envfile, strings.Join(run.Env, "\n"))
		defer os.Remove(envfile)

		cmd_string = fmt.Sprintf("docker run -t --rm --env-file '%s' '%s'", envfile, run_image_name)
	} else {
		cmd_string = fmt.Sprintf("docker run -t --rm '%s'", run_image_name)
	}

	// Run
	cmd = exec.Command("sh", "-c", cmd_string)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()
}