github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/cloud/providers/static/static.go (about) 1 package static 2 3 import ( 4 "time" 5 6 "github.com/evergreen-ci/evergreen" 7 "github.com/evergreen-ci/evergreen/cloud" 8 "github.com/evergreen-ci/evergreen/db/bsonutil" 9 "github.com/evergreen-ci/evergreen/hostutil" 10 "github.com/evergreen-ci/evergreen/model/distro" 11 "github.com/evergreen-ci/evergreen/model/host" 12 "github.com/mongodb/grip" 13 "github.com/pkg/errors" 14 ) 15 16 const ProviderName = "static" 17 18 type StaticManager struct{} 19 20 type Settings struct { 21 Hosts []Host `mapstructure:"hosts" json:"hosts" bson:"hosts"` 22 } 23 24 type Host struct { 25 Name string `mapstructure:"name" json:"name" bson:"name"` 26 } 27 28 var ( 29 // bson fields for the Settings struct 30 HostsKey = bsonutil.MustHaveTag(Settings{}, "Hosts") 31 32 // bson fields for the Host struct 33 NameKey = bsonutil.MustHaveTag(Host{}, "Name") 34 ) 35 36 // Validate checks that the settings from the configuration are valid. 37 func (s *Settings) Validate() error { 38 for _, h := range s.Hosts { 39 if h.Name == "" { 40 return errors.New("host 'name' field can not be blank") 41 } 42 } 43 return nil 44 } 45 46 func (staticMgr *StaticManager) SpawnInstance(distro *distro.Distro, hostOpts cloud.HostOptions) (*host.Host, error) { 47 return nil, errors.New("cannot start new instances with static provider") 48 } 49 50 // get the status of an instance 51 func (staticMgr *StaticManager) GetInstanceStatus(host *host.Host) (cloud.CloudStatus, error) { 52 return cloud.StatusRunning, nil 53 } 54 55 // get instance DNS 56 func (staticMgr *StaticManager) GetDNSName(host *host.Host) (string, error) { 57 return host.Id, nil 58 } 59 60 func (staticMgr *StaticManager) CanSpawn() (bool, error) { 61 return false, nil 62 } 63 64 // terminate an instance 65 func (staticMgr *StaticManager) TerminateInstance(host *host.Host) error { 66 // a decommissioned static host will be removed from the database 67 if host.Status == evergreen.HostDecommissioned { 68 grip.Debugf("Removing decommissioned %s static host (%s)", host.Distro, host.Host) 69 if err := host.Remove(); err != nil { 70 grip.Errorf("Error removing decommissioned %s static host (%s): %+v", 71 host.Distro, host.Host, err) 72 } 73 } 74 grip.Debugf("Not terminating static '%s' host: %s", host.Distro.Id, host.Host) 75 return nil 76 } 77 78 func (_ *StaticManager) GetSettings() cloud.ProviderSettings { 79 return &Settings{} 80 } 81 82 func (staticMgr *StaticManager) Configure(settings *evergreen.Settings) error { 83 //no-op. maybe will need to load something from settings in the future. 84 return nil 85 } 86 87 func (staticMgr *StaticManager) IsSSHReachable(host *host.Host, keyPath string) (bool, error) { 88 sshOpts, err := staticMgr.GetSSHOptions(host, keyPath) 89 if err != nil { 90 return false, err 91 } 92 return hostutil.CheckSSHResponse(host, sshOpts) 93 } 94 95 func (staticMgr *StaticManager) IsUp(host *host.Host) (bool, error) { 96 return true, nil 97 } 98 99 func (staticMgr *StaticManager) OnUp(host *host.Host) error { 100 return nil 101 } 102 103 func (staticMgr *StaticManager) GetSSHOptions(h *host.Host, keyPath string) (opts []string, err error) { 104 if keyPath != "" { 105 opts = append(opts, "-i", keyPath) 106 } 107 for _, opt := range h.Distro.SSHOptions { 108 opts = append(opts, "-o", opt) 109 } 110 return opts, nil 111 } 112 113 // determine how long until a payment is due for the host. static hosts always 114 // return 0 for this number 115 func (staticMgr *StaticManager) TimeTilNextPayment(host *host.Host) time.Duration { 116 return time.Duration(0) 117 }