Esempio n. 1
0
func Run(identifier, container string) {
	packages_metadir_exists, _ := common.Exists(common.Packages_metadir)
	var package_exists = false
	var existing_package = common.Package{}
	if packages_metadir_exists != true {
		os.MkdirAll(common.Packages_metadir, 0777)
		// Make this debug log
		fmt.Printf("Creating %v\n", common.Packages_metadir)
	} else {
		files, _ := ioutil.ReadDir(common.Packages_metadir)
		for i := 0; i < len(files); i++ {
			folder := files[i]
			metadata_file := common.Packages_metadir + "/" + folder.Name() + "/metadata.json"
			p_meta_matches := strings.HasPrefix(folder.Name(), identifier)
			p_meta_exists, _ := common.Exists(metadata_file)
			if p_meta_exists && p_meta_matches {
				p, _ := common.ReadPackageJSON(metadata_file)
				package_exists = true
				existing_package = p
			} else {
				// Write debugging messages here?`
			}
		}
	}
	if package_exists {
		record(existing_package, container)
		deploy(existing_package, container)
	} else {
		fmt.Printf("No package found matching %v\n", identifier)
		os.Exit(1)
	}

}
Esempio n. 2
0
func record(p common.Package, container string) {
	this_deploy_metadir := common.Deployments_metadir + "/" + container + "/" + p.ID
	deployments_metadir_exists, _ := common.Exists(this_deploy_metadir)
	// TODO: Error handling
	if deployments_metadir_exists != true {
		os.MkdirAll(this_deploy_metadir, 0777)
		if verbose {
			fmt.Printf("Creating %v\n", common.Deployments_metadir)
		}
	}
	t := time.Now()
	mapD := map[string]string{"id": p.ID, "source-file": p.SourceFile, "deployable-uri": p.DeployableURI, "destination": p.Destination, "md5sum": p.Md5sum, "deployed": t.Format(time.RFC3339)}
	mapB, _ := json.Marshal(mapD)
	if verbose {
		fmt.Println(string(mapB))
	}

	d1 := []byte(string(mapB))
	error := ioutil.WriteFile(this_deploy_metadir+"/metadata.json", d1, 0644)
	if error != nil {
		fmt.Println(error)
		os.Exit(1)
	}
}
Esempio n. 3
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
}