forked from poshbotio/PoshBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_entrypoint.ps1
128 lines (107 loc) · 4.5 KB
/
docker_entrypoint.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
[cmdletbinding()]
param()
function Get-FromEnv {
[cmdletbinding()]
param(
[parameter(Mandatory)]
$Name,
[parameter(Mandatory)]
$Default
)
$envValue = Get-ChildItem -Path Env: |
Where-Object { $_.Name.ToUpper() -eq $Name.ToUpper() } |
Select-Object -First 1 |
ForEach-Object {
$_.Value
}
if ($envValue -eq $null) {
Write-Verbose "$Name = $Default"
Write-Output $Default
} else {
Write-Verbose "$Name = $envValue"
Write-Output $envValue
}
}
$VerbosePreference = 'Continue'
# SLACK_TOKEN and BOT_ADMINS environment variables are REQUIRED
# If they where not passed in then bail
$slackToken = Get-FromEnv -Name 'SLACK_TOKEN' -Default [string]::empty
$BotAdmins = Get-FromEnv -Name 'BOT_ADMINS' -Default @([string]::empty)
if ([string]::IsNullOrEmpty($SlackToken) -or $BotAdmins.Count -eq 0) {
Write-Error -Message 'Please specify your Slack token and initial list of bot administrators'
exit 1
}
Import-Module -Name PoshBot -ErrorAction Ignore
$cmdPrefix = Get-FromEnv -Name 'POSHBOT_CMD_PREFIX' -Default '!'
$altCmdPrefixes = Get-FromEnv -Name 'POSHBOT_ALT_CMD_PREFIXES' -Default 'poshbot'
$altCmdPrefixes = $altCmdPrefixes -split ';'
$pluginRepos = Get-FromEnv -Name 'POSHBOT_PLUGIN_REPOSITORIES' -Default 'PSGallery'
$pluginRepos = $pluginRepos -split ';'
$logDir = Get-FromEnv -Name 'POSHBOT_LOG_DIR' -Default 'c:/poshbot_data/logs'
$botName = Get-FromEnv -Name 'POSHBOT_NAME' -Default 'PoshBot_Docker'
$confDir = Get-FromEnv -Name 'POSHBOT_CONF_DIR' -Default 'c:/poshbot_data'
$logLevel = Get-FromEnv -Name 'POSHBOT_LOG_LEVEL' -Default 'Verbose'
$pluginDir = Get-FromEnv -Name 'POSHBOT_PLUGIN_DIR' -Default 'c:/poshbot'
$muteUnknownCommand = Get-FromEnv -Name 'POSHBOT_MUTE_UNKNOWN_CMD' -Default $false
$manifestsToLoad = Get-FromEnv -Name 'POSHBOT_MANIFESTS_TO_LOAD' -Default @()
$manifestsToLoad = $manifestsToLoad -split ';'
$altCmdPrefixSep = Get-FromEnv -Name 'POSHBOT_ALT_CMD_PREFIX_SEP' -Default @(':',',',';')
$altCmdPrefixSep = $altCmdPrefixSep.ToCharArray()
$sendCmdRespToPriv = Get-FromEnv -Name 'POSHBOT_SEND_CMD_RESP_TO_PRIV' -Default @()
$sendCmdRespToPriv = $sendCmdRespToPriv -split ';'
$configPSD1 = (Join-Path -Path $confDir -ChildPath 'PoshBot.psd1')
if (-not (Test-Path -Path $configPSD1)) {
# Create the initial configuration
$hash = @{
Name = $botName
ConfigurationDirectory = $confDir
CommandPrefix = $cmdPrefix
PluginRepository = $pluginRepos
LogDirectory = $logDir
BotAdmins = $BotAdmins
BackendConfiguration = @{
Token = $slackToken
Name = 'SlackBackend'
}
LogLevel = $logLevel
AlternateCommandPrefixes = $altCmdPrefixes
PluginDirectory = $pluginDir
MuteUnknownCommand = $muteUnknownCommand
ModuleManifestsToLoad = $manifestsToLoad
AlternateCommandPrefixSeperators = $altCmdPrefixSep
SendCommandResponseToPrivate = $sendCmdRespToPriv
PluginConfiguration = @{}
}
$config = New-PoshBotConfiguration @hash -Verbose
if (-not (Test-Path -Path $confDir)) {
New-Item -Path $confDir -ItemType Directory -Force > $null
}
$config | Save-PoshBotConfiguration -Path $configPSD1 -Force -Verbose
$pbc = Get-PoshBotConfiguration -Path $configPSD1 -Verbose
} else {
# There was a previous configuraiton
# Merge any values from env vars into config
$config = Import-PowerShellDataFile -Path $configPSD1
$config.Name = $botName
$config.ConfigurationDirectory = $confDir
$config.CommandPrefix = $cmdPrefix
$config.PluginRepository = $pluginRepos
$config.LogDirectory = $logDir
$config.BotAdmins = $BotAdmins
$config.BackendConfiguration = @{
Token = $slackToken
Name = 'SlackBackend'
}
$config.LogLevel = $logLevel
$config.AlternateCommandPrefixes = $altCmdPrefixes
$config.PluginDirectory = $pluginDir
$config.MuteUnknownCommand = $muteUnknownCommand
$config.ModuleManifestsToLoad = $manifestsToLoad
$config.AlternateCommandPrefixSeperators = $altCmdPrefixSep
$config.SendCommandResponseToPrivate = $sendCmdRespToPriv
$config | Save-PoshBotConfiguration -Path $configPSD1 -Force -Verbose
$pbc = Get-PoshBotConfiguration -Path $configPSD1 -Verbose
}
$backend = New-PoshBotSlackBackend -Configuration $pbc.BackendConfiguration -Verbose
$bot = New-PoshBotInstance -Configuration $pbc -Backend $backend -Verbose
$bot.Start()