Example #1
0
func main() {
	flag.Parse()
	if *image == "" {
		die("image name is empty or missing")
	}
	u, err := url.Parse(*baseURL)
	if err != nil {
		die("unable to parse baseURL:", err)
	}
	if *basicUser == "" || *basicPass == "" {
		*basicUser, *basicPass = userpass(u.Host)
	}
	c := registry.NewClient()
	c.BaseURL = u
	basic := registry.BasicAuth{
		Username: *basicUser,
		Password: *basicPass,
	}
	token, err := c.Hub.GetReadTokenWithAuth(*image, basic)
	if err != nil {
		die("failed to obtain read token:", err)
	}
	tags, err := c.Repository.ListTags(*image, token)
	if err != nil {
		die("failed to obtain tag list:", err)
	}
	p, err := json.MarshalIndent(NewTags(tags), "", "\t")
	if err != nil {
		die("failed to JSON encode tags:", err)
	}
	fmt.Printf("%s\n", p)
}
Example #2
0
func NewRemoteRegistry() *registryApi {
	conn := &remoteConnection{
		client:     registry.NewClient(),
		layerCache: cache.New(cacheDuration, cacheCleanupInterval),
		tagCache:   cache.New(cacheDuration, cacheCleanupInterval),
	}
	reg := newRegistryApi(conn)

	return reg
}
Example #3
0
func ExampleRepositoryService_Delete_dockerHub() {
	c := registry.NewClient()
	basicAuth := registry.BasicAuth{"user", "pass"}
	tokenAuth, err := c.Hub.GetDeleteToken("foo/bar", basicAuth)

	err = c.Repository.Delete("foo/bar", tokenAuth)
	if err != nil {
		panic(err)
	}
}
Example #4
0
func ExampleRepositoryService_Delete_privateRegistry() {
	c := registry.NewClient()
	c.BaseURL, _ = url.Parse("http://my.registry:8080/v1/")
	auth := registry.BasicAuth{"user", "pass"}

	err := c.Repository.Delete("foo/bar", auth)
	if err != nil {
		panic(err)
	}
}
Example #5
0
func ExampleRepositoryService_SetTag_dockerHub() {
	c := registry.NewClient()
	basicAuth := registry.BasicAuth{"user", "pass"}
	tokenAuth, err := c.Hub.GetWriteToken("foo/bar", basicAuth)

	imageID := "2427658c75a1e3d0af0e7272317a8abfaee4c15729b6840e3c2fca342fe47bf1"
	err = c.Repository.SetTag("foo/bar", imageID, "1.0", tokenAuth)
	if err != nil {
		panic(err)
	}
}
Example #6
0
func ExampleRepositoryService_SetTag_privateRegistry() {
	c := registry.NewClient()
	c.BaseURL, _ = url.Parse("http://my.registry:8080/v1/")
	auth := registry.BasicAuth{"user", "pass"}

	imageID := "2427658c75a1e3d0af0e7272317a8abfaee4c15729b6840e3c2fca342fe47bf1"
	err := c.Repository.SetTag("foo/bar", imageID, "1.0", auth)
	if err != nil {
		panic(err)
	}
}
Example #7
0
func ExampleRepositoryService_GetImageID_dockerHub() {
	c := registry.NewClient()
	auth, err := c.Hub.GetReadToken("ubuntu")

	imageID, err := c.Repository.GetImageID("ubuntu", "latest", auth)
	if err != nil {
		panic(err)
	}

	fmt.Println(imageID)
}
Example #8
0
func ExampleRepositoryService_GetImageID_privateRegistry() {
	c := registry.NewClient()
	c.BaseURL, _ = url.Parse("http://my.registry:8080/v1/")
	auth := registry.BasicAuth{"user", "pass"}

	imageID, err := c.Repository.GetImageID("ubuntu", "latest", auth)
	if err != nil {
		panic(err)
	}

	fmt.Println(imageID)
}
Example #9
0
func ExampleSearchService_Query_dockerHub() {
	c := registry.NewClient()

	results, err := c.Search.Query("mysql", 1, 25)
	if err != nil {
		panic(err)
	}

	for _, result := range results.Results {
		fmt.Println(result.Name)
	}
}
Example #10
0
func ExampleImageService_GetMetadata_dockerHub() {
	c := registry.NewClient()
	auth, err := c.Hub.GetReadToken("ubuntu")

	imageID := "2427658c75a1e3d0af0e7272317a8abfaee4c15729b6840e3c2fca342fe47bf1"
	meta, err := c.Image.GetMetadata(imageID, auth)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v", meta)
}
Example #11
0
func ExampleSearchService_Query_privateRegistry() {
	c := registry.NewClient()
	c.BaseURL, _ = url.Parse("http://my.registry:8080/v1/")

	results, err := c.Search.Query("mysql", 1, 25)
	if err != nil {
		panic(err)
	}

	for _, result := range results.Results {
		fmt.Println(result.Name)
	}
}
Example #12
0
func ExampleImageService_GetMetadata_privateRegistry() {
	c := registry.NewClient()
	c.BaseURL, _ = url.Parse("http://my.registry:8080/v1/")
	auth := registry.BasicAuth{"user", "pass"}

	imageID := "2427658c75a1e3d0af0e7272317a8abfaee4c15729b6840e3c2fca342fe47bf1"
	meta, err := c.Image.GetMetadata(imageID, auth)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v", meta)
}
Example #13
0
func ExampleImageService_GetAncestry_dockerHub() {
	c := registry.NewClient()
	auth, err := c.Hub.GetReadToken("ubuntu")

	images, err := c.Image.GetAncestry("ubuntu", auth)
	if err != nil {
		panic(err)
	}

	for _, imageID := range images {
		fmt.Println(imageID)
	}
}
Example #14
0
func ExampleRepositoryService_ListTags_dockerHub() {
	c := registry.NewClient()
	auth, err := c.Hub.GetReadToken("ubuntu")

	tags, err := c.Repository.ListTags("ubuntu", auth)
	if err != nil {
		panic(err)
	}

	for tag, imageID := range tags {
		fmt.Printf("%s = %s", tag, imageID)
	}
}
Example #15
0
func ExampleImageService_GetLayer_dockerHub() {
	c := registry.NewClient()
	auth, err := c.Hub.GetReadToken("ubuntu")

	buffer := &bytes.Buffer{}
	imageID := "2427658c75a1e3d0af0e7272317a8abfaee4c15729b6840e3c2fca342fe47bf1"
	err = c.Image.GetLayer(imageID, buffer, auth)
	if err != nil {
		panic(err)
	}

	fmt.Println(buffer)
}
Example #16
0
func ExampleImageService_GetAncestry_privateRegistry() {
	c := registry.NewClient()
	c.BaseURL, _ = url.Parse("http://my.registry:8080/v1/")
	auth := registry.BasicAuth{"user", "pass"}

	images, err := c.Image.GetAncestry("ubuntu", auth)
	if err != nil {
		panic(err)
	}

	for _, imageID := range images {
		fmt.Println(imageID)
	}
}
Example #17
0
func ExampleImageService_GetLayer_privateRegistry() {
	c := registry.NewClient()
	c.BaseURL, _ = url.Parse("http://my.registry:8080/v1/")
	auth := registry.BasicAuth{"user", "pass"}

	buffer := &bytes.Buffer{}
	imageID := "2427658c75a1e3d0af0e7272317a8abfaee4c15729b6840e3c2fca342fe47bf1"
	err := c.Image.GetLayer(imageID, buffer, auth)
	if err != nil {
		panic(err)
	}

	fmt.Println(buffer)
}
Example #18
0
func ExampleRepositoryService_ListTags_privateRegistry() {
	c := registry.NewClient()
	c.BaseURL, _ = url.Parse("http://my.registry:8080/v1/")
	auth := registry.BasicAuth{"user", "pass"}

	tags, err := c.Repository.ListTags("ubuntu", auth)
	if err != nil {
		panic(err)
	}

	for tag, imageID := range tags {
		fmt.Printf("%s = %s", tag, imageID)
	}
}
Example #19
0
// CheckAccess checks whether a user can read or write an image
// TODO(termie): this really uses the docker registry code rather than the
//               client so, maybe this is the wrong place
func (c *DockerClient) CheckAccess(opts CheckAccessOptions) (bool, error) {
	logger := util.RootLogger().WithField("Logger", "Docker")
	logger.Debug("Checking access for ", opts.Repository)

	// Do the steps described here: https://gist.github.com/termie/bc0334b086697a162f67
	name := normalizeRepo(opts.Repository)
	logger.Debug("Normalized repo ", name)

	auth := registry.BasicAuth{
		Username: opts.Auth.Username,
		Password: opts.Auth.Password,
	}
	client := registry.NewClient()

	reg := normalizeRegistry(opts.Registry)
	logger.Debug("Normalized Registry ", reg)

	client.BaseURL, _ = url.Parse(reg)

	if opts.Access == "write" {
		if _, err := client.Hub.GetWriteToken(name, auth); err != nil {
			if err.Error() == "Server returned status 401" || err.Error() == "Server returned status 403" {
				return false, nil
			}
			return false, err
		}
	} else if opts.Access == "read" {
		if opts.Auth.Username != "" {
			if _, err := client.Hub.GetReadTokenWithAuth(name, auth); err != nil {
				if err.Error() == "Server returned status 401" || err.Error() == "Server returned status 403" {
					return false, nil
				}
				return false, err
			}
		} else {
			if _, err := client.Hub.GetReadToken(name); err != nil {
				if err.Error() == "Server returned status 401" || err.Error() == "Server returned status 403" {
					return false, nil
				}
				return false, err
			}
		}
	} else {
		return false, fmt.Errorf("Invalid access type requested: %s", opts.Access)
	}
	return true, nil
}
Example #20
0
func NewStorage(cli *docker.Client, repo string) (res *Storage, err error) {
	res = &Storage{
		cli:  cli,
		reg:  registry.NewClient(),
		auth: registry.NilAuth{},
		full: repo,
	}

	// Split repo
	split := strings.Split(repo, "/")
	res.index = split[0]
	if strings.Contains(res.index, ".") || strings.Contains(res.index, ":") {
		// registry present
		res.chunk = strings.Join(split[1:], "/")
	} else {
		// hub
		res.index = ""
		res.chunk = repo
	}
	res.reg.BaseURL, err = url.Parse(registryUrl(res.index))
	return
}
Example #21
0
func main() {
	repo := "mysql"
	tag := "latest"

	// Check for image name as command line argument
	if len(os.Args) > 1 {
		// Separate repo and tag
		parts := strings.Split(os.Args[1], ":")
		repo = parts[0]

		if len(parts) > 1 {
			tag = parts[1]
		}
	}

	c := registry.NewClient()

	// First step is to retrieve a read-only token from the Hub
	auth, err := c.Hub.GetReadToken(repo)
	if err != nil {
		panic(err)
	}

	// Get image ID for tag
	imageID, err := c.Repository.GetImageID(repo, tag, auth)
	if err != nil {
		panic(err)
	}

	// Retrieve all ancestors of given layer ID
	layers, err := c.Image.GetAncestry(imageID, auth)
	if err != nil {
		panic(err)
	}

	var commands []string

	// Iterate over all ancestors
	for _, layerID := range layers {
		// Retrieve metadata for given layer ID
		m, err := c.Image.GetMetadata(layerID, auth)
		if err != nil {
			panic(err)
		}

		// Format the command string
		cmdParts := m.ContainerConfig.Cmd
		if len(cmdParts) == 3 {
			cmd := cmdParts[2]

			if strings.HasPrefix(cmd, NOP_PREFIX) {
				commands = append(commands, strings.Split(cmd, NOP_PREFIX)[1])
			} else {
				commands = append(commands, fmt.Sprintf("RUN %s", cmd))
			}
		}
	}

	// Iterate over commands in reverse order and print them
	for i := len(commands) - 1; i >= 0; i-- {
		fmt.Println(squeeze(commands[i]))
	}
}