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 ¶
- Variables
- func RequiredEnv(providerName string) (vars []string, err error)
- type DeployConfig
- type Error
- type Flavor
- type Provider
- func (p *Provider) CheapestServerFlavor(cores, ramMB int, regex string) (fr Flavor, err error)
- func (p *Provider) CheckServer(serverID string) (working bool, err error)
- func (p *Provider) Deploy(config *DeployConfig) (err error)
- func (p *Provider) DestroyServer(serverID string) (err error)
- func (p *Provider) GetQuota() (quota *Quota, err error)
- func (p *Provider) InCloud() bool
- func (p *Provider) PrivateKey() string
- func (p *Provider) Servers() map[string]*Server
- func (p *Provider) Spawn(os string, osUser string, flavorID string, diskGB int, ttd time.Duration, ...) (server *Server, err error)
- func (p *Provider) TearDown() (err error)
- type Quota
- type Resources
- type Server
- func (s *Server) Alive() bool
- func (s *Server) Allocate(cores, ramMB, diskGB int)
- func (s *Server) CreateFile(content string, dest string) (err error)
- func (s *Server) Destroy() error
- func (s *Server) Destroyed() bool
- func (s *Server) HasSpaceFor(cores, ramMB, diskGB int) int
- func (s *Server) MkDir(dest string) (err error)
- func (s *Server) Release(cores, ramMB, diskGB int)
- func (s *Server) RunCmd(cmd string, background bool) (response string, err error)
- func (s *Server) SSHClient() (*ssh.Client, error)
- func (s *Server) UploadFile(source string, dest string) (err error)
Constants ¶
This section is empty.
Variables ¶
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" ErrBadRegex = "your flavor regular expression was not valid" )
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 ¶
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 DeployConfig ¶ added in v0.5.0
type DeployConfig struct { RequiredPorts []int GatewayIP string CIDR string DNSNameServers []string }
DeployConfig are the configuration options that you supply to Deploy(). RequiredPorts is the slice of port numbers that your application needs to be able to communicate to any servers you spawn (eg. [22] for ssh) through. If a network and subnet need to be created, the GatewayIP and CIDR options will be used; they default to 192.168.0.1 and 192.168.0.0:18 respectively, allowing for 16381 servers to be Spawn()d later, with a maximum ip of 192.168.63.254. DNSNameServers is a slice of DNS name server IPs. It defaults to Google's: []string{"8.8.4.4", "8.8.8.8"}.
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.
type Flavor ¶
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 ¶
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 ¶
CheapestServerFlavor returns details of the smallest (cheapest) server "flavor" available that satisfies your minimum ram (MB) and CPU (core count) requirements, and that also matches the given regex (empty string for the regex means not limited by regex). 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. You don't test for size of disk here, because during Spawn() you will request a certain amount of disk space, and if that is larger than the flavor's root disk a larger volume will be created automatically.
func (*Provider) CheckServer ¶
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(config *DeployConfig) (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). Deploy() 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 ¶
DestroyServer destroys a server given its id, that you would have gotten from the ID property of Spawn()'s return value.
func (*Provider) GetQuota ¶
GetQuota returns details of the maximum resources the user can request, and the current resources used.
func (*Provider) InCloud ¶ added in v0.4.0
InCloud tells you if your process is currently running on a cloud server where the *Server related methods will all work correctly. (That is, if this returns true, you are on the same network as any server you Spawn().)
func (*Provider) PrivateKey ¶
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 ¶
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, diskGB int, ttd time.Duration, externalIP bool, postCreationScript []byte) (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) and at least the given amount of disk space (creating a temporary volume of the required size if the flavor's root disk is too small). 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. postCreationScript is the []byte contents of a script that will be run on the server after it has been created, before it is used for anything else; empty slice means do nothing.
func (*Provider) TearDown ¶
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, including the resource file that contains the private key.
type Quota ¶
type Quota struct { MaxRAM int // total MBs allowed MaxCores int // total CPU cores allowed MaxInstances int // max number of instances allowed MaxVolume int // max GBs of volume storage that can be allocated UsedRAM int UsedCores int UsedInstances int UsedVolume 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 OS string // the name of the Operating System image UserName string // the username needed to log in to the server AdminPass string Flavor Flavor Disk int // GB of available disk space 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) Allocate ¶
Allocate records that the given resources have now been used up on this server.
func (*Server) CreateFile ¶
CreateFile creates a new file with the given content on the server.
func (*Server) Destroyed ¶
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 ¶
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) RunCmd ¶
RunCmd runs the given command on the server, optionally in the background. You get the command's STDOUT as a string response.