From a4c4827e2b8639439768b7a0066b37d09f79ac53 Mon Sep 17 00:00:00 2001 From: Aaron Prindle Date: Wed, 14 Jun 2017 14:27:01 -0700 Subject: [PATCH] Removed systemd dependency from minikube, updated none driver to reflect this --- pkg/minikube/cluster/cluster.go | 5 +- pkg/minikube/cluster/commands.go | 57 +++++++++++++++++++++-- pkg/minikube/machine/drivers/none/none.go | 18 ++++--- 3 files changed, 66 insertions(+), 14 deletions(-) diff --git a/pkg/minikube/cluster/cluster.go b/pkg/minikube/cluster/cluster.go index 85ac0684af7b..38c7e0a0e6c4 100644 --- a/pkg/minikube/cluster/cluster.go +++ b/pkg/minikube/cluster/cluster.go @@ -148,10 +148,7 @@ func GetLocalkubeStatus(api libmachine.API) (string, error) { if err != nil { return "", err } - - statusCmd := localkubeStatusCommand - - s, err := RunCommand(h, statusCmd, false) + s, err := RunCommand(h, localkubeStatusCommand, false) if err != nil { return "", err } diff --git a/pkg/minikube/cluster/commands.go b/pkg/minikube/cluster/commands.go index 6de89dc75eda..fe5972870cf4 100644 --- a/pkg/minikube/cluster/commands.go +++ b/pkg/minikube/cluster/commands.go @@ -28,8 +28,14 @@ import ( ) // Kill any running instances. + var localkubeStartCmdTemplate = "/usr/local/bin/localkube {{.Flags}} --generate-certs=false --logtostderr=true --enable-dns=false" +var startCommandNoneTemplate = ` +# Run with nohup so it stays up. Redirect logs to useful places. +sudo sh -c 'PATH=/usr/local/sbin:$PATH nohup {{.LocalkubeStartCmd}} > {{.Stdout}} 2> {{.Stderr}} < /dev/null & echo $! > {{.Pidfile}} &' +` + var localkubeSystemdTmpl = `[Unit] Description=Localkube Documentation=https://github.com/kubernetes/minikube/tree/master/pkg/localkube @@ -47,11 +53,15 @@ ExecReload=/bin/kill -s HUP $MAINPID WantedBy=multi-user.target ` -var startCommandTemplate = ` +var startCommandTemplate = "if [[ `systemctl` =~ -\\.mount ]] &>/dev/null;" + `then {{.StartCommandSystemd}} sudo systemctl daemon-reload sudo systemctl enable localkube.service sudo systemctl restart localkube.service || true +else + sudo killall localkube || true + {{.StartCommandNone}} +fi ` func GetStartCommand(kubernetesConfig KubernetesConfig) (string, error) { @@ -59,6 +69,10 @@ func GetStartCommand(kubernetesConfig KubernetesConfig) (string, error) { if err != nil { return "", err } + startCommandNone, err := GetStartCommandNone(kubernetesConfig, localkubeStartCommand) + if err != nil { + return "", err + } startCommandSystemd, err := GetStartCommandSystemd(kubernetesConfig, localkubeStartCommand) if err != nil { return "", err @@ -66,8 +80,10 @@ func GetStartCommand(kubernetesConfig KubernetesConfig) (string, error) { t := template.Must(template.New("startCommand").Parse(startCommandTemplate)) buf := bytes.Buffer{} data := struct { + StartCommandNone string StartCommandSystemd string }{ + StartCommandNone: startCommandNone, StartCommandSystemd: startCommandSystemd, } if err := t.Execute(&buf, data); err != nil { @@ -76,6 +92,26 @@ func GetStartCommand(kubernetesConfig KubernetesConfig) (string, error) { return buf.String(), nil } +func GetStartCommandNone(kubernetesConfig KubernetesConfig, localkubeStartCmd string) (string, error) { + t := template.Must(template.New("startCommand").Parse(startCommandNoneTemplate)) + buf := bytes.Buffer{} + data := struct { + LocalkubeStartCmd string + Stdout string + Stderr string + Pidfile string + }{ + LocalkubeStartCmd: localkubeStartCmd, + Stdout: constants.RemoteLocalKubeOutPath, + Stderr: constants.RemoteLocalKubeErrPath, + Pidfile: constants.LocalkubePIDPath, + } + if err := t.Execute(&buf, data); err != nil { + return "", err + } + return buf.String(), nil +} + func GetStartCommandSystemd(kubernetesConfig KubernetesConfig, localkubeStartCmd string) (string, error) { t, err := template.New("localkubeConfig").Parse(localkubeSystemdTmpl) if err != nil { @@ -130,6 +166,7 @@ func GenLocalkubeStartCmd(kubernetesConfig KubernetesConfig) (string, error) { flagVals = append(flagVals, fmt.Sprintf("--extra-config=%s", e.String())) } flags := strings.Join(flagVals, " ") + t := template.Must(template.New("localkubeStartCmd").Parse(localkubeStartCmdTemplate)) buf := bytes.Buffer{} data := struct { @@ -145,7 +182,12 @@ func GenLocalkubeStartCmd(kubernetesConfig KubernetesConfig) (string, error) { return buf.String(), nil } -const logsTemplate = "sudo journalctl {{.Flags}} -u localkube" +const logsTemplate = "if [[ `systemctl` =~ -\\.mount ]] &>/dev/null; " + `then + sudo journalctl {{.Flags}} -u localkube +else + tail -n +1 {{.Flags}} {{.RemoteLocalkubeErrPath}} {{.RemoteLocalkubeOutPath}} %s +fi +` func GetLogsCommand(follow bool) (string, error) { t, err := template.New("logsTemplate").Parse(logsTemplate) @@ -173,7 +215,16 @@ func GetLogsCommand(follow bool) (string, error) { return buf.String(), nil } -var localkubeStatusCommand = `sudo systemctl is-active localkube 2>&1 1>/dev/null && echo "Running" || echo "Stopped"` +var localkubeStatusCommand = fmt.Sprintf("if [[ `systemctl` =~ -\\.mount ]] &>/dev/null; "+`then + sudo systemctl is-active localkube &>/dev/null && echo "Running" || echo "Stopped" +else + if ps $(cat %s) &>/dev/null; then + echo "Running" + else + echo "Stopped" + fi +fi +`, constants.LocalkubePIDPath) func GetMountCleanupCommand(path string) string { return fmt.Sprintf("sudo umount %s;", path) diff --git a/pkg/minikube/machine/drivers/none/none.go b/pkg/minikube/machine/drivers/none/none.go index f7455f570a3d..7e68ecc44333 100644 --- a/pkg/minikube/machine/drivers/none/none.go +++ b/pkg/minikube/machine/drivers/none/none.go @@ -17,7 +17,6 @@ limitations under the License. package none import ( - "errors" "fmt" "io/ioutil" "os" @@ -50,10 +49,6 @@ func NewDriver(hostName, storePath string) *Driver { // PreCreateCheck checks that VBoxManage exists and works func (d *Driver) PreCreateCheck() error { - // check that systemd is installed as it is a requirement - if _, err := exec.LookPath("systemctl"); err != nil { - return errors.New("systemd is a requirement in order to use the none driver") - } return nil } @@ -96,10 +91,19 @@ func (d *Driver) GetURL() (string, error) { } func (d *Driver) GetState() (state.State, error) { - command := `sudo systemctl is-active localkube 2>&1 1>/dev/null && echo "Running" || echo "Stopped"` + var localkubeStatusCommand = fmt.Sprintf("if [[ `systemctl` =~ -\\.mount ]] &>/dev/null; "+`then + sudo systemctl is-active localkube &>/dev/null && echo "Running" || echo "Stopped" +else + if ps $(cat %s) &>/dev/null; then + echo "Running" + else + echo "Stopped" + fi +fi +`, constants.LocalkubePIDPath) path := filepath.Join(constants.GetMinipath(), "tmp-cmd") - ioutil.WriteFile(filepath.Join(constants.GetMinipath(), "tmp-cmd"), []byte(command), os.FileMode(0644)) + ioutil.WriteFile(filepath.Join(constants.GetMinipath(), "tmp-cmd"), []byte(localkubeStatusCommand), os.FileMode(0644)) defer os.Remove(path) cmd := exec.Command("sudo", "/bin/sh", path) out, err := cmd.CombinedOutput()