Esempio n. 1
0
func CreateDockerImage(file, container, destination string) {
	fmt.Println("\n2  - Commencing creation of new Docker image identical to patch")
	// Make this part of debug
	fmt.Println(" a - Retrieving image for running container")
	image_name := docker.PsFilterFormat("[name="+container+"]", "{{.Image}}")

	fmt.Println(" b - Creating temporary Docker context")
	id := stringid.GenerateRandomID()
	tmp_docker_context := "/tmp/" + id
	os.MkdirAll(tmp_docker_context, 0777)
	filepieces := strings.Split(file, "/")
	filename := filepieces[len(filepieces)-1]

	fmt.Println(" c - Pulling snapshot package into Docker context")
	common.Cp(file, tmp_docker_context+"/"+filename)

	dfile := CreateDockerFile(image_name, filename, destination, tmp_docker_context)
	// Make this part of debug
	fmt.Printf("Temporary Dockerfile at %v\n", dfile.Name())
	docker.Build(dfile, container, "latest", tmp_docker_context)
	// Run Docker tag as <image name>_<container name>:latest
	// TODO: accept -t imagename:tag
	// Implement push when the flag is set
	//docker.Push(container, "latest")
	//os.RemoveAll(tmp_docker_context)
}
Esempio n. 2
0
func Pack(file, destination string) string {
	packages_metadir_exists, _ := common.Exists(packages_metadir)
	// TODO: Error handling
	var package_exists = false
	var existing_package_id = ""
	currentmd5sumfull := md5sum(file)
	if packages_metadir_exists != true {
		os.MkdirAll(packages_metadir, 0777)
		if verbose {
			fmt.Printf("Creating %v\n", packages_metadir)
		}
	} else {
		files, _ := ioutil.ReadDir(packages_metadir)
		for i := 0; i < len(files); i++ {
			folder := files[i]
			metadata_file := packages_metadir + "/" + folder.Name() + "/metadata.json"
			if verbose {
				fmt.Printf("Reading %v\n", metadata_file)
			}
			p_meta_exists, _ := common.Exists(metadata_file)
			if p_meta_exists == true {
				p, _ := common.ReadPackageJSON(metadata_file)
				if verbose {
					fmt.Printf("Comparing %v with %v\n", file, p.SourceFile)
				}
				savedmd5 := strings.Split(p.Md5sum, " ")[0]
				currentmd5 := strings.Split(currentmd5sumfull, " ")[0]
				if verbose {
					fmt.Println(savedmd5)
					fmt.Println(currentmd5)
				}
				if p.SourceFile == file && savedmd5 == currentmd5 {
					package_exists = true
					existing_package_id = p.ID
					break
				}
			}
		}
	}

	if package_exists {
		return existing_package_id
	}

	id := stringid.GenerateRandomID()
	// Replace os-dependent implementation with the one above
	//id, err := exec.Command("uuidgen").Output()
	//if err != nil {
	//	fmt.Println("Issue accessing uuidgen")
	//	os.Exit(1)
	//}

	// Move the writing of Package JSON to a separate function
	t := time.Now()
	sanitised_id := string(id)[:len(id)-1]

	this_package_metadir := packages_metadir + "/" + sanitised_id
	this_package_metadir_exists, _ := common.Exists(this_package_metadir)
	// TODO: Error handling
	if this_package_metadir_exists != true {
		os.MkdirAll(this_package_metadir, 0777)
		// Make this debug log
		fmt.Printf("Creating %v\n", this_package_metadir)
	} else {
		fmt.Println(packages_metadir + " exists! This should never happen. Exiting...")
		os.Exit(1)
	}

	filepieces := strings.Split(file, "/")
	filename := filepieces[len(filepieces)-1]

	contents_cached := this_package_metadir + "/" + filename

	abs_file, _ := filepath.Abs(file)
	common.Cp(abs_file, contents_cached)
	md5sum := md5sum(contents_cached)

	mapD := map[string]string{"id": sanitised_id, "source-file": abs_file, "deployable-uri": contents_cached, "destination": destination, "create": t.Format(time.RFC3339), "md5sum": md5sum}
	mapB, _ := json.Marshal(mapD)
	fmt.Println(string(mapB))

	d1 := []byte(string(mapB))
	error := ioutil.WriteFile(this_package_metadir+"/metadata.json", d1, 0644)
	if error != nil {
		fmt.Println(error)
		os.Exit(1)
	}

	return sanitised_id
}