File size: 10,957 Bytes
a399cf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env bash
#
# launch.sh - deploy the Windows GPU cloud-gaming host.
#
# Validates the launch kit, creates the S3 artifact bucket, deploys the
# CloudFormation stack, waits for the instance to register with Systems Manager,
# then pushes the Windows bootstrap script over SSM. No inbound RDP, ever.
#
# Quick start:
#   export AWS_PROFILE=my-profile KEY_NAME=my-keypair
#   ./launch.sh --instance-type g6.xlarge --volume-gib 600
#
set -euo pipefail

# ----------------------------------------------------------------- defaults
STACK_NAME="${STACK_NAME:-steam-gpu}"
REGION="${AWS_REGION:-${AWS_DEFAULT_REGION:-us-east-1}}"
INSTANCE_TYPE="${INSTANCE_TYPE:-g6.xlarge}"
GAME_VOLUME_GIB="${GAME_VOLUME_GIB:-600}"
ADMIN_CIDR="${ADMIN_CIDR:-}"
KEY_NAME="${KEY_NAME:-}"
ARTIFACT_BUCKET="${ARTIFACT_BUCKET:-}"
BOOTSTRAP="${BOOTSTRAP:-1}"
DRY_RUN="${DRY_RUN:-0}"

TEMPLATE="cloudformation/steam-gpu-host.yaml"
BOOTSTRAP_PS1="scripts/bootstrap.ps1"
DIAGNOSE_PS1="scripts/diagnose.ps1"
SSM_DOC="ssm/SteamGpu-Diagnose.json"

# ------------------------------------------------------------------- output
if [[ -t 1 ]]; then
  C_B=$'\033[1;34m'; C_Y=$'\033[1;33m'; C_R=$'\033[1;31m'; C_G=$'\033[1;32m'; C_0=$'\033[0m'
else
  C_B=''; C_Y=''; C_R=''; C_G=''; C_0=''
fi
log()  { printf '%s[launch]%s %s\n' "$C_B" "$C_0" "$*"; }
ok()   { printf '%s[ ok ]%s %s\n' "$C_G" "$C_0" "$*"; }
warn() { printf '%s[warn]%s %s\n' "$C_Y" "$C_0" "$*" >&2; }
die()  { printf '%s[fail]%s %s\n' "$C_R" "$C_0" "$*" >&2; exit 1; }

usage() {
  sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//'
  cat <<'EOF'

Options:
  --stack-name NAME        CloudFormation stack name            (default: steam-gpu)
  --region REGION          AWS region                           (default: us-east-1)
  --instance-type TYPE     g4dn.xlarge|g5.xlarge|g6.xlarge|...   (default: g6.xlarge)
  --key-name NAME          EC2 key pair (decrypts the local Administrator password)
  --admin-cidr CIDR        CIDR allowed to reach DCV, e.g. 1.2.3.4/32
                           (default: your current public IP)
  --volume-gib N           Steam library EBS size in GiB        (default: 600)
  --artifact-bucket NAME   Bucket for bootstrap scripts         (default: created)
  --no-bootstrap           Create infrastructure only; skip the SSM bootstrap
  --dry-run                Validate and print the plan; change nothing
  -h, --help               This text

Every option is also settable as the uppercase environment variable of the same name:
STACK_NAME AWS_REGION INSTANCE_TYPE KEY_NAME ADMIN_CIDR GAME_VOLUME_GIB
ARTIFACT_BUCKET DRY_RUN
EOF
  exit 0
}

# --------------------------------------------------------------------- args
while [[ $# -gt 0 ]]; do
  case "$1" in
    --stack-name)      STACK_NAME="$2"; shift 2 ;;
    --region)          REGION="$2"; shift 2 ;;
    --instance-type)   INSTANCE_TYPE="$2"; shift 2 ;;
    --key-name)        KEY_NAME="$2"; shift 2 ;;
    --admin-cidr)      ADMIN_CIDR="$2"; shift 2 ;;
    --volume-gib)      GAME_VOLUME_GIB="$2"; shift 2 ;;
    --artifact-bucket) ARTIFACT_BUCKET="$2"; shift 2 ;;
    --no-bootstrap)    BOOTSTRAP=0; shift ;;
    --dry-run)         DRY_RUN=1; shift ;;
    -h|--help)         usage ;;
    *)                 die "unknown option: $1 (try --help)" ;;
  esac
done

# ------------------------------------------------------------ preconditions
command -v aws     >/dev/null 2>&1 || die "aws CLI not found"
command -v python3 >/dev/null 2>&1 || die "python3 not found (needed by the validator)"

[[ -f "$TEMPLATE" ]] || die "missing $TEMPLATE - run this from the launch kit root"

aws sts get-caller-identity >/dev/null 2>&1 \
  || die "no usable AWS credentials. Set AWS_PROFILE or AWS_ACCESS_KEY_ID/SECRET."

ACCOUNT="$(aws sts get-caller-identity --query Account --output text)"
CALLER="$(aws sts get-caller-identity --query Arn --output text)"
log "account ${ACCOUNT}  region ${REGION}  stack ${STACK_NAME}"
log "caller  ${CALLER}"

if [[ -z "$ADMIN_CIDR" ]]; then
  MYIP="$(curl -fsS https://checkip.amazonaws.com | tr -d '\n')" \
    || die "could not determine your public IP; pass --admin-cidr explicitly"
  ADMIN_CIDR="${MYIP}/32"
  warn "defaulted --admin-cidr to ${ADMIN_CIDR}. Your IP changes; re-run or update the rule later."
fi
[[ "$ADMIN_CIDR" =~ /32$ ]] || die "--admin-cidr must be a single host, e.g. 203.0.113.7/32"

# ---------------------------------------------------------------- step 1: validate
log "step 1/5 - validating the launch kit (no AWS calls)"
PY=python3
$PY -c 'import yaml' 2>/dev/null || {
  warn "pyyaml missing; attempting a local install"
  $PY -m pip install --quiet --user pyyaml || die "install pyyaml to validate: python3 -m pip install pyyaml"
}
if $PY scripts/validate.py --root . --pretty; then
  ok "validation passed"
else
  die "validation failed - fix the checks above before deploying"
fi

# ---------------------------------------------------------------- step 2: bucket
if [[ -z "$ARTIFACT_BUCKET" ]]; then
  ARTIFACT_BUCKET="steam-gpu-launchkit-${ACCOUNT}-${REGION}"
  log "step 2/5 - no --artifact-bucket given; will use ${ARTIFACT_BUCKET}"
else
  log "step 2/5 - using artifact bucket ${ARTIFACT_BUCKET}"
fi

# ---------------------------------------------------------------- step 3: deploy
DEPLOY_CMD=(
  aws cloudformation deploy
  --stack-name "$STACK_NAME"
  --template-file "$TEMPLATE"
  --capabilities CAPABILITY_NAMED_IAM
  --no-fail-on-empty-changeset
  --region "$REGION"
  --parameter-overrides
      "InstanceType=${INSTANCE_TYPE}"
      "KeyName=${KEY_NAME}"
      "AdminCidr=${ADMIN_CIDR}"
      "ArtifactBucket=${ARTIFACT_BUCKET}"
      "GameVolumeSizeGiB=${GAME_VOLUME_GIB}"
)

log "step 3/5 - deploying the stack"
printf '  %s\n' "${DEPLOY_CMD[*]}"

if [[ "$DRY_RUN" == "1" ]]; then
  warn "dry run: stopping before any AWS change."
  ok "plan printed. Re-run without --dry-run to apply."
  exit 0
fi

[[ -n "$KEY_NAME" ]] || die "KEY_NAME/--key-name is required for a real deploy"

# Create the artifact bucket first: the instance role's inline policy references
# it, and the bootstrap step needs to read the scripts from it.
if ! aws s3api head-bucket --bucket "$ARTIFACT_BUCKET" --region "$REGION" >/dev/null 2>&1; then
  log "creating artifact bucket ${ARTIFACT_BUCKET}"
  if [[ "$REGION" == "us-east-1" ]]; then
    aws s3api create-bucket --bucket "$ARTIFACT_BUCKET" --region "$REGION" >/dev/null
  else
    aws s3api create-bucket --bucket "$ARTIFACT_BUCKET" --region "$REGION" \
      --create-bucket-configuration "LocationConstraint=${REGION}" >/dev/null
  fi
  aws s3api put-public-access-block --bucket "$ARTIFACT_BUCKET" \
    --public-access-block-configuration \
    "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true" >/dev/null
  aws s3api put-bucket-encryption --bucket "$ARTIFACT_BUCKET" \
    --server-side-encryption-configuration \
    '{"Rules":[{"ApplyServerSideEncryptionByDefault":{"SSEAlgorithm":"AES256"}}]}' >/dev/null
  ok "bucket ready (public access blocked, SSE-S3 on)"
fi

aws s3 cp "$BOOTSTRAP_PS1" "s3://${ARTIFACT_BUCKET}/bootstrap.ps1" --region "$REGION" >/dev/null
aws s3 cp "$DIAGNOSE_PS1"  "s3://${ARTIFACT_BUCKET}/diagnose.ps1"  --region "$REGION" >/dev/null
ok "bootstrap.ps1 and diagnose.ps1 uploaded"

