Skip to content

Commit

Permalink
added foreman skeleton app and heroku deploy instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
gleicon committed Nov 25, 2012
1 parent f15d643 commit ff901d3
Show file tree
Hide file tree
Showing 25 changed files with 508 additions and 7 deletions.
25 changes: 20 additions & 5 deletions cyclone/app.py
Expand Up @@ -54,14 +54,16 @@ def new_project(**kwargs):
else:
ext = n.rsplit(".", 1)[-1]
fd = open(os.path.join(dst, mod), "w", 0644)
if ext in ("conf", "html", "py", "md", "sh"):
if ext in ("conf", "html", "py", "md", "sh") or n in ('Procfile'):
fd.write(string.Template(zf.read(n)).substitute(kwargs))
else:
fd.write(zf.read(n))
fd.close()

# make sure we can actually run start.sh
os.chmod(os.path.join(dst, 'start.sh'), 0755)
if os.path.exists(os.path.join(dst, 'start.sh')):
os.chmod(os.path.join(dst, 'start.sh'), 0755)

if kwargs["use_git"] is True:
os.chdir(kwargs["project_path"])
os.system("git init")
Expand All @@ -80,6 +82,8 @@ def usage(version, target):
-s --set-pkg-version Set version on package name [default: False]
-t --target=PATH Set path where project is created [default: %s]
-l --license=FILE Append the following license file [default: Apache 2]
-f --foreman Create a foreman based project (suited to run on heroku
and other PaaS)
""" % (version, target))
sys.exit(0)

Expand All @@ -92,10 +96,12 @@ def main():
license_file = None
default_version, version = "0.1", None
default_target, target = os.getcwd(), None
foreman = False

shortopts = "hgsp:m:v:t:l:"
shortopts = "hgsp:m:v:t:l:f"
longopts = ["help", "git", "set-pkg-version",
"project=", "modname=", "version=", "target=", "license="]
"project=", "modname=", "version=", "target=", "license=",
"foreman"]
try:
opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
except getopt.GetoptError:
Expand Down Expand Up @@ -125,6 +131,10 @@ def main():

elif o in ("-l", "--license"):
license_file = a

elif o in ("-f", "--foreman"):
foreman = True


if license_file is None:
license = DEFAULT_LICENSE
Expand Down Expand Up @@ -167,6 +177,11 @@ def main():
"'%s': permission denied" % target)
sys.exit(1)

if foreman is False:
skel_file = "app_skel.zip"
else:
skel_file = "foreman_skel.zip"

name = "Foo Bar"
email = "root@localhost"
if use_git is True:
Expand All @@ -185,7 +200,7 @@ def main():

skel = zipfile.ZipFile(open(
os.path.join(os.path.dirname(os.path.abspath(__file__)),
"appskel.zip"), "rb"))
skel_file), "rb"))

if set_pkg_version is True:
project_name = "%s-%s" % (project, version)
Expand Down
Binary file added cyclone/foreman_skel.zip
Binary file not shown.
1 change: 1 addition & 0 deletions foreman_skel/.env
@@ -0,0 +1 @@
PYTHONPATH=.
3 changes: 3 additions & 0 deletions foreman_skel/.gitignore
@@ -0,0 +1,3 @@
*.swp
*.pyc
dropin.cache
1 change: 1 addition & 0 deletions foreman_skel/Procfile
@@ -0,0 +1 @@
web: python $modname/main.py foobar.conf
120 changes: 120 additions & 0 deletions foreman_skel/README.md
@@ -0,0 +1,120 @@
# cyclone-based project for heroku and other PaaS based on foreman.

This is the source code of $project_name
$name <$email>

## About

This file has been created automatically by cyclone-tool for $project_name.
It contains the following files:

- ``Procman``: standard foreman file
- ``$modname.conf``: configuration file for the web server
- ``$modname/__init__.py``: information such as author and version of this package
- ``$modname/web.py``: map of url handlers and main class of the web server
- ``$modname/config.py``: configuration parser for ``$modname.conf``
- ``$modname/views.py``: code of url handlers for the web server
- ``scripts/localefix.py``: script to fix html text before running ``xgettext``
- ``scripts/cookie_secret.py``: script for generating new secret key for the web server

### Running

For development and testing:

gem install foreman
cd $project_name
foreman start

For production on any foreman based env:

Follow foreman instructions, configure Procman as needed. Check the .env file and the configuration file for your app.

For production at heroku:

- Start a git repo
git init
git add .
git commit -m 'first'
heroku create $project_name (or whatever name you want)
git push heroku master
- check your app, make it better, create a db, etc

### Convert this document to HTML

Well, since this is a web server, it might be a good idea to convert this document
to HTML before getting into customization details.

This can be done using [markdown](http://daringfireball.net/projects/markdown/).

brew install markdown
markdown README.md > frontend/static/readme.html

And point your browser to <http://localhost:8888/static/readme.html> after this server
is running.

## Customization

This section is dedicated to explaining how to customize your brand new package.

### Databases

cyclone provides built-in support for SQLite and Redis databases.
It also supports any RDBM supported by the ``twisted.enterprise.adbapi`` module,
like MySQL or PostgreSQL.

The default configuration file ``$modname.conf`` ships with pre-configured
settings for SQLite, Redis and MySQL.

The code for loading all the database settings is in ``$modname/config.py``.
Feel free to comment or even remove such code, and configuration entries. It
shouldn't break the web server.

Take a look at ``$modname/utils.py``, which is where persistent database
connections are initialized.


### Internationalization

cyclone uses the standard ``gettext`` library for dealing with string
translation.

Make sure you have the ``gettext`` package installed. If you don't, you won't
be able to translate your software.

For installing the ``gettext`` package on Debian and Ubuntu systems, do this:

apt-get install gettext

For Mac OS X, I'd suggest using [HomeBrew](http://mxcl.github.com/homebrew>).
If you already use HomeBrew, run:

brew install gettext
brew link gettext

For generating translatable files for HTML and Python code of your software,
run this:

cat frontend/template/*.html $modname/*.py | python scripts/localefix.py | \
xgettext - --language=Python --from-code=utf-8 --keyword=_:1,2 -d $modname

Then translate $modname.po, compile and copy to the appropriate locale
directory:

(pt_BR is used as example here)
vi $modname.po
mkdir -p frontend/locale/pt_BR/LC_MESSAGES/
msgfmt $modname.po -o frontend/locale/pt_BR/LC_MESSAGES/$modname.mo

There are sample translations for both Spanish and Portuguese in this package,
already compiled.


### Cookie Secret

The current cookie secret key in ``$modname.conf`` was generated during the
creation of this package. However, if you need a new one, you may run the
``scripts/cookie_secret.py`` script to generate a random key.

## Credits

- [cyclone](http://github.com/fiorix/cyclone) web server.
Binary file not shown.
25 changes: 25 additions & 0 deletions foreman_skel/frontend/locale/es_ES/LC_MESSAGES/modname.po
@@ -0,0 +1,25 @@
# cyclone-tools sample translation.
# Copyright (C) 2011 Alexandre Fiori
# This file is distributed under the same license as the cyclone package.
# Alexandre Fiori <fiorix@gmail.com>, 2011.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: 0.1\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-02-24 15:36-0300\n"
"PO-Revision-Date: 2011-02-28 15:44+-0300\n"
"Last-Translator: Alexandre Fiori <fiorix@gmail.com>\n"
"Language-Team: Alexandre Fiori <fiorix@gmail.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: standard input:5
msgid "cyclone web server"
msgstr "servidor web cyclone"

#: standard input:8
msgid "It works!"
msgstr "¡Funciona!"
Binary file not shown.
25 changes: 25 additions & 0 deletions foreman_skel/frontend/locale/pt_BR/LC_MESSAGES/modname.po
@@ -0,0 +1,25 @@
# cyclone-tools sample translation.
# Copyright (C) 2011 Alexandre Fiori
# This file is distributed under the same license as the cyclone package.
# Alexandre Fiori <fiorix@gmail.com>, 2011.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: 0.1\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-02-24 15:36-0300\n"
"PO-Revision-Date: 2011-02-28 15:44+-0300\n"
"Last-Translator: Alexandre Fiori <fiorix@gmail.com>\n"
"Language-Team: Alexandre Fiori <fiorix@gmail.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: standard input:5
msgid "cyclone web server"
msgstr "servidor web cyclone"

#: standard input:8
msgid "It works!"
msgstr "Funciona!"
Binary file added foreman_skel/frontend/static/favicon.ico
Binary file not shown.
26 changes: 26 additions & 0 deletions foreman_skel/frontend/template/base.html
@@ -0,0 +1,26 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{_("cyclone web server")}}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap-responsive.css" rel="stylesheet">
<link rel="shortcut icon" type="image/ico" href="{{ static_url('favicon.ico') }}">
<style>
body {padding-top: 60px;}
</style>

</head>

<body>
<div class="container">
<!--
you can create multiple block entries per template
ex: sidebar, menu, footer
-->
{% block page %}{% end %}
</div> <!-- /container -->
</body>
</html>
14 changes: 14 additions & 0 deletions foreman_skel/frontend/template/index.html
@@ -0,0 +1,14 @@
{% extends 'base.html' %}
{% block page %}
<h1>{{_("It works!")}}</h1>
<p><small>
<a href="/lang/en_US">English</a>&nbsp;
<a href="/lang/es_ES">Español</a>&nbsp;
<a href="/lang/pt_BR">Português</a>
</small></p>
<p>hello = {{hello}}</p>
<p>awesome = {{awesome}}</p>
<form class="well" method="post">
<button type="submit" class="btn">Post example</button>
</form>
{% end %}
7 changes: 7 additions & 0 deletions foreman_skel/frontend/template/post.html
@@ -0,0 +1,7 @@
{% extends 'base.html' %}
{% block page %}
<h1>POST example</h1>
<p>This variable exists = {{fields.ip}}</p>
<p>This variable does not exist = {{fields.something}}</p>
<p>This one comes from .conf = {{fields.mysql_host}}</p>
{% end %}
31 changes: 31 additions & 0 deletions foreman_skel/modname.conf
@@ -0,0 +1,31 @@
[server]
debug = true
xheaders = false
xsrf_cookies = false
cookie_secret = $cookie_secret

[frontend]
locale_path = frontend/locale
static_path = frontend/static
template_path = frontend/template

[sqlite]
enabled = yes
database = :memory:

[redis]
enabled = no
host = 127.0.0.1
port = 6379
dbid = 0
poolsize = 10

[mysql]
enabled = no
host = 127.0.0.1
port = 3306
username = foo
password = bar
database = dummy
poolsize = 10
debug = no
6 changes: 6 additions & 0 deletions foreman_skel/modname/__init__.py
@@ -0,0 +1,6 @@
# coding: utf-8
#
$license

__author__ = "$name <$email>"
__version__ = "$version"

0 comments on commit ff901d3

Please sign in to comment.