#!/usr/bin/env bash # Run as root on a new conductor via the guide's Run Command helper. set -euo pipefail umask 077 slot=$1 bucket=$2 region=$3 [[ $slot == c1 || $slot == c2 ]] root=/srv/xyops mkdir -p "$root"/{conf,logs,data,build} cd "$root" aws s3 cp "s3://$bucket/bootstrap/settings.json" settings.json --region "$region" --only-show-errors out() { jq -er --arg k "$1" '.[$k]' settings.json; } secret() { aws secretsmanager get-secret-value --region "$region" --secret-id "$(out "$1")" --query SecretString --output text; } # Install Docker on the host, then build a thin layer with the pinned verifier. dnf -qy install docker systemctl enable --now docker aws s3 cp "s3://$bucket/bootstrap/verify-user.js" build/verify-user.js --only-show-errors base=$(out Image) cat > build/Dockerfile </dev/null # Copy the full configuration from this exact image, without booting a server. # Use a transient container so the image's anonymous data volume is removed. if [[ ! -f conf/config.json ]]; then docker run --rm --entrypoint tar xyops-aws -C /opt/xyops/sample_conf -cf - . | tar -C conf -xf - fi aws s3 cp "s3://$bucket/bootstrap/ca.crt" conf/cluster-ca.crt --only-show-errors aws s3 cp "s3://$bucket/bootstrap/$slot.crt" conf/tls.crt --only-show-errors aws s3 cp "s3://$bucket/bootstrap/$slot.key" conf/tls.key --only-show-errors curl -fsS --retry 3 "https://truststore.pki.rds.amazonaws.com/$region/$region-bundle.pem" -o conf/rds-ca.pem # Make credentials available only to this host's bootstrap process and config. # Strip the sample empty S3 credentials object so the SDK uses the instance role. secret KeySecret | tr -d '\n' > .cluster-key secret AppSecret | tr -d '\n' > .app-password trap 'rm -f .cluster-key .app-password' EXIT jq --arg site "$(out BrowserName)" --arg host "$(out DBHost)" --arg region "$region" --arg bucket "$bucket" \ --rawfile pass .app-password --rawfile ca conf/rds-ca.pem ' .base_app_url = ("https://" + $site) | .masters = "c1.xyops.internal,c2.xyops.internal" | .xysat_local = false | .default_user_privileges = {} | .multi.protocol = "wss:" | .multi.socket_opts.rejectUnauthorized = true | .satellite.config.port = 5523 | .satellite.config.secure = true | .satellite.config.socket_opts.rejectUnauthorized = true | .Storage.engine = "Hybrid" | .Storage.Hybrid = {docEngine:"Postgres",binaryEngine:"S3"} | .Storage.Postgres += {host:$host,database:"xyops",user:"xyops_app",password:$pass,ssl:{ca:$ca,rejectUnauthorized:true}} | .Storage.AWS = {region:$region} | .Storage.S3.params = {Bucket:$bucket} | .Storage.S3.keyPrefix = "xyops/" ' conf/config.json > conf/config.new mv conf/config.new conf/config.json jq -n --rawfile key .cluster-key '{secret_key:$key}' > conf/overrides.json # Cognito custom attributes are included in userInfo with the profile scope. # Use the immutable subject as the username, not a user-editable display name. jq -n --arg alb "$(out ALB)" --arg issuer "https://cognito-idp.$region.amazonaws.com/$(out Pool)" --arg client "$(out Client)" ' {enabled:true,hybrid:false,command:"node /opt/xyops/guide/verify-user.js",logout_url:"/alb-logout", whitelist:["10.77.0.0/24","10.77.1.0/24"],cleanup_username:false,cleanup_full_name:false, header_map:{username:"x-forwarded-user",email:"x-forwarded-email",full_name:"x-forwarded-name",groups:"x-forwarded-groups"}, group_role_map:{operator:["awsoperator"]},group_privilege_map:{setup:["admin"]},replace_roles:true,replace_privileges:true, jwt:{provider:"aws_alb",header:"x-amzn-oidc-data",claim_map:{username:"sub",email:"email",full_name:"name",groups:"custom:xyops_role"}, aws_alb:{alb_arn:$alb,issuer:$issuer,client_id:$client,grace_seconds:0}}} ' > conf/sso.json # The private listener is reachable from the ALB, peers and workers only. docker run -d --name xyops --init --restart unless-stopped --hostname "$slot.xyops.internal" \ -e NODE_EXTRA_CA_CERTS=/opt/xyops/conf/cluster-ca.crt \ -v "$root/conf:/opt/xyops/conf" -v "$root/logs:/opt/xyops/logs" -v "$root/data:/opt/xyops/data" \ -p 5523:5523 xyops-aws >/dev/null