-
Notifications
You must be signed in to change notification settings - Fork 0
/
cSABnzbd.psm1
115 lines (102 loc) · 3.79 KB
/
cSABnzbd.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
enum Ensure
{
Absent
Present
}
[DscResource()]
class cSABnzbdInstall
{
[DscProperty(Key)]
[string] $Ensure
[DscProperty()]
[PSCredential]$ServiceCredential
# Gets the resource's current state.
[cSABnzbdInstall] Get()
{
# If SABnzbd is installed
$Package = Get-Package -Name 'Sabnzbd' -ErrorAction SilentlyContinue
if ($null -ne $Package)
{
$this.Ensure = [Ensure]::Present
}
else
{
$this.Ensure = [Ensure]::Absent
}
return $this
}
# Sets the desired state of the resource.
[void] Set()
{
if ($this.Ensure -eq [Ensure]::Present)
{
# Get Sabnzbd info from github
$ReleaseInfo = $this.GetLatestVersion()
$SetupAsset = $ReleaseInfo.assets.where{$_.name.contains('.exe')}
$DownloadURI = $SetupAsset.browser_download_url
# Download from Github
$DownloadDestination = Join-Path -Path $ENV:temp -ChildPath 'SABnzbd-setup.exe'
Invoke-WebRequest -Uri $DownloadURI -OutFile $DownloadDestination
# Start install
Start-Process -FilePath $DownloadDestination -ArgumentList '/S' -Wait
# Make program data location
$SABProgramData = Join-Path -Path $env:ProgramData -ChildPath 'sabnzbd'
$null = mkdir -Path $SABProgramData
# Service install paths (and ini)
$SABIni = Join-Path -Path $SABProgramData -ChildPath 'sabnzbd.ini'
$SABInstall = Join-Path -Path ${env:ProgramFiles(x86)} -ChildPath 'SABnzbd'
$SABhelper = Join-Path -Path $SABInstall -ChildPath 'SABnzbd-helper.exe'
$SABService = Join-Path -Path $SABInstall -ChildPath 'SABnzbd-service.exe'
# Install Services
if ($null -eq $this.ServiceCredential)
{
& $SABhelper --startup auto install
& $SABService --startup auto -f $SABIni install
}
else
{
$Username = $this.ServiceCredential.UserName
$Password = $this.ServiceCredential.GetNetworkCredential().Password
& $SABhelper --startup auto --username $Username --password $Password install
& $SABService --startup auto --username $Username --password $Password -f $SABIni install
}
# Start service
Start-Service 'sabnzbd'
}
else
{
$SABInstall = Join-Path -Path ${env:ProgramFiles(x86)} -ChildPath 'SABnzbd'
$SABUninstaller = Join-Path -Path $SABInstall -ChildPath 'uninstall.exe'
Start-Process -FilePath $SABUninstaller -ArgumentList '/S' -Wait
}
}
# Tests if the resource is in the desired state.
[bool] Test()
{
# If SABnzbd is installed, check we have the latest version installed
$Package = Get-Package -Name 'Sabnzbd' -ErrorAction SilentlyContinue
if ($this.Ensure -eq [Ensure]::Present)
{
if ($null -eq $Package)
{
return $false
}
else
{
# Get Sabnzbd info from github
$GitVersion = $this.GetLatestVersion().tag_name
return ($Package.version -eq $GitVersion)
}
}
else
{
# If it should be absent, check if null and return result
return ($null -eq $Package)
}
}
[PSCustomObject] GetLatestVersion ()
{
$ReleaseInfo = Invoke-RestMethod -Uri 'https://api.github.com/repos/sabnzbd/sabnzbd/releases/latest' -UseBasicParsing
return $ReleaseInfo
}
}