Polaris icon indicating copy to clipboard operation
Polaris copied to clipboard

Provide ability to run Polaris as a service

Open affieuk opened this issue 8 years ago • 8 comments

Please provide the ability to run Polaris as a service

affieuk avatar Oct 19 '17 14:10 affieuk

You mean like a hosted solution? Can you go into a little bit more detail?

TylerLeonhardt avatar Oct 19 '17 15:10 TylerLeonhardt

So I didn't went to mention a Windows service, but that is the idea, obviously Polaris is xplat, so the code will need to cater for a Linux service also. I was thinking that there would be some binary and then we can have a function that wraps New-Service. Maybe some sort of config file that contains the webroutes, and if the user is expected load or wanted to take advantage of a 'proper' web server then they can reverse proxy behind IIS, Apache, NGINX.

Since we already have a dll, can't it be put behind rundll32?

I'm envisaging a Windows API box, that will accept http / rest calls from non Windows boxes, it can take it whatever requests and then assuming the Windows box is domain joined can execute anything PowerShell can.

affieuk avatar Oct 19 '17 21:10 affieuk

Hiyo!

First off, awesome idea, in the sense that simplifying / illustrating a tool as it would actually run in a production environment is hugely helpful.

Random-person-completely-unrelated-to-this-project's opinion: It might make more sense to document some examples of how you might run this as a service in different environments (e.g. nssm in Windows, among other options)

Why? Boiled down, there are so many ways to do this, and even if you pick one for a particular environment, you won't know how it's delivered (config mgmt? containers/schedulers? ad hoc command on a single server? something else?)

If we document some common example(s) for common operating environments, folks could use or adapt these to whatever systems they're using.

Cheers!

RamblingCookieMonster avatar Oct 28 '17 18:10 RamblingCookieMonster

Totally agree! Thanks for the feedback folks

TylerLeonhardt avatar Oct 31 '17 01:10 TylerLeonhardt

Here is what I'm currently doing:

Polaris Script Path: C:\Scripts\Polaris.ps1

Import-Module C:\Polaris\Polaris.psm1
New-WebRoute -Path "/helloworld" -Method "GET" -ScriptBlock {
    $response.Send('Hello World');
}
Start-Polaris

# Hack to keep script running
Read-Host

NSSM Script

function Install-Service {
    Param(
        [string]$nssmPath = '.',
        [string]$Name,
        [string]$Description,
        [string]$Executable,
        [string]$Arguments
    )

    $nssm = Join-Path -Path $nssmPath -ChildPath 'nssm.exe'
    & $nssm install $name $executable $arguments
    $null = & $nssm set $name Description $description
    Start-Service $name
}

Install-Service -Name Polaris -Description 'PowerShell HTTP API Service' -Executable pwsh.exe -Arguments '-ExecutionPolicy Bypass -Command C:\Scripts\Polaris.ps1'

Test

Invoke-RestMethod -Uri http://localhost:8080/helloworld -Method GET

affieuk avatar Nov 01 '17 17:11 affieuk

I'm not sure this method of doing Read-Host works anymore. The web server doesn't appear to respond to requests when using that now (at least for me). I was able to get this working by adding -NoExit to my NSSM arguments.

awickham10 avatar Dec 03 '18 18:12 awickham10

@awickham10 - The Read-Host would block the thread yes. Add this instead and it should work for you:

while ($Polaris.Listener.IsListening) {
   Wait-Event callbackeventbridge.callbackcomplete
}

Tiberriver256 avatar Dec 03 '18 20:12 Tiberriver256

That works great - thank you!

awickham10 avatar Dec 03 '18 20:12 awickham10