-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcctl.sh
More file actions
executable file
·163 lines (142 loc) · 3.53 KB
/
mcctl.sh
File metadata and controls
executable file
·163 lines (142 loc) · 3.53 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
set -euo pipefail
# mcctl.sh - local helper to control the Minecraft world via Lambda Function URL
# and to connect to ephemeral EC2 via SSM Session Manager.
#
# Usage:
# ./mcctl.sh start <world>
# ./mcctl.sh stop <world>
# ./mcctl.sh snapshot <world>
# ./mcctl.sh status <world>
# ./mcctl.sh connect <world>
# ./mcctl.sh outputs
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TF_DIR="$ROOT_DIR/terraform"
need_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "Missing command: $1" >&2
exit 1
}
}
need_cmd terraform
need_cmd aws
need_cmd curl
# terraform output helper
_tf_out() {
local name="$1"
terraform -chdir="$TF_DIR" output -raw "$name" 2>/dev/null
}
function_url() {
local url
url="$(_tf_out function_url)"
if [[ -z "$url" ]]; then
echo "Failed to read terraform output: function_url" >&2
exit 1
fi
echo "$url"
}
ddb_table() {
local t
t="$(_tf_out dynamodb_table_name)"
if [[ -z "$t" ]]; then
echo "Failed to read terraform output: dynamodb_table_name" >&2
exit 1
fi
echo "$t"
}
world_required() {
local world="${1:-}"
if [[ -z "$world" ]]; then
echo "World name required." >&2
exit 1
fi
}
cmd_outputs() {
echo "function_url=$(_tf_out function_url)"
echo "config_bucket_name=$(_tf_out config_bucket_name || true)"
echo "snapshot_bucket_name=$(_tf_out snapshot_bucket_name || true)"
echo "dynamodb_table_name=$(_tf_out dynamodb_table_name)"
}
cmd_start() {
local world="$1"
local url
url="$(function_url)"
curl -sS -X POST "$url" \
-H "Content-Type: application/json" \
-d "{\"action\":\"start\",\"world\":\"$world\"}" | cat
}
cmd_stop() {
local world="$1"
local url
url="$(function_url)"
curl -sS -X POST "$url" \
-H "Content-Type: application/json" \
-d "{\"action\":\"stop\",\"world\":\"$world\"}" | cat
}
cmd_status() {
local world="$1"
local url
url="$(function_url)"
curl -sS "${url}?action=status&world=${world}" | cat
}
cmd_snapshot() {
local world="$1"
local url
url="$(function_url)"
curl -sS -X POST "$url" \
-H "Content-Type: application/json" \
-d "{\"action\":\"snapshot\",\"world\":\"$world\"}" | cat
}
cmd_connect() {
local world="$1"
local table
table="$(ddb_table)"
# Query DynamoDB for instance_id
local instance_id
instance_id=$(aws dynamodb get-item \
--table-name "$table" \
--key "{\"world\":{\"S\":\"$world\"}}" \
--query "Item.instance_id.S" \
--output text 2>/dev/null || true)
if [[ -z "$instance_id" || "$instance_id" == "None" ]]; then
echo "No instance_id found for world '$world' in DynamoDB table '$table'." >&2
echo "Try: ./mcctl.sh status $world" >&2
exit 1
fi
echo "Starting SSM session to instance: $instance_id" >&2
echo "(If this fails, ensure your IAM user has ssm:StartSession and Session Manager plugin is installed.)" >&2
aws ssm start-session --target "$instance_id"
}
main() {
local cmd="${1:-}"
shift || true
case "$cmd" in
outputs)
cmd_outputs
;;
start)
world_required "${1:-}"; cmd_start "$1"
;;
stop)
world_required "${1:-}"; cmd_stop "$1"
;;
status)
world_required "${1:-}"; cmd_status "$1"
;;
snapshot)
world_required "${1:-}"; cmd_snapshot "$1"
;;
connect)
world_required "${1:-}"; cmd_connect "$1"
;;
""|help|-h|--help)
sed -n '1,40p' "$0" | sed 's/^# \{0,1\}//'
;;
*)
echo "Unknown command: $cmd" >&2
echo "Run: ./mcctl.sh help" >&2
exit 1
;;
esac
}
main "$@"