2019-01-09 16:54:15 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-03-02 00:07:55 +00:00
|
|
|
"io/ioutil"
|
2019-01-09 16:54:15 +00:00
|
|
|
"os"
|
2019-03-02 00:07:55 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2019-01-09 16:54:15 +00:00
|
|
|
|
|
|
|
"github.com/rancher/k3s/pkg/agent"
|
|
|
|
"github.com/rancher/k3s/pkg/cli/cmds"
|
2019-03-08 22:47:44 +00:00
|
|
|
"github.com/rancher/k3s/pkg/datadir"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/rancher/norman/signal"
|
2019-02-08 04:28:09 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-01-09 16:54:15 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2019-03-02 00:07:55 +00:00
|
|
|
func readToken(path string) (string, error) {
|
|
|
|
if path == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
tokenBytes, err := ioutil.ReadFile(path)
|
|
|
|
if err == nil {
|
|
|
|
return strings.TrimSpace(string(tokenBytes)), nil
|
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
logrus.Infof("Waiting for %s to be available\n", path)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
func Run(ctx *cli.Context) error {
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
return fmt.Errorf("agent must be ran as root")
|
|
|
|
}
|
|
|
|
|
2019-03-02 00:07:55 +00:00
|
|
|
if cmds.AgentConfig.TokenFile != "" {
|
|
|
|
token, err := readToken(cmds.AgentConfig.TokenFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmds.AgentConfig.Token = token
|
|
|
|
}
|
|
|
|
|
2019-01-22 21:14:58 +00:00
|
|
|
if cmds.AgentConfig.Token == "" && cmds.AgentConfig.ClusterSecret == "" {
|
2019-01-09 16:54:15 +00:00
|
|
|
return fmt.Errorf("--token is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmds.AgentConfig.ServerURL == "" {
|
|
|
|
return fmt.Errorf("--server is required")
|
|
|
|
}
|
|
|
|
|
2019-02-08 04:28:09 +00:00
|
|
|
logrus.Infof("Starting k3s agent %s", ctx.App.Version)
|
|
|
|
|
2019-03-08 22:47:44 +00:00
|
|
|
dataDir, err := datadir.LocalHome(cmds.AgentConfig.DataDir, cmds.AgentConfig.Rootless)
|
2019-01-09 16:54:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := cmds.AgentConfig
|
2019-02-08 04:12:49 +00:00
|
|
|
cfg.Debug = ctx.GlobalBool("debug")
|
2019-01-09 16:54:15 +00:00
|
|
|
cfg.DataDir = dataDir
|
|
|
|
|
|
|
|
contextCtx := signal.SigTermCancelContext(context.Background())
|
2019-02-08 04:28:09 +00:00
|
|
|
|
2019-01-09 16:54:15 +00:00
|
|
|
return agent.Run(contextCtx, cfg)
|
|
|
|
}
|