# Mole Agent Windows Installer # Run from an Administrator PowerShell prompt to register the Scheduled Task. $InstallDir = "C:\ProgramData\MoleAgent" $SourceDir = $PSScriptRoot Write-Host "=============================================" -ForegroundColor Cyan Write-Host " Installing Mole Miner Scanner Agent" -ForegroundColor Cyan Write-Host "=============================================" -ForegroundColor Cyan # 1. Verify Python is installed Write-Host "Step 1: Verifying Python installation..." -ForegroundColor Yellow $PythonPath = Get-Command python -ErrorAction SilentlyContinue if (-not $PythonPath) { # Check common python path defaults if not on system PATH $UserLocalApp = "$env:USERPROFILE\AppData\Local\Programs\Python" if (Test-Path $UserLocalApp) { $PythonPath = Get-ChildItem -Path $UserLocalApp -Filter "python.exe" -Recurse | Select-Object -First 1 } } if (-not $PythonPath) { Write-Error "Python 3 is not detected on this system. Please download and install Python from https://www.python.org/downloads/ (ensure you check 'Add Python to PATH' during installation)." Exit 1 } else { Write-Host "Found Python at: $PythonPath" -ForegroundColor Green } # 2. Check for pip and install dependencies Write-Host "Step 2: Checking pip and installing dependencies..." -ForegroundColor Yellow & python -m pip --version if ($LASTEXITCODE -ne 0) { Write-Error "pip is not available. Please ensure pip is installed for Python." Exit 1 } Write-Host "Installing pyasic, pyyaml, and aiohttp via pip..." -ForegroundColor Yellow & python -m pip install --upgrade pip & python -m pip install pyasic pyyaml aiohttp if ($LASTEXITCODE -ne 0) { Write-Warning "Some dependencies failed to install. Please verify your internet connection and run: pip install pyasic pyyaml aiohttp" } else { Write-Host "Dependencies installed successfully!" -ForegroundColor Green } # 3. Create target directory and copy (or download) agent files Write-Host "Step 3: Creating target directory and staging agent files..." -ForegroundColor Yellow if (-not (Test-Path $InstallDir)) { New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null } $DownloadBase = "https://agent.molehash.com/latest" # Use a local mole_agent.py when the installer sits next to one (zip bundle), # otherwise pull the latest release from the download host. if (Test-Path "$SourceDir\mole_agent.py") { Copy-Item -Path "$SourceDir\mole_agent.py" -Destination "$InstallDir\mole_agent.py" -Force } else { Write-Host "mole_agent.py not found next to installer - downloading latest..." -ForegroundColor Yellow Invoke-WebRequest -UseBasicParsing -Uri "$DownloadBase/mole_agent.py" -OutFile "$InstallDir\mole_agent.py" Write-Host "Downloaded mole_agent.py from $DownloadBase" -ForegroundColor Green } if (-not (Test-Path "$InstallDir\config.yaml")) { if (Test-Path "$SourceDir\config.yaml") { Copy-Item -Path "$SourceDir\config.yaml" -Destination "$InstallDir\config.yaml" -Force Write-Host "Copied config.yaml template. Please edit $InstallDir\config.yaml with your subnet and site settings." -ForegroundColor Yellow } else { # No template shipped: generate one with obvious placeholders so a # half-configured agent fails loudly instead of talking to nothing. @' # Mole Agent Configuration # Get site_id and api_key from your MoleHash dashboard # (Settings -> Billing -> "Select Site to Configure"). site_id: "REPLACE_WITH_YOUR_SITE_ID" api_key: "REPLACE_WITH_YOUR_SITE_API_KEY" # The local subnet range to scan scan_range: "192.168.1.0/24" # Connection timeout per miner in seconds timeout: 3 # Ports to probe for ASIC miners scan_ports: - 4028 - 80 # Central manager URL main_app_url: "https://api.molehash.com" '@ | Set-Content -Path "$InstallDir\config.yaml" -Encoding utf8 Write-Host "Generated config.yaml template. EDIT $InstallDir\config.yaml with your site_id, api_key and scan_range before the agent can report." -ForegroundColor Yellow } } else { Write-Host "Existing config.yaml found in $InstallDir. Preserving it." -ForegroundColor Green } Write-Host "Agent files copied to: $InstallDir" -ForegroundColor Green # 4. Register Scheduled Task Write-Host "Step 4: Registering Scheduled Task in Windows..." -ForegroundColor Yellow $TaskName = "MoleMinerScanner" $ActionScript = "$InstallDir\mole_agent.py" $ActionConfig = "$InstallDir\config.yaml" # We use pythonw.exe to run the agent in the background without opening a command window. $PythonWPath = $PythonPath.Source -replace "python.exe", "pythonw.exe" if (-not (Test-Path $PythonWPath)) { $PythonWPath = "pythonw" } # Command arguments to pass to pythonw $Arguments = "`"$ActionScript`" --config `"$ActionConfig`"" # Check if task already exists and remove it $ExistingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue if ($ExistingTask) { Write-Host "Removing existing scheduled task..." -ForegroundColor Yellow Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false } # Register Scheduled Task using native Cmdlets $Action = New-ScheduledTaskAction -Execute $PythonWPath -Argument $Arguments # Trigger: Run every 5 minutes indefinitely, starting immediately $Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration ([TimeSpan]::MaxValue) $Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable Register-ScheduledTask -TaskName $TaskName -Action $Action -Trigger $Trigger -Settings $Settings -Description "Runs Mole Miner Scanner every 5 minutes to fetch ASIC telemetry." | Out-Null Write-Host "=============================================" -ForegroundColor Green Write-Host " Mole Agent Installed Successfully!" -ForegroundColor Green Write-Host " Task '$TaskName' has been registered." -ForegroundColor Green Write-Host " It will run every 5 minutes in the background." -ForegroundColor Green Write-Host " Logs will be written to: $InstallDir\mole_agent.log" -ForegroundColor Green Write-Host "=============================================" -ForegroundColor Green