-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
73 lines (69 loc) · 1.76 KB
/
cli.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
package main
import (
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"golang.org/x/crypto/ssh"
)
var sshConfig = &ssh.ClientConfig{
User: "ec2-user",
Auth: []ssh.AuthMethod{
publicKey("myFirstKey"),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
func main() {
const (
tagKey = "Name"
tagValue = "minecraft"
)
command := os.Args[1]
var ipAddress string
sess, err := session.NewSession(&aws.Config{
Region: aws.String("eu-west-1"),
})
if err != nil {
panic(err)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
switch command {
case "help":
fmt.Println("The commands are:\n",
"\tstart-server spawns server if needed and starts up\n",
"\tsave-world downloads the minecraft world and saves in current folder\n",
"\tterminate-server stops and destroys the server")
case "start":
ipAddress = start(sess, tagKey, tagValue)
fmt.Printf("ip: %s\n", ipAddress)
case "stop":
instances := fetchRunningInstancesByTag(sess, tagKey, tagValue)
if len(instances) == 0 {
fmt.Println("No minecraft instances to shut down")
os.Exit(1)
} else if len(instances) > 1 {
fmt.Println("More than one minecraft instance running. Check AWS")
os.Exit(1)
}
fmt.Println(*instances[0].InstanceId)
fmt.Println(len(instances))
stopMcServer()
time.Sleep(5 * time.Second) // give 5 sec for server to save world
err := downloadWorld()
if err != nil {
fmt.Println("Could not download world, canceling instance termination")
os.Exit(1)
}
//stop(sess, tagKey, tagValue)
case "download":
downloadWorld()
case "fetch":
instances := fetchRunningInstancesByTag(sess, tagKey, tagValue)
fmt.Println(len(instances))
default:
fmt.Printf("'%s' is not a command, type 'help'\n", command)
}
}