예제 #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 buildBuilder() {
	image_name := config.GetProjectImage()

	builder_image_name := fmt.Sprintf("%s-builder", image_name)

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

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

	version := strings.TrimLeft(config.GetProjectVersion(), "v")
	fmt.Printf("INFO: Adding env variable VERSION=%s\n\n", version)
	utils.DockerImageAddEnv(builder_image_name, "VERSION", version)

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Printf("\nINFO: %s %s\n\n",
		"Building image with builder",
		image_name)

	// Build image
	cmd_string = fmt.Sprintf("docker run --rm '%s' | docker build -t '%s' -", builder_image_name, 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)
	}

	fmt.Printf("INFO: Adding env variable VERSION=%s\n\n", version)
	utils.DockerImageAddEnv(image_name, "VERSION", version)
}
예제 #3
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)
	}
}
예제 #4
0
파일: build.go 프로젝트: tomologic/wrench
func buildSimple() {
	image_name := config.GetProjectImage()

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

	cmd_string := fmt.Sprintf("docker build -t '%s' .", 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)
	}

	version := strings.TrimLeft(config.GetProjectVersion(), "v")
	fmt.Printf("INFO: Adding env variable VERSION=%s\n\n", version)
	utils.DockerImageAddEnv(image_name, "VERSION", version)
}
예제 #5
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()
}