forked from aws/ec2-macos-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2-macos-init.go
120 lines (104 loc) · 2.7 KB
/
ec2-macos-init.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"runtime"
"strconv"
"github.com/aws/ec2-macos-init/lib/ec2macosinit"
)
const (
baseDir = "/usr/local/aws/ec2-macos-init"
configFile = "init.toml"
instanceHistoryDir = "instances"
historyFileName = "history.json"
loggingTag = "ec2-macOS-init"
gitHubLink = "https://github.com/aws/ec2-macos-init"
)
// Build time variables
var CommitDate string
var Version string
// printUsage prints the help text when invalid arguments are provided.
func printUsage() {
fmt.Println("Usage: ec2-macos-init <command> <arguments>")
fmt.Println("Commands are:")
fmt.Println(" run - Run init using configuration located in " + path.Join(baseDir, configFile))
fmt.Println(" clean - Remove instance history from disk")
fmt.Println(" version - Print version information")
fmt.Println("For more help: ec2-macos-init <command> -h")
}
// printVersion prints the output for the version command.
func printVersion() {
fmt.Printf("\nEC2 macOS Init\n"+
"Version: %s [%s]\n"+
"%s\n"+
"Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n\n",
Version, CommitDate, gitHubLink,
)
}
// checkRootPermissions checks to see if the init application is being run as root.
func checkRootPermissions() (root bool, err error) {
cmd := exec.Command("id", "-u")
output, err := cmd.Output()
if err != nil {
return false, err
}
// Convert for comparison
i, err := strconv.Atoi(string(output[:len(output)-1]))
if err != nil {
return false, err
}
// 0 = root
// 501 = non-root user
if i == 0 {
return true, nil
}
return false, nil
}
func main() {
// Set up logging
logger, err := ec2macosinit.NewLogger(loggingTag, true, true)
if err != nil {
log.Fatalf("Unable to start logging: %s", err)
}
// Check runtime OS
if !(runtime.GOOS == "darwin") {
logger.Fatal(1, "Can only be run from macOS!")
}
// Check that this is being run by a user with root permissions
root, err := checkRootPermissions()
if err != nil {
logger.Fatalf(71, "Error while checking root permissions: %s", err)
}
if !root {
logger.Fatal(64, "Must be run with root permissions!")
}
// Check for no command
if len(os.Args) < 2 {
logger.Info("Must provide a command!")
printUsage()
os.Exit(2)
}
// Setup InitConfig
config := &ec2macosinit.InitConfig{
HistoryPath: path.Join(baseDir, instanceHistoryDir),
HistoryFilename: historyFileName,
Log: logger,
}
// Command switch
switch command := os.Args[1]; command {
case "run":
run(config)
case "clean":
clean(config)
case "version":
printVersion()
os.Exit(0)
default:
logger.Errorf("%s is not a valid command", command)
printUsage()
os.Exit(2)
}
}