linodeold

package
v0.0.0-...-deba36d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Manager = schema.ResourceManager{
	Name:              "linodeold",
	ResourcePrototype: &Linode{},
	GroupPrototype:    &LinodeGroup{},
	Provision: func(group interface{}, resource interface{}, l schema.Logger) error {
		g := group.(*LinodeGroup)
		s := resource.(*Linode)

		apikey, err := g.APIKey.Render(nil)
		if err != nil {
			return err
		}

		label := g.DecommissionTag
		if len(label) > 0 {
			label += "_"
		}
		label += s.Name

		if err := os.MkdirAll(cacheDir, 0700); err != nil {
			return err
		}

		privateKey, err := s.SSHPrivateKey.RenderFileBytes(nil)
		if err != nil {
			return err
		}

		info := &machineInfo{}
		machineInfoFilePath := filepath.Join(cacheDir, label)
		if b, err := ioutil.ReadFile(machineInfoFilePath); err == nil {
			decoder := snobgob.NewDecoder(bytes.NewReader(b))
			if err := decoder.Decode(info); err == nil {
				if err := ssh.WaitForSSH(info.PublicIPs[0], 22, "root", "", privateKey, time.Millisecond*200); err == nil {
					s.info = info
					return nil
				}
			}
		}

		accountsLock.Lock()
		account, found := accounts[apikey]
		if !found {
			account = &linodeAccount{client: &client{apikey: apikey}}
			accounts[apikey] = account
		}
		accountsLock.Unlock()

		servers, err := account.listServers(l)
		if err != nil {
			return err
		}

		server, found := servers[label]
		if !found {
			l.Logf("Creating new server")

			publicKey, err := s.SSHPublicKey.RenderFileBytes(nil)
			if err != nil {
				return err
			}

			rootPassword, err := s.RootPassword.Render(nil)
			if err != nil {
				return err
			}
			if rootPassword == "" {
				rootPassword, err = ssh.GenerateRandomPassword(32)
				if err != nil {
					return err
				}
			}

			err = account.create(s, label, l, rootPassword, string(publicKey))
			if err != nil {
				l.Errf("Could not create server %v: %v", s.Name, err)

				if nodes, err := account.listServers(l); err == nil {
					for _, node := range nodes {
						if node.LABEL == label {
							l.Logf("Trying to remove half-backed server %v", node.LABEL)
							account.decommissionServer(node, l)
						}
					}
				}

				return errors.New("Could not create server.")
			}

			servers, err = account.listServers(l)

			server, found = servers[label]
			if !found {
				return errors.New("could not find server in linode (linode api listLinode operation) right after creation. This should never happen")
			}
		}

		info = &machineInfo{
			PublicIPs:  make([]string, 0),
			PrivateIPs: make([]string, 0),
		}

		ips, err := account.client.lindeIPList(server.LINODEID)
		if err != nil {
			return fmt.Errorf("Could not get ip for server %v: %v", s.Name, err)
		}
		for _, ip := range ips {
			if ip.ISPUBLIC == 1 {
				info.PublicIPs = append(info.PublicIPs, ip.IPADDRESS)
			} else {
				info.PrivateIPs = append(info.PrivateIPs, ip.IPADDRESS)
			}
		}

		b := bytes.NewBuffer(nil)
		encoder := snobgob.NewEncoder(b)
		if err := encoder.Encode(info); err == nil {
			ioutil.WriteFile(machineInfoFilePath, b.Bytes(), 0700)
		}
		s.info = info

		ssh.WaitForSSH(info.PublicIPs[0], 22, "root", "", privateKey, time.Second*30)

		return nil
	},
	FindUnused: func(shouldExist map[interface{}][]string, decommisionRoot *commandtree.Command, l schema.Logger) ([]string, error) {
		unusedNames := make([]string, 0)

		for groupIface, names := range shouldExist {
			if group, ok := groupIface.(*LinodeGroup); ok {

				apikey, err := group.APIKey.Render(nil)
				if err != nil {
					return nil, err
				}

				accountsLock.Lock()
				account, found := accounts[apikey]
				if !found {
					account = &linodeAccount{client: &client{apikey: apikey}}
					accounts[apikey] = account
				}
				accountsLock.Unlock()

				servers, err := account.listServers(l)
				if err != nil {
					return nil, err
				}

				for _, node := range servers {
					if strings.HasPrefix(node.LABEL, group.DecommissionTag+"_") {
						found := false
						for _, name := range names {
							if node.LABEL == group.DecommissionTag+"_"+name {
								found = true
								break
							}
						}

						if !found {
							unusedNames = append(unusedNames, node.LABEL)
							decommisionRoot.Add("Decommission "+node.LABEL, &decommisionCommand{account: account, node: node})
						}
					}
				}
			}
		}

		return unusedNames, nil
	},
}

Manager is the main entry point for this resource type

Functions

This section is empty.

Types

type Linode

type Linode struct {
	Name          string
	Datacenter    schema.Template `required:"true" description:"The datacenter to host the linode in."`
	Plan          schema.Template `required:"true" description:"The linode plan to use (server size)"`
	Distribution  schema.Template `required:"true" description:"The distribution to use for the OS partition"`
	Disks         schema.Template `required:"true" default:"swap:256" description:"How the disks should be configured"`
	Kernel        schema.Template `required:"true" defaultdescription:"Which kernel is the system using"`
	SSHPrivateKey schema.Template `required:"true" defaultdescription:"The private key used to authenticate against to the server"`
	SSHPublicKey  schema.Template `required:"true" defaultdescription:"The public key used to authenticate "`
	RootPassword  schema.Template `default:"" defaultdescription:"The password to set for the root user. If none given, will create a random 32 char password."`
	PrivateIPs    int             `required:"true" default:"0" defaultdescription:"How many private ips to assign to the machine"`
	// contains filtered or unexported fields
}

func (*Linode) OpenConnection

func (s *Linode) OpenConnection() (schema.ServerConnection, error)

type LinodeGroup

type LinodeGroup struct {
	APIKey          schema.Template `required:"true" description:"The api key for the linode account"`
	DecommissionTag string          `` /* 166-byte string literal not displayed */
}

func (*LinodeGroup) Validate

func (g *LinodeGroup) Validate() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL