#cloud-config
repo_update: true
repo_upgrade: critical

packages:
  - screen
  - htop

users:
  - default
  - name: engine
    sudo: ALL=(ALL) NOPASSWD:ALL
    gecos: Web Performance Load Engine

runcmd:
 # === Standard Load Engine setup (same as http-engine.cloud-init) ===
 - wget --tries 3 '$INSTALLER_URL' --output-document /home/engine/load-engine.tar.gz
 - mkdir -p /home/engine/.ssh
 - cp /home/ec2-user/.ssh/authorized_keys /home/engine/.ssh/authorized_keys
 - tar -C /home/engine/ -xzf /home/engine/load-engine.tar.gz --strip 1
 #
 # === Chrome installation (real browser engine addition) ===
 - dnf install -y dnf-plugins-core
 - dnf config-manager --add-repo https://dl.google.com/linux/chrome/rpm/stable/x86_64
 - rpm --import https://dl.google.com/linux/linux_signing_key.pub
 - dnf install -y google-chrome-stable
 - echo 'exclude=google-chrome*' >> /etc/dnf/dnf.conf
 - [sh, -c, 'google-chrome-stable --headless --no-sandbox --disable-dev-shm-usage --dump-dom about:blank > /dev/null 2>&1 || echo WARNING: Chrome headless validation failed']
 #
 # === Engine startup (with Chrome cleanup on restart) ===
 - printf '#!/bin/sh\nkillall -9 chrome chromedriver 2>/dev/null; killall java 2>/dev/null; cd; screen -dmS cloudengine ./Start --angel\n' >> /home/engine/cloud-restart.sh
 - chown engine:engine -R /home/engine
 - chmod u+rw -R /home/engine
 - chmod go-wx -R /home/engine
 - chmod go-rwx -R /home/engine/.ssh
 - chmod u+x /home/engine/cloud-restart.sh
 - chmod u+x /home/engine/bin/*.sh
 - /home/engine/bin/set-autoshutdown.sh
 - printf 'engine hard nofile 200000\n'                       >> /etc/security/limits.conf
 - printf 'engine soft nofile 200000\n'                       >> /etc/security/limits.conf
 - printf 'engine hard nproc unlimited\n'                     >> /etc/security/limits.conf
 - printf 'engine soft nproc unlimited\n'                     >> /etc/security/limits.conf
 - printf 'net.ipv4.ip_local_port_range = 1025 65535\n'       >> /etc/sysctl.conf
 - printf 'su -l engine -c /home/engine/cloud-restart.sh &\n' >> /etc/rc.d/rc.local
 - su -l engine -c /home/engine/cloud-restart.sh
