-
Notifications
You must be signed in to change notification settings - Fork 262
/
docker_run.ps1
83 lines (71 loc) · 2.96 KB
/
docker_run.ps1
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
param (
[String] $Alerter = "email",
[String] $AlerterConfig = "",
[String] $Config = "",
[String] $ChatId = "",
[String] $Email = "",
[String] $Image = "ericjmarti/inventory-hunter:latest",
[String] $Relay = "",
[String] $Webhook = ""
)
if (-Not $Config) { Throw "missing config argument" }
if (-Not (Test-Path $Config -PathType Leaf)) { Throw "$Config does not exist or is not a regular file" }
if ($AlerterConfig) {
if (-Not (Test-Path $AlerterConfig -PathType Leaf)) { Throw "$AlerterConfig does not exist or is not a regular file" }
} elseif ($Alerter -eq "email") {
if (-Not $email) { Throw "missing email argument" }
if (-Not $relay) { Throw "missing relay argument" }
} else {
if (-Not $webhook) { Throw "missing webhook argument" }
if ($Alerter -eq "telegram") {
if (-Not $chatid) { Throw "missing telegram chat id argument" }
}
}
if ($Image -eq "ericjmarti/inventory-hunter:latest") {
docker pull $Image
} else {
$Result = docker images -q $Image
if ([String]::IsNullOrEmpty($Result)) {
Write-Host "the $Image docker image does not exist... please build the image and try again"
Write-Host "build command: docker build -t $Image ."
Exit 1
}
}
$Config = (Resolve-Path -Path $Config)
if ($AlerterConfig) { $AlerterConfig = (Resolve-Path -Path $AlerterConfig) }
$ScriptDir = Split-Path $MyInvocation.MyCommand.Path
$LogDir = "${ScriptDir}\log"
New-Item $LogDir -ItemType Directory -ea 0 | Out-Null
$ContainerName = [System.IO.Path]::GetFileNameWithoutExtension($Config)
$DataDir = "${ScriptDir}\data\${ContainerName}"
$LogFile = "${LogDir}\${ContainerName}.txt"
New-Item $DataDir -ItemType Directory -ea 0 | Out-Null
New-Item $LogFile -ItemType File -ea 0 | Out-Null
$Volumes = "-v ${DataDir}:/data -v ${LogFile}:/log.txt -v ${Config}:/config.yaml"
if ($AlerterConfig) { $Volumes = "$Volumes -v ${AlerterConfig}:/alerters.yaml" }
$Entrypoint = "--entrypoint=/src/run.bash"
$DockerRunCmd = "docker run -d --rm $Entrypoint --name $ContainerName --network host $Volumes $Image --alerter $Alerter"
if ($AlerterConfig) {
$DockerRunCmd = "$DockerRunCmd --alerter-config /alerters.yaml"
} elseif ($Alerter -eq "email") {
$DockerRunCmd = "$DockerRunCmd --email $Email --relay $Relay"
} else {
$DockerRunCmd = "$DockerRunCmd --webhook $Webhook"
if ($Alerter -eq "telegram") {
$DockerRunCmd = "$DockerRunCmd --chat-id $ChatId"
}
}
$DockerPsCmd = "docker ps -a -f name=$ContainerName"
# Write-Host "$ $DockerRunCmd"
Invoke-Expression $DockerRunCmd
Write-Host
Write-Host "started docker container named $ContainerName"
Write-Host
Write-Host "view the status of this container using the following command:"
Write-Host "$ $DockerPsCmd"
Write-Host
Invoke-Expression $DockerPsCmd
Write-Host
Write-Host "view logs for this container using the following command:"
Write-Host "$ docker logs -f $ContainerName"
Write-Host