github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/cs/cloudinit.go (about)

     1  package cs
     2  
     3  import "gopkg.in/yaml.v3"
     4  
     5  type Cloudinit struct {
     6  	Groups  map[string][]string `yaml:"groups,omitempty"`
     7  	Users   []*CloudinitUser    `yaml:"users,omitempty"`
     8  	RunCMDs []string            `yaml:"runcmd,omitempty"`
     9  }
    10  
    11  type CloudinitUser struct {
    12  	Name              string   `yaml:"name,omitempty"`
    13  	PlainTextPasswd   string   `yaml:"plain_text_passwd,omitempty"`
    14  	ExpireDate        string   `yaml:"expiredate,omitempty"`
    15  	Gecos             string   `yaml:"gecos,omitempty"`
    16  	Homedir           string   `yaml:"homedir,omitempty"`
    17  	PrimaryGroup      string   `yaml:"primary_group,omitempty"`
    18  	Groups            []string `yaml:"groups,omitempty"`
    19  	SelinuxUser       string   `yaml:"selinux_user,omitempty"`
    20  	LockPasswd        *bool    `yaml:"lock_passwd,omitempty"`
    21  	Inactive          *bool    `yaml:"inactive,omitempty"`
    22  	Passwd            string   `yaml:"passwd,omitempty"`
    23  	SSHImportID       string   `yaml:"ssh_import_id,omitempty"`
    24  	SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys,omitempty"`
    25  	SSHRedirectUser   *bool    `yaml:"ssh_redirect_user,omitempty"`
    26  	Sudo              string   `yaml:"sudo,omitempty"`
    27  }
    28  
    29  func NewCloudinit() *Cloudinit {
    30  	return &Cloudinit{
    31  		Groups: map[string][]string{},
    32  		Users:  []*CloudinitUser{},
    33  	}
    34  }
    35  
    36  func (c *Cloudinit) AddGroupWithoutUsers(name string) *Cloudinit {
    37  	c.Groups[name] = []string{}
    38  	return c
    39  }
    40  
    41  func (c *Cloudinit) AddUser(
    42  	name string,
    43  	lockPasswd bool,
    44  	password string,
    45  	groups []string,
    46  	primaryGroup string,
    47  	sshAuthorizedKeys []string,
    48  	sudo string,
    49  ) *Cloudinit {
    50  	c.Users = append(c.Users, &CloudinitUser{
    51  		Name:              name,
    52  		PlainTextPasswd:   password,
    53  		ExpireDate:        "",
    54  		Gecos:             "",
    55  		Homedir:           "",
    56  		PrimaryGroup:      primaryGroup,
    57  		Groups:            groups,
    58  		SelinuxUser:       "",
    59  		LockPasswd:        &lockPasswd,
    60  		Inactive:          nil,
    61  		Passwd:            "",
    62  		SSHImportID:       "",
    63  		SSHAuthorizedKeys: sshAuthorizedKeys,
    64  		SSHRedirectUser:   nil,
    65  		Sudo:              sudo,
    66  	})
    67  	return c
    68  }
    69  
    70  func (c *Cloudinit) AddCmd(cmd string) *Cloudinit {
    71  	c.RunCMDs = append(c.RunCMDs, cmd)
    72  	return c
    73  }
    74  
    75  func (c *Cloudinit) ToYamlString() (string, error) {
    76  	data, err := yaml.Marshal(c)
    77  	if err != nil {
    78  		return "", err
    79  	}
    80  	return "#cloud-config\n" + string(data), nil
    81  }