pelican-eggs / eggs

Service eggs for the pterodactyl panel
MIT License
2.85k stars 2.32k forks source link

alt:V Script fix #669

Closed DatMayo closed 4 years ago

DatMayo commented 4 years ago

Panel Version: 0.7.18 Daemon Version: 0.6.13 Service: alt:V Modified: yes Background: They changed the name of the node module, so its neccessary to change the downloading. There is still an issue open to remove the old "node-module" from their server.cfg (See: https://github.com/altmp/altv-issues/issues/626)

#!/bin/bash
# AltV Install Script
#
# Server Files: /mnt/server
# BUILD=stable ## this should be an egg variable.
BASE_URL="https://cdn.altv.mp/" ## Where the files are stored.

## install required dependancies.
function install_deps () {
    apt update
    apt install -y libatomic1 zip unzip jq curl wget
    echo -e "deps installed"
    echo -e
}

## check the has on files and compare to a remote source.
## usage 'check_hash "file" "remote_hash_source"'
function check_hash () {
    echo -e "Checking for file ${1}"
    echo -e "running: curl -sSL ${2} | jq -r --arg FILENAME \"${3}\" '.hashList | .[\$FILENAME]'"
    hash=$(curl -sSL ${2} | jq -r --arg FILENAME ${3} '.hashList | .[$FILENAME]')
    echo -e "hash: $hash"

    if [ -f ${2} ]; then

        echo -e "hash is $(sha1sum ${1} | awk '{ print $1 }')"
        echo -e "current has is ${hash}"

        if [ "$(sha1sum ${2} | awk '{ print $1 }')" == "${hash}" ]; then
            echo -e "Hash matched"
            echo -e "No need to download new file."
            return 0
        else
            echo -e "Hash didn't match"
            echo -e "Need to download the correct version"
            return 1
        fi
    else
        echo "No file found"
        return 1
    fi
}

## makes sure a download link is valid.
function validate_download () {
    DOWNLOAD_URL=${1}

    echo -e "Download url is ${DOWNLOAD_URL}"

    if [ ! -z "${DOWNLOAD_URL}" ]; then 
        if curl --output /dev/null --silent --head --fail ${DOWNLOAD_URL}; then
            echo -e "link is valid. setting download link to ${DOWNLOAD_URL}"
            DOWNLOAD_LINK=${DOWNLOAD_URL}
        else        
            echo -e "link is invalid closing out"
            exit 2
        fi
    fi

}

function download_files () {
    ## validate download link and get files
    validate_download ${1}
    echo -e "running wget ${DOWNLOAD_URL} -O ${2}"
    wget ${DOWNLOAD_URL} -O ${2}
}

## This is a file array for files to get downloaded.
## They are all comma separated lists.
## remote folder (0), json file with hashes (1), file name in json file (2), file name (3), file folder (4), extra commands (5)
file_array=( \
"server/${BUILD}/x64_linux/","update.json","altv-server","altv-server",/mnt/server/,execute \
"server/${BUILD}/x64_linux/","update.json","data/vehmodels.bin","vehmodels.bin",/mnt/server/data/ \
"server/${BUILD}/x64_linux/","update.json","data/vehmods.bin","vehmods.bin",/mnt/server/data/ \
"others/",,"server.cfg",server.cfg,/mnt/server/ \
"js-module/${BUILD}/x64_linux/","update.json","modules/js-module/libnode.so.72","libnode.so.72",/mnt/server/ \
"js-module/${BUILD}/x64_linux/","update.json","modules/js-module/libjs-module.so","libjs-module.so",/mnt/server/modules/ \
"coreclr-module/${BUILD}/x64_linux/",,"AltV.Net.Host.dll","AltV.Net.Host.dll",/mnt/server/ \
"coreclr-module/${BUILD}/x64_linux/",,"AltV.Net.Host.runtimeconfig.json","AltV.Net.Host.runtimeconfig.json",/mnt/server/ \
"coreclr-module/stable/x64_linux/","update.json","modules/libcsharp-module.so","libcsharp-module.so",/mnt/server/modules/ \
"samples/",,"resources.zip","resources.zip",/mnt/server/,unzip \
)

install_deps

## this goes through each value in the array and then checks the hash
for EACH in ${file_array[@]}
do
    ## this needs to be here to break apart the arrays.
    IFS=',' read -r -a array <<< "$EACH"

    if [ ! -z "${array[1]}" ]; then
        ## check hash on 'folder/file' with 'remote file base url/remote folder/ json file' 'json file name' 
        if check_hash "${array[4]}${array[3]}" "${BASE_URL}${array[0]}${array[1]}" "${array[2]}"; then
            echo -e "file up to date"
        else
            echo -e "downloading most recent file"
        fi
            ## make the directory if it doesn't exist.
            if [ ! -d ${array[4]} ]; then
                mkdir ${array[4]}
            fi
            download_files "${BASE_URL}${array[0]}${array[2]}" "${array[4]}${array[3]}"
    else
        ## make the directory if it doesn't exist.
        if [ ! -d ${array[4]} ]; then
            mkdir ${array[4]}
        fi
        download_files "${BASE_URL}${array[0]}${array[2]}" "${array[4]}${array[3]}"
    fi

    ## run extra commands if needed.
    case ${array[5]} in
    unzip)
        cd ${array[4]}
        unzip -o ${array[3]}
        rm ${array[3]}
        ;;
    execute)
        chmod +x ${array[4]}${array[3]}
        ;;
    esac

