Skip to content

Instantly share code, notes, and snippets.

@wrunk
Created May 31, 2011 18:29
Show Gist options
  • Save wrunk/1001022 to your computer and use it in GitHub Desktop.
Save wrunk/1001022 to your computer and use it in GitHub Desktop.
Simple Nudge app using Eventlet. Good starting point for a webapp.
#!/usr/bin/env python
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import nudge.arg as args
from nudge import Endpoint, Args
from nudge.renderer import Plain
from nudge.publisher import ServicePublisher
from optparse import OptionParser
import sys
import eventlet
import eventlet.wsgi
eventlet.monkey_patch()
class App(object):
def deploy(self):
''' Run a script to git pull and kill the django app which
supervisord will auto-restart '''
try:
subprocess.call(['git', 'pull'])
# To avoid giving passwordless sudo, kill and let supervisord
# restart
subprocess.call(['pkill', '-f', 'run_django_app'])
except Exception as e:
logging.exception(e)
return '''pulled and restarted'''
def view_log(self, byte_position=None):
''' View a log file on the server in plain text.
Optionally allow a byte position to start from. '''
fp = open('file', 'r')
log_data = fp.read()
fp.close()
return log_data
def kill_log(self, byte_position=None):
''' These logs will get long and annoying '''
fp = open('file', 'w')
fp.close()
return 'Killed log'
service = App()
service_description = [
Endpoint(name='deploy',
method='GET',
uri='/test/deploy$',
function=service.deploy,
renderer=Plain(),
),
Endpoint(name='view_log',
method='GET',
uri='/test/view_log$',
function=service.view_log,
renderer=Plain(),
),
Endpoint(name='kill_log',
method='GET',
uri='/test/kill_log$',
function=service.kill_log,
renderer=Plain(),
),
]
if __name__ == "__main__":
parser = OptionParser()
parser.add_option(
"-p", "--port", dest="port", help="port to run on", default=8080
)
(options, args) = parser.parse_args(sys.argv)
sp = ServicePublisher(
endpoints=service_description,
debug=True
)
port = int(options.port)
eventlet.wsgi.server(
eventlet.listen(('', port)),
sp,
max_size=100,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment