2018-12-28 16:41:37 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/rancher/rke/docker"
|
|
|
|
"github.com/rancher/rke/hosts"
|
|
|
|
"github.com/rancher/rke/log"
|
2019-06-18 18:24:21 +00:00
|
|
|
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
|
2018-12-28 16:41:37 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ContainerName = "file-deployer"
|
|
|
|
ServiceName = "file-deploy"
|
|
|
|
ConfigEnv = "FILE_DEPLOY"
|
|
|
|
)
|
|
|
|
|
|
|
|
func deployFile(ctx context.Context, uniqueHosts []*hosts.Host, alpineImage string, prsMap map[string]v3.PrivateRegistry, fileName, fileContents string) error {
|
|
|
|
for _, host := range uniqueHosts {
|
2019-04-24 18:33:46 +00:00
|
|
|
log.Infof(ctx, "[%s] Deploying file [%s] to node [%s]", ServiceName, fileName, host.Address)
|
2018-12-28 16:41:37 +00:00
|
|
|
if err := doDeployFile(ctx, host, fileName, fileContents, alpineImage, prsMap); err != nil {
|
2019-04-24 18:33:46 +00:00
|
|
|
return fmt.Errorf("[%s] Failed to deploy file [%s] on node [%s]: %v", ServiceName, fileName, host.Address, err)
|
2018-12-28 16:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func doDeployFile(ctx context.Context, host *hosts.Host, fileName, fileContents, alpineImage string, prsMap map[string]v3.PrivateRegistry) error {
|
|
|
|
// remove existing container. Only way it's still here is if previous deployment failed
|
|
|
|
if err := docker.DoRemoveContainer(ctx, host.DClient, ContainerName, host.Address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
containerEnv := []string{ConfigEnv + "=" + fileContents}
|
|
|
|
imageCfg := &container.Config{
|
|
|
|
Image: alpineImage,
|
|
|
|
Cmd: []string{
|
|
|
|
"sh",
|
|
|
|
"-c",
|
2019-06-18 18:24:21 +00:00
|
|
|
fmt.Sprintf("t=$(mktemp); echo -e \"$%s\" > $t && mv $t %s && chmod 600 %s", ConfigEnv, fileName, fileName),
|
2018-12-28 16:41:37 +00:00
|
|
|
},
|
|
|
|
Env: containerEnv,
|
|
|
|
}
|
|
|
|
hostCfg := &container.HostConfig{
|
|
|
|
Binds: []string{
|
|
|
|
fmt.Sprintf("%s:/etc/kubernetes:z", path.Join(host.PrefixPath, "/etc/kubernetes")),
|
|
|
|
},
|
|
|
|
}
|
2019-04-21 20:39:08 +00:00
|
|
|
if err := docker.DoRunOnetimeContainer(ctx, host.DClient, imageCfg, hostCfg, ContainerName, host.Address, ServiceName, prsMap); err != nil {
|
2018-12-28 16:41:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := docker.DoRemoveContainer(ctx, host.DClient, ContainerName, host.Address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-24 18:33:46 +00:00
|
|
|
logrus.Debugf("[%s] Successfully deployed file [%s] on node [%s]", ServiceName, fileName, host.Address)
|
2018-12-28 16:41:37 +00:00
|
|
|
return nil
|
|
|
|
}
|