Update to plugin (#847)

* make vagrant stuff more pluginable

* Touch up vbox file

* Refactor plugins
This commit is contained in:
Faiq Raza
2018-03-13 14:33:46 -07:00
committed by GitHub
parent 05c1bf1468
commit 52adedad86
4 changed files with 26 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
package controlplane package main
import ( import (
"bytes" "bytes"
@@ -11,34 +11,36 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"github.com/fnproject/fn/poolmanager/server/controlplane"
idgen "github.com/fnproject/fn/api/id" idgen "github.com/fnproject/fn/api/id"
) )
const vboxNamePrefix = "fn-vagrant" const (
vagrantEnv = "VAGRANT_PATH"
var whichVBox *exec.Cmd vboxNamePrefix = "fn-vagrant"
)
func init() { func init() {
whichVBox = exec.Command("which", "vagrant") vagrantPath, ok := os.LookupEnv(vagrantEnv)
if !ok {
log.Panicf("Missing config key: %v", vagrantEnv)
}
ControlPlane = VirtualBoxCP{
runnerMap: make(map[string][]*controlplane.Runner),
vagrantPath: vagrantPath,
}
}
func main() {
} }
type VirtualBoxCP struct { type VirtualBoxCP struct {
runnerMap map[string][]*Runner runnerMap map[string][]*controlplane.Runner
vagrantPath string vagrantPath string
} }
func NewVirtualBoxCP(vagrantPath string) (*VirtualBoxCP, error) { func (v *VirtualBoxCP) provision() (*controlplane.Runner, error) {
runnerMap := make(map[string][]*Runner)
if err := whichVBox.Run(); err != nil {
return nil, err
}
return &VirtualBoxCP{
runnerMap: runnerMap,
vagrantPath: vagrantPath,
}, nil
}
func (v *VirtualBoxCP) provision() (*Runner, error) {
//set up dir //set up dir
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
@@ -77,7 +79,7 @@ func (v *VirtualBoxCP) provision() (*Runner, error) {
//Gets the address that its broadcasting at //Gets the address that its broadcasting at
//VBoxManage guestproperty get "cp_default_1520116902053_77841" "/VirtualBox/GuestInfo/Net/1/V4/Broadcast" //VBoxManage guestproperty get "cp_default_1520116902053_77841" "/VirtualBox/GuestInfo/Net/1/V4/Broadcast"
func getRunner(node string) (*Runner, error) { func getRunner(node string) (*controlplane.Runner, error) {
//TODO make the vagrant file templated //TODO make the vagrant file templated
vmsCmd := exec.Command("VBoxManage", "list", "vms") vmsCmd := exec.Command("VBoxManage", "list", "vms")
var vmsOut bytes.Buffer var vmsOut bytes.Buffer
@@ -117,13 +119,13 @@ func getRunner(node string) (*Runner, error) {
if len(addr) != 2 { if len(addr) != 2 {
return nil, fmt.Errorf("Unable to get address got:'%s' as output", out.String()) return nil, fmt.Errorf("Unable to get address got:'%s' as output", out.String())
} }
return &Runner{ return &controlplane.Runner{
Id: realNode, Id: realNode,
Address: addr[1], Address: addr[1],
}, nil }, nil
} }
func (v *VirtualBoxCP) GetLBGRunners(lgbID string) ([]*Runner, error) { func (v *VirtualBoxCP) GetLBGRunners(lgbID string) ([]*controlplane.Runner, error) {
runners, ok := v.runnerMap[lgbID] runners, ok := v.runnerMap[lgbID]
if !ok { if !ok {
return nil, errors.New("Not Found") return nil, errors.New("Not Found")
@@ -132,7 +134,7 @@ func (v *VirtualBoxCP) GetLBGRunners(lgbID string) ([]*Runner, error) {
} }
func (v *VirtualBoxCP) ProvisionRunners(lgbID string, n int) (int, error) { func (v *VirtualBoxCP) ProvisionRunners(lgbID string, n int) (int, error) {
runners := make([]*Runner, 0, n) runners := make([]*controlplane.Runner, 0, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
runner, err := v.provision() runner, err := v.provision()
runners = append(runners, runner) runners = append(runners, runner)
@@ -225,3 +227,5 @@ func copyFile(src string, dst string) error {
// Will return nil if no errors occurred // Will return nil if no errors occurred
return d.Close() return d.Close()
} }
var ControlPlane VirtualBoxCP