datasource: Move datasources into their own packages.
This commit is contained in:
parent
49ac083af5
commit
8566a2c118
@ -8,6 +8,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/coreos-cloudinit/datasource"
|
"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/initialize"
|
||||||
"github.com/coreos/coreos-cloudinit/pkg"
|
"github.com/coreos/coreos-cloudinit/pkg"
|
||||||
"github.com/coreos/coreos-cloudinit/system"
|
"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.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.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.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=<url>', using the cloud-config served by an HTTP GET to <url>", datasource.ProcCmdlineLocation, datasource.ProcCmdlineCloudConfigFlag))
|
flag.BoolVar(&sources.procCmdLine, "from-proc-cmdline", false, fmt.Sprintf("Parse %s for '%s=<url>', using the cloud-config served by an HTTP GET to <url>", 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(&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(&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")
|
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) {
|
func mergeCloudConfig(mdcc, udcc initialize.CloudConfig) (cc initialize.CloudConfig) {
|
||||||
if mdcc.Hostname != "" {
|
if mdcc.Hostname != "" {
|
||||||
if udcc.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 {
|
} else {
|
||||||
udcc.Hostname = mdcc.Hostname
|
udcc.Hostname = mdcc.Hostname
|
||||||
}
|
}
|
||||||
@ -183,7 +188,7 @@ func mergeCloudConfig(mdcc, udcc initialize.CloudConfig) (cc initialize.CloudCon
|
|||||||
}
|
}
|
||||||
if mdcc.NetworkConfigPath != "" {
|
if mdcc.NetworkConfigPath != "" {
|
||||||
if udcc.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 {
|
} else {
|
||||||
udcc.NetworkConfigPath = mdcc.NetworkConfigPath
|
udcc.NetworkConfigPath = mdcc.NetworkConfigPath
|
||||||
}
|
}
|
||||||
@ -196,19 +201,19 @@ func mergeCloudConfig(mdcc, udcc initialize.CloudConfig) (cc initialize.CloudCon
|
|||||||
func getDatasources() []datasource.Datasource {
|
func getDatasources() []datasource.Datasource {
|
||||||
dss := make([]datasource.Datasource, 0, 5)
|
dss := make([]datasource.Datasource, 0, 5)
|
||||||
if sources.file != "" {
|
if sources.file != "" {
|
||||||
dss = append(dss, datasource.NewLocalFile(sources.file))
|
dss = append(dss, file.NewDatasource(sources.file))
|
||||||
}
|
}
|
||||||
if sources.url != "" {
|
if sources.url != "" {
|
||||||
dss = append(dss, datasource.NewRemoteFile(sources.url))
|
dss = append(dss, url.NewDatasource(sources.url))
|
||||||
}
|
}
|
||||||
if sources.configDrive != "" {
|
if sources.configDrive != "" {
|
||||||
dss = append(dss, datasource.NewConfigDrive(sources.configDrive))
|
dss = append(dss, configdrive.NewDatasource(sources.configDrive))
|
||||||
}
|
}
|
||||||
if sources.metadataService {
|
if sources.metadataService {
|
||||||
dss = append(dss, datasource.NewMetadataService())
|
dss = append(dss, metadata.NewDatasource())
|
||||||
}
|
}
|
||||||
if sources.procCmdLine {
|
if sources.procCmdLine {
|
||||||
dss = append(dss, datasource.NewProcCmdline())
|
dss = append(dss, proc_cmdline.NewDatasource())
|
||||||
}
|
}
|
||||||
return dss
|
return dss
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package datasource
|
package configdrive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -6,13 +6,18 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ec2ApiVersion = "2009-04-04"
|
||||||
|
openstackApiVersion = "latest"
|
||||||
|
)
|
||||||
|
|
||||||
type configDrive struct {
|
type configDrive struct {
|
||||||
root string
|
root string
|
||||||
readFile func(filename string) ([]byte, error)
|
readFile func(filename string) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfigDrive(root string) *configDrive {
|
func NewDatasource(root string) *configDrive {
|
||||||
return &configDrive{root, ioutil.ReadFile}
|
return &configDrive{path.Join(root, "openstack"), ioutil.ReadFile}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cd *configDrive) IsAvailable() bool {
|
func (cd *configDrive) IsAvailable() bool {
|
||||||
@ -48,11 +53,11 @@ func (cd *configDrive) Type() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cd *configDrive) ec2Root() 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 {
|
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) {
|
func (cd *configDrive) tryReadFile(filename string) ([]byte, error) {
|
@ -1,4 +1,4 @@
|
|||||||
package datasource
|
package configdrive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
@ -1,4 +1,4 @@
|
|||||||
package datasource
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -9,7 +9,7 @@ type localFile struct {
|
|||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalFile(path string) *localFile {
|
func NewDatasource(path string) *localFile {
|
||||||
return &localFile{path}
|
return &localFile{path}
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package datasource
|
package metadata
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@ -21,6 +21,8 @@ import (
|
|||||||
// [2] http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html#instancedata-data-categories
|
// [2] http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html#instancedata-data-categories
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
Ec2ApiVersion = "2009-04-04"
|
||||||
|
OpenstackApiVersion = "2012-08-10"
|
||||||
BaseUrl = "http://169.254.169.254/"
|
BaseUrl = "http://169.254.169.254/"
|
||||||
Ec2UserdataUrl = BaseUrl + Ec2ApiVersion + "/user-data"
|
Ec2UserdataUrl = BaseUrl + Ec2ApiVersion + "/user-data"
|
||||||
Ec2MetadataUrl = BaseUrl + Ec2ApiVersion + "/meta-data"
|
Ec2MetadataUrl = BaseUrl + Ec2ApiVersion + "/meta-data"
|
||||||
@ -29,11 +31,7 @@ const (
|
|||||||
|
|
||||||
type metadataService struct{}
|
type metadataService struct{}
|
||||||
|
|
||||||
type getter interface {
|
func NewDatasource() *metadataService {
|
||||||
GetRetry(string) ([]byte, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMetadataService() *metadataService {
|
|
||||||
return &metadataService{}
|
return &metadataService{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +74,7 @@ func (ms *metadataService) Type() string {
|
|||||||
return "metadata-service"
|
return "metadata-service"
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchMetadata(client getter) ([]byte, error) {
|
func fetchMetadata(client pkg.Getter) ([]byte, error) {
|
||||||
attrs := make(map[string]interface{})
|
attrs := make(map[string]interface{})
|
||||||
if keynames, err := fetchAttributes(client, fmt.Sprintf("%s/public-keys", Ec2MetadataUrl)); err == nil {
|
if keynames, err := fetchAttributes(client, fmt.Sprintf("%s/public-keys", Ec2MetadataUrl)); err == nil {
|
||||||
keyIDs := make(map[string]string)
|
keyIDs := make(map[string]string)
|
||||||
@ -131,7 +129,7 @@ func fetchMetadata(client getter) ([]byte, error) {
|
|||||||
return json.Marshal(attrs)
|
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)
|
resp, err := client.GetRetry(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -144,7 +142,7 @@ func fetchAttributes(client getter, url string) ([]string, error) {
|
|||||||
return data, scanner.Err()
|
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 {
|
if attrs, err := fetchAttributes(client, url); err == nil && len(attrs) > 0 {
|
||||||
return attrs[0], nil
|
return attrs[0], nil
|
||||||
} else {
|
} else {
|
@ -1,4 +1,4 @@
|
|||||||
package datasource
|
package metadata
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
@ -1,4 +1,4 @@
|
|||||||
package datasource
|
package proc_cmdline
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -18,7 +18,7 @@ type procCmdline struct {
|
|||||||
Location string
|
Location string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProcCmdline() *procCmdline {
|
func NewDatasource() *procCmdline {
|
||||||
return &procCmdline{Location: ProcCmdlineLocation}
|
return &procCmdline{Location: ProcCmdlineLocation}
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package datasource
|
package proc_cmdline
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -75,7 +75,7 @@ func TestProcCmdlineAndFetchConfig(t *testing.T) {
|
|||||||
t.Errorf("Test produced error: %v", err)
|
t.Errorf("Test produced error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
p := NewProcCmdline()
|
p := NewDatasource()
|
||||||
p.Location = file.Name()
|
p.Location = file.Name()
|
||||||
cfg, err := p.FetchUserdata()
|
cfg, err := p.FetchUserdata()
|
||||||
if err != nil {
|
if err != nil {
|
@ -1,4 +1,4 @@
|
|||||||
package datasource
|
package url
|
||||||
|
|
||||||
import "github.com/coreos/coreos-cloudinit/pkg"
|
import "github.com/coreos/coreos-cloudinit/pkg"
|
||||||
|
|
||||||
@ -6,7 +6,7 @@ type remoteFile struct {
|
|||||||
url string
|
url string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRemoteFile(url string) *remoteFile {
|
func NewDatasource(url string) *remoteFile {
|
||||||
return &remoteFile{url}
|
return &remoteFile{url}
|
||||||
}
|
}
|
||||||
|
|
@ -57,6 +57,10 @@ type HttpClient struct {
|
|||||||
client *http.Client
|
client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Getter interface {
|
||||||
|
GetRetry(string) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
func NewHttpClient() *HttpClient {
|
func NewHttpClient() *HttpClient {
|
||||||
hc := &HttpClient{
|
hc := &HttpClient{
|
||||||
MaxBackoff: time.Second * 5,
|
MaxBackoff: time.Second * 5,
|
||||||
|
11
test
11
test
@ -13,7 +13,16 @@ COVER=${COVER:-"-cover"}
|
|||||||
|
|
||||||
source ./build
|
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
|
if [ -z "$PKG" ]; then
|
||||||
GOFMTPATH="$TESTPKGS coreos-cloudinit.go"
|
GOFMTPATH="$TESTPKGS coreos-cloudinit.go"
|
||||||
|
Loading…
Reference in New Issue
Block a user