Allszn commited on
Commit
a399cf4
·
verified ·
1 Parent(s): 0c1b2f9

Add launch script

Browse files
Files changed (1) hide show
  1. launch.sh +261 -0
launch.sh ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ #
3
+ # launch.sh - deploy the Windows GPU cloud-gaming host.
4
+ #
5
+ # Validates the launch kit, creates the S3 artifact bucket, deploys the
6
+ # CloudFormation stack, waits for the instance to register with Systems Manager,
7
+ # then pushes the Windows bootstrap script over SSM. No inbound RDP, ever.
8
+ #
9
+ # Quick start:
10
+ # export AWS_PROFILE=my-profile KEY_NAME=my-keypair
11
+ # ./launch.sh --instance-type g6.xlarge --volume-gib 600
12
+ #
13
+ set -euo pipefail
14
+
15
+ # ----------------------------------------------------------------- defaults
16
+ STACK_NAME="${STACK_NAME:-steam-gpu}"
17
+ REGION="${AWS_REGION:-${AWS_DEFAULT_REGION:-us-east-1}}"
18
+ INSTANCE_TYPE="${INSTANCE_TYPE:-g6.xlarge}"
19
+ GAME_VOLUME_GIB="${GAME_VOLUME_GIB:-600}"
20
+ ADMIN_CIDR="${ADMIN_CIDR:-}"
21
+ KEY_NAME="${KEY_NAME:-}"
22
+ ARTIFACT_BUCKET="${ARTIFACT_BUCKET:-}"
23
+ BOOTSTRAP="${BOOTSTRAP:-1}"
24
+ DRY_RUN="${DRY_RUN:-0}"
25
+
26
+ TEMPLATE="cloudformation/steam-gpu-host.yaml"
27
+ BOOTSTRAP_PS1="scripts/bootstrap.ps1"
28
+ DIAGNOSE_PS1="scripts/diagnose.ps1"
29
+ SSM_DOC="ssm/SteamGpu-Diagnose.json"
30
+
31
+ # ------------------------------------------------------------------- output
32
+ if [[ -t 1 ]]; then
33
+ C_B=$'\033[1;34m'; C_Y=$'\033[1;33m'; C_R=$'\033[1;31m'; C_G=$'\033[1;32m'; C_0=$'\033[0m'
34
+ else
35
+ C_B=''; C_Y=''; C_R=''; C_G=''; C_0=''
36
+ fi
37
+ log() { printf '%s[launch]%s %s\n' "$C_B" "$C_0" "$*"; }
38
+ ok() { printf '%s[ ok ]%s %s\n' "$C_G" "$C_0" "$*"; }
39
+ warn() { printf '%s[warn]%s %s\n' "$C_Y" "$C_0" "$*" >&2; }
40
+ die() { printf '%s[fail]%s %s\n' "$C_R" "$C_0" "$*" >&2; exit 1; }
41
+
42
+ usage() {
43
+ sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//'
44
+ cat <<'EOF'
45
+
46
+ Options:
47
+ --stack-name NAME CloudFormation stack name (default: steam-gpu)
48
+ --region REGION AWS region (default: us-east-1)
49
+ --instance-type TYPE g4dn.xlarge|g5.xlarge|g6.xlarge|... (default: g6.xlarge)
50
+ --key-name NAME EC2 key pair (decrypts the local Administrator password)
51
+ --admin-cidr CIDR CIDR allowed to reach DCV, e.g. 1.2.3.4/32
52
+ (default: your current public IP)
53
+ --volume-gib N Steam library EBS size in GiB (default: 600)
54
+ --artifact-bucket NAME Bucket for bootstrap scripts (default: created)
55
+ --no-bootstrap Create infrastructure only; skip the SSM bootstrap
56
+ --dry-run Validate and print the plan; change nothing
57
+ -h, --help This text
58
+
59
+ Every option is also settable as the uppercase environment variable of the same name:
60
+ STACK_NAME AWS_REGION INSTANCE_TYPE KEY_NAME ADMIN_CIDR GAME_VOLUME_GIB
61
+ ARTIFACT_BUCKET DRY_RUN
62
+ EOF
63
+ exit 0
64
+ }
65
+
66
+ # --------------------------------------------------------------------- args
67
+ while [[ $# -gt 0 ]]; do
68
+ case "$1" in
69
+ --stack-name) STACK_NAME="$2"; shift 2 ;;
70
+ --region) REGION="$2"; shift 2 ;;
71
+ --instance-type) INSTANCE_TYPE="$2"; shift 2 ;;
72
+ --key-name) KEY_NAME="$2"; shift 2 ;;
73
+ --admin-cidr) ADMIN_CIDR="$2"; shift 2 ;;
74
+ --volume-gib) GAME_VOLUME_GIB="$2"; shift 2 ;;
75
+ --artifact-bucket) ARTIFACT_BUCKET="$2"; shift 2 ;;
76
+ --no-bootstrap) BOOTSTRAP=0; shift ;;
77
+ --dry-run) DRY_RUN=1; shift ;;
78
+ -h|--help) usage ;;
79
+ *) die "unknown option: $1 (try --help)" ;;
80
+ esac
81
+ done
82
+
83
+ # ------------------------------------------------------------ preconditions
84
+ command -v aws >/dev/null 2>&1 || die "aws CLI not found"
85
+ command -v python3 >/dev/null 2>&1 || die "python3 not found (needed by the validator)"
86
+
87
+ [[ -f "$TEMPLATE" ]] || die "missing $TEMPLATE - run this from the launch kit root"
88
+
89
+ aws sts get-caller-identity >/dev/null 2>&1 \
90
+ || die "no usable AWS credentials. Set AWS_PROFILE or AWS_ACCESS_KEY_ID/SECRET."
91
+
92
+ ACCOUNT="$(aws sts get-caller-identity --query Account --output text)"
93
+ CALLER="$(aws sts get-caller-identity --query Arn --output text)"
94
+ log "account ${ACCOUNT} region ${REGION} stack ${STACK_NAME}"
95
+ log "caller ${CALLER}"
96
+
97
+ if [[ -z "$ADMIN_CIDR" ]]; then
98
+ MYIP="$(curl -fsS https://checkip.amazonaws.com | tr -d '\n')" \
99
+ || die "could not determine your public IP; pass --admin-cidr explicitly"
100
+ ADMIN_CIDR="${MYIP}/32"
101
+ warn "defaulted --admin-cidr to ${ADMIN_CIDR}. Your IP changes; re-run or update the rule later."
102
+ fi
103
+ [[ "$ADMIN_CIDR" =~ /32$ ]] || die "--admin-cidr must be a single host, e.g. 203.0.113.7/32"
104
+
105
+ # ---------------------------------------------------------------- step 1: validate
106
+ log "step 1/5 - validating the launch kit (no AWS calls)"
107
+ PY=python3
108
+ $PY -c 'import yaml' 2>/dev/null || {
109
+ warn "pyyaml missing; attempting a local install"
110
+ $PY -m pip install --quiet --user pyyaml || die "install pyyaml to validate: python3 -m pip install pyyaml"
111
+ }
112
+ if $PY scripts/validate.py --root . --pretty; then
113
+ ok "validation passed"
114
+ else
115
+ die "validation failed - fix the checks above before deploying"
116
+ fi
117
+
118
+ # ---------------------------------------------------------------- step 2: bucket
119
+ if [[ -z "$ARTIFACT_BUCKET" ]]; then
120
+ ARTIFACT_BUCKET="steam-gpu-launchkit-${ACCOUNT}-${REGION}"
121
+ log "step 2/5 - no --artifact-bucket given; will use ${ARTIFACT_BUCKET}"
122
+ else
123
+ log "step 2/5 - using artifact bucket ${ARTIFACT_BUCKET}"
124
+ fi
125
+
126
+ # ---------------------------------------------------------------- step 3: deploy
127
+ DEPLOY_CMD=(
128
+ aws cloudformation deploy
129
+ --stack-name "$STACK_NAME"
130
+ --template-file "$TEMPLATE"
131
+ --capabilities CAPABILITY_NAMED_IAM
132
+ --no-fail-on-empty-changeset
133
+ --region "$REGION"
134
+ --parameter-overrides
135
+ "InstanceType=${INSTANCE_TYPE}"
136
+ "KeyName=${KEY_NAME}"
137
+ "AdminCidr=${ADMIN_CIDR}"
138
+ "ArtifactBucket=${ARTIFACT_BUCKET}"
139
+ "GameVolumeSizeGiB=${GAME_VOLUME_GIB}"
140
+ )
141
+
142
+ log "step 3/5 - deploying the stack"
143
+ printf ' %s\n' "${DEPLOY_CMD[*]}"
144
+
145
+ if [[ "$DRY_RUN" == "1" ]]; then
146
+ warn "dry run: stopping before any AWS change."
147
+ ok "plan printed. Re-run without --dry-run to apply."
148
+ exit 0
149
+ fi
150
+
151
+ [[ -n "$KEY_NAME" ]] || die "KEY_NAME/--key-name is required for a real deploy"
152
+
153
+ # Create the artifact bucket first: the instance role's inline policy references
154
+ # it, and the bootstrap step needs to read the scripts from it.
155
+ if ! aws s3api head-bucket --bucket "$ARTIFACT_BUCKET" --region "$REGION" >/dev/null 2>&1; then
156
+ log "creating artifact bucket ${ARTIFACT_BUCKET}"
157
+ if [[ "$REGION" == "us-east-1" ]]; then
158
+ aws s3api create-bucket --bucket "$ARTIFACT_BUCKET" --region "$REGION" >/dev/null
159
+ else
160
+ aws s3api create-bucket --bucket "$ARTIFACT_BUCKET" --region "$REGION" \
161
+ --create-bucket-configuration "LocationConstraint=${REGION}" >/dev/null
162
+ fi
163
+ aws s3api put-public-access-block --bucket "$ARTIFACT_BUCKET" \
164
+ --public-access-block-configuration \
165
+ "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true" >/dev/null
166
+ aws s3api put-bucket-encryption --bucket "$ARTIFACT_BUCKET" \
167
+ --server-side-encryption-configuration \
168
+ '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"AES256"}}]}' >/dev/null
169
+ ok "bucket ready (public access blocked, SSE-S3 on)"
170
+ fi
171
+
172
+ aws s3 cp "$BOOTSTRAP_PS1" "s3://${ARTIFACT_BUCKET}/bootstrap.ps1" --region "$REGION" >/dev/null
173
+ aws s3 cp "$DIAGNOSE_PS1" "s3://${ARTIFACT_BUCKET}/diagnose.ps1" --region "$REGION" >/dev/null
174
+ ok "bootstrap.ps1 and diagnose.ps1 uploaded"
175
+
176
+ "${DEPLOY_CMD[@]}"
177
+ ok "stack ${STACK_NAME} deployed"
178
+
179
+ read -r INSTANCE_ID DCV_ENDPOINT ROLE_ARN <<<"$(aws cloudformation describe-stacks \
180
+ --stack-name "$STACK_NAME" --region "$REGION" \
181
+ --query 'Stacks[0].Outputs' --output text \
182
+ | awk '/InstanceId/{id=$2} /PublicDcvEndpoint/{ep=$2} /InstanceRoleArn/{rn=$2} END{print id, ep, rn}')"
183
+
184
+ log "instance ${INSTANCE_ID}"
185
+ log "dcv endpoint ${DCV_ENDPOINT}"
186
+ log "role arn ${ROLE_ARN}"
187
+
188
+ # ---------------------------------------------------------------- step 4: SSM
189
+ log "step 4/5 - waiting for the instance to register with Systems Manager"
190
+ DEADLINE=$(( SECONDS + 600 ))
191
+ while (( SECONDS < DEADLINE )); do
192
+ PING="$(aws ssm describe-instance-information \
193
+ --region "$REGION" \
194
+ --filters "Key=InstanceIds,Values=${INSTANCE_ID}" \
195
+ --query 'InstanceInformationList[0].PingStatus' --output text 2>/dev/null || echo None)"
196
+ if [[ "$PING" == "Online" ]]; then
197
+ ok "instance is Online in Systems Manager"
198
+ break
199
+ fi
200
+ printf ' ... %s (waiting)\n' "$PING"
201
+ sleep 20
202
+ done
203
+ [[ "$PING" == "Online" ]] || die "instance never registered with SSM; check the instance role and outbound 443"
204
+
205
+ # Register the diagnostic document so it is available from console, CLI or a schedule.
206
+ aws ssm create-document \
207
+ --name SteamGpu-Diagnose \
208
+ --document-type Command \
209
+ --document-format JSON \
210
+ --content "file://${SSM_DOC}" \
211
+ --region "$REGION" \
212
+ --tags Key=Project,Value=steam-gpu >/dev/null 2>&1 \
213
+ && ok "SSM document SteamGpu-Diagnose registered" \
214
+ || warn "SSM document already exists or could not be created; continuing"
215
+
216
+ # ---------------------------------------------------------------- step 5: bootstrap
217
+ if [[ "$BOOTSTRAP" == "1" ]]; then
218
+ log "step 5/5 - running the Windows bootstrap over SSM (10-20 minutes; the box reboots)"
219
+ CMD_ID="$(aws ssm send-command \
220
+ --region "$REGION" \
221
+ --instance-ids "$INSTANCE_ID" \
222
+ --document-name "AWS-RunPowerShellScript" \
223
+ --comment "steam-gpu bootstrap via launch.sh" \
224
+ --timeout-seconds 3600 \
225
+ --parameters "commands=[\"Set-ExecutionPolicy Bypass -Scope Process -Force; Read-S3Object -BucketName '${ARTIFACT_BUCKET}' -Key bootstrap.ps1 -File C:\\\\bootstrap.ps1; & C:\\\\bootstrap.ps1\"]" \
226
+ --query 'Command.CommandId' --output text)"
227
+ log "command id ${CMD_ID}"
228
+ log "follow along with:"
229
+ log " aws ssm list-command-invocations --command-id ${CMD_ID} --region ${REGION} --details \\"
230
+ log " --query 'CommandInvocations[0].CommandPlugins[0].Output' --output text"
231
+ else
232
+ log "step 5/5 - skipped (--no-bootstrap)"
233
+ warn "upload the DCV MSI and driver yourself, then run scripts/bootstrap.ps1 from the instance"
234
+ fi
235
+
236
+ printf '\n'
237
+ ok "launch complete"
238
+ cat <<EOF
239
+
240
+ Next steps
241
+ 1. Upload the Amazon DCV server MSI to the instance (C:\\DCV) and re-run the DCV
242
+ section of scripts/bootstrap.ps1, then reboot for the GPU driver.
243
+ 2. Confirm the licence:
244
+ nvidia-smi -q
245
+ expect: Product Name : NVIDIA Cloud Gaming / License Status : Licensed
246
+ 3. Open ${DCV_ENDPOINT} in a browser and connect to the console session.
247
+ 4. Configure Steam Offline Mode (see README): save credentials, clear the queue,
248
+ launch each game once online, then Steam > Go Offline.
249
+ 5. Run the health report:
250
+ aws ssm send-command --document-name SteamGpu-Diagnose \\
251
+ --instance-ids ${INSTANCE_ID} --region ${REGION}
252
+
253
+ RDP without exposing 3389
254
+ aws ssm start-session --target ${INSTANCE_ID} --region ${REGION} \\
255
+ --document-name AWS-StartPortForwardingSession \\
256
+ --parameters '{"portNumber":["3389"],"localPortNumber":["13389"]}'
257
+
258
+ Cost
259
+ ${INSTANCE_TYPE} runs until you stop it. Stop ${INSTANCE_ID} between sessions.
260
+
261
+ EOF