Last active
August 18, 2017 10:24
-
-
Save andreaturli/a6738e712fd9adce91a89251184404dc to your computer and use it in GitHub Desktop.
Python Web Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Python Web Server | |
# this example deploys a very simple python web server | |
services: | |
- type: vanilla-bash-server | |
name: My Bash Web Server VM | |
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:-an AMP 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") | |
# custom | |
my.app.port: 8020 | |
my.message: "good to meet you" | |
brooklyn.enrichers: | |
# publish the URL as a sensor; the GUI will pick this up (main.uri) | |
- type: org.apache.brooklyn.enricher.stock.Transformer | |
brooklyn.config: | |
uniqueTag: url-generator | |
enricher.sourceSensor: host.name | |
# use the definition from Attributes class, as it has a RendererHint so GUI makes it a link | |
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") | |
location: localhost |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment