acantril / learn-cantrill-io-labs

Standard and Advanced Demos for learn.cantrill.io courses

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

STAGE 2B - Add Userdata - ISSUE

nicetry001 opened this issue · comments

Hello,

I encountered error while i just followed the demo it seems that the user-data is failing whenever i launch an instance from template.

Errno 2] No such file or directory: '/var/cache/dnf/amazonlinux-39b85c012216ab5c/packages/krb5-devel-1.21-3.amzn2023.0.4.x86_64.rpm'

when i tried to perform a -x only, it gets me this error regardless,
what i saw was this.

https://repost.aws/questions/QU_tj7NQl6ReKoG53zzEqYOw/amazon-linux-2023-issue-with-installing-packages-with-cloud-init

" I am attempting to install packages via cloud-init in an Amazon Linux 2023 instance. Sometimes it works, sometimes it fails with: "RPM: error: can't create transaction lock on /var/lib/rpm/.rpm.lock (Resource temporarily unavailable)" This issue occurs when attempting pkg installs in both a cloudconfig and a script. The most annoying part is that it fails most of the time but not always. And it doesn't always fail in the same place. Sometimes it get through cloudconfig pkgs and fails in the pkgs being installed by the script, sometimes it doesn't get that far.

""

but i didn't get far.

after speaking with chatgpt dozen of times,

#!/bin/bash -xe
sleep 120

DBPassword=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/DBPassword --with-decryption --query Parameters[0].Value)
DBPassword=$(echo "$DBPassword" | sed -e 's/^"//' -e 's/"$//')

DBRootPassword=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/DBRootPassword --with-decryption --query Parameters[0].Value)
DBRootPassword=$(echo "$DBRootPassword" | sed -e 's/^"//' -e 's/"$//')

DBUser=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/DBUser --query Parameters[0].Value)
DBUser=$(echo "$DBUser" | sed -e 's/^"//' -e 's/"$//')

DBName=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/DBName --query Parameters[0].Value)
DBName=$(echo "$DBName" | sed -e 's/^"//' -e 's/"$//')

DBEndpoint=$(aws ssm get-parameters --region us-east-1 --names /A4L/Wordpress/DBEndpoint --query Parameters[0].Value)
DBEndpoint=$(echo "$DBEndpoint" | sed -e 's/^"//' -e 's/"$//')

Function to check the exit status and retry if necessary

function check_exit_status {
if [ $1 -ne 0 ]; then
echo "Error occurred. Retrying..."
return 1
fi
return 0
}

Loop until all commands succeed or manual intervention

while true; do
# Update package manager
dnf -y update
check_exit_status $?
if [ $? -ne 0 ]; then
continue
fi

# Install required packages
dnf install wget php-mysqlnd httpd php-fpm php-mysqli mariadb105-server php-json php php-devel stress -y
check_exit_status $?
if [ $? -ne 0 ]; then
    continue
fi

# Start and enable services
systemctl enable httpd
systemctl enable mariadb
systemctl start httpd
systemctl start mariadb
check_exit_status $?
if [ $? -ne 0 ]; then
    continue
fi

# Set MySQL root password
mysqladmin -u root password "$DBRootPassword"
check_exit_status $?
if [ $? -ne 0 ]; then
    continue
fi

# Download and configure WordPress
wget http://wordpress.org/latest.tar.gz -P /var/www/html
cd /var/www/html
tar -zxvf latest.tar.gz
cp -rvf wordpress/* .
rm -R wordpress
rm latest.tar.gz
check_exit_status $?
if [ $? -ne 0 ]; then
    continue
fi

# Configure WordPress database connection
cp ./wp-config-sample.php ./wp-config.php
sed -i "s/'database_name_here'/'$DBName'/g" wp-config.php
sed -i "s/'username_here'/'$DBUser'/g" wp-config.php
sed -i "s/'password_here'/'$DBPassword'/g" wp-config.php
sed -i "s/'localhost'/'$DBEndpoint'/g" wp-config.php
check_exit_status $?
if [ $? -ne 0 ]; then
    continue
fi

# Set permissions
usermod -a -G apache ec2-user
chown -R ec2-user:apache /var/www
chmod 2775 /var/www
find /var/www -type d -exec chmod 2775 {} \;
find /var/www -type f -exec chmod 0664 {} \;
check_exit_status $?
if [ $? -ne 0 ]; then
    continue
fi

# Setup MySQL database
echo "CREATE DATABASE $DBName;" >> /tmp/db.setup
echo "CREATE USER '$DBUser'@'localhost' IDENTIFIED BY '$DBPassword';" >> /tmp/db.setup
echo "GRANT ALL ON $DBName.* TO '$DBUser'@'localhost';" >> /tmp/db.setup
echo "FLUSH PRIVILEGES;" >> /tmp/db.setup
mysql -u root --password="$DBRootPassword" < /tmp/db.setup
rm /tmp/db.setup
check_exit_status $?
if [ $? -ne 0 ]; then
    continue
fi

echo "All tasks succeeded."
break

done

perform a sleep 120 so the cloud-init finish all it's setup before running the script,

is this ok?