Skip to content

Commit

Permalink
new example VM reconfigure to Slack
Browse files Browse the repository at this point in the history
Signed-off-by: opvizordz <dzimmer@opvizor.com>
  • Loading branch information
opvizordz committed Nov 26, 2019
1 parent a3988ad commit b35a3e2
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 0 deletions.
75 changes: 75 additions & 0 deletions examples/powercli/hwchange-slack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# PowerCLI Function Example

## Description

This function demonstrates using PowerCLI to send VM configuration changes to Slack when the VM Reconfigure Event is triggered

There is a blog post covering this example in detail: [Audit VM configuration changes using the vCenter Event Broker
](https://www.opvizor.com/audit-vm-configuration-changes-using-the-vcenter-event-broker)

The custom PowerShell template for OpenFaaS is using [PSSlack](https://github.com/RamblingCookieMonster/PSSlack)

## Instruction

Step 1 - Setup Slack

Make sure to create a channel for the notifications and a [Slack webhook](https://my.slack.com/services/new/incoming-webhook/).


Step 2 - Update `stack.yml` and `vcconfig.json` with your enviornment information

`stack.yml` **lines: gateway, image**

```
provider:
name: faas
gateway: https://veba.mynetwork.local
functions:
powercli-reconfigure:
lang: powercli
handler: ./handler
image: opvizorpa/powercli-slack:latest
environment:
write_debug: true
read_debug: true
function_debug: false
secrets:
- vcconfig
annotations:
topic: vm.reconfigured
```

`vcconfig.json`

```
{
"VC" : "my-vCenter",
"VC_USERNAME" : "user@vsphere.local",
"VC_PASSWORD" : "userpassword",
"SLACK_URL" : "https://my.slack.com/services/new/incoming-webhook/",
"SLACK_CHANNEL" : "vcevent"
}
```

Step 3 - Build the function container

```
faas-cli build -f stack.yml
```

Step 4 - Push the function container to Docker Registry (default but can be changed to internal registry)

```
faas-cli push -f stack.yml
```

Step 5 - Deploy function to VEBA

```
VEBA_GATEWAY=https://veba.primp-industries.com
export OPENFAAS_URL=${VEBA_GATEWAY} # this is handy so you don't have to keep specifying OpenFaaS endpoint in command-line
faas-cli login --username admin --password-stdin --tls-no-verify # login with your admin password
faas-cli secret create vcconfig --from-file=vcconfig.json --tls-no-verify # create secret, only required once
faas-cli deploy -f stack.yml --tls-no-verify
45 changes: 45 additions & 0 deletions examples/powercli/hwchange-slack/handler/script.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Process function Secrets passed in
$VC_CONFIG_FILE = "/var/openfaas/secrets/vcconfig"
$VC_CONFIG = (Get-Content -Raw -Path $VC_CONFIG_FILE | ConvertFrom-Json)
if($env:function_debug -eq "true") {
Write-host "DEBUG: `"$VC_CONFIG`""
}

# Process payload sent from vCenter Server Event
$json = $args | ConvertFrom-Json
if($env:function_debug -eq "true") {
Write-Host "DEBUG: `"$json`""
}

$eventObjectName = $json.objectName

# import and configure Slack
Import-Module PSSlack | Out-Null


Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -DisplayDeprecationWarnings $false -ParticipateInCeip $false -Confirm:$false | Out-Null

# Connect to vCenter Server
Write-Host "Connecting to vCenter Server ..."
Connect-VIServer -Server $($VC_CONFIG.VC) -User $($VC_CONFIG.VC_USERNAME) -Password $($VC_CONFIG.VC_PASSWORD)

# Retrieve VM changes
$Message = (Get-VM $eventObjectName | Get-ViEvent -MaxSamples 1).FullFormattedMessage

# Bold format for titles
[string]$Message = $Message -replace "Modified","*Modified*" -replace "Added","*Added*" -replace "Deleted","*Deleted*"

# Send VM changes
Write-Host "Detected change to $eventObjectName ..."

New-SlackMessageAttachment -Color $([System.Drawing.Color]::red) `
-Title 'VM Change detected' `
-Text "$Message" `
-Fallback 'ouch' |
New-SlackMessage -Channel $($VC_CONFIG.SLACK_CHANNEL) `
-IconEmoji :fire: |
Send-SlackMessage -Uri $($VC_CONFIG.SLACK_URL)


Write-Host "Disconnecting from vCenter Server ..."
Disconnect-VIServer * -Confirm:$false
16 changes: 16 additions & 0 deletions examples/powercli/hwchange-slack/stack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
provider:
name: faas
gateway: https://veba.mynetwork.local
functions:
powercli-reconfigure:
lang: powercli
handler: ./handler
image: opvizorpa/powercli-slack:latest
environment:
write_debug: true
read_debug: true
function_debug: false
secrets:
- vcconfig
annotations:
topic: vm.reconfigured
27 changes: 27 additions & 0 deletions examples/powercli/hwchange-slack/template/powercli/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM vmware/powerclicore:latest

RUN mkdir -p /home/app
USER root
RUN echo "Pulling watchdog binary from Github." \
&& curl -sSL https://github.com/openfaas/faas/releases/download/0.9.14/fwatchdog > /usr/bin/fwatchdog \
&& chmod +x /usr/bin/fwatchdog \
&& cp /usr/bin/fwatchdog /root

# Add PSSlack https://github.com/RamblingCookieMonster/PSSlack
RUN pwsh -c "\$ProgressPreference = \"SilentlyContinue\"; Install-Module PSSlack"

WORKDIR /root

USER root

# Populate example here - i.e. "cat", "sha512sum" or "node index.js"
SHELL [ "pwsh", "-command" ]
ENV fprocess="xargs pwsh ./function/script.ps1"
COPY function function
# Set to true to see request in function logs
ENV write_debug="true"

EXPOSE 8080

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1
CMD [ "fwatchdog" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -DisplayDeprecationWarnings $false -ParticipateInCeip $false -Confirm:$false
write-host "write your powercli code here"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: powercli
fprocess: xargs pwsh ./function/script.ps1

7 changes: 7 additions & 0 deletions examples/powercli/hwchange-slack/vcconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"VC" : "my-vCenter",
"VC_USERNAME" : "user@vsphere.local",
"VC_PASSWORD" : "userpassword",
"SLACK_URL" : "https://my.slack.com/services/new/incoming-webhook/",
"SLACK_CHANNEL" : "vcevent"
}

0 comments on commit b35a3e2

Please sign in to comment.