Compare commits

..

1 Commits

Author SHA1 Message Date
Minghe
c9630a53c3 should treat CICD code block seperately (#391)
* should treat CICD code block seperately

* auto provision when host is not health

* fix lint
2019-12-05 11:38:54 +08:00
4 changed files with 89 additions and 31 deletions

10
fx.go
View File

@@ -156,7 +156,7 @@ func main() {
},
Action: handle(
middlewares.LoadConfig,
middlewares.Setup,
middlewares.Provision,
middlewares.Parse("up"),
middlewares.Binding,
middlewares.Build,
@@ -170,7 +170,7 @@ func main() {
Action: handle(
middlewares.Parse("down"),
middlewares.LoadConfig,
middlewares.Setup,
middlewares.Provision,
handlers.Down,
),
},
@@ -181,7 +181,7 @@ func main() {
Action: handle(
middlewares.Parse("list"),
middlewares.LoadConfig,
middlewares.Setup,
middlewares.Provision,
handlers.List,
),
},
@@ -211,7 +211,7 @@ func main() {
},
Action: handle(
middlewares.LoadConfig,
middlewares.Setup,
middlewares.Provision,
handlers.BuildImage,
),
},
@@ -226,7 +226,7 @@ func main() {
},
Action: handle(
middlewares.LoadConfig,
middlewares.Setup,
middlewares.Provision,
handlers.ExportImage,
),
},

View File

@@ -9,6 +9,7 @@ import (
"github.com/metrue/fx/constants"
"github.com/metrue/fx/infra"
"github.com/metrue/fx/pkg/spinner"
sshOperator "github.com/metrue/go-ssh-client"
)
@@ -27,24 +28,44 @@ func NewProvisioner(ip string, user string) *Provisioner {
}
// Provision provision a host, install docker and start dockerd
func (d *Provisioner) Provision() ([]byte, error) {
func (d *Provisioner) Provision() (config []byte, err error) {
spinner.Start("provisioning")
defer func() {
spinner.Stop("provisioning", err)
}()
// TODO clean up, skip check localhost or not if in CICD env
if d.isLocalHost() {
if os.Getenv("CICD") == "" {
if !d.hasDocker() {
return nil, fmt.Errorf("please make sure docker installed and running")
}
if err := d.StartFxAgentLocally(); err != nil {
return nil, err
}
config, _ := json.Marshal(map[string]string{
"ip": d.IP,
"user": d.User,
})
return config, nil
if os.Getenv("CICD") != "" {
if err := d.Install(); err != nil {
return nil, err
}
if err := d.StartDockerd(); err != nil {
return nil, err
}
if err := d.StartFxAgent(); err != nil {
return nil, err
}
config, _ := json.Marshal(map[string]string{
"ip": d.IP,
"user": d.User,
})
return config, nil
}
if d.isLocalHost() {
if !d.hasDocker() {
return nil, fmt.Errorf("please make sure docker installed and running")
}
if err := d.StartFxAgentLocally(); err != nil {
return nil, err
}
config, _ := json.Marshal(map[string]string{
"ip": d.IP,
"user": d.User,
})
return config, nil
}
if err := d.Install(); err != nil {
@@ -56,11 +77,10 @@ func (d *Provisioner) Provision() ([]byte, error) {
if err := d.StartFxAgent(); err != nil {
return nil, err
}
config, _ := json.Marshal(map[string]string{
return json.Marshal(map[string]string{
"ip": d.IP,
"user": d.User,
})
return config, nil
}
func (d *Provisioner) isLocalHost() bool {
@@ -77,8 +97,10 @@ func (d *Provisioner) hasDocker() bool {
// HealthCheck check healthy status of host
func (d *Provisioner) HealthCheck() (bool, error) {
// TODO
return true, nil
if d.isLocalHost() {
return d.IfFxAgentRunningLocally(), nil
}
return d.IfFxAgentRunning(), nil
}
// Install docker on host
@@ -152,7 +174,6 @@ func (d *Provisioner) StartFxAgent() error {
func (d *Provisioner) StartFxAgentLocally() error {
startCmd := fmt.Sprintf("docker run -d --name=%s --rm -v /var/run/docker.sock:/var/run/docker.sock -p 0.0.0.0:%s:1234 bobrik/socat TCP-LISTEN:1234,fork UNIX-CONNECT:/var/run/docker.sock", constants.AgentContainerName, constants.AgentPort)
params := strings.Split(startCmd, " ")
fmt.Println(params)
var cmd *exec.Cmd
if len(params) > 1 {
// nolint: gosec
@@ -168,4 +189,29 @@ func (d *Provisioner) StartFxAgentLocally() error {
return nil
}
// IfFxAgentRunningLocally check if fx agent is running
func (d *Provisioner) IfFxAgentRunningLocally() bool {
cmd := exec.Command("docker", "inspect", "fx-agent")
if err := cmd.Run(); err != nil {
return false
}
return true
}
// IfFxAgentRunning check if fx agent is running
func (d *Provisioner) IfFxAgentRunning() bool {
inspectCmd := infra.Sudo("docker inspect fx-agent", d.User)
sshKeyFile, _ := infra.GetSSHKeyFile()
sshPort := infra.GetSSHPort()
ssh := sshOperator.New(d.IP).WithUser(d.User).WithKey(sshKeyFile).WithPort(sshPort)
if err := ssh.RunCommand(inspectCmd, sshOperator.CommandOptions{
Stdout: os.Stdout,
Stdin: os.Stdin,
Stderr: os.Stderr,
}); err != nil {
return false
}
return true
}
var _ infra.Provisioner = &Provisioner{}

View File

@@ -63,7 +63,7 @@ func (k *Provisioner) SetupMaster() error {
sshKeyFile, _ := infra.GetSSHKeyFile()
ssh := sshOperator.New(k.master.IP).WithUser(k.master.User).WithKey(sshKeyFile)
installCmd := fmt.Sprintf("curl -sLS https://get.k3s.io | INSTALL_K3S_EXEC='server --tls-san %s' INSTALL_K3S_VERSION='%s' sh -", k.master.IP, version)
if err := ssh.RunCommand(infra.Sudo(installCmd, k.master.IP), sshOperator.CommandOptions{
if err := ssh.RunCommand(infra.Sudo(installCmd, k.master.User), sshOperator.CommandOptions{
Stdout: os.Stdout,
Stdin: os.Stdin,
Stderr: os.Stderr,
@@ -80,7 +80,7 @@ func (k *Provisioner) getToken() (string, error) {
ssh := sshOperator.New(k.master.IP).WithUser(k.master.User).WithKey(sshKeyFile)
script := "cat /var/lib/rancher/k3s/server/node-token"
var outPipe bytes.Buffer
if err := ssh.RunCommand(infra.Sudo(script, k.master.IP), sshOperator.CommandOptions{
if err := ssh.RunCommand(infra.Sudo(script, k.master.User), sshOperator.CommandOptions{
Stdout: bufio.NewWriter(&outPipe),
Stdin: os.Stdin,
Stderr: os.Stderr,
@@ -123,7 +123,7 @@ func (k *Provisioner) GetKubeConfig() ([]byte, error) {
getConfigCmd := "cat /etc/rancher/k3s/k3s.yaml\n"
ssh := sshOperator.New(k.master.IP).WithUser(k.master.User).WithKey(sshKeyFile)
var outPipe bytes.Buffer
if err := ssh.RunCommand(infra.Sudo(getConfigCmd, k.master.IP), sshOperator.CommandOptions{
if err := ssh.RunCommand(infra.Sudo(getConfigCmd, k.master.User), sshOperator.CommandOptions{
Stdout: bufio.NewWriter(&outPipe),
Stdin: os.Stdin,
Stderr: os.Stderr,

View File

@@ -14,17 +14,29 @@ import (
"github.com/pkg/errors"
)
// Setup create k8s or docker cli
func Setup(ctx context.Contexter) (err error) {
// Provision make sure infrastructure is healthy
func Provision(ctx context.Contexter) (err error) {
fxConfig := ctx.Get("config").(*config.Config)
cloud := fxConfig.Clouds[fxConfig.CurrentCloud]
var deployer infra.Deployer
if cloud["type"] == config.CloudTypeDocker {
provisioner := dockerInfra.CreateProvisioner(cloud["host"], cloud["user"])
ok, err := provisioner.HealthCheck()
if err != nil {
return err
}
if !ok {
if _, err := provisioner.Provision(); err != nil {
return err
}
}
docker, err := dockerHTTP.Create(cloud["host"], constants.AgentPort)
if err != nil {
return errors.Wrapf(err, "please make sure docker is installed and running on your host")
}
// TODO should clean up, but it needed in middlewares.Build
ctx.Set("docker", docker)
deployer, err = dockerInfra.CreateDeployer(docker)