Skip to content

Instantly share code, notes, and snippets.

@nakomis
Created October 5, 2016 13:48
Show Gist options
  • Save nakomis/a6d146b9cb6ebcdb2043acaaf8e1e2ba to your computer and use it in GitHub Desktop.
Save nakomis/a6d146b9cb6ebcdb2043acaaf8e1e2ba to your computer and use it in GitHub Desktop.
name: Python Web Server
location: my-first-location
services:
- type: vanilla-bash-server
name: My Bash Web Server VM
brooklyn.initializers:
- type: org.apache.brooklyn.core.effector.ssh.SshCommandEffector
brooklyn.config:
name: showlog
description: Show the access log on the index page
command: >
grep Logs index.html >/dev/null || {
echo '<h3>Logs</h3>' >> index.html
sed -i '/h3.Logs..h3/a <a href="output.txt"><p>Access Log</p></a>'
index.html
}
- type: org.apache.brooklyn.core.effector.ssh.SshCommandEffector
brooklyn.config:
name: hidelog
description: Hide the access log on the index page
command: |
sed -i '/h3.Logs..h3/d' index.html
sed -i '/Access Log/d' index.html
- type: org.apache.brooklyn.core.effector.ssh.SshCommandEffector
brooklyn.config:
name: reset
description: Reset the access log to be empty
command: |
cat /dev/null > output.txt
- type: org.apache.brooklyn.core.effector.ssh.SshCommandEffector
brooklyn.config:
name: addlink
description: Add a link to the index page
command: |
grep Links index.html >/dev/null || echo '<h3>Links</h3>' >> index.html
sed -i "/h3.Links..h3/a <div><a href='${url}'>${text}</a><div>" index.html
parameters:
url:
description: The URL for the link
defaultValue: https://brooklyn.apache.org/
text:
description: The text for the link
defaultValue: Brooklyn
- type: org.apache.brooklyn.core.effector.ssh.SshCommandEffector
brooklyn.config:
name: space
description: Add an astronomy photo to the index page
command: |
POD=$(curl "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY" |
python -c 'import sys, json; print json.load(sys.stdin)["url"]' )
echo "<div><img src='${POD}'/></div>" >> index.html
- type: org.apache.brooklyn.core.effector.ssh.SshCommandEffector
brooklyn.config:
name: spaceSolution
description: Add an astronomy photo to the index page
command: |
POD=$(curl "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=${date}" |
python -c 'import sys, json; print json.load(sys.stdin)["url"]' )
echo "<div><img src='${POD}'/></div>" >> index.html
parameters:
date:
description: The date for the photo as YYYY-MM-DD
brooklyn.config:
install.command: |
# install python if not present
which python || \
{ apt-get update && apt-get install python ; } || \
{ yum update && yum install python ; } || \
{ echo WARNING: cannot install python && exit 1 ; }
customize.command: |
# create the web page to serve
cat > index.html << EOF
Hello world.
<p>
I am ${ENTITY_INFO}, ${MESSAGE:-a Brooklyn sample}.
<p>
Created at: `date`
<p>
I am running at ${HOSTNAME}, with on-box IP configuration:
<pre>
`ifconfig | grep inet`
</pre>
EOF
launch.command: >
# launch in background (ensuring no streams open), and record PID to
file
nohup python -m SimpleHTTPServer ${PORT:-8020} < /dev/null > output.txt
2>&1 &
echo $! > ${PID_FILE:-pid.txt}
sleep 5
ps -p `cat ${PID_FILE:-pid.txt}`
if [ $? -ne 0 ] ; then
cat output.txt
echo WARNING: python web server not running
exit 1
fi
shell.env:
HOSTNAME: '$brooklyn:attributeWhenReady("host.name")'
PORT: '$brooklyn:config("my.app.port")'
ENTITY_INFO: '$brooklyn:component("this", "")'
MESSAGE: '$brooklyn:config("my.message")'
my.app.port: 8020
my.message: good to meet you
brooklyn.enrichers:
- type: org.apache.brooklyn.enricher.stock.Transformer
brooklyn.config:
uniqueTag: url-generator
enricher.sourceSensor: host.subnet.hostname
enricher.targetSensor: >-
$brooklyn:sensor("org.apache.brooklyn.core.entity.Attributes",
"main.uri")
enricher.targetValue:
'$brooklyn:formatString':
- 'http://%s:%s/'
- '$brooklyn:attributeWhenReady("host.name")'
- '$brooklyn:config("my.app.port")'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment