From dd8eb6a8df76d82b5dc22f378640c33421ea0691 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Mon, 1 Dec 2014 17:32:31 -0800 Subject: [PATCH 1/2] configuration.Parse should take io.Reader --- configuration/configuration.go | 20 +++++++++++++++++--- configuration/configuration_test.go | 24 ++++++++++++------------ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/configuration/configuration.go b/configuration/configuration.go index 85d74d95..2d7e476b 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -2,6 +2,8 @@ package configuration import ( "fmt" + "io" + "io/ioutil" "os" "regexp" "strconv" @@ -21,6 +23,13 @@ type Configuration struct { // Storage is the configuration for the registry's storage driver Storage Storage `yaml:"storage"` + + // HTTP contains configuration parameters for the registry's http + // interface. + HTTP struct { + // Addr specifies the bind address for the registry instance. + Addr string `yaml:"addr"` + } `yaml:"http"` } // v0_1Configuration is a Version 0.1 Configuration struct @@ -178,16 +187,21 @@ type Parameters map[string]string // following the scheme below: // Configuration.Abc may be replaced by the value of REGISTRY_ABC, // Configuration.Abc.Xyz may be replaced by the value of REGISTRY_ABC_XYZ, and so forth -func Parse(in []byte) (*Configuration, error) { +func Parse(rd io.Reader) (*Configuration, error) { + in, err := ioutil.ReadAll(rd) + if err != nil { + return nil, err + } + var untypedConfig struct { Version Version } var config *Configuration - err := yaml.Unmarshal(in, &untypedConfig) - if err != nil { + if err := yaml.Unmarshal(in, &untypedConfig); err != nil { return nil, err } + if untypedConfig.Version == "" { return nil, fmt.Errorf("Please specify a configuration version. Current version is %s", CurrentVersion) } diff --git a/configuration/configuration_test.go b/configuration/configuration_test.go index 31d15b7a..0e227653 100644 --- a/configuration/configuration_test.go +++ b/configuration/configuration_test.go @@ -1,12 +1,12 @@ package configuration import ( + "bytes" "os" "testing" - "gopkg.in/yaml.v2" - . "gopkg.in/check.v1" + "gopkg.in/yaml.v2" ) // Hook up gocheck into the "go test" runner @@ -72,14 +72,14 @@ func (suite *ConfigSuite) SetUpTest(c *C) { func (suite *ConfigSuite) TestMarshalRoundtrip(c *C) { configBytes, err := yaml.Marshal(suite.expectedConfig) c.Assert(err, IsNil) - config, err := Parse(configBytes) + config, err := Parse(bytes.NewReader(configBytes)) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } // TestParseSimple validates that configYamlV0_1 can be parsed into a struct matching configStruct func (suite *ConfigSuite) TestParseSimple(c *C) { - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -89,7 +89,7 @@ func (suite *ConfigSuite) TestParseSimple(c *C) { func (suite *ConfigSuite) TestParseInmemory(c *C) { suite.expectedConfig.Storage = Storage{"inmemory": Parameters{}} - config, err := Parse([]byte(inmemoryConfigYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(inmemoryConfigYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -100,7 +100,7 @@ func (suite *ConfigSuite) TestParseWithSameEnvStorage(c *C) { os.Setenv("REGISTRY_STORAGE", "s3") os.Setenv("REGISTRY_STORAGE_S3_REGION", "us-east-1") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -117,7 +117,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvStorageParams(c *C) { os.Setenv("REGISTRY_STORAGE_S3_SECURE", "true") os.Setenv("REGISTRY_STORAGE_S3_NEWPARAM", "some Value") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -129,7 +129,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvStorageType(c *C) { os.Setenv("REGISTRY_STORAGE", "inmemory") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -144,7 +144,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvStorageTypeAndParams(c *C) { os.Setenv("REGISTRY_STORAGE", "filesystem") os.Setenv("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY", "/tmp/testroot") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -154,7 +154,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvStorageTypeAndParams(c *C) { func (suite *ConfigSuite) TestParseWithSameEnvLoglevel(c *C) { os.Setenv("REGISTRY_LOGLEVEL", "info") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -166,7 +166,7 @@ func (suite *ConfigSuite) TestParseWithDifferentEnvLoglevel(c *C) { os.Setenv("REGISTRY_LOGLEVEL", "error") - config, err := Parse([]byte(configYamlV0_1)) + config, err := Parse(bytes.NewReader([]byte(configYamlV0_1))) c.Assert(err, IsNil) c.Assert(config, DeepEquals, suite.expectedConfig) } @@ -177,7 +177,7 @@ func (suite *ConfigSuite) TestParseInvalidVersion(c *C) { suite.expectedConfig.Version = MajorMinorVersion(CurrentVersion.Major(), CurrentVersion.Minor()+1) configBytes, err := yaml.Marshal(suite.expectedConfig) c.Assert(err, IsNil) - _, err = Parse(configBytes) + _, err = Parse(bytes.NewReader(configBytes)) c.Assert(err, NotNil) } From 33016268302360f4fedf7753b830365c6ded36c4 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Mon, 1 Dec 2014 17:36:20 -0800 Subject: [PATCH 2/2] Add registry main cmd --- cmd/registry/config.yml | 5 +++ cmd/registry/main.go | 85 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 cmd/registry/config.yml create mode 100644 cmd/registry/main.go diff --git a/cmd/registry/config.yml b/cmd/registry/config.yml new file mode 100644 index 00000000..2f9ad0a2 --- /dev/null +++ b/cmd/registry/config.yml @@ -0,0 +1,5 @@ +version: 0.1 +loglevel: debug +storage: inmemory +http: + addr: localhost:5000 \ No newline at end of file diff --git a/cmd/registry/main.go b/cmd/registry/main.go new file mode 100644 index 00000000..150c7d6b --- /dev/null +++ b/cmd/registry/main.go @@ -0,0 +1,85 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + _ "net/http/pprof" + "os" + + "github.com/gorilla/handlers" + + log "github.com/Sirupsen/logrus" + + "github.com/docker/docker-registry" + "github.com/docker/docker-registry/configuration" + _ "github.com/docker/docker-registry/storagedriver/filesystem" + _ "github.com/docker/docker-registry/storagedriver/inmemory" + _ "github.com/docker/docker-registry/storagedriver/s3" +) + +func main() { + flag.Usage = usage + flag.Parse() + + config, err := resolveConfiguration() + if err != nil { + fatalf("configuration error: %v", err) + } + + app := registry.NewApp(*config) + handler := handlers.CombinedLoggingHandler(os.Stdout, app) + log.SetLevel(logLevel(config.Loglevel)) + + log.Infof("listening on %v", config.HTTP.Addr) + if err := http.ListenAndServe(config.HTTP.Addr, handler); err != nil { + log.Fatalln(err) + } +} + +func usage() { + fmt.Fprintln(os.Stderr, "usage:", os.Args[0], "") + flag.PrintDefaults() +} + +func fatalf(format string, args ...interface{}) { + fmt.Fprintf(os.Stderr, format+"\n", args...) + usage() + os.Exit(1) +} + +func resolveConfiguration() (*configuration.Configuration, error) { + var configurationPath string + + if flag.NArg() > 0 { + configurationPath = flag.Arg(0) + } else if os.Getenv("REGISTRY_CONFIGURATION_PATH") != "" { + configurationPath = os.Getenv("REGISTRY_CONFIGURATION_PATH") + } + + if configurationPath == "" { + return nil, fmt.Errorf("configuration path unspecified") + } + + fp, err := os.Open(configurationPath) + if err != nil { + return nil, err + } + + config, err := configuration.Parse(fp) + if err != nil { + return nil, fmt.Errorf("error parsing %s: %v", configurationPath, err) + } + + return config, nil +} + +func logLevel(level configuration.Loglevel) log.Level { + l, err := log.ParseLevel(string(level)) + if err != nil { + log.Warnf("error parsing level %q: %v", level, err) + l = log.InfoLevel + } + + return l +}