// handleShadow takes a /etc/shadow style file and returns a cloud-config func handleShadow(contents string, scripts_dir string) (*initialize.CloudConfig, error) { config := initialize.CloudConfig{} passwd := contents // root:$1$NyBnu0Gl$GBoj9u6lx3R8nyqHuxPwz/:15839:0::::: re := regexp.MustCompile("root:([^:]+):.+\n") keys := re.FindStringSubmatch(passwd) if len(keys) == 2 { passwd_hash := keys[1] // set the password for both users root := system.User{ Name: "root", PasswordHash: passwd_hash, } config.Users = append(config.Users, root) core := system.User{ Name: "core", PasswordHash: passwd_hash, } config.Users = append(config.Users, core) } else { return nil, errors.New("unable to parse password hash from shadow") } return &config, nil }
func runConfig(config *initialize.CloudConfig) error { f, err := ioutil.TempFile("", "rackspace-cloudinit-") if err != nil { return err } log.Println("writing to:", f.Name()) _, err = f.WriteString(config.String()) if err != nil { return err } // systemd-run coreos-cloudinit --file f.Name() props := []dbus.Property{ dbus.PropDescription("Unit generated and executed by coreos-cloudinit on behalf of user"), dbus.PropExecStart([]string{"/usr/bin/coreos-cloudinit", "--from-file", f.Name()}, false), } tmp_file := filepath.Base(f.Name()) name := fmt.Sprintf("%s.service", tmp_file) log.Printf("Creating transient systemd unit '%s'", name) conn, err := dbus.New() if err != nil { return err } _, err = conn.StartTransientUnit(name, "replace", props...) return err }
// handlHostname takes a gentoo style /etc/conf.d/hostname and returns a cloud-config func handleHostname(contents string, scripts_dir string) (*initialize.CloudConfig, error) { config := initialize.CloudConfig{} hostname := contents // HOSTNAME="polvi-test" re := regexp.MustCompile("HOSTNAME=\"(.+)\"") keys := re.FindStringSubmatch(hostname) if len(keys) == 2 { hostname := keys[1] config.Hostname = hostname } return &config, nil }
// setKey core and root users authorized_keys to the passed key func setKey(config *initialize.CloudConfig, key string) *initialize.CloudConfig { config.SSHAuthorizedKeys = append(config.SSHAuthorizedKeys, key) // set the password for both users if len(config.Users) == 0 { root := system.User{ Name: "root", } root.SSHAuthorizedKeys = append(root.SSHAuthorizedKeys, key) config.Users = append(config.Users, root) } else { config.Users[0].SSHAuthorizedKeys = append(config.Users[0].SSHAuthorizedKeys, key) } return config }