Compare commits

..

2 Commits

Author SHA1 Message Date
Anthony 5bb722a83d Added Uninstall 2023-10-30 21:21:51 +08:00
Anthony f38ef098fa Added debug logs 2023-10-30 21:13:03 +08:00
3 changed files with 28 additions and 26 deletions

View File

@ -1,33 +1,30 @@
#!/bin/bash
# Generate random username and password
echo "Script started" >> /home/jelastic/add-sftp-user-addon/script_output.log
USERNAME="user$(shuf -i 10000-99999 -n 1)"
PASSWORD=$(openssl rand -base64 12)
# Set the user's home directory within the ROOT directory
USER_HOME="/var/www/webroot/ROOT/$USERNAME"
ROOT_DIRECTORY="/var/www/webroot/ROOT"
# Check if user already exists
if id "$USERNAME" &>/dev/null; then
echo "User $USERNAME already exists."
echo "User $USERNAME already exists." >> /home/jelastic/add-sftp-user-addon/script_output.log
exit 1
fi
# Create user with the specified home directory
useradd -m -d $USER_HOME $USERNAME
# Create user
useradd -m -d $ROOT_DIRECTORY $USERNAME
if [ $? -ne 0 ]; then
echo "Failed to create user $USERNAME."
exit 1
echo "Failed to create user $USERNAME." >> /home/jelastic/add-sftp-user-addon/script_output.log
fi
echo "$USERNAME:$PASSWORD" | chpasswd
# Set user's group to both litespeed and root
usermod -aG litespeed,root $USERNAME
# Adjust permissions for the user to write to their home directory
mkdir -p $USER_HOME
chown $USERNAME:root $USER_HOME
chmod 775 $USER_HOME
# Adjust permissions for the user to write to the ROOT_DIRECTORY
chown $USERNAME:root $ROOT_DIRECTORY
chmod 775 $ROOT_DIRECTORY
# Get the hostname (if needed later in the script)
HOSTNAME=$(hostname -f)
echo "Script completed for user $USERNAME with hostname $HOSTNAME" >> /home/jelastic/add-sftp-user-addon/script_output.log

View File

@ -1,12 +0,0 @@
#!/bin/bash
# Root User
sudo su
# Update sshd_config
echo -e "\nMatch User user*\nPasswordAuthentication yes" >> /etc/ssh/sshd_config
# Adjust permissions
chown root:root /var/www/webroot/ROOT
chmod 755 /var/www/webroot/ROOT
#Restart SSH daemon
sudo systemctl restart sshd

View File

@ -45,6 +45,8 @@ onInstall:
commands:
- systemctl restart sshd
user: root
- api: environment.control.RestartContainer
nodeGroup: cp
- return: installSuccess
actions:
@ -52,12 +54,27 @@ actions:
- cmd[cp]:
commands: bash /home/jelastic/add-sftp-user-addon/add-sftp.sh
user: root
- if ("${response.exitStatus}" != "0"):
return: sftpError
- return: sftpSuccess
onUninstall:
- cmd[cp]:
commands:
- rm -rf /home/jelastic/add-sftp-user-addon/
user: root
- return: uninstallSuccess
responses:
uninstallSuccess:
type: success
message: "Uninstalled Successfully"
installSuccess:
type: success
message: "Installed Successfully"
sftpError:
type: error
message: "Failed to add SFTP user. Please check the server logs for more details."
sftpSuccess:
type: success
email: |-