#    echo -e "HashFile: ${BASE_URL}${array[0]}${array[1]}"
#    echo -e "Download URL ${BASE_URL}${array[0]}${array[2]}"
#    echo -e "Filename ${array[3]}"
#    echo -e "File Folder ${array[4]}"
#    echo -e "commands to run ${array[5]}"
#    echo -e
done

echo -e "install complete"

Script as 1-Liner:

#!\/bin\/bash\r\n# AltV Install Script\r\n#\r\n# Server Files: \/mnt\/server\r\n# BUILD=stable ## this should be an egg variable.\r\nBASE_URL=\"https:\/\/cdn.altv.mp\/\" ## Where the files are stored.\r\n\r\n## install required dependancies.\r\nfunction install_deps () {\r\n    apt update\r\n    apt install -y libatomic1 zip unzip jq curl wget\r\n    echo -e \"deps installed\"\r\n    echo -e\r\n}\r\n\r\n## check the has on files and compare to a remote source.\r\n## usage 'check_hash \"file\" \"remote_hash_source\"'\r\nfunction check_hash () {\r\n    echo -e \"Checking for file ${1}\"\r\n    echo -e \"running: curl -sSL ${2} | jq -r --arg FILENAME \\\"${3}\\\" '.hashList | .[\\$FILENAME]'\"\r\n    hash=$(curl -sSL ${2} | jq -r --arg FILENAME ${3} '.hashList | .[$FILENAME]')\r\n    echo -e \"hash: $hash\"\r\n\r\n    if [ -f ${2} ]; then\r\n\r\n        echo -e \"hash is $(sha1sum ${1} | awk '{ print $1 }')\"\r\n        echo -e \"current has is ${hash}\"\r\n\r\n        if [ \"$(sha1sum ${2} | awk '{ print $1 }')\" == \"${hash}\" ]; then\r\n            echo -e \"Hash matched\"\r\n            echo -e \"No need to download new file.\"\r\n            return 0\r\n        else\r\n            echo -e \"Hash didn't match\"\r\n            echo -e \"Need to download the correct version\"\r\n            return 1\r\n        fi\r\n    else\r\n        echo \"No file found\"\r\n        return 1\r\n    fi\r\n}\r\n\r\n## makes sure a download link is valid.\r\nfunction validate_download () {\r\n    DOWNLOAD_URL=${1}\r\n\r\n    echo -e \"Download url is ${DOWNLOAD_URL}\"\r\n\r\n    if [ ! -z \"${DOWNLOAD_URL}\" ]; then \r\n        if curl --output \/dev\/null --silent --head --fail ${DOWNLOAD_URL}; then\r\n            echo -e \"link is valid. setting download link to ${DOWNLOAD_URL}\"\r\n            DOWNLOAD_LINK=${DOWNLOAD_URL}\r\n        else        \r\n            echo -e \"link is invalid closing out\"\r\n            exit 2\r\n        fi\r\n    fi\r\n\r\n}\r\n\r\nfunction download_files () {\r\n    ## validate download link and get files\r\n    validate_download ${1}\r\n    echo -e \"running wget ${DOWNLOAD_URL} -O ${2}\"\r\n    wget ${DOWNLOAD_URL} -O ${2}\r\n}\r\n\r\n## This is a file array for files to get downloaded.\r\n## They are all comma separated lists.\r\n## remote folder (0), json file with hashes (1), file name in json file (2), file name (3), file folder (4), extra commands (5)\r\nfile_array=( \\\r\n\"server\/${BUILD}\/x64_linux\/\",\"update.json\",\"altv-server\",\"altv-server\",\/mnt\/server\/,execute \\\r\n\"server\/${BUILD}\/x64_linux\/\",\"update.json\",\"data\/vehmodels.bin\",\"vehmodels.bin\",\/mnt\/server\/data\/ \\\r\n\"server\/${BUILD}\/x64_linux\/\",\"update.json\",\"data\/vehmods.bin\",\"vehmods.bin\",\/mnt\/server\/data\/ \\\r\n\"others\/\",,\"server.cfg\",server.cfg,\/mnt\/server\/ \\\r\n\"js-module\/${BUILD}\/x64_linux\/\",\"update.json\",\"modules\/js-module\/libnode.so.72\",\"libnode.so.72\",\/mnt\/server\/ \\\r\n\"js-module\/${BUILD}\/x64_linux\/\",\"update.json\",\"modules\/js-module\/libjs-module.so\",\"libjs-module.so\",\/mnt\/server\/modules\/ \\\r\n\"coreclr-module\/${BUILD}\/x64_linux\/\",,\"AltV.Net.Host.dll\",\"AltV.Net.Host.dll\",\/mnt\/server\/ \\\r\n\"coreclr-module\/${BUILD}\/x64_linux\/\",,\"AltV.Net.Host.runtimeconfig.json\",\"AltV.Net.Host.runtimeconfig.json\",\/mnt\/server\/ \\\r\n\"coreclr-module\/stable\/x64_linux\/\",\"update.json\",\"modules\/libcsharp-module.so\",\"libcsharp-module.so\",\/mnt\/server\/modules\/ \\\r\n\"samples\/\",,\"resources.zip\",\"resources.zip\",\/mnt\/server\/,unzip \\\r\n)\r\n\r\ninstall_deps\r\n\r\n## this goes through each value in the array and then checks the hash\r\nfor EACH in ${file_array[@]}\r\ndo\r\n    ## this needs to be here to break apart the arrays.\r\n    IFS=',' read -r -a array <<< \"$EACH\"\r\n\r\n    if [ ! -z \"${array[1]}\" ]; then\r\n        ## check hash on 'folder\/file' with 'remote file base url\/remote folder\/ json file' 'json file name' \r\n        if check_hash \"${array[4]}${array[3]}\" \"${BASE_URL}${array[0]}${array[1]}\" \"${array[2]}\"; then\r\n            echo -e \"file up to date\"\r\n        else\r\n            echo -e \"downloading most recent file\"\r\n        fi\r\n            ## make the directory if it doesn't exist.\r\n            if [ ! -d ${array[4]} ]; then\r\n                mkdir ${array[4]}\r\n            fi\r\n            download_files \"${BASE_URL}${array[0]}${array[2]}\" \"${array[4]}${array[3]}\"\r\n    else\r\n        ## make the directory if it doesn't exist.\r\n        if [ ! -d ${array[4]} ]; then\r\n            mkdir ${array[4]}\r\n        fi\r\n        download_files \"${BASE_URL}${array[0]}${array[2]}\" \"${array[4]}${array[3]}\"\r\n    fi\r\n\r\n    ## run extra commands if needed.\r\n    case ${array[5]} in\r\n    unzip)\r\n        cd ${array[4]}\r\n        unzip -o ${array[3]}\r\n        rm ${array[3]}\r\n        ;;\r\n    execute)\r\n        chmod +x ${array[4]}${array[3]}\r\n        ;;\r\n    esac\r\n\r\n#    echo -e \"HashFile: ${BASE_URL}${array[0]}${array[1]}\"\r\n#    echo -e \"Download URL ${BASE_URL}${array[0]}${array[2]}\"\r\n#    echo -e \"Filename ${array[3]}\"\r\n#    echo -e \"File Folder ${array[4]}\"\r\n#    echo -e \"commands to run ${array[5]}\"\r\n#    echo -e\r\ndone\r\n\r\necho -e \"install complete\"
gOOvER commented 4 years ago

Why don't you create a PR?

DatMayo commented 4 years ago

I was in a rush sry, I will do it and close this issue as soon as I'm done :)