From c5fb496a5dce106c81d868a34a3ff2d30bcab4ba Mon Sep 17 00:00:00 2001 From: Ivan Mikushin Date: Thu, 2 Jun 2016 18:25:59 -0700 Subject: [PATCH] use gce metadata service to get user-data and other metadata --- cmd/cloudinit/cloudinit.go | 8 +-- cmd/cloudinit/gce.go | 140 ------------------------------------- 2 files changed, 2 insertions(+), 146 deletions(-) delete mode 100644 cmd/cloudinit/gce.go diff --git a/cmd/cloudinit/cloudinit.go b/cmd/cloudinit/cloudinit.go index 12f92476..e1606e62 100644 --- a/cmd/cloudinit/cloudinit.go +++ b/cmd/cloudinit/cloudinit.go @@ -32,6 +32,7 @@ import ( "github.com/coreos/coreos-cloudinit/datasource/file" "github.com/coreos/coreos-cloudinit/datasource/metadata/digitalocean" "github.com/coreos/coreos-cloudinit/datasource/metadata/ec2" + "github.com/coreos/coreos-cloudinit/datasource/metadata/gce" "github.com/coreos/coreos-cloudinit/datasource/metadata/packet" "github.com/coreos/coreos-cloudinit/datasource/proc_cmdline" "github.com/coreos/coreos-cloudinit/datasource/url" @@ -254,12 +255,7 @@ func getDatasources(cfg *rancherConfig.CloudConfig) []datasource.Datasource { } case "gce": if network { - gceCloudConfigFile, err := GetAndCreateGceDataSourceFilename() - if err != nil { - log.Errorf("Could not retrieve GCE CloudConfig %s", err) - continue - } - dss = append(dss, file.NewDatasource(gceCloudConfigFile)) + dss = append(dss, gce.NewDatasource("http://metadata.google.internal/")) } case "packet": if !network { diff --git a/cmd/cloudinit/gce.go b/cmd/cloudinit/gce.go deleted file mode 100644 index 947715af..00000000 --- a/cmd/cloudinit/gce.go +++ /dev/null @@ -1,140 +0,0 @@ -package cloudinit - -import ( - "strings" - - log "github.com/Sirupsen/logrus" - yaml "github.com/cloudfoundry-incubator/candiedyaml" - "github.com/rancher/os/util" - "google.golang.org/cloud/compute/metadata" -) - -type GceCloudConfig struct { - FileName string - UserData string - NonUserDataSSHKeys []string -} - -const ( - gceCloudConfigFile = "/var/lib/rancher/conf/gce_cloudinit_config.yml" -) - -func NewGceCloudConfig() *GceCloudConfig { - - userData, err := metadata.InstanceAttributeValue("user-data") - if err != nil { - log.Errorf("Could not retrieve user-data: %s", err) - } - - projectSSHKeys, err := metadata.ProjectAttributeValue("sshKeys") - if err != nil { - log.Errorf("Could not retrieve project SSH Keys: %s", err) - } - - instanceSSHKeys, err := metadata.InstanceAttributeValue("sshKeys") - if err != nil { - log.Errorf("Could not retrieve instance SSH Keys: %s", err) - } - - nonUserDataSSHKeysRaw := projectSSHKeys + "\n" + instanceSSHKeys - nonUserDataSSHKeys := gceSshKeyFormatter(nonUserDataSSHKeysRaw) - - gceCC := &GceCloudConfig{ - FileName: gceCloudConfigFile, - UserData: userData, - NonUserDataSSHKeys: nonUserDataSSHKeys, - } - - return gceCC -} - -func GetAndCreateGceDataSourceFilename() (string, error) { - gceCC := NewGceCloudConfig() - err := gceCC.saveToFile(gceCC.FileName) - if err != nil { - log.Errorf("Error: %s", err) - return "", err - } - return gceCC.FileName, nil -} - -func (cc *GceCloudConfig) saveToFile(filename string) error { - //Get Merged UserData sshkeys - data, err := cc.getMergedUserData() - if err != nil { - log.Errorf("Could not process userdata: %s", err) - return err - } - //write file - writeFile(filename, data) - return nil -} - -func (cc *GceCloudConfig) getMergedUserData() ([]byte, error) { - var returnUserData []byte - userdata := make(map[string]interface{}) - - if cc.UserData != "" { - log.Infof("Found UserData Config") - err := yaml.Unmarshal([]byte(cc.UserData), &userdata) - if err != nil { - log.Errorf("Could not unmarshal data: %s", err) - return nil, err - } - } - - var auth_keys []string - if _, exists := userdata["ssh_authorized_keys"]; exists { - udSshKeys := userdata["ssh_authorized_keys"].([]interface{}) - log.Infof("userdata %s", udSshKeys) - - for _, value := range udSshKeys { - auth_keys = append(auth_keys, value.(string)) - } - } - if cc.NonUserDataSSHKeys != nil { - for _, value := range cc.NonUserDataSSHKeys { - auth_keys = append(auth_keys, value) - } - } - userdata["ssh_authorized_keys"] = auth_keys - - yamlUserData, err := yaml.Marshal(&userdata) - if err != nil { - log.Errorf("Could not Marshal userdata: %s", err) - return nil, err - } else { - returnUserData = append([]byte("#cloud-config\n"), yamlUserData...) - } - - return returnUserData, nil -} - -func writeFile(filename string, data []byte) error { - if err := util.WriteFileAtomic(filename, data, 400); err != nil { - log.Errorf("Could not write file %v", err) - return err - } - return nil -} - -func gceSshKeyFormatter(rawKeys string) []string { - keySlice := strings.Split(rawKeys, "\n") - var cloudFormatedKeys []string - - if len(keySlice) > 0 { - for i := range keySlice { - keyString := keySlice[i] - sIdx := strings.Index(keyString, ":") - if sIdx != -1 { - key := strings.TrimSpace(keyString[sIdx+1:]) - keyA := strings.Split(key, " ") - key = strings.Join(keyA, " ") - if key != "" { - cloudFormatedKeys = append(cloudFormatedKeys, key) - } - } - } - } - return cloudFormatedKeys -}