Skip to content

Commit

Permalink
Removed systemd dependency from minikube, updated none driver to refl…
Browse files Browse the repository at this point in the history
…ect this
  • Loading branch information
aaron-prindle committed Jun 15, 2017
1 parent 09f683b commit a4c4827
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
5 changes: 1 addition & 4 deletions pkg/minikube/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
57 changes: 54 additions & 3 deletions pkg/minikube/cluster/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,27 +53,37 @@ 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) {
localkubeStartCommand, err := GenLocalkubeStartCmd(kubernetesConfig)
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
}
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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions pkg/minikube/machine/drivers/none/none.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package none

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a4c4827

Please sign in to comment.