Skip to content

Commit

Permalink
updating work
Browse files Browse the repository at this point in the history
  • Loading branch information
vsoch committed Nov 2, 2016
1 parent 48f9d2c commit 6dfdb37
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 211 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -64,5 +64,9 @@ The web interface includes the following:
- complete documentation


#### Static deployment with your own mysql database
We have recently added support for generating a local battery that will work with your own mysql database. See [expfactory-server/mysql](expfactory-server/mysql) for details.


### Functions Provided
You can also use the library as a module, and import expfactory functions into your application. Please see our [documentation](http://expfactory.readthedocs.org/en/latest/getting-started.html) for details
2 changes: 2 additions & 0 deletions expfactory/battery.py
Expand Up @@ -197,6 +197,8 @@ def template_experiments(battery_dest,battery_repo,valid_experiments,template_lo
load_template = add_custom_variables(custom_variables["load"],load_template)

# load experiment scripts
if not os.path.exists("%s/static/js" %(battery_dest)):
os.mkdir("%s/static/js" %(battery_dest))
template_output = "%s/static/js/load_experiments.js" %(battery_dest)
filey = open(template_output,'w')
filey.writelines(load_template)
Expand Down
56 changes: 56 additions & 0 deletions server/mysql/README.md
@@ -0,0 +1,56 @@
# Expfactory Server (SQL)

This will provide a static (SQL) server setup, that can either be run on a server natively (locally), or with a MySQL Dockerized database. Instructions for composer are included below:

- Integration with [Concerto](https://github.com/campsych/concerto-platform/wiki):

The basic instructions are also included here.


## Customizing your SQL Server
This is primarily intended for development, and if you would like a complete Dockerized application we recommend either using expfactory-docker or submitting a PR to this repo. The following command, run from this directory, will set up a mysql database available on port 3306 called `expfactory-mysql`

docker run \
--detach \
--name=expfactory-mysql \
--env="MYSQL_ROOT_PASSWORD=composer" \
--env="MYSQL_USER=composer" \
--env="MYSQL_PASSWORD=composer" \
--publish 6603:3306 \
--volume=conf.d:/etc/mysql/conf.d \
--volume=data:/var/lib/mysql \
mysql

After doing the above, you will want to copy the sql file into the container, and run it to configure the database:


docker cp create_expfactory_table.sql expfactory-mysql:/create_expfactory_table.sql
docker exec -it expfactory-mysql mysql -u=composer -password=composer > create_expfactory_table.sql

You of course (are recommended) to set up a proper database. If you use the setup provided above, continue. Otherwise, be sure to change the host/etc credentials in `create_expfactory_table.php` and `database_conncect.php`.


## Generating the battery
If you have a server that can serve static files and want to deploy experiments using a MySQL database, this is the solution you should try. The batteries themselves are stored as static html files with a uid parameter in the query string to pass the subject id: e.g. `https://mywebserver/itest/digit_span-en/?uid=123456789`

PHP scripts, also hosted by the web server, are called on experiment completion to save the results (in JSON format) to your MySQL database.

This folder contains the required server resources to deploy.
Ensure to define your database connection parameters in `database_connect.php.`
In this folder, you will also find a [create_expfactory_table.sql](create_expfactory_table.sql) and a [concerto.conf](concerto.conf) file as an example of how to make [Concerto](https://github.com/campsych/concerto-platform/wiki).

### 1. Edit your post url
Edit the `post_url` variable in [webserver-battery-template.html](templates/webserver-battery-template.html) to change the default URL to the PHP script (`/itest/save_data.php`) if needed. If not a local URL, cross-origin resource sharing should be enabled : refer to [http://enable-cors.org/server_apache.html](http://enable-cors.org/server_apache.html)

### 2. Generate your static files
Then run [setup_battery_for_webserver.py](setup_battery_for_webserver.py) to generate the battery in a specified output folder (likely a web directory)


python setup_battery_for_webserver.py --output /var/www/vhosts/expfactory-server/digit_span-en --experiments digit_span


### 3. Testing
Though the [run_battery.py](run_battery.py) script has no use in production, you may test your batteries easily with:


python run_battery.py --port 8080 /var/www/vhosts/expfactory-server/digit_span-en
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions server/mysql/create_expfactory_table.sql
@@ -0,0 +1,3 @@
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user '=composer'@'localhost' (using password: YES)

File renamed without changes.
File renamed without changes.
88 changes: 88 additions & 0 deletions server/mysql/setup_battery_for_webserver.py
@@ -0,0 +1,88 @@
from expfactory.battery import *
from expfactory.utils import copy_directory
from expfactory.vm import download_repo
import argparse
from glob import glob
import os
import sys
import shutil
import tempfile


# Get CLI parameters
parser = argparse.ArgumentParser()
parser.add_argument('--output', dest="output", help='Battery output directory',default=None)
parser.add_argument('--experiments', dest="experiments", help='Experiment(s) to use in the battery, separated by commas.')

try:
args = parser.parse_args()
except:
parser.print_help()
sys.exit(0)

if args.output != None:
if os.path.exists(args.output):
print("Output directory should not exist, remove and run again.")
sys.exit(0)


# We will set up the repo downloads, etc, in a temporary place
working_dir = tempfile.mkdtemp()
experiments = ",".split(args.experiments)

# Download the missing repos
battery_repo='battery'
experiment_repo='experiments'
survey_repo='surveys'
game_repo='games'

# Download repos to working directory
print('Downloading expfactory repos...')
for repo in (battery_repo, experiment_repo, survey_repo, game_repo):
folder = "%s/%s" %(working_dir,repo)
if not (os.path.isdir(folder)):
download_repo(repo_type=repo,
destination=folder)

battery_dest = "%s/%s" %(working_dir,battery_repo)

# Generate battery with the experiment(s)
print('Generating base...')
base = generate_base(battery_dest=battery_dest,
tasks=experiments,
experiment_repo="%s/%s" %(working_dir,experiment_repo),
survey_repo="%s/%s" %(working_dir,survey_repo),
game_repo="%s/%s" %(working_dir,game_repo),
add_experiments=True,
add_surveys=False,
add_games=False,
battery_repo=battery_dest)

# Customize the battery
custom_variables = dict()
custom_variables["load"] = [("[SUB_TOTALTIME_SUB]", 30)]

template_exp = "templates/webserver-battery-template.html"
template_exp_output = "%s/index.html" %(base["battery_repo"])

print('Generating experiment and battery templates...')
template_experiments(battery_dest=base["battery_repo"],
battery_repo=base["battery_repo"],
valid_experiments=base["experiments"],
custom_variables=custom_variables,
template_exp=template_exp,
template_exp_output=template_exp_output)

# Copy needed php files there
add_files = glob("%s/*.php" %os.getcwd())
for add_file in add_files:
shutil.copyfile(add_file,"%s/%s" %(base['battery_repo'],
os.path.basename(add_file)))

# Remove git data from the battery
if args.output != None:
copy_directory(base["battery_repo"],args.output)
battery_dest = args.output

print('Battery generated in %s' %(battery_dest))
shutil.rmtree(working_dir)
28 changes: 0 additions & 28 deletions server/sql/Dockerfile

This file was deleted.

23 changes: 0 additions & 23 deletions server/sql/README.md

This file was deleted.

8 changes: 0 additions & 8 deletions server/sql/create_expfactory_table.sql

This file was deleted.

20 changes: 0 additions & 20 deletions server/sql/docker-compose.yml

This file was deleted.

21 changes: 0 additions & 21 deletions server/sql/nginx.conf

This file was deleted.

46 changes: 0 additions & 46 deletions server/sql/run.py

This file was deleted.

65 changes: 0 additions & 65 deletions server/sql/setup_battery_for_webserver.py

This file was deleted.

0 comments on commit 6dfdb37

Please sign in to comment.