Ejemplo n.º 1
0
func populate() {
	force, err := strconv.ParseBool(module.params["force"])
	if err != nil {
		panic(fmt.Sprintf("failed to parse bool: %s", err))
	}
	template, _ := strconv.ParseBool(module.params["template"])
	validate, _ := strconv.ParseBool(module.params["validate"])
	verbose, _ := strconv.ParseBool(module.params["verbose"])

	userMgr := cloudconfig.NewUserManager(
		cloudconfig.NewCloudInitUser(module.params["name"], module.params["password"], command.ToSlice(module.params["groups"]), command.ToSlice(module.params["ssh_authorized_keys"])),
		logger.NilLogger(), "update", force, module.params["dest"], module.params["src"], template, validate, verbose)

	if _, err := userMgr.Manage(); err != nil {
		status = NewStatus(module.params["name"], fmt.Sprintf("%s", err), time.Now(), false, true)
		panic(status)
	}

}
Ejemplo n.º 2
0
// Run -
func (c *UsersCommand) Run(args []string) int {
	var (
		action            string
		force             bool
		format            string
		destination       string
		groups            string
		level             string
		logfile           string
		passwd            string
		sshAuthorizedKeys string
		source            string
		template          bool
		validate          bool
		verbose           bool
	)

	cmdFlags := flag.NewFlagSet("users", flag.ContinueOnError)
	cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
	cmdFlags.StringVar(&action, "action", "view", "action to perform on user. choose from: add, update, remove, and view")
	cmdFlags.StringVar(&destination, "dest", "/etc/configdrive/cloud-config.yml", "File to write")
	cmdFlags.BoolVar(&force, "force", false, "Force overwrite the entire entry on update")
	cmdFlags.StringVar(&format, "format", "text", "Format response as either json or regular text.")
	cmdFlags.StringVar(&groups, "groups", "", "comma separated list of groups")
	cmdFlags.StringVar(&level, "level", "info", "logging level: error, warn, info, or debug")
	cmdFlags.StringVar(&logfile, "log", "/tmp/cloudconfig.log", "logfile path")
	cmdFlags.StringVar(&passwd, "passwd", "", "sha-512 password hash for user")
	cmdFlags.StringVar(&source, "src", "/etc/configdrive/cloud-config.yml", "File to read")
	cmdFlags.StringVar(&sshAuthorizedKeys, "ssh-authorized-keys", "", "add ssh-key to user")
	cmdFlags.BoolVar(&template, "template", false, "render go template")
	cmdFlags.BoolVar(&validate, "validate", false, "validate and exit, do not write file")
	cmdFlags.BoolVar(&verbose, "verbose", false, "increase verbosity")

	if err := cmdFlags.Parse(args); err != nil {
		return 1
	}

	args = cmdFlags.Args()
	if len(args) < 1 {
		c.UI.Error("A username must be specified.")
		c.UI.Error("")
		c.UI.Error(c.Help())
		return 1
	} else if len(args) > 1 {
		c.UI.Error("Too many command line arguments. Only a name must be specified.")
		c.UI.Error("")
		c.UI.Error(c.Help())
		return 1
	}

	// login passed as argument
	name := args[0]

	log := logger.NewCLILogger(level, logfile, "user", format, c.UI)

	userMgr := cloudconfig.NewUserManager(
		cloudconfig.NewCloudInitUser(name, passwd, ToSlice(groups), ToSlice(sshAuthorizedKeys)),
		log, action, force, destination, source, template, validate, verbose)

	if exitCode, err := userMgr.Manage(); err != nil {
		c.UI.Error(fmt.Sprintf("%s", err))
		return exitCode
	}

	log.Flush()

	return 0
}