diff --git a/alternates/non-interactive/Dockerfile b/alternates/non-interactive/Dockerfile index 8694b33..dd81377 100644 --- a/alternates/non-interactive/Dockerfile +++ b/alternates/non-interactive/Dockerfile @@ -62,9 +62,9 @@ RUN wget -q https://www.mathworks.com/mpm/glnxa64/mpm \ && sudo ln -s ${MATLAB_INSTALL_LOCATION}/bin/matlab /usr/local/bin/matlab # Install matlab-batch to enable the use of MATLAB batch licensing tokens. -RUN wget -q https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1/glnxa64/matlab-batch \ - && sudo mv matlab-batch /usr/local/bin \ - && sudo chmod +x /usr/local/bin/matlab-batch +RUN wget -q 'https://raw.githubusercontent.com/mathworks-ref-arch/matlab-dockerfile/main/alternates/non-interactive/install/install-matlab-batch.sh' \ + && sudo bash ./install-matlab-batch.sh \ + && rm ./install-matlab-batch.sh # The following environment variables allow MathWorks to understand how this MathWorks # product (MATLAB Non-Interactive Dockerfile) is being used. This information helps us make MATLAB even better. diff --git a/alternates/non-interactive/MATLAB-BATCH.md b/alternates/non-interactive/MATLAB-BATCH.md index 46a38de..1b492d9 100644 --- a/alternates/non-interactive/MATLAB-BATCH.md +++ b/alternates/non-interactive/MATLAB-BATCH.md @@ -5,25 +5,58 @@ MATLAB® batch licensing executable (**matlab-batch**) is a command line tool The MATLAB batch licensing project is still in the pilot phase. To inquire about eligibility requirements, fill out this form on the MathWorks® website: [Batch Licensing Pilot Eligibility](https://www.mathworks.com/support/batch-tokens.html). -## Download MATLAB Batch Licensing Executable +## Install MATLAB Batch Licensing Executable -### Linux +You can install the MATLAB batch licensing executable either by using the installer script or by downloading it and setting it up manually. -From a Linux® terminal, use `wget` to download the latest version of `matlab-batch`. +The installer script sets up the MATLAB Batch Licensing Executable in a default location and adds it to your system path for easy command line access. It also allows you to install the executable in a preferred location. + +### Install MATLAB Batch Licensing Executable Using the Installer Script + +On macOS, Linux®, and emulated Linux environments (Cygwin™, MinGW®, MSYS2) on Windows®, use the following commands to install `matlab-batch`: +```bash +curl -s 'https://raw.githubusercontent.com/mathworks-ref-arch/matlab-dockerfile/main/alternates/non-interactive/install/install-matlab-batch.sh' -o 'install-matlab-batch.sh' +sudo bash ./install-matlab-batch.sh +``` + +To install it in a custom location, such as `/opt/my-custom-installs/`, use this command: +```bash +sudo bash ./install-matlab-batch.sh -i '/opt/my-custom-location' +``` +> Note: the -i option accepts both absolute and relative paths. + +On Windows, use the following commands to install `matlab-batch` in a PowerShell session with elevated permissions (you must start PowerShell with the "Run as Administrator" option): +```powershell +Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/mathworks-ref-arch/matlab-dockerfile/main/alternates/non-interactive/install/install-matlab-batch.ps1' -OutFile 'install-matlab-batch.ps1' +.\install-matlab-batch.ps1 +``` + +To install it in a custom location, such as `C:\Program Files\my-custom-location`, use this command: +```powershell +.\install-matlab-batch.ps1 -InstallLocation 'C:\Program Files\my-custom-location' +``` + +> Note: the -InstallLocation option accepts both absolute and relative paths. + +### Download MATLAB Batch Licensing Executable and Install Manually + +#### Linux + +From a Linux terminal, use `wget` to download the latest version of `matlab-batch`. wget https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1/glnxa64/matlab-batch -Give the downloaded file executable permissions so that you can run `matlab-batch`. +Grant the downloaded file executable permissions so that you can run `matlab-batch`. chmod +x matlab-batch -### Windows +#### Windows -From a Windows® PowerShell command prompt, use `Invoke-WebRequest` to download the latest version of `matlab-batch`. +From a Windows PowerShell command prompt, use `Invoke-WebRequest` to download the latest version of `matlab-batch`. Invoke-WebRequest https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1/win64/matlab-batch.exe -OutFile matlab-batch.exe -### macOS +#### macOS From a macOS terminal, use `curl` to download the latest version of `matlab-batch` for your macOS architecture. diff --git a/alternates/non-interactive/install/install-matlab-batch.ps1 b/alternates/non-interactive/install/install-matlab-batch.ps1 new file mode 100644 index 0000000..2557e90 --- /dev/null +++ b/alternates/non-interactive/install/install-matlab-batch.ps1 @@ -0,0 +1,140 @@ +<# +.SYNOPSIS + Installs matlab-batch on a Windows system. + +.DESCRIPTION + Downloads the matlab-batch executable. + Creates a directory to place the executable into. + Adds the executable onto the user PATH. + +.PARAMETER InstallLocation + Specifies the location where matlab-batch will be downloaded to and installed in. + Defaults to $ProgramFiles\MathWorks. + +.PARAMETER Help + Shows a help message for the install script if present. + +.EXAMPLE + Install-Batch + Install-Batch -Help + Install-Batch -InstallLocation C:\MyPrograms + +.NOTES + Copyright 2024 The MathWorks, Inc. +#> +param ( + [string]$InstallLocation='C:\Program Files\MathWorks', + [switch]$Help + ) + +$DEFAULT_DOWNLOAD_BASE_URL = 'https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1/win64/matlab-batch.exe' + +# Set strict error handling +$ErrorActionPreference = 'Stop' + +# Show help +function Show-Help { + Write-Output "Usage: install-matlab-batch.ps1 [parameters]" + Write-Output "" + Write-Output "Options:" + Write-Output " -Help Display this help message." + Write-Output ' -InstallLocation Specify an install location. Defaults to $ProgramFiles\MathWorks.' + Write-Output "" +} + +# Function to download the matlab-batch executable binary +function Download { + param ( + [string]$Url, + [string]$Filename + ) + Invoke-WebRequest -Uri $Url -OutFile $Filename +} + +function Install-MatlabBatch { + param ( + [string]$InstallDirectory + ) + + Write-Output 'Starting Install-MatlabBatch...' + + # Resolve $InstallDirectory to an absolute path + $InstallDirectory = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine((Get-Location), $InstallDirectory)) + "\matlab-batch" + + # Create standard location for install if it does not exist + New-Item -ItemType Directory -Force -Path "$InstallDirectory" + + # Define the default download URL + $BaseUrl = $DEFAULT_DOWNLOAD_BASE_URL + try { + Download "$BaseUrl" "$InstallDirectory\matlab-batch.exe" + } + catch { + Write-Host "Failed to download matlab-batch ($($_.Exception.Message))" + exit 1 + } + + # Add the installation directory to the system PATH + $SystemPath = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine) + $PathEntries = $SystemPath.Split(';') + + # Check for existing entries for the matlab-batch folder + $ExistingEntries = @($PathEntries | Where-Object { $_ -like "*matlab-batch*" }) + + if ($ExistingEntries.Count -eq 0) { + # No entry for matlab-batch on PATH + $NewPath = $SystemPath + ';' + $InstallDirectory + [System.Environment]::SetEnvironmentVariable('Path', $NewPath, [System.EnvironmentVariableTarget]::Machine) + Write-Output "Added new installation directory to PATH. You will have to start a fresh PowerShell session to find it on PATH." + } else { + if ($ExistingEntries -contains $InstallDirectory) { + # There are matlab-batch entries on PATH and an entry matches InstallLocation + $FirstEntry = $ExistingEntries[0] + + if ($FirstEntry -ne $InstallDirectory) { + # Matching entry is not at the start of the PATH, move it to the first entry + $PathEntries = $PathEntries | Where-Object { $_ -ne $InstallDirectory } + $FirstMatlabBatchIndex = [Array]::IndexOf($PathEntries, $FirstEntry) + $PathEntries = $PathEntries[0..($FirstMatlabBatchIndex-1)] + $InstallDirectory + $PathEntries[$FirstMatlabBatchIndex..($PathEntries.Length-1)] + $NewPath = $PathEntries -join ';' + [System.Environment]::SetEnvironmentVariable('Path', $NewPath, [System.EnvironmentVariableTarget]::Machine) + Write-Output "Moved current installation directory to the first position among matlab-batch entries. You will have to start a fresh PowerShell session to find it on PATH." + } else { + # Matching entry is the first matlab-batch entry on PATH, do nothing + Write-Output "The current installation directory is already the first entry on PATH. No changes made." + } + } else { + # There are matlab-batch entries on PATH and no entry matches InstallLocation + $FirstEntry = $ExistingEntries[0] + $FirstMatlabBatchIndex = [Array]::IndexOf($PathEntries, $FirstEntry) + $PathEntries = $PathEntries[0..($FirstMatlabBatchIndex-1)] + $InstallDirectory + $PathEntries[$FirstMatlabBatchIndex..($PathEntries.Length-1)] + $NewPath = $PathEntries -join ';' + [System.Environment]::SetEnvironmentVariable('Path', $NewPath, [System.EnvironmentVariableTarget]::Machine) + Write-Output "Added new installation directory to the beginning of the matlab-batch entries. You will have to start a fresh PowerShell session to find it on PATH." + } + } + + Write-Output "Done with Install-MatlabBatch." +} + +try { + # Check if PowerShell is running as Administrator + $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) + $isAdministrator = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + + if ($Help) { + Show-Help + exit 0 + } + + if ($isAdministrator) { + Install-MatlabBatch -InstallDirectory $InstallLocation + } else { + Write-Host "PowerShell is not running as Administrator. Please run PowerShell as Administrator to proceed with installation." + } +} +catch { + $ScriptPath = $MyInvocation.MyCommand.Path + Write-Output "ERROR - An error occurred while running script: $ScriptPath. Error: $_" + throw +} diff --git a/alternates/non-interactive/install/install-matlab-batch.sh b/alternates/non-interactive/install/install-matlab-batch.sh new file mode 100644 index 0000000..6c88e12 --- /dev/null +++ b/alternates/non-interactive/install/install-matlab-batch.sh @@ -0,0 +1,134 @@ +#!/bin/bash +# Copyright 2024 The MathWorks, Inc. + +DEFAULT_DOWNLOAD_BASE_URL='https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1' +DEFAULT_UNIX_INSTALL_LOCATION='/opt/mathworks' +DEFAULT_WIN_INSTALL_LOCATION='C:\Program Files\MathWorks' + +# Exit on any failure, treat unset substitution variables as errors +set -euo pipefail + +# Function to display help message +show_help() { + echo "Usage: $0 [options]" + echo "" + echo "Options:" + echo " -h Display this help message." + echo " -i Specify an install location." +} + +# Initialize our own variables: +input_value="" + +# Process command-line options + +OPTSTRING=":hi:" + +while getopts "$OPTSTRING" opt; do + case ${opt} in + h) + show_help + exit 0 + ;; + i) + input_value=$OPTARG + ;; + \?) + echo "Invalid option: -${OPTARG}" 1>&2 + show_help + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." 1>&2 + exit 1 + ;; + esac +done + +# Use the input value if provided +if [ -n "$input_value" ]; then + install_dir=$input_value +else + echo "Using default install location." +fi + +# Function to download the matlab-batch executable binary +download() { + url=$1 + filename=$2 + + if [ -x "$(command -v wget)" ]; then + wget -O "$filename" "$url" 2>&1 | ( ! grep -i "failed\|error\|denied" >&2 ) + elif [ -x "$(command -v curl)" ] && curl --help | grep -q -- "--retry-all-errors"; then + curl --retry 3 --retry-all-errors -sSfLo "$filename" "$url" + elif [ -x "$(command -v curl)" ]; then + curl --retry 3 -sSfLo "$filename" "$url" + else + echo "Could not find wget or curl command" >&2 + return 1 + fi +} + +# Detect the user environment and prepare accordingly +OS=$(uname) + +case $OS in +Linux) + mw_arch='glnxa64' + bin_ext='' + install_dir=${install_dir:-$DEFAULT_UNIX_INSTALL_LOCATION} + ;; +Darwin) + arch=$(uname -m) + if echo "$arch" | grep -qE '^(x86_64|amd64)$'; then + mw_arch='maci64' + else + mw_arch='maca64' + fi + bin_ext='' + install_dir=${install_dir:-$DEFAULT_UNIX_INSTALL_LOCATION} + ;; +CYGWIN*|MINGW32*|MSYS*|MINGW*) + mw_arch='win64' + bin_ext='.exe' + install_dir=${install_dir:-$DEFAULT_WIN_INSTALL_LOCATION} + ;; +*) + echo "'$OS' operating system not supported" + exit 1 + ;; +esac + +# Create standard location for install if it does not exist +mkdir -p "$install_dir" + +# Download the matlab-batch executable to a standard location +base_url=${DEFAULT_DOWNLOAD_BASE_URL} + +{ + output=$(download "$base_url/$mw_arch/matlab-batch$bin_ext" "$install_dir/matlab-batch$bin_ext" 2>&1); +} || { + msg=$(echo "$output" | head -1 | sed -e 's/^[[:space:]]*//') + echo "Failed to download matlab-batch to the specified location ($msg)." + exit 1; +} + +# Allow executable permissions for matlab-batch +chmod a+x "$install_dir/matlab-batch$bin_ext" + +# Place matlab-batch on the PATH if it is possible in the current environment +if [ "$OS" = "Linux" ] || [ "$OS" = "Darwin" ]; then + # Convert install_dir to an absolute path if it is not already + install_dir=$(cd "$install_dir" && pwd) + + # Check if install_dir is not /usr/local/bin and proceed with creating a symlink if true + if [ "$install_dir" != "/usr/local/bin" ] && ln -fs "$install_dir/matlab-batch" /usr/local/bin/matlab-batch; then + echo "symlink created in /usr/local/bin." + elif [ "$install_dir" = "/usr/local/bin" ]; then + echo "matlab-batch is installed in /usr/local/bin, no need to create a symbolic link." >&2 + else + echo "Unable to create symbolic links for matlab-batch in /usr/local/bin. To run matlab-batch, '$install_dir/matlab-batch' must be on the system path." >&2 + fi +fi + +echo "matlab-batch installed successfully." \ No newline at end of file