Skip to content

Commit

Permalink
Installer updates (#2401)
Browse files Browse the repository at this point in the history
* Update install.sh to work in cloned directory.

* Update install.ps1 to work in cloned directory, and clean up some output.

* Update install.bat to be more informative and less lazy.

* Update run.py to show pip packages when checking, and handle missing discord.py when extensions are installed.

* Tidy up comments

* Update comment for disk check option.

* Don't forget TerminateSignal can set an exit code now.

* Remove refreshenv call
  • Loading branch information
itsTheFae committed May 14, 2024
1 parent bea0b46 commit 20e674d
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 62 deletions.
12 changes: 10 additions & 2 deletions install.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

CHCP 65001 > NUL

REM --> This script only exists to easily launch the PowerShell script.
REM --> The install.bat script only exists to easily launch the PowerShell script.
REM --> By default powershell might error about running unsigned scripts...

powershell.exe -noprofile -executionpolicy bypass -file install.ps1
CD "%~dp0"

SET InstFile="%~dp0%\install.ps1"
IF exist %InstFile% (
powershell.exe -noprofile -executionpolicy bypass -file "%InstFile%"
) ELSE (
echo Could not locate install.ps1
echo Please ensure it is available to continue the automatic install.
)
121 changes: 80 additions & 41 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,82 +53,103 @@ $DEFAULT_URL_BASE = "https://discordapp.com/api"
# ----------------------------------------------INSTALLING DEPENDENCIES------------------------------------------------

# Check if git is installed
"checking if git is already installed..."
Invoke-Expression "winget list -q Git.Git"
"Checking if git is already installed..."
Invoke-Expression "winget list -q Git.Git" | Out-Null
if (!($LastExitCode -eq 0))
{
# install git
"installing git..."
"Installing git..."
Invoke-Expression "winget install Git.Git"
"Done."
}
else
{
"git already installed"
"Git already installed."
}
""

# Check if Any python 3 is installed
"checking if python is already installed..."
Invoke-Expression "winget list -q Python.Python.3"
"Checking if python is already installed..."
Invoke-Expression "winget list -q Python.Python.3" | Out-Null
if (!($LastExitCode -eq 0))
{
# install python version 3.11 with the py.exe launcher.
"installing python..."
"Installing python..."
Invoke-Expression "winget install Python.Python.3.11 --custom \`"/passive Include_launcher=1\`""
"Done."
}
else
{
"python already installed"
"Python already installed."
}
""

# Check if ffmpeg is installed
"checking if FFmpeg is already installed..."
Invoke-Expression "winget list -q Gyan.FFmpeg"
"Checking if FFmpeg is already installed..."
Invoke-Expression "winget list -q Gyan.FFmpeg" | Out-Null
if (!($LastExitCode -eq 0))
{
# install FFmpeg
"installing FFmpeg..."
"Installing FFmpeg..."
Invoke-Expression "winget install Gyan.FFmpeg"
"Done."
}
else
{
"FFmpeg already installed"
"FFmpeg already installed."
}
""

# TODO: fix this, it fails due to some missing addition to powershell environment.
Invoke-Expression "refreshenv"
# NOTE: if we need to refresh the environment vars (Path, etc.) after installing
# the above packages, we may need to add some other dependency which provides
# RefreshEnv.bat or manually manage paths to newly installed exes.
# Users should be able to get around this by restarting the powershell script.

# --------------------------------------------------PULLING THE BOT----------------------------------------------------

"MusicBot currently has three branches available."
" master - An older MusicBot, for older discord.py. May not work without tweaks!"
" review - Newer MusicBot, usually stable with less updates than the dev branch."
" dev - The newest MusicBot, latest features and changes which may need testing."
""
$experimental = Read-Host "Enter the branch name you want to install"
if($experimental -eq "dev")
{
"installing dev branch..."
$branch = "dev"
}
if($experimental -eq "review")
{
"installing review branch..."
$branch = "review"
}
else
{
"installing master branch..."
$branch = "master"
}
# Test if we need to pull the bot or not by checking for some files.
$MB_Reqs_File=(pwd).Path + '\requirements.txt'
$MB_Module_Dir=(pwd).Path + '\musicbot'
$MB_Git_Dir=(pwd).Path + '\.git'

Invoke-Expression "git clone https://github.com/Just-Some-Bots/MusicBot.git MusicBot -b $branch"
Invoke-Expression "cd MusicBot"
if((Test-Path $MB_Reqs_File) -and (Test-Path $MB_Module_Dir) -and (Test-Path $MB_Git_Dir) ) {
""
"Installer detected an existing clone, and will continue installing with the current source."
""
} else {
""
"MusicBot currently has three branches available."
" master - An older MusicBot, for older discord.py. May not work without tweaks!"
" review - Newer MusicBot, usually stable with less updates than the dev branch."
" dev - The newest MusicBot, latest features and changes which may need testing."
""
$experimental = Read-Host "Enter the branch name you want to install"
if($experimental -eq "dev")
{
"Installing dev branch..."
$branch = "dev"
}
if($experimental -eq "review")
{
"Installing review branch..."
$branch = "review"
}
else
{
"Installing master branch..."
$branch = "master"
}

Invoke-Expression "git clone https://github.com/Just-Some-Bots/MusicBot.git MusicBot -b $branch"
Invoke-Expression "cd MusicBot"
""
}

# --------------------------------------------INSTALL PYTHON DEPENDENCIES----------------------------------------------

if (Get-Command "python" -errorAction SilentlyContinue)
{
Invoke-Expression "python -c 'import sys; exit(0 if sys.version_info >= (3, 8) else 1)'"
Invoke-Expression "python -c 'import sys; exit(0 if sys.version_info >= (3, 8) else 1)'" | Out-Null
if($LastExitCode -eq 0)
{
$PYTHON = "python"
Expand All @@ -139,22 +160,38 @@ $versionArray = "3.8", "3.9", "3.10", "3.11", "3.12"

foreach ($version in $versionArray)
{
Invoke-Expression "py -$version -c 'exit()'"
Invoke-Expression "py -$version -c 'exit()'" 2>$null
if($LastExitCode -eq 0)
{
$PYTHON = "py -$version"
}
}

Invoke-Expression "$PYTHON -m pip install --upgrade -r requirements.txt"
"Using $PYTHON to install and run MusicBot..."
Invoke-Expression "$PYTHON -m pip install --upgrade -r requirements.txt"

# -------------------------------------------------CONFIGURE THE BOT---------------------------------------------------
""
"MusicBot is almost ready to run, we just need to configure the bot."
"This installer provides an automated, but minimal, guided configuration."
"It will ask you to enter a bot token."
""
$iagree = Read-Host "Would you like to continue with configuration? [y/n]"
if($iagree -ne "Y" -and $iagree -ne "y")
{
"All done!"
"Remember to configure your bot token and other options before you start."
"You can use run.bat to start the MusicBot."
Return
}


Copy-Item ".\config\example_options.ini" -Destination ".\config\options.ini"

# GET AND VERIFY TOKEN
""
"Please enter your bot token. This can be found in your discordapp developer page."
$token = Read-Host "Enter Token:" -AsSecureString
$token = Read-Host "Enter Token" -AsSecureString
$token_plain = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($token))
$header = @{
"Authorization" = "Bot $token_plain"
Expand All @@ -166,6 +203,8 @@ $result_content = $result.Content
if (!($result_code -eq 200))
{
"Error getting user profile, is the token correct? ($result_code $result_content)"
""
"You can finish the configuration manually by editing the options.ini file in the config folder."
Return
}
$result_object = ConvertFrom-Json -InputObject $result_content
Expand Down
29 changes: 26 additions & 3 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,28 @@ function find_python() {
}

function pull_musicbot_git() {
echo ""
# Check if we're running inside a previously pulled repo.
GitDir="${PWD}/.git"
BotDir="${PWD}/musicbot"
ReqFile="${PWD}/requirements.txt"
if [ -d "$GitDir" ] && [ -d "$BotDir" ] && [ -f "$ReqFile" ] ; then
echo "Existing MusicBot repo detected."
read -rp "Would you like to install using the current repo? [Y/n]" UsePwd
if [ "${UsePwd,,}" == "y" ] || [ "${UsePwd,,}" == "yes" ] ; then
echo ""
CloneDir="${PWD}"
VenvDir="${CloneDir}/Venv"

$PyBin -m pip install --upgrade -r requirements.txt
echo ""

cp ./config/example_options.ini ./config/options.ini
return 0
fi
echo "Installer will attempt to create a new directory for MusicBot."
fi

cd ~ || exit_err "Fatal: Could not change to home directory."

if [ -d "${CloneDir}" ] ; then
Expand Down Expand Up @@ -155,13 +177,12 @@ function pull_musicbot_git() {
cd "${CloneDir}" || exit_err "Fatal: Could not change to MusicBot directory."

$PyBin -m pip install --upgrade -r requirements.txt
echo ""

cp ./config/example_options.ini ./config/options.ini
}

function setup_as_service() {
local DIR
DIR="$(pwd)"
echo ""
echo "The installer can also install MusicBot as a system service file."
echo "This starts the MusicBot at boot and after failures."
Expand Down Expand Up @@ -192,7 +213,7 @@ function setup_as_service() {
sed -i "s,#User=mbuser,User=${BotSysUserName},g" ./musicbot.service
sed -i "s,#Group=mbusergroup,Group=${BotSysGroupName},g" ./musicbot.service
sed -i "s,/usr/bin/pythonversionnum,${PyBinPath},g" ./musicbot.service
sed -i "s,mbdirectory,${DIR},g" ./musicbot.service
sed -i "s,mbdirectory,${PWD},g" ./musicbot.service

# Copy the service file into place and enable it.
sudo cp ~/${CloneDir}/musicbot.service /etc/systemd/system/
Expand Down Expand Up @@ -401,6 +422,8 @@ if [[ "${iagree,,}" != "y" && "${iagree,,}" != "yes" ]] ; then
exit 2
fi

echo ""
echo "Attempting to install required system packages..."
echo ""

case $DISTRO_NAME in
Expand Down
4 changes: 2 additions & 2 deletions musicbot/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class InvalidDataError(MusicbotException):


# The no processing entry type failed and an entry was a playlist/vice versa
# TODO: Add typing options instead of is_playlist
class WrongEntryTypeError(ExtractionError):
def __init__(self, message: str, is_playlist: bool, use_url: str) -> None:
super().__init__(message)
Expand Down Expand Up @@ -168,4 +167,5 @@ def get_name(self) -> str:

# signal to end the bot "gracefully"
class TerminateSignal(Signal):
exit_code: int = 0
def __init__(self, exit_code: int = 0):
self.exit_code: int = exit_code

0 comments on commit 20e674d

Please sign in to comment.