Files
fx-serverless/middlewares/driver.go
Minghe b569820d3e Provisioners (#495)
* seperate privisioners by platforms
* refactor provisioner
* fix image build
2020-03-19 21:53:31 +08:00

65 lines
1.7 KiB
Go

package middlewares
import (
"fmt"
"runtime"
"time"
"github.com/apex/log"
"github.com/metrue/fx/constants"
dockerHTTP "github.com/metrue/fx/container_runtimes/docker/http"
"github.com/metrue/fx/context"
dockerDriver "github.com/metrue/fx/driver/docker"
k8sInfra "github.com/metrue/fx/driver/k8s"
"github.com/metrue/fx/provisioner"
darwin "github.com/metrue/fx/provisioner/darwin"
linux "github.com/metrue/fx/provisioner/linux"
"github.com/metrue/go-ssh-client"
)
// Driver initialize infrastructure driver
func Driver(ctx context.Contexter) (err error) {
host := ctx.Get("host").(string)
sshClient := ctx.Get("ssh").(ssh.Clienter)
kubeconf := ctx.Get("kubeconf").(string)
if host != "" {
// TODO port should be configurable
docker := dockerHTTP.New(host, constants.AgentPort)
driver := dockerDriver.New(dockerDriver.Options{
DockerClient: docker,
})
if err := driver.Ping(ctx.GetContext()); err != nil {
log.Infof("provisioning %s ...", host)
var provisioner provisioner.Provisioner
// TODO actually we should get os of target host, not the host of fx
// running on
if runtime.GOOS == "darwin" {
provisioner = darwin.New(sshClient)
} else {
provisioner = linux.New(sshClient)
}
isRemote := (host != "127.0.0.1" && host != "localhost")
if err := provisioner.Provision(ctx.GetContext(), isRemote); err != nil {
return err
}
time.Sleep(2 * time.Second)
}
if err := docker.Initialize(); err != nil {
return fmt.Errorf("initialize docker client failed: %s", err)
}
ctx.Set("docker_driver", driver)
}
if kubeconf != "" {
driver, err := k8sInfra.CreateDeployer(kubeconf)
if err != nil {
return err
}
ctx.Set("k8s_driver", driver)
}
return nil
}