Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Please, update this variables with the relevant information
- R53_ZONE=<The zone id for your domain, like Z2O1E40J43Y93G>
- EMAIL=<Your email>
- PASS_PREFIX=<Some very secret prefix for generating passwords>
- # Getting the instance name tag
- TAG_NAME="Name"
- INSTANCE_ID="`wget -qO- http://instance-data/latest/meta-data/instance-id`"
- REGION="`wget -qO- http://instance-data/latest/meta-data/placement/availability-zone | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
- WORKSTATION_NAME="`aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=$TAG_NAME" --region $REGION --output=text | cut -f5`"
- # Updating the system
- apt update
- apt upgrade -y
- snap install core; snap refresh core
- # Configuring skel for tmux
- wget -P /etc/skel https://gist.githubusercontent.com/ciberado/601b0fad4d3eea3a086124aa68942830/raw/8154c6dfc5429aa7c0bf077fa36c8f259526a366/.tmux.conf
- wget -P /etc/skel https://gist.githubusercontent.com/ciberado/601b0fad4d3eea3a086124aa68942830/raw/8154c6dfc5429aa7c0bf077fa36c8f259526a366/.tmux.conf.local
- cat << EOF >> /etc/skel/.tmux.conf
- set -g status-interval 1
- set -g status-right '%H:%M:%S'
- EOF
- # Creating users
- groupadd students
- for i in $(seq 1 10); do
- userdel student${i}
- rm -fr /home/student${i}
- useradd -g students -s /bin/bash -m student${i}
- usermod -aG student${i}
- echo "student${i}:${PASS_PREFIX}1234"|chpasswd
- done
- # Updating R53 with the name of the workstation
- PUBLIC_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
- DOMAIN=aprender.cloud
- cat << EOF > dns.json
- {
- "Changes":[
- {
- "Action":"CREATE",
- "ResourceRecordSet":{
- "Name":"$WORKSTATION_NAME.$DOMAIN",
- "Type":"A",
- "TTL":300,
- "ResourceRecords":[
- {
- "Value":"$PUBLIC_IP"
- }
- ]
- }
- }
- ]
- }
- EOF
- apt install awscli -y
- aws route53 change-resource-record-sets \
- --hosted-zone-id $R53_ZONE \
- --change-batch file://dns.json
- sleep 30
- # Generating TLS certificates, thanks to Letsencrypt
- snap install --classic certbot
- ln -s /snap/bin/certbot /usr/bin/certbot
- certbot certonly -n --standalone --agree-tos --email $EMAIL -d $WORKSTATION_NAME.aprender.cloud
- # Installing ttyd
- CERT_PATH=/etc/letsencrypt/live/$WORKSTATION_NAME.$DOMAIN/
- wget https://github.com/tsl0922/ttyd/releases/download/1.6.3/ttyd.x86_64
- mv ttyd.x86_64 /usr/local/bin/ttyd
- chmod +x /usr/local/bin/ttyd
- # ttyd -p 443 --ssl --ssl-cert $CERT_PATH/fullchain.pem --ssl-key $CERT_PATH/privkey.pem --ssl-ca $CERT_PATH/chain.pem bash
- cat << EOF > /etc/systemd/system/ttyd.service
- [Unit]
- Description=TTYD
- After=syslog.target
- After=network.target
- [Service]
- ExecStart=/usr/local/bin/ttyd -p 443 --ssl --ssl-cert $CERT_PATH/fullchain.pem --ssl-key $CERT_PATH/privkey.pem --ssl-ca $CERT_PATH/chain.pem login
- Type=simple
- Restart=always
- User=root
- Group=root
- [Install]
- WantedBy=multi-user.target
- EOF
- service ttyd start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement