Exemplo n.º 1
0
// add -
func (u *UserManager) add(config *cloudinit.CloudConfig) error {
	for _, user := range config.Users {
		if user.Name == u.User.Name {
			u.Log.Errorf("user '%s' found, exiting", u.User.Name)
			return nil // exit happily
		}
	}

	u.Log.Infof("user '%s' not found, adding", u.User.Name)
	config.Users = append(config.Users, *u.User)
	if err := WriteDestination(u.Destination, config); err != nil {
		u.Log.Errorf("failed to write new user_data: %s, error: %s", u.Destination, err)
	}
	return nil
}
Exemplo n.º 2
0
// remove -
func (u *UserManager) remove(config *cloudinit.CloudConfig) error {
	u.Log.Infof("searching for user '%s'", u.User.Name)
	for i, user := range config.Users {
		if user.Name == u.User.Name {
			u.Log.Infof("user '%s' found, removing", u.User.Name)
			config.Users = append(config.Users[:i], config.Users[i+1:]...)
			if err := WriteDestination(u.Destination, config); err != nil {
				u.Log.Errorf("failed to write new user_data: %s, error: %s", u.Destination, err)
				return err
			}
			return nil
		}
	}
	u.Log.Errorf("user '%s' not found, exiting", u.User.Name)
	return nil
}
Exemplo n.º 3
0
// update - TODO(johnt337): refactor this down...
func (u *UserManager) update(config *cloudinit.CloudConfig) error {
	isDirty := false
	for i, user := range config.Users {
		if user.Name == u.User.Name {

			if !u.Force {
				if u.User.PasswordHash == "" {
					u.Log.Infof("use '-force' to truncate the password")
				} else {
					if config.Users[i].PasswordHash == u.User.PasswordHash {
						u.Log.Infof("password matches")
					} else {
						u.Log.Infof("password differs, updating")
						config.Users[i].PasswordHash = u.User.PasswordHash
						isDirty = true
					}
				}

				if len(u.User.SSHAuthorizedKeys) <= 0 {
					u.Log.Infof("use '-force' to truncate the sshkeys")
				} else {
					for _, newSSH := range u.User.SSHAuthorizedKeys {
						addKey := true
						for _, oldSSH := range config.Users[i].SSHAuthorizedKeys {
							if newSSH == oldSSH {
								u.Log.Infof("sshkey already exists, skipping")
								addKey = false
							}
						}
						if addKey {
							if newSSH != "" {
								u.Log.Infof("adding sshkey")
								config.Users[i].SSHAuthorizedKeys = append(config.Users[i].SSHAuthorizedKeys, newSSH)
								isDirty = true
							}
						}
					}
				}

				if len(u.User.Groups) <= 0 {
					u.Log.Infof("use '-force' to truncate the groups")
				} else {
					for _, newGroup := range u.User.Groups {
						addGroup := true
						for _, oldGroup := range config.Users[i].Groups {

							if newGroup == oldGroup {
								u.Log.Infof("group '%s' already exists, skipping", newGroup)
								addGroup = false
							}
						}
						if addGroup {
							if newGroup != "" {
								u.Log.Infof("adding group '%s' to user '%s'", newGroup, u.User.Name)
								config.Users[i].Groups = append(config.Users[i].Groups, newGroup)
								isDirty = true
							}
						}
					}
				}

			} else {
				u.Log.Infof("force overwriting user '%s'!", u.User.Name)
				isDirty = true
			}

			if isDirty {
				u.Log.Infof("updating user '%s'", u.User.Name)
				// pop the old user entry, then push the new one
				config.Users = append(config.Users[:i], config.Users[i+1:]...)
				config.Users = append(config.Users, *u.User)

				u.Log.Infof("validating generated cloudconfig")
				err := u.validate(config)

				if u.Validate {
					if err != nil {
						return fmt.Errorf("cloudconfig is invalid, exiting")
					}
					u.Log.Infof("cloudconfig is valid, exiting")
					return nil

				}
				if err := WriteDestination(u.Destination, config); err != nil {
					u.Log.Errorf("failed to write new user_data: %s, error: %s", u.Destination, err)
					return err
				}
			} else {
				u.Log.Infof("ignoring user '%s'", u.User.Name)
			}
			return nil
		}
	}
	u.Log.Errorf("user '%s' not found, exiting", u.User.Name)
	return nil
}