"${DEPLOY_CMD[@]}"
ok "stack ${STACK_NAME} deployed"

read -r INSTANCE_ID DCV_ENDPOINT ROLE_ARN <<<"$(aws cloudformation describe-stacks \
  --stack-name "$STACK_NAME" --region "$REGION" \
  --query 'Stacks[0].Outputs' --output text \
  | awk '/InstanceId/{id=$2} /PublicDcvEndpoint/{ep=$2} /InstanceRoleArn/{rn=$2} END{print id, ep, rn}')"

log "instance     ${INSTANCE_ID}"
log "dcv endpoint ${DCV_ENDPOINT}"
log "role arn     ${ROLE_ARN}"

# ---------------------------------------------------------------- step 4: SSM
log "step 4/5 - waiting for the instance to register with Systems Manager"
DEADLINE=$(( SECONDS + 600 ))
while (( SECONDS < DEADLINE )); do
  PING="$(aws ssm describe-instance-information \
    --region "$REGION" \
    --filters "Key=InstanceIds,Values=${INSTANCE_ID}" \
    --query 'InstanceInformationList[0].PingStatus' --output text 2>/dev/null || echo None)"
  if [[ "$PING" == "Online" ]]; then
    ok "instance is Online in Systems Manager"
    break
  fi
  printf '  ... %s (waiting)\n' "$PING"
  sleep 20
done
[[ "$PING" == "Online" ]] || die "instance never registered with SSM; check the instance role and outbound 443"

# Register the diagnostic document so it is available from console, CLI or a schedule.
aws ssm create-document \
  --name SteamGpu-Diagnose \
  --document-type Command \
  --document-format JSON \
  --content "file://${SSM_DOC}" \
  --region "$REGION" \
  --tags Key=Project,Value=steam-gpu >/dev/null 2>&1 \
  && ok "SSM document SteamGpu-Diagnose registered" \
  || warn "SSM document already exists or could not be created; continuing"

# ---------------------------------------------------------------- step 5: bootstrap
if [[ "$BOOTSTRAP" == "1" ]]; then
  log "step 5/5 - running the Windows bootstrap over SSM (10-20 minutes; the box reboots)"
  CMD_ID="$(aws ssm send-command \
    --region "$REGION" \
    --instance-ids "$INSTANCE_ID" \
    --document-name "AWS-RunPowerShellScript" \
    --comment "steam-gpu bootstrap via launch.sh" \
    --timeout-seconds 3600 \
    --parameters "commands=[\"Set-ExecutionPolicy Bypass -Scope Process -Force; Read-S3Object -BucketName '${ARTIFACT_BUCKET}' -Key bootstrap.ps1 -File C:\\\\bootstrap.ps1; & C:\\\\bootstrap.ps1\"]" \
    --query 'Command.CommandId' --output text)"
  log "command id ${CMD_ID}"
  log "follow along with:"
  log "  aws ssm list-command-invocations --command-id ${CMD_ID} --region ${REGION} --details \\"
  log "    --query 'CommandInvocations[0].CommandPlugins[0].Output' --output text"
else
  log "step 5/5 - skipped (--no-bootstrap)"
  warn "upload the DCV MSI and driver yourself, then run scripts/bootstrap.ps1 from the instance"
fi

printf '\n'
ok "launch complete"
cat <<EOF

Next steps
  1. Upload the Amazon DCV server MSI to the instance (C:\\DCV) and re-run the DCV
     section of scripts/bootstrap.ps1, then reboot for the GPU driver.
  2. Confirm the licence:
       nvidia-smi -q
     expect: Product Name : NVIDIA Cloud Gaming / License Status : Licensed
  3. Open ${DCV_ENDPOINT} in a browser and connect to the console session.
  4. Configure Steam Offline Mode (see README): save credentials, clear the queue,
     launch each game once online, then Steam > Go Offline.
  5. Run the health report:
       aws ssm send-command --document-name SteamGpu-Diagnose \\
         --instance-ids ${INSTANCE_ID} --region ${REGION}

RDP without exposing 3389
  aws ssm start-session --target ${INSTANCE_ID} --region ${REGION} \\
    --document-name AWS-StartPortForwardingSession \\
    --parameters '{"portNumber":["3389"],"localPortNumber":["13389"]}'

Cost
  ${INSTANCE_TYPE} runs until you stop it. Stop ${INSTANCE_ID} between sessions.

EOF