2014-04-27 18:06:09 -04:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2014-12-11 20:55:15 -05:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/engine"
|
2014-04-27 18:06:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service exposes registry capabilities in the standard Engine
|
|
|
|
// interface. Once installed, it extends the engine with the
|
|
|
|
// following calls:
|
|
|
|
//
|
|
|
|
// 'auth': Authenticate against the public registry
|
2014-04-27 18:21:42 -04:00
|
|
|
// 'search': Search for images on the public registry
|
2014-04-27 18:06:09 -04:00
|
|
|
// 'pull': Download images from any registry (TODO)
|
|
|
|
// 'push': Upload images to any registry (TODO)
|
|
|
|
type Service struct {
|
2014-10-06 21:54:52 -04:00
|
|
|
Config *ServiceConfig
|
2014-04-27 18:06:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns a new instance of Service ready to be
|
|
|
|
// installed no an engine.
|
2014-10-06 21:54:52 -04:00
|
|
|
func NewService(options *Options) *Service {
|
2014-08-20 11:31:24 -04:00
|
|
|
return &Service{
|
2014-10-06 21:54:52 -04:00
|
|
|
Config: NewServiceConfig(options),
|
2014-08-20 11:31:24 -04:00
|
|
|
}
|
2014-04-27 18:06:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Install installs registry capabilities to eng.
|
|
|
|
func (s *Service) Install(eng *engine.Engine) error {
|
|
|
|
eng.Register("auth", s.Auth)
|
2014-04-27 18:21:42 -04:00
|
|
|
eng.Register("search", s.Search)
|
2014-10-06 21:54:52 -04:00
|
|
|
eng.Register("resolve_repository", s.ResolveRepository)
|
|
|
|
eng.Register("resolve_index", s.ResolveIndex)
|
|
|
|
eng.Register("registry_config", s.GetRegistryConfig)
|
2014-04-27 18:06:09 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth contacts the public registry with the provided credentials,
|
|
|
|
// and returns OK if authentication was sucessful.
|
|
|
|
// It can be used to verify the validity of a client's credentials.
|
|
|
|
func (s *Service) Auth(job *engine.Job) engine.Status {
|
2014-12-11 20:55:15 -05:00
|
|
|
var (
|
|
|
|
authConfig = new(AuthConfig)
|
|
|
|
endpoint *Endpoint
|
|
|
|
index *IndexInfo
|
|
|
|
status string
|
|
|
|
err error
|
|
|
|
)
|
2014-04-27 18:06:09 -04:00
|
|
|
|
|
|
|
job.GetenvJson("authConfig", authConfig)
|
2014-08-20 11:31:24 -04:00
|
|
|
|
2014-12-11 20:55:15 -05:00
|
|
|
addr := authConfig.ServerAddress
|
|
|
|
if addr == "" {
|
|
|
|
// Use the official registry address if not specified.
|
|
|
|
addr = IndexServerAddress()
|
2014-04-27 18:06:09 -04:00
|
|
|
}
|
2014-08-20 11:31:24 -04:00
|
|
|
|
2014-12-11 20:55:15 -05:00
|
|
|
if index, err = ResolveIndexInfo(job, addr); err != nil {
|
2014-04-27 18:06:09 -04:00
|
|
|
return job.Error(err)
|
|
|
|
}
|
2014-12-11 20:55:15 -05:00
|
|
|
|
|
|
|
if endpoint, err = NewEndpoint(index); err != nil {
|
|
|
|
log.Errorf("unable to get new registry endpoint: %s", err)
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
authConfig.ServerAddress = endpoint.String()
|
|
|
|
|
|
|
|
if status, err = Login(authConfig, endpoint, HTTPRequestFactory(nil)); err != nil {
|
|
|
|
log.Errorf("unable to login against registry endpoint %s: %s", endpoint, err)
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("successful registry login for endpoint %s: %s", endpoint, status)
|
2014-10-28 21:20:30 -04:00
|
|
|
job.Printf("%s\n", status)
|
2014-08-20 11:31:24 -04:00
|
|
|
|
2014-04-27 18:06:09 -04:00
|
|
|
return engine.StatusOK
|
|
|
|
}
|
2014-04-27 18:21:42 -04:00
|
|
|
|
|
|
|
// Search queries the public registry for images matching the specified
|
|
|
|
// search terms, and returns the results.
|
|
|
|
//
|
|
|
|
// Argument syntax: search TERM
|
|
|
|
//
|
|
|
|
// Option environment:
|
|
|
|
// 'authConfig': json-encoded credentials to authenticate against the registry.
|
|
|
|
// The search extends to images only accessible via the credentials.
|
|
|
|
//
|
|
|
|
// 'metaHeaders': extra HTTP headers to include in the request to the registry.
|
|
|
|
// The headers should be passed as a json-encoded dictionary.
|
|
|
|
//
|
|
|
|
// Output:
|
|
|
|
// Results are sent as a collection of structured messages (using engine.Table).
|
|
|
|
// Each result is sent as a separate message.
|
|
|
|
// Results are ordered by number of stars on the public registry.
|
|
|
|
func (s *Service) Search(job *engine.Job) engine.Status {
|
|
|
|
if n := len(job.Args); n != 1 {
|
|
|
|
return job.Errorf("Usage: %s TERM", job.Name)
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
term = job.Args[0]
|
|
|
|
metaHeaders = map[string][]string{}
|
|
|
|
authConfig = &AuthConfig{}
|
|
|
|
)
|
|
|
|
job.GetenvJson("authConfig", authConfig)
|
|
|
|
job.GetenvJson("metaHeaders", metaHeaders)
|
|
|
|
|
2014-10-06 21:54:52 -04:00
|
|
|
repoInfo, err := ResolveRepositoryInfo(job, term)
|
2014-08-09 03:16:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
// *TODO: Search multiple indexes.
|
|
|
|
endpoint, err := repoInfo.GetEndpoint()
|
2014-08-09 03:16:54 -04:00
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
2014-08-26 19:21:04 -04:00
|
|
|
r, err := NewSession(authConfig, HTTPRequestFactory(metaHeaders), endpoint, true)
|
2014-04-27 18:21:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
results, err := r.SearchRepositories(repoInfo.GetSearchTerm())
|
2014-04-27 18:21:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
outs := engine.NewTable("star_count", 0)
|
|
|
|
for _, result := range results.Results {
|
|
|
|
out := &engine.Env{}
|
|
|
|
out.Import(result)
|
|
|
|
outs.Add(out)
|
|
|
|
}
|
|
|
|
outs.ReverseSort()
|
|
|
|
if _, err := outs.WriteListTo(job.Stdout); err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
|
|
|
|
// ResolveRepository splits a repository name into its components
|
|
|
|
// and configuration of the associated registry.
|
|
|
|
func (s *Service) ResolveRepository(job *engine.Job) engine.Status {
|
|
|
|
var (
|
|
|
|
reposName = job.Args[0]
|
|
|
|
)
|
|
|
|
|
2015-01-07 18:42:01 -05:00
|
|
|
repoInfo, err := s.Config.NewRepositoryInfo(reposName)
|
2014-10-06 21:54:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := engine.Env{}
|
|
|
|
err = out.SetJson("repository", repoInfo)
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
out.WriteTo(job.Stdout)
|
|
|
|
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience wrapper for calling resolve_repository Job from a running job.
|
|
|
|
func ResolveRepositoryInfo(jobContext *engine.Job, reposName string) (*RepositoryInfo, error) {
|
|
|
|
job := jobContext.Eng.Job("resolve_repository", reposName)
|
|
|
|
env, err := job.Stdout.AddEnv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info := RepositoryInfo{}
|
|
|
|
if err := env.GetJson("repository", &info); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIndex takes indexName and returns index info
|
|
|
|
func (s *Service) ResolveIndex(job *engine.Job) engine.Status {
|
|
|
|
var (
|
|
|
|
indexName = job.Args[0]
|
|
|
|
)
|
|
|
|
|
2015-01-07 18:42:01 -05:00
|
|
|
index, err := s.Config.NewIndexInfo(indexName)
|
2014-10-06 21:54:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := engine.Env{}
|
|
|
|
err = out.SetJson("index", index)
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
out.WriteTo(job.Stdout)
|
|
|
|
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience wrapper for calling resolve_index Job from a running job.
|
|
|
|
func ResolveIndexInfo(jobContext *engine.Job, indexName string) (*IndexInfo, error) {
|
|
|
|
job := jobContext.Eng.Job("resolve_index", indexName)
|
|
|
|
env, err := job.Stdout.AddEnv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info := IndexInfo{}
|
|
|
|
if err := env.GetJson("index", &info); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRegistryConfig returns current registry configuration.
|
|
|
|
func (s *Service) GetRegistryConfig(job *engine.Job) engine.Status {
|
|
|
|
out := engine.Env{}
|
|
|
|
err := out.SetJson("config", s.Config)
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
out.WriteTo(job.Stdout)
|
|
|
|
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|