This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
forked from machine-drivers/docker-machine-driver-hyperkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (66 loc) · 2.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"path/filepath"
"syscall"
"github.com/docker/machine/libmachine/drivers/plugin"
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
"github.com/rancher-sandbox/docker-machine-driver-hyperkit/cmd"
"github.com/rancher-sandbox/docker-machine-driver-hyperkit/cmd/priv"
"github.com/rancher-sandbox/docker-machine-driver-hyperkit/pkg/hyperkit"
)
func main() {
if syscall.Geteuid() != 0 {
executable, err := os.Executable()
if err != nil {
cmd.Abort("Cannot determine name of executable: %v", err)
}
permErr := "%s needs to run with elevated permissions. " +
"Please run the following command, then try again: " +
"sudo chown root:wheel %s && sudo chmod u+s %s"
cmd.Abort(permErr, filepath.Base(executable), executable, executable)
}
if len(os.Args) > 1 {
// All of the privileged commands will call os.Exit() and never return
switch os.Args[1] {
case "hyperkit":
priv.Hyperkit()
case "nfs-exports":
priv.NFSExports()
case "uuid-to-mac-addr":
priv.UUIDtoMacAddr()
}
}
// Drop root privileges before running driver mode, or commands via cobra
if err := syscall.Setuid(syscall.Getuid()); err != nil {
cmd.Abort("Cannot drop privileges: %v", err)
}
if os.Getenv(localbinary.PluginEnvKey) == localbinary.PluginEnvVal {
plugin.RegisterDriver(hyperkit.NewDriver("", ""))
return
}
// Add the directory name of the current executable to the front of the PATH to load
// the driver from the same directory (the current executable may be a symlink to the
// driver, living in a different directory).
err := os.Setenv("PATH", os.ExpandEnv(fmt.Sprintf("%s:$PATH", cmd.DriverDir())))
if err != nil {
cmd.Abort("Cannot update PATH: %v", err)
}
err = cmd.Execute()
if err != nil {
cmd.Abort("Command failed: %v", err)
}
}