From 8566a2c118c6c10179a63bec45c8809b1bf10221 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Wed, 30 Jul 2014 13:56:36 -0700 Subject: [PATCH] datasource: Move datasources into their own packages. --- coreos-cloudinit.go | 21 ++++++++++++------- datasource/{ => configdrive}/configdrive.go | 15 ++++++++----- .../{ => configdrive}/configdrive_test.go | 2 +- datasource/{ => file}/file.go | 4 ++-- datasource/{ => metadata}/metadata_service.go | 16 +++++++------- .../{ => metadata}/metadata_service_test.go | 2 +- datasource/{ => proc_cmdline}/proc_cmdline.go | 4 ++-- .../{ => proc_cmdline}/proc_cmdline_test.go | 4 ++-- datasource/{ => url}/url.go | 4 ++-- pkg/http_client.go | 4 ++++ test | 11 +++++++++- 11 files changed, 54 insertions(+), 33 deletions(-) rename datasource/{ => configdrive}/configdrive.go (80%) rename datasource/{ => configdrive}/configdrive_test.go (99%) rename datasource/{ => file}/file.go (89%) rename datasource/{ => metadata}/metadata_service.go (92%) rename datasource/{ => metadata}/metadata_service_test.go (99%) rename datasource/{ => proc_cmdline}/proc_cmdline.go (96%) rename datasource/{ => proc_cmdline}/proc_cmdline_test.go (97%) rename datasource/{ => url}/url.go (90%) diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index 5c3e062..4c4ba5d 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -8,6 +8,11 @@ import ( "time" "github.com/coreos/coreos-cloudinit/datasource" + "github.com/coreos/coreos-cloudinit/datasource/configdrive" + "github.com/coreos/coreos-cloudinit/datasource/file" + "github.com/coreos/coreos-cloudinit/datasource/metadata" + "github.com/coreos/coreos-cloudinit/datasource/proc_cmdline" + "github.com/coreos/coreos-cloudinit/datasource/url" "github.com/coreos/coreos-cloudinit/initialize" "github.com/coreos/coreos-cloudinit/pkg" "github.com/coreos/coreos-cloudinit/system" @@ -42,7 +47,7 @@ func init() { flag.StringVar(&sources.configDrive, "from-configdrive", "", "Read data from provided cloud-drive directory") flag.BoolVar(&sources.metadataService, "from-metadata-service", false, "Download data from metadata service") flag.StringVar(&sources.url, "from-url", "", "Download user-data from provided url") - flag.BoolVar(&sources.procCmdLine, "from-proc-cmdline", false, fmt.Sprintf("Parse %s for '%s=', using the cloud-config served by an HTTP GET to ", datasource.ProcCmdlineLocation, datasource.ProcCmdlineCloudConfigFlag)) + flag.BoolVar(&sources.procCmdLine, "from-proc-cmdline", false, fmt.Sprintf("Parse %s for '%s=', using the cloud-config served by an HTTP GET to ", proc_cmdline.ProcCmdlineLocation, proc_cmdline.ProcCmdlineCloudConfigFlag)) flag.StringVar(&convertNetconf, "convert-netconf", "", "Read the network config provided in cloud-drive and translate it from the specified format into networkd unit files (requires the -from-configdrive flag)") flag.StringVar(&workspace, "workspace", "/var/lib/coreos-cloudinit", "Base directory coreos-cloudinit should use to store data") flag.StringVar(&sshKeyName, "ssh-key-name", initialize.DefaultSSHKeyName, "Add SSH keys to the system with the given name") @@ -172,7 +177,7 @@ func main() { func mergeCloudConfig(mdcc, udcc initialize.CloudConfig) (cc initialize.CloudConfig) { if mdcc.Hostname != "" { if udcc.Hostname != "" { - fmt.Printf("Warning: user-data hostname (%s) overrides metadata hostname (%s)", udcc.Hostname, mdcc.Hostname) + fmt.Printf("Warning: user-data hostname (%s) overrides metadata hostname (%s)\n", udcc.Hostname, mdcc.Hostname) } else { udcc.Hostname = mdcc.Hostname } @@ -183,7 +188,7 @@ func mergeCloudConfig(mdcc, udcc initialize.CloudConfig) (cc initialize.CloudCon } if mdcc.NetworkConfigPath != "" { if udcc.NetworkConfigPath != "" { - fmt.Printf("Warning: user-data NetworkConfigPath %s overrides metadata NetworkConfigPath %s", udcc.NetworkConfigPath, mdcc.NetworkConfigPath) + fmt.Printf("Warning: user-data NetworkConfigPath %s overrides metadata NetworkConfigPath %s\n", udcc.NetworkConfigPath, mdcc.NetworkConfigPath) } else { udcc.NetworkConfigPath = mdcc.NetworkConfigPath } @@ -196,19 +201,19 @@ func mergeCloudConfig(mdcc, udcc initialize.CloudConfig) (cc initialize.CloudCon func getDatasources() []datasource.Datasource { dss := make([]datasource.Datasource, 0, 5) if sources.file != "" { - dss = append(dss, datasource.NewLocalFile(sources.file)) + dss = append(dss, file.NewDatasource(sources.file)) } if sources.url != "" { - dss = append(dss, datasource.NewRemoteFile(sources.url)) + dss = append(dss, url.NewDatasource(sources.url)) } if sources.configDrive != "" { - dss = append(dss, datasource.NewConfigDrive(sources.configDrive)) + dss = append(dss, configdrive.NewDatasource(sources.configDrive)) } if sources.metadataService { - dss = append(dss, datasource.NewMetadataService()) + dss = append(dss, metadata.NewDatasource()) } if sources.procCmdLine { - dss = append(dss, datasource.NewProcCmdline()) + dss = append(dss, proc_cmdline.NewDatasource()) } return dss } diff --git a/datasource/configdrive.go b/datasource/configdrive/configdrive.go similarity index 80% rename from datasource/configdrive.go rename to datasource/configdrive/configdrive.go index 4841999..870f3be 100644 --- a/datasource/configdrive.go +++ b/datasource/configdrive/configdrive.go @@ -1,4 +1,4 @@ -package datasource +package configdrive import ( "io/ioutil" @@ -6,13 +6,18 @@ import ( "path" ) +const ( + ec2ApiVersion = "2009-04-04" + openstackApiVersion = "latest" +) + type configDrive struct { root string readFile func(filename string) ([]byte, error) } -func NewConfigDrive(root string) *configDrive { - return &configDrive{root, ioutil.ReadFile} +func NewDatasource(root string) *configDrive { + return &configDrive{path.Join(root, "openstack"), ioutil.ReadFile} } func (cd *configDrive) IsAvailable() bool { @@ -48,11 +53,11 @@ func (cd *configDrive) Type() string { } func (cd *configDrive) ec2Root() string { - return path.Join(cd.root, "ec2", Ec2ApiVersion) + return path.Join(cd.root, "ec2", ec2ApiVersion) } func (cd *configDrive) openstackRoot() string { - return path.Join(cd.root, "openstack", "latest") + return path.Join(cd.root, "openstack", openstackApiVersion) } func (cd *configDrive) tryReadFile(filename string) ([]byte, error) { diff --git a/datasource/configdrive_test.go b/datasource/configdrive/configdrive_test.go similarity index 99% rename from datasource/configdrive_test.go rename to datasource/configdrive/configdrive_test.go index a000d4a..26bc46b 100644 --- a/datasource/configdrive_test.go +++ b/datasource/configdrive/configdrive_test.go @@ -1,4 +1,4 @@ -package datasource +package configdrive import ( "os" diff --git a/datasource/file.go b/datasource/file/file.go similarity index 89% rename from datasource/file.go rename to datasource/file/file.go index a542ff4..b629641 100644 --- a/datasource/file.go +++ b/datasource/file/file.go @@ -1,4 +1,4 @@ -package datasource +package file import ( "io/ioutil" @@ -9,7 +9,7 @@ type localFile struct { path string } -func NewLocalFile(path string) *localFile { +func NewDatasource(path string) *localFile { return &localFile{path} } diff --git a/datasource/metadata_service.go b/datasource/metadata/metadata_service.go similarity index 92% rename from datasource/metadata_service.go rename to datasource/metadata/metadata_service.go index 571b1a2..7f08a85 100644 --- a/datasource/metadata_service.go +++ b/datasource/metadata/metadata_service.go @@ -1,4 +1,4 @@ -package datasource +package metadata import ( "bufio" @@ -21,6 +21,8 @@ import ( // [2] http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html#instancedata-data-categories const ( + Ec2ApiVersion = "2009-04-04" + OpenstackApiVersion = "2012-08-10" BaseUrl = "http://169.254.169.254/" Ec2UserdataUrl = BaseUrl + Ec2ApiVersion + "/user-data" Ec2MetadataUrl = BaseUrl + Ec2ApiVersion + "/meta-data" @@ -29,11 +31,7 @@ const ( type metadataService struct{} -type getter interface { - GetRetry(string) ([]byte, error) -} - -func NewMetadataService() *metadataService { +func NewDatasource() *metadataService { return &metadataService{} } @@ -76,7 +74,7 @@ func (ms *metadataService) Type() string { return "metadata-service" } -func fetchMetadata(client getter) ([]byte, error) { +func fetchMetadata(client pkg.Getter) ([]byte, error) { attrs := make(map[string]interface{}) if keynames, err := fetchAttributes(client, fmt.Sprintf("%s/public-keys", Ec2MetadataUrl)); err == nil { keyIDs := make(map[string]string) @@ -131,7 +129,7 @@ func fetchMetadata(client getter) ([]byte, error) { return json.Marshal(attrs) } -func fetchAttributes(client getter, url string) ([]string, error) { +func fetchAttributes(client pkg.Getter, url string) ([]string, error) { resp, err := client.GetRetry(url) if err != nil { return nil, err @@ -144,7 +142,7 @@ func fetchAttributes(client getter, url string) ([]string, error) { return data, scanner.Err() } -func fetchAttribute(client getter, url string) (string, error) { +func fetchAttribute(client pkg.Getter, url string) (string, error) { if attrs, err := fetchAttributes(client, url); err == nil && len(attrs) > 0 { return attrs[0], nil } else { diff --git a/datasource/metadata_service_test.go b/datasource/metadata/metadata_service_test.go similarity index 99% rename from datasource/metadata_service_test.go rename to datasource/metadata/metadata_service_test.go index 5867d42..e1d8f96 100644 --- a/datasource/metadata_service_test.go +++ b/datasource/metadata/metadata_service_test.go @@ -1,4 +1,4 @@ -package datasource +package metadata import ( "bytes" diff --git a/datasource/proc_cmdline.go b/datasource/proc_cmdline/proc_cmdline.go similarity index 96% rename from datasource/proc_cmdline.go rename to datasource/proc_cmdline/proc_cmdline.go index 16918b2..02205c3 100644 --- a/datasource/proc_cmdline.go +++ b/datasource/proc_cmdline/proc_cmdline.go @@ -1,4 +1,4 @@ -package datasource +package proc_cmdline import ( "errors" @@ -18,7 +18,7 @@ type procCmdline struct { Location string } -func NewProcCmdline() *procCmdline { +func NewDatasource() *procCmdline { return &procCmdline{Location: ProcCmdlineLocation} } diff --git a/datasource/proc_cmdline_test.go b/datasource/proc_cmdline/proc_cmdline_test.go similarity index 97% rename from datasource/proc_cmdline_test.go rename to datasource/proc_cmdline/proc_cmdline_test.go index 65115c9..9c3e8b8 100644 --- a/datasource/proc_cmdline_test.go +++ b/datasource/proc_cmdline/proc_cmdline_test.go @@ -1,4 +1,4 @@ -package datasource +package proc_cmdline import ( "fmt" @@ -75,7 +75,7 @@ func TestProcCmdlineAndFetchConfig(t *testing.T) { t.Errorf("Test produced error: %v", err) } - p := NewProcCmdline() + p := NewDatasource() p.Location = file.Name() cfg, err := p.FetchUserdata() if err != nil { diff --git a/datasource/url.go b/datasource/url/url.go similarity index 90% rename from datasource/url.go rename to datasource/url/url.go index 3debca0..ed73ba4 100644 --- a/datasource/url.go +++ b/datasource/url/url.go @@ -1,4 +1,4 @@ -package datasource +package url import "github.com/coreos/coreos-cloudinit/pkg" @@ -6,7 +6,7 @@ type remoteFile struct { url string } -func NewRemoteFile(url string) *remoteFile { +func NewDatasource(url string) *remoteFile { return &remoteFile{url} } diff --git a/pkg/http_client.go b/pkg/http_client.go index d0da7ea..7135798 100644 --- a/pkg/http_client.go +++ b/pkg/http_client.go @@ -57,6 +57,10 @@ type HttpClient struct { client *http.Client } +type Getter interface { + GetRetry(string) ([]byte, error) +} + func NewHttpClient() *HttpClient { hc := &HttpClient{ MaxBackoff: time.Second * 5, diff --git a/test b/test index 53dfc91..77469ce 100755 --- a/test +++ b/test @@ -13,7 +13,16 @@ COVER=${COVER:-"-cover"} source ./build -declare -a TESTPKGS=(initialize system datasource pkg network) +declare -a TESTPKGS=(initialize + system + datasource + datasource/configdrive + datasource/file + datasource/metadata + datasource/proc_cmdline + datasource/url + pkg + network) if [ -z "$PKG" ]; then GOFMTPATH="$TESTPKGS coreos-cloudinit.go"