Exemplo n.º 1
0
// was getDockerImageUrl(Dockerimage string) (download_url string, err error)
func findDockerImageInShock(Dockerimage string) (node *shock.ShockNode, download_url string, err error) {

	shock_docker_repo := shock.ShockClient{conf.SHOCK_DOCKER_IMAGE_REPOSITORY, ""}

	logger.Debug(1, fmt.Sprint("try to import docker image, Dockerimage=", Dockerimage))
	//query url = type=dockerimage&name=wgerlach/bowtie2:2.2.0"

	query_response_p, err := shock_docker_repo.Query(url.Values{"type": {"dockerimage"}, "name": {Dockerimage}})
	if err != nil {
		return nil, "", errors.New(fmt.Sprintf("shock node not found for image=%s, err=%s", Dockerimage, err.Error()))
	}
	logger.Debug(1, fmt.Sprintf("query result: %v", query_response_p))

	datalen := len((*query_response_p).Data)

	if datalen == 0 {
		return nil, "", errors.New(fmt.Sprintf("image %s not found in shocks docker repo", Dockerimage))
	} else if datalen > 1 {
		return nil, "", errors.New(fmt.Sprintf("more than one image %s found in shocks docker repo", Dockerimage))
	}

	node = &(*query_response_p).Data[0]
	logger.Debug(1, fmt.Sprintf("found SHOCK node for docker image: %s", node.Id))

	download_url, err = shock_docker_repo.Get_node_download_url(*node)
	if err != nil {
		return nil, "", errors.New(fmt.Sprintf("Could not create download url, err=%s", err.Error()))
	}

	return
}
Exemplo n.º 2
0
func dockerBuildImage(client *docker.Client, Dockerimage string) (err error) {
	logger.Debug(1, fmt.Sprintf("(dockerBuildImage) %s:", Dockerimage))

	shock_docker_repo := shock.ShockClient{Host: conf.SHOCK_DOCKER_IMAGE_REPOSITORY, Token: ""}

	logger.Debug(1, fmt.Sprint("try to build docker image from dockerfile, Dockerimage=", Dockerimage))

	query_response_p, err := shock_docker_repo.Query(url.Values{"dockerfile": {"1"}, "tag": {Dockerimage}})
	if err != nil {
		return errors.New(fmt.Sprintf("shock node not found for dockerfile=%s, err=%s", Dockerimage, err.Error()))
	}
	logger.Debug(1, fmt.Sprintf("query result: %v", query_response_p))

	datalen := len((*query_response_p).Data)

	if datalen == 0 {
		return errors.New(fmt.Sprintf("Dockerfile %s not found in shocks docker repo", Dockerimage))
	} else if datalen > 1 {
		return errors.New(fmt.Sprintf("more than one Dockerfile %s found in shocks docker repo", Dockerimage))
	}

	node := (*query_response_p).Data[0]
	logger.Debug(1, fmt.Sprintf("found SHOCK node for Dockerfile: %s", node.Id))

	download_url, err := shock_docker_repo.Get_node_download_url(node)
	if err != nil {
		return errors.New(fmt.Sprintf("Could not create download url, err=%s", err.Error()))
	}

	// get and build Dockerfile
	var buf bytes.Buffer
	opts := docker.BuildImageOptions{
		Name:           "testImage",
		Remote:         download_url,
		SuppressOutput: true,
		OutputStream:   &buf,
	}
	err = client.BuildImage(opts)
	if err != nil {
		return errors.New(fmt.Sprintf("Error importing docker image, err=%s", err.Error()))
	}

	return nil
}
Exemplo n.º 3
0
// was getDockerImageUrl(Dockerimage string) (download_url string, err error)
func findDockerImageInShock(Dockerimage string, datatoken string) (node *shock.ShockNode, download_url string, err error) {

	logger.Debug(1, fmt.Sprint("datatoken for dockerimage: ", datatoken[0:15]))
	logger.Debug(1, fmt.Sprint("try to import docker image, Dockerimage=", Dockerimage))

	shock_docker_repo := shock.ShockClient{Host: conf.SHOCK_DOCKER_IMAGE_REPOSITORY, Token: datatoken}

	dockerimage_array := strings.Split(Dockerimage, ":")

	if len(dockerimage_array) != 2 {
		return nil, "", errors.New(fmt.Sprintf("could not split dockerimage name %s into two pieces", Dockerimage))
	}

	dockerimage_repo := dockerimage_array[0]
	dockerimage_tag := dockerimage_array[1]

	var version_array = [...]string{"unknown", "dev", "develop", "alpha", "a", "beta", "b", "c", "d", "e"}
	var version_strings = make(map[string]int)
	for i, val := range version_array {
		version_strings[val] = i
	}

	if dockerimage_tag == "latest" {
		query_response_p, err := shock_docker_repo.Query(url.Values{"type": {"dockerimage"}, "repository": {dockerimage_repo}})
		if err != nil {
			return nil, "", errors.New(fmt.Sprintf("shock node not found for image repo=%s, err=%s", dockerimage_repo, err.Error()))
		}
		logger.Debug(1, fmt.Sprintf("query result: %v", query_response_p))
		datalen := len((*query_response_p).Data)
		if datalen == 0 {
			return nil, "", errors.New(fmt.Sprintf("image repo %s not found in shocks docker repo", dockerimage_repo))
		}

		images := (*query_response_p).Data

		dsn_array := make([]DockerShockNode, 0, 100)

		reg_version, err := regexp.Compile(`^([0-9]+)\-?([^0-9]*)$`)

		for _, image := range images {

			//version_int_array := make([]int, len(version_str_array) )

			attr_map, ok := image.Attributes.(map[string]interface{}) // is of type map[string]interface{}

			if !ok {
				return nil, "", errors.New(fmt.Sprintf("could not convert attributes=%s", Dockerimage))
			}

			attr := DockerImageAttributes{}

			// conversion from shock attributes interface to struct
			// name
			value, ok := attr_map["name"]
			if !ok {
				return nil, "", errors.New(fmt.Sprintf("error reading docker image info from shock node=%s", Dockerimage))
			}
			value_str, ok := value.(string)
			if !ok || value_str == "" {
				return nil, "", errors.New(fmt.Sprintf("error reading docker image info from shock node=%s", Dockerimage))
			}
			attr.Name = value_str

			// tag
			value, ok = attr_map["tag"]
			if !ok {
				return nil, "", errors.New(fmt.Sprintf("error reading docker image info from shock node=%s", Dockerimage))
			}
			value_str, ok = value.(string)
			if !ok || value_str == "" {
				return nil, "", errors.New(fmt.Sprintf("error reading docker image info from shock node=%s", Dockerimage))
			}
			attr.Tag = value_str

			// repository
			value, ok = attr_map["repository"]
			if !ok {
				return nil, "", errors.New(fmt.Sprintf("error reading docker image info from shock node=%s", Dockerimage))
			}
			value_str, ok = value.(string)
			if !ok || value_str == "" {
				return nil, "", errors.New(fmt.Sprintf("error reading docker image info from shock node=%s", Dockerimage))
			}
			attr.Repository = value_str

			version_str_array := strings.Split(attr.Tag, ".")
			version_str_array_len := len(version_str_array)
			dsn := DockerShockNode{ShockNode: image,
				Attributes: attr,
				Version:    make([]int, 2*version_str_array_len, 2*version_str_array_len)}

			logger.Debug(1, fmt.Sprintf("dsn.Attributes.Tag: "+dsn.Attributes.Tag))

			for j, val := range version_str_array {
				logger.Debug(1, fmt.Sprintf("version_str_array: j=%d val=%s ", j, val))
				// j*2+1 is reserved for characters in version number // TODO 2.2b -> 2,0,2,1
				dsn.Version[j*2] = 0
				dsn.Version[j*2+1] = 0

				reg_version_matches := reg_version.FindStringSubmatch(val)
				//if len(reg_version_matches_all) == 0 {
				//	return nil, "", errors.New(fmt.Sprintf("could not parse version tag \"%s\" \"%s\" %s", val, attr.Tag, Dockerimage))
				//}
				//if len(reg_version_matches_all) > 1 {
				//	return nil, "", errors.New(fmt.Sprintf("to many matches in version tag \"%s\" \"%s\" %s", val, attr.Tag, Dockerimage))
				//}
				//reg_version_matches := reg_version_matches_all[0]

				if len(reg_version_matches) > 1 {
					val_number := reg_version_matches[1]
					logger.Debug(1, fmt.Sprintf("version match 0 (number): %s ", val_number))

					val_int, err := strconv.Atoi(val_number)
					if err != nil {
						return nil, "", errors.New(fmt.Sprintf("could not convert version string into number \"%s\" %s", val_number, Dockerimage))
					}
					dsn.Version[j*2] = val_int
				}

				if len(reg_version_matches) > 2 {
					val_text := reg_version_matches[2]
					logger.Debug(1, fmt.Sprintf("version match 1 (text): %s ", val_text))

					dsn.Version[j*2+1] = version_strings[val_text]

				}
				logger.Debug(1, fmt.Sprintf("version pair: (j*2=%d) %d %d ", j*2, dsn.Version[j*2], dsn.Version[j*2+1]))
			}
			dsn_array = append(dsn_array, dsn)
		}
		if len(dsn_array) == 0 {
			return nil, "", errors.New(fmt.Sprintf("I did not find the \"latest\" docker image %s", Dockerimage))
		}

		logger.Debug(1, fmt.Sprintf("dockerimage count of versions available=%d", len(dsn_array)))

		sort.Sort(sort.Reverse(DockerShockNodeArray(dsn_array)))
		node = &dsn_array[0].ShockNode // TODO check that highest version number is unique !

		logger.Debug(1, fmt.Sprintf("dockerimage latest has been requested and this tag was found: %s", dsn_array[0].Attributes.Tag))

	} else {

		//query url = type=dockerimage&name=wgerlach/bowtie2:2.2.0"

		query_response_p, err := shock_docker_repo.Query(url.Values{"type": {"dockerimage"}, "name": {Dockerimage}})
		if err != nil {
			return nil, "", errors.New(fmt.Sprintf("shock node not found for image=%s, err=%s", Dockerimage, err.Error()))
		}
		logger.Debug(1, fmt.Sprintf("query result: %v", query_response_p))

		datalen := len((*query_response_p).Data)

		if datalen == 0 {
			return nil, "", errors.New(fmt.Sprintf("image %s not found in shocks docker repo", Dockerimage))
		} else if datalen > 1 {
			return nil, "", errors.New(fmt.Sprintf("more than one image %s found in shocks docker repo", Dockerimage))
		}

		node = &(*query_response_p).Data[0]

	}

	logger.Debug(1, fmt.Sprintf("found SHOCK node for docker image: %s", node.Id))

	download_url, err = shock_docker_repo.Get_node_download_url(*node)
	if err != nil {
		return nil, "", errors.New(fmt.Sprintf("Could not create download url, err=%s", err.Error()))
	}

	return
}