user-data.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/bash -x
  2. exec > >(tee /var/log/user-data.log | logger -t user-data -s 2>/dev/console) 2>&1
  3. ${pre_install}
  4. # Install AWS CLI
  5. apt-get update
  6. DEBIAN_FRONTEND=noninteractive apt-get install -y \
  7. awscli \
  8. jq \
  9. curl \
  10. wget \
  11. git \
  12. uidmap \
  13. build-essential \
  14. unzip
  15. USER_NAME=runners
  16. useradd -m -s /bin/bash $USER_NAME
  17. USER_ID=$(id -ru $USER_NAME)
  18. # install and configure cloudwatch logging agent
  19. wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
  20. dpkg -i -E ./amazon-cloudwatch-agent.deb
  21. amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c ssm:${ssm_key_cloudwatch_agent_config}
  22. # configure systemd for running service in users accounts
  23. cat >/etc/systemd/user@UID.service <<-EOF
  24. [Unit]
  25. Description=User Manager for UID %i
  26. After=user-runtime-dir@%i.service
  27. Wants=user-runtime-dir@%i.service
  28. [Service]
  29. LimitNOFILE=infinity
  30. LimitNPROC=infinity
  31. User=%i
  32. PAMName=systemd-user
  33. Type=notify
  34. [Install]
  35. WantedBy=default.target
  36. EOF
  37. echo export XDG_RUNTIME_DIR=/run/user/$USER_ID >>/home/$USER_NAME/.profile
  38. systemctl daemon-reload
  39. systemctl enable user@UID.service
  40. systemctl start user@UID.service
  41. curl -fsSL https://get.docker.com/rootless >>/opt/rootless.sh && chmod 755 /opt/rootless.sh
  42. su -l $USER_NAME -c /opt/rootless.sh
  43. echo export DOCKER_HOST=unix:///run/user/$USER_ID/docker.sock >>/home/$USER_NAME/.profile
  44. echo export PATH=/home/$USER_NAME/bin:$PATH >>/home/$USER_NAME/.profile
  45. # Run docker service by default
  46. loginctl enable-linger $USER_NAME
  47. su -l $USER_NAME -c "systemctl --user enable docker"
  48. ${install_runner}
  49. # config runner for rootless docker
  50. cd /opt/actions-runner/
  51. echo DOCKER_HOST=unix:///run/user/$USER_ID/docker.sock >>.env
  52. echo PATH=/home/$USER_NAME/bin:$PATH >>.env
  53. ${post_install}
  54. cd /opt/actions-runner
  55. ${start_runner}