vodia-teams
Version:
Microsoft Teams Direct Routing configuration tool
193 lines (163 loc) • 9.69 kB
JavaScript
#!/usr/bin/env node
const { execSync } = require('child_process');
const os = require('os');
const fs = require('fs');
const path = require('path');
const platform = os.platform();
console.log('Installing prerequisites for vodia-teams...');
try {
if (platform === 'linux') {
console.log('Detected Linux. Installing PowerShell...');
// Try using snap first (works on most distros)
try {
console.log('Attempting to install PowerShell using Snap...');
execSync('snap version', { stdio: 'ignore' }); // Check if snap is available
execSync('sudo snap install powershell --classic', { stdio: 'inherit' });
console.log('PowerShell installed successfully using Snap!');
} catch (snapError) {
console.log('Snap installation failed or not available. Trying apt method...');
// Fall back to apt method
try {
console.log('Attempting to install PowerShell using apt...');
// Check if we're running on Ubuntu
let isUbuntu = false;
let ubuntuVersion = '';
let ubuntuCodename = '';
try {
const osRelease = fs.readFileSync('/etc/os-release', 'utf8');
isUbuntu = osRelease.includes('ID=ubuntu');
if (isUbuntu) {
ubuntuVersion = execSync('lsb_release -rs').toString().trim();
ubuntuCodename = execSync('lsb_release -cs').toString().trim();
}
} catch (error) {
console.log('Could not determine Linux distribution. Will try generic approach.');
}
if (isUbuntu) {
// Ubuntu-specific installation
console.log(`Detected Ubuntu ${ubuntuVersion} (${ubuntuCodename})`);
// Download Microsoft key
console.log('Downloading Microsoft repository key...');
execSync('curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo gpg --dearmor -o /usr/share/keyrings/microsoft-archive-keyring.gpg', { stdio: 'inherit' });
// Add repository based on Ubuntu version
console.log('Adding Microsoft repository...');
execSync(`echo "deb [arch=amd64,arm64,armhf signed-by=/usr/share/keyrings/microsoft-archive-keyring.gpg] https://packages.microsoft.com/ubuntu/${ubuntuVersion}/prod ${ubuntuCodename} main" | sudo tee /etc/apt/sources.list.d/microsoft.list`, { stdio: 'inherit' });
// Update and install
console.log('Updating package lists...');
execSync('sudo apt update', { stdio: 'inherit' });
console.log('Installing PowerShell...');
execSync('sudo apt install -y powershell', { stdio: 'inherit' });
} else {
// Generic Debian-based approach
console.log('Trying generic Debian-based approach...');
// Add Microsoft signing key
console.log('Adding Microsoft signing key...');
execSync('wget -q https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb', { stdio: 'inherit' });
execSync('sudo dpkg -i packages-microsoft-prod.deb', { stdio: 'inherit' });
execSync('rm packages-microsoft-prod.deb', { stdio: 'inherit' });
// Update packages list and install PowerShell
console.log('Updating package lists...');
execSync('sudo apt update', { stdio: 'inherit' });
console.log('Installing PowerShell...');
execSync('sudo apt install -y powershell', { stdio: 'inherit' });
}
console.log('PowerShell installed successfully using apt!');
} catch (aptError) {
console.log('Apt installation failed. Trying yum method...');
// Try yum method (RHEL/CentOS/Fedora)
try {
console.log('Attempting to install PowerShell using yum...');
// Add Microsoft repository
console.log('Adding Microsoft repository...');
execSync('curl -sSL https://packages.microsoft.com/config/rhel/8/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo', { stdio: 'inherit' });
// Install PowerShell
console.log('Installing PowerShell...');
execSync('sudo yum install -y powershell', { stdio: 'inherit' });
console.log('PowerShell installed successfully using yum!');
} catch (yumError) {
console.log('Automatic PowerShell installation failed.');
console.log('Please install PowerShell manually: https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-linux');
console.log('For Ubuntu 24.04: sudo snap install powershell --classic');
}
}
}
} else if (platform === 'darwin') {
console.log('Detected macOS. Installing PowerShell...');
// Check if Homebrew is installed
try {
execSync('brew --version', { stdio: 'ignore' });
// Install PowerShell using Homebrew
console.log('Installing PowerShell...');
execSync('brew install --cask powershell', { stdio: 'inherit' });
console.log('PowerShell installed successfully!');
} catch (error) {
console.log('Homebrew not found. Please install Homebrew first:');
console.log('/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"');
console.log('Then install PowerShell: brew install --cask powershell');
}
} else if (platform === 'win32') {
console.log('Detected Windows. Checking PowerShell version...');
// Check if PowerShell is already installed
try {
const psVersion = execSync('powershell -Command "$PSVersionTable.PSVersion.Major"').toString().trim();
if (parseInt(psVersion) >= 7) {
console.log('PowerShell 7+ already installed.');
} else {
console.log('Older PowerShell detected. Installing PowerShell 7+...');
// Create temp directory for download
const tempDir = path.join(os.tmpdir(), 'ps7-install');
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
}
// Download and install PowerShell 7
const installerPath = path.join(tempDir, 'PowerShell-7.3.4-win-x64.msi');
console.log('Downloading PowerShell 7...');
execSync(`powershell -Command "Invoke-WebRequest -Uri 'https://github.com/PowerShell/PowerShell/releases/download/v7.3.4/PowerShell-7.3.4-win-x64.msi' -OutFile '${installerPath}'"`, { stdio: 'inherit' });
console.log('Installing PowerShell 7...');
execSync(`msiexec.exe /i "${installerPath}" /quiet ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 ENABLE_PSREMOTING=1 REGISTER_MANIFEST=1`, { stdio: 'inherit' });
console.log('PowerShell 7 installed successfully!');
}
} catch (error) {
console.log('Error installing PowerShell 7. Please install PowerShell 7+ manually:');
console.log('https://github.com/PowerShell/PowerShell/releases');
}
}
// Install Microsoft Teams module
console.log('\nInstalling Microsoft Teams PowerShell module...');
try {
// Check if PowerShell is installed
if (platform === 'win32') {
execSync('where pwsh', { stdio: 'ignore' });
} else {
execSync('which pwsh', { stdio: 'ignore' });
}
// Try to install for current user first (works without admin privileges)
try {
console.log('Installing Teams module for current user...');
execSync('pwsh -Command "Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser; Set-PSRepository -Name PSGallery -InstallationPolicy Trusted; Install-Module -Name MicrosoftTeams -Force -AllowClobber -Scope CurrentUser"', { stdio: 'inherit' });
console.log('Microsoft Teams PowerShell module installed successfully!');
} catch (userScopeError) {
// Fall back to machine-wide installation if user has admin privileges
console.log('User-level installation failed. Trying system-wide installation (requires admin rights)...');
execSync('pwsh -Command "Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force; Set-PSRepository -Name PSGallery -InstallationPolicy Trusted; Install-Module -Name MicrosoftTeams -Force -AllowClobber"', { stdio: 'inherit' });
console.log('Microsoft Teams PowerShell module installed successfully!');
}
// Verify installation
console.log('Verifying Teams module installation...');
const moduleCheck = execSync('pwsh -Command "Get-Module -Name MicrosoftTeams -ListAvailable | Select-Object -ExpandProperty Version"').toString().trim();
console.log(`Microsoft Teams module version: ${moduleCheck}`);
} catch (error) {
console.log('Error installing Microsoft Teams module. Make sure PowerShell is installed.');
console.log('You can manually install it with these PowerShell commands:');
console.log('pwsh');
console.log('Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser');
console.log('Set-PSRepository -Name PSGallery -InstallationPolicy Trusted');
console.log('Install-Module -Name MicrosoftTeams -Force -AllowClobber -Scope CurrentUser');
}
console.log('\nPrerequisites installation completed!');
console.log('You may need to restart your terminal for changes to take effect.');
} catch (error) {
console.error('Error installing prerequisites:', error.message);
console.log('Please install PowerShell and the Microsoft Teams module manually.');
console.log('For instructions, see: https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell');
}