Permalink
Browse files

Merge branch 'master' of github.com:TeamGamma/directedstudies

  • Loading branch information...
2 parents 687dd45 + f359e6c commit fbcbd7e398880da39c56b6c16361b861ba555eef @alimjiwa alimjiwa committed Mar 21, 2012
View
@@ -2,4 +2,4 @@ Vagrantfile
.DS_Store
*.pyc
*.egg-info
-workloads
+config.py
@@ -0,0 +1,32 @@
+from datetime import timedelta
+
+TRANSACTION_SERVER_HOST = '192.168.252.{{ transaction_server }}'
+TRANSACTION_SERVER_PORT = 6000
+
+DATABASE_CONNECTION_ARGS = {
+ 'drivername': 'mysql',
+ 'host': '192.168.252.{{ database_server }}',
+ 'port': 3306,
+ 'database': 'sps',
+ 'username': 'root',
+ 'password': 'root',
+}
+DATABASE_ENGINE_ARGS = {
+ 'echo': False,
+}
+
+DATABASE_TABLE_ARGS = {
+ 'mysql_engine': 'InnoDB',
+}
+
+QUOTE_CLIENT = '{{ quote_client }}'
+
+TRANSACTION_TIMEOUT = timedelta(seconds=60)
+
+TEST_WITH_SQLITE = True
+
+DUMPLOG_DIR = '/tmp/'
+
+# Interval between trigger checks in seconds
+TRIGGER_INTERVAL = 3
+
@@ -0,0 +1,22 @@
+# This file describes the network interfaces available on your system
+# and how to activate them. For more information, see interfaces(5).
+
+# ResNet1: Connection to rest of lab and internet - no heavy traffic allowed
+auto eth2
+iface eth2 inet dhcp
+
+# ResNet2 : Use for internal system traffic
+iface eth3 inet static
+address 192.168.252.{{ machine_number }}
+netmask 255.255.255.0
+
+# ResNet3 : Use for load testing
+iface eth0 inet static
+address 192.168.251.{{ machine_number }}
+netmask 255.255.255.0
+
+# ResNet4 : Use for transferring funny pictures of kittens
+iface eth1 inet static
+address 192.168.250.{{ machine_number }}
+netmask 255.255.255.0
+
View
@@ -0,0 +1,13 @@
+from fabric.api import env, roles, hosts
+
+def default_roles(*role_list):
+ def selectively_attach(func):
+ if not env.roles and not env.hosts:
+ return roles(*role_list)(func)
+ else:
+ if env.hosts:
+ func = hosts(*env.hosts)(func)
+ if env.roles:
+ func = roles(*env.roles)(func)
+ return func
+ return selectively_attach
View
@@ -12,26 +12,62 @@
from fabric.api import env, settings, roles, execute
from fabric.operations import sudo, run, put
from fabric.context_managers import cd, hide
-from fabric.contrib.files import exists
-from os.path import join, abspath, dirname
+from fabric.contrib.files import exists, upload_template
+from deploy_utils import default_roles
+from os import path
+import re
github_repo = 'git://github.com/TeamGamma/directedstudies.git'
-fabdir = abspath(dirname(__file__))
+fabdir = path.abspath(path.dirname(__file__))
-vm_address = '127.0.0.1:2222'
env.roledefs = {
- 'db': [vm_address],
- 'web': [vm_address],
- 'transaction': [vm_address],
+ 'db': ['a01'],
+ 'web': ['a02'],
+ 'transaction': ['a03'],
+ 'vm': ['vagrant@127.0.0.1:2222']
}
# password auth
-env.user = 'vagrant'
-env.password = 'vagrant'
+env.user = 'direct'
+env.password = 'direct'
# Prevents errors with some terminal commands (service)
env.always_use_pty = False
+@default_roles('transaction', 'web', 'db')
+def update_network():
+ """
+ Updates /etc/network/interfaces and brings up the network interfaces.
+ """
+ name = env.host
+ machine_number = int(name[1:])
+ upload_template('network/interfaces', '/etc/network/interfaces',
+ template_dir=path.join(fabdir, 'config'),
+ use_jinja=True, use_sudo=True, backup=True,
+ context=dict(machine_number=machine_number))
+ run('cat /etc/network/interfaces')
+ for interface in ['eth0', 'eth1', 'eth3']:
+ sudo('ifdown %s' % interface)
+ sudo('ifup %s' % interface)
+
+
+@default_roles('transaction', 'web', 'db')
+def update_config_file(quote_client='sps.quotes.client.RandomQuoteClient'):
+ """
+ Updates the remote config file with the local one
+ """
+ # Use fabfile's predefined server names
+ context = {
+ "transaction_server": _machine_num(env.roledefs['transaction'][0]),
+ "database_server": _machine_num(env.roledefs['db'][0]),
+ "quote_client": quote_client,
+ }
+ upload_template('config_template.py',
+ '/srv/directedstudies/config.py', template_dir=path.join(fabdir, 'config'),
+ use_jinja=True, use_sudo=True, backup=True, context=context)
+
+ run('cat /srv/directedstudies/config.py')
+
def blank():
""" Used for testing fabric configuration """
pass
@@ -61,7 +97,7 @@ def deploy_base():
sudo('apt-get update')
# ubuntu goodies
- sudo("apt-get install --assume-yes build-essential python-pip python-dev python-mysqldb git-core sqlite3 apache2 libapache2-mod-wsgi python-mysqldb fabric")
+ sudo("apt-get install --assume-yes build-essential python-pip python-dev python-mysqldb git-core sqlite3 python-mysqldb fabric python-lxml")
# Make top-level folder if it doesn't exist
sudo('mkdir -p /srv')
@@ -80,14 +116,19 @@ def deploy_base():
# Install python libs
sudo('pip install -r requirements.txt')
+ update_config_file()
+
@roles('web')
def deploy_web():
""" Deploys the web server """
deploy_base()
- #configure apache for wsgi by copying the repo config to remote install
- put(join(fabdir, 'config/apache/wsgi_configuration'),
+ # Install apache
+ sudo('apt-get -y install apache2 libapache2-mod-wsgi')
+
+ # configure apache for wsgi by copying the repo config to remote install
+ put(path.join(fabdir, 'config/apache/wsgi_configuration'),
'/etc/apache2/sites-available/wsgi_configuration', use_sudo=True)
# create a link to the site config and delete default site config
@@ -113,13 +154,18 @@ def deploy_db():
sudo('apt-get -y install mysql-server')
# Configure the database server
- put(join(fabdir, 'config/mysql/my.cnf'), '/etc/mysql/my.cnf', use_sudo=True)
+ put(path.join(fabdir, 'config/mysql/my.cnf'), '/etc/mysql/my.cnf', use_sudo=True)
# Restart database server
sudo('service mysql restart')
- # Create the database
- sudo('echo "CREATE DATABASE IF NOT EXISTS sps;" | mysql -h127.0.0.1 -uroot -proot')
+ # Create the database (totally insecure)
+ sql = """
+ CREATE DATABASE IF NOT EXISTS sps;
+ CREATE USER 'root'@'%' IDENTIFIED BY 'root';
+ GRANT ALL ON sps.* TO 'root'@'%';
+ """
+ sudo('echo "%s" | mysql -h127.0.0.1 -uroot -proot' % sql)
with cd('/srv/directedstudies/'):
#use rob's local fabfile to deal with setting up the tables
@@ -144,6 +190,7 @@ def update_code():
with cd('/srv/directedstudies'):
sudo('git pull')
+
@roles('transaction')
def restart_transaction_server():
""" Restarts the transaction server """
@@ -161,3 +208,11 @@ def restart_db():
sudo('service mysql restart')
+def _machine_num(servername):
+ """
+ Translates a server DNS name (a01) to an IP address on the internal
+ network
+ """
+ hostname = servername.split(':')[0]
+ return int(re.search('\d+', hostname).group(0))
+
View
@@ -33,8 +33,17 @@ def drop_tables():
from sps.database.session import get_session
from sps.database.models import Base
session = get_session()
+
+ # Drop all tables except user
+ tables = Base.metadata.tables
+ non_user_tables = [table for tablename, table in tables.items() if tablename != 'users']
+ Base.metadata.drop_all(bind=session.connection(), checkfirst=False,
+ tables=non_user_tables)
+
+ # Now drop the user table
Base.metadata.drop_all(bind=session.connection(), checkfirst=False)
+
def setup_database():
"""
Recreates all tables and installs some example fixtures in the database
View
@@ -8,4 +8,3 @@ ipython==0.12
unittest2==0.5.1
eventlet==0.9.16
SQLAlchemy==0.7.5
-lxml==2.3.3
View
@@ -1,29 +1,34 @@
#!/usr/bin/env python
import sys
-print >> sys.stderr, 'STARTED'
-from sps.config import config
+from sps.config import config, read_config_file
from sps.transactions.server import TransactionServer
+from os.path import exists, normpath
def main():
from optparse import OptionParser
parser = OptionParser()
- parser.add_option("-p", "--port", dest="port",
- default=config.TRANSACTION_SERVER_PORT,
- help="port to run server on", metavar="PORT")
parser.add_option("-d", "--debug", dest="debug", action="store_true",
default=False,
help="Run server in debug mode (autoreload)")
-
- (opt, args) = parser.parse_args()
- print opt, args
+ parser.add_option("-f", "--config-file", dest="config",
+ default=config.CONFIG_FILE_PATH,
+ help="Path to configuration file", metavar="FILE")
import logging
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger('tserver')
+ (opt, args) = parser.parse_args()
+ log.debug(opt)
+ log.debug(args)
+
+ if exists(opt.config):
+ log.info('Using config file "%s"', normpath(opt.config))
+ read_config_file(opt.config)
+
if(opt.debug):
log.info('Debug mode enabled')
from sps.utils.autoreload_eventlet import autoreload
@@ -33,8 +38,8 @@ def main():
exit(0)
try:
- log.info('Starting transaction server on port %d' % opt.port)
- transaction_server = TransactionServer(('0.0.0.0', opt.port))
+ log.info('Starting transaction server on port %d' % config.TRANSACTION_SERVER_PORT)
+ transaction_server = TransactionServer(('0.0.0.0', config.TRANSACTION_SERVER_PORT))
transaction_server.start()
except KeyboardInterrupt:
print >> sys.stderr, '\nInterrupted'
View
@@ -1,65 +0,0 @@
-from .database.session import get_session, setup_database
-from .config import ConfigObject, config
-
-from .database.models import (
- Money,
- Base,
- User,
- Query,
- Transaction,
- Trigger,
- StockPurchase,
-)
-
-from .quotes.client import (
- RandomQuoteClient,
- DummyQuoteClient,
- SENGQuoteClient,
-)
-
-from .transactions.commands import (
- CommandError,
- InsufficientFundsError,
- InsufficientStockError,
- UnknownCommandError,
- UserNotFoundError,
- InvalidInputError,
- NoBuyTransactionError,
- NoSellTransactionError,
- NoTriggerError,
- ExpiredBuyTransactionError,
- ExpiredSellTransactionError,
- BuyTransactionActiveError,
- SellTransactionActiveError,
- CommandHandler,
- EchoCommand,
- UppercaseCommand,
- ADDCommand,
- QUOTECommand,
- BUYCommand,
- COMMIT_BUYCommand,
- CANCEL_BUYCommand,
- SELLCommand,
- COMMIT_SELLCommand,
- CANCEL_SELLCommand,
- SET_BUY_AMOUNTCommand,
- SET_SELL_AMOUNTCommand,
- CANCEL_SET_BUYCommand,
- SET_BUY_TRIGGERCommand,
- SET_SELL_TRIGGERCommand,
- CANCEL_SET_SELLCommand,
- DUMPLOG_USERCommand,
- DISPLAY_SUMMARYCommand,
- DUMPLOGCommand,
-)
-
-from .transactions.server import TransactionServer
-
-from .transactions.xml import (
- QuoteResponse,
- ErrorResponse,
- ResultResponse,
- DumplogResponse,
- SummaryResponse,
-)
-
View
@@ -6,11 +6,15 @@
"""
from datetime import timedelta
+from os.path import abspath, dirname, join
class ConfigObject():
"""
Provides default values for all possible keys in the configuration file
"""
+ # Default location of config file is in directory above this one
+ CONFIG_FILE_PATH = join(abspath(dirname(__file__)), '..', 'config.py')
+
TRANSACTION_SERVER_HOST = 'localhost'
TRANSACTION_SERVER_PORT = 6000
View
@@ -1,3 +1,5 @@
+import eventlet.patcher
+eventlet.patcher.monkey_patch()
import MySQLdb
from eventlet.db_pool import ConnectionPool
from sqlalchemy.engine.url import URL
@@ -3,8 +3,8 @@
import re
import time
import eventlet
-#import eventlet.patcher
-#eventlet.patcher.monkey_patch()
+import eventlet.patcher
+eventlet.patcher.monkey_patch()
import logging
from sps.transactions.commands import CommandHandler, CommandError
Oops, something went wrong.

0 comments on commit fbcbd7e

Please sign in to comment.