cloud

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2016 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Overview

Package cloud provides functions to interact with cloud providers such as OpenStack and AWS (not yet implemented).

You use it to create cloud resources so that you can spawn servers, then delete those resources when you're done.

Please note that the methods in this package are NOT safe to be used by more than 1 process at a time.

It's a pseudo plug-in system in that it is designed so that you can easily add a go file that implements the methods of the provideri interface, to support a new cloud provider. On the other hand, there is no dynamic loading of these go files; they are all imported (they all belong to the cloud package), and the correct one used at run time. To "register" a new provideri implementation you must add a case for it to New() and RequiredEnv() and rebuild.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadProvider     = "unknown provider name"
	ErrMissingEnv      = "missing environment variables: "
	ErrBadResourceName = "your resource name prefix contains disallowed characters"
	ErrNoFlavor        = "no server flavor can meet your resource requirements"
	ErrBadFlavor       = "no server flavor with that id exists"
)

Err* constants are found in the returned Errors under err.Err, so you can cast and check if it's a certain type of error. ErrMissingEnv gets appended to with missing environment variable names, so check based on prefix.

Functions

func RequiredEnv

func RequiredEnv(providerName string) (vars []string, err error)

RequiredEnv returns the environment variables that are needed by the given provider before New() will work for it. See New() for possible providerNames.

Types

type Error

type Error struct {
	Provider string // the provider's Name
	Op       string // name of the method
	Err      string // one of our Err* vars
}

Error records an error and the operation and provider caused it.

func (Error) Error

func (e Error) Error() string

type Flavor

type Flavor struct {
	ID    string
	Cores int
	RAM   int // MB
	Disk  int // GB
}

Flavor describes a "flavor" of server, which is a certain (virtual) hardware configuration

type Provider

type Provider struct {
	Name string
	// contains filtered or unexported fields
}

Provider gives you access to all of the methods you'll need to interact with a cloud provider.

func New

func New(name string, resourceName string, savePath string) (p *Provider, err error)

New creates a new Provider to interact with the given cloud provider. Possible names so far are "openstack" ("aws" is planned). You must provide a resource name that will be used to name any created cloud resources. You must also provide a file path prefix to save details of created resources to (the actual file created will be suffixed with your resourceName). Note that the file could contain created private key details, so should be kept accessible only by you.

func (*Provider) CheapestServerFlavor

func (p *Provider) CheapestServerFlavor(cores, ramMB, diskGB int) (fr Flavor, err error)

CheapestServerFlavor returns details of the smallest (cheapest) server "flavor" available that satisfies your minimum ram (MB), disk (GB) and CPU (core count) requirements. Use the ID property of the return value for passing to Spawn(). If no flavor meets your requirements you will get an error matching ErrNoFlavor.

func (*Provider) CheckServer

func (p *Provider) CheckServer(serverID string) (working bool, err error)

CheckServer asks the provider if the status of the given server (id retrieved via Spawn() or Servers()) indicates it is working fine. (If it's not and was previously thought to be a spawned server with an external IP, then it will be removed from the results of Servers().)

func (*Provider) Deploy

func (p *Provider) Deploy(requiredPorts []int) (err error)

Deploy triggers the creation of required cloud resources such as networks, ssh keys, security profiles and so on, such that you can subsequently Spawn() and ssh to your created server successfully. If a resource we need already exists with the resourceName you supplied to New(), we assume it belongs to us and we don't create another (so it is safe to call Deploy multiple times with the same args to New() and Deploy(): you don't need to check if you have already deployed). You must provide a slice of port numbers that your application needs to be able to communicate to any servers you spawn (eg. [22] for ssh) through. Saves the resources it created to disk, which are what TearDown() will delete when you call it. (They are saved to disk so that TearDown() can work if you call it in a different session to when you Deploy()ed, and so that PrivateKey() can work if you call it in a different session to the Deploy() call that actually created the ssh key.)

func (*Provider) DestroyServer

func (p *Provider) DestroyServer(serverID string) (err error)

DestroyServer destroys a server given its id, that you would have gotten from the ID property of Spawn()'s return value.

func (*Provider) GetQuota

func (p *Provider) GetQuota() (quota *Quota, err error)

GetQuota returns details of the maximum resources the user can request, and the current resources used.

func (*Provider) PrivateKey

func (p *Provider) PrivateKey() string

PrivateKey returns a PEM format string of the private key that was created by Deploy() (on its first invocation with the same arguments to New()).

func (*Provider) Servers

func (p *Provider) Servers() map[string]*Server

Servers returns a mapping of serverID => *Server for all servers that were Spawn()ed with an external IP (including those spawned in past sessions where the same arguments to New() were used). You should use s.Alive() before trying to use one of these servers. Do not alter the return value!

func (*Provider) Spawn

func (p *Provider) Spawn(os string, osUser string, flavorID string, ttd time.Duration, externalIP bool) (server *Server, err error)

Spawn creates a new server using an OS image with a name prefixed with the given os name, with the given flavor ID (that you could get from CheapestServerFlavor().ID). If you supply a non-zero value for the ttd argument, then this amount of time after the last s.Release() call you make (or after the last cmd you started with s.RunCmd exits) that causes the server to be considered idle, the server will be destroyed. If you need an external IP so that you can ssh to the server externally, supply true as the last argument. Returns a *Server so you can s.Destroy it later, find out its ip address so you can ssh to it, and get its admin password in case you need to sudo on the server. You will need to know the username that you can log in with on your chosen OS image. If you call Spawn() while running on a cloud server, then the newly spawned server will be in the same network and security group as the current server.

func (*Provider) TearDown

func (p *Provider) TearDown() (err error)

TearDown deletes all resources recorded during Deploy() or loaded from a previous session during New(). It also deletes any servers with names prefixed with the resourceName given to the initial New() call. If currently running on a cloud server, however, it will not delete anything needed by this server.

type Quota

type Quota struct {
	MaxRAM        int // total MBs allowed
	MaxCores      int // total CPU cores allowed
	MaxInstances  int // max number of instances allowed
	UsedRAM       int
	UsedCores     int
	UsedInstances int
}

Quota struct describes the limit on what resources you are allowed to use (0 values mean that resource is unlimited), and how much you have already used.

type Resources

type Resources struct {
	ResourceName string             // the resource name prefix that resources were created with
	Details      map[string]string  // whatever key values the provider needs to describe what it created
	PrivateKey   string             // PEM format string of the key user would need to ssh in to any created servers
	Servers      map[string]*Server // the serverID => *Server mapping of any servers Spawn()ed with an external ip
}

Resources struct contains provider-specific details of every resource that we created, in a format understood by TearDown(), so that we can delete those resources when they're no longer needed. There are also fields for important things the user needs to know.

type Server

type Server struct {
	ID        string
	IP        string // ip address that you could SSH to
	UserName  string // the username needed to log in to the server
	AdminPass string
	Flavor    Flavor
	TTD       time.Duration // amount of idle time allowed before destruction
	// contains filtered or unexported fields
}

Server provides details of the server that Spawn() created for you, and some methods that let you keep track of how you use that server.

func (*Server) Alive

func (s *Server) Alive() bool

Alive tells you if a server is usable.

func (*Server) Allocate

func (s *Server) Allocate(cores, ramMB, diskGB int)

Allocate records that the given resources have now been used up on this server.

func (*Server) CreateFile

func (s *Server) CreateFile(content string, dest string) (err error)

CreateFile creates a new file with the given content on the server.

func (*Server) Destroy

func (s *Server) Destroy() error

Destroy immediately destroys the server.

func (*Server) Destroyed

func (s *Server) Destroyed() bool

Destroyed tells you if a server was destroyed using Destroy() or the automatic destruction due to being idle. It is NOT the opposite of Alive(), since it does not check if the server is still usable.

func (*Server) HasSpaceFor

func (s *Server) HasSpaceFor(cores, ramMB, diskGB int) int

HasSpaceFor considers the current usage (according to prior Allocation calls) and tells you how many of a cmd needing the given resources can run on this server.

func (*Server) MkDir

func (s *Server) MkDir(dest string) (err error)

MkDir creates a directory (and it's parents as necessary) on the server.

func (*Server) Release

func (s *Server) Release(cores, ramMB, diskGB int)

Release records that the given resources have now been freed.

func (*Server) RunCmd

func (s *Server) RunCmd(cmd string, background bool) (response string, err error)

RunCmd runs the given command on the server, optionally in the background. You get the command's STDOUT as a string response.

func (*Server) SSHClient

func (s *Server) SSHClient() (*ssh.Client, error)

SSHClient returns an ssh.Client object that could be used to ssh to the server. Requires that port 22 is accessible for SSH.

func (*Server) UploadFile

func (s *Server) UploadFile(source string, dest string) (err error)

UploadFile uploads a local file to the given location on the server.

Jump to

Keyboard shortcuts

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