1+ # Note, this contains some advanced info.
2+
13import os
24import sys
35import subprocess
46
7+ # Import Cloudify's context object.
8+ # This provides several useful functions as well as allowing to pass
9+ # contextual information of an application.
510from cloudify import ctx
611
712
813IS_WIN = os .name == 'nt'
9- # is this needed?
1014IS_DARWIN = (sys .platform == 'darwin' )
15+
16+ # Get the port from the blueprint. We're running in the context of the
17+ # `http_web_server` node so we can read its `port` property.
1118PORT = ctx .node .properties ['port' ]
1219
1320
@@ -16,11 +23,15 @@ def run_server():
1623 if not IS_WIN :
1724 webserver_cmd .insert (0 , 'nohup' )
1825
26+ # The ctx object provides a built in logger.
1927 ctx .logger .info ('Running WebServer locally on port: {0}' .format (PORT ))
28+ # emulating /dev/null
2029 dn = open (os .devnull , 'wb' )
2130 process = subprocess .Popen (webserver_cmd , stdout = dn , stderr = dn )
2231
23- if not IS_WIN :
32+ # we need this for linux because on linux process.pid is not accurate.
33+ # it provides somewhat arbitrary values.
34+ if not IS_WIN and not IS_DARWIN :
2435 del webserver_cmd [0 ]
2536 get_pid = ['pidof' , '-s' ]
2637 get_pid .extend (webserver_cmd )
@@ -31,6 +42,10 @@ def run_server():
3142
3243def set_pid (pid ):
3344 ctx .logger .info ('Setting `pid` runtime property: {0}' .format (pid ))
45+ # We can set runtime information in our context object which
46+ # can later be read somewhere in the context of the instance.
47+ # For instance, we want to save the `pid` here so that when we
48+ # run `uninstall.py`, we can destroy the process.
3449 ctx .instance .runtime_properties ['pid' ] = pid
3550
3651
0 commit comments