def test_awskeys_invalid(capfd): import psiturk.experiment_server_controller as control from psiturk.psiturk_config import PsiturkConfig from psiturk.psiturk_shell import PsiturkNetworkShell import psiturk.psiturk_shell as ps config = PsiturkConfig() config.load_config() config.set('Shell Parameters', 'persistent_history_file', '') config.set('AWS Access', 'aws_access_key_id', '') config.set('AWS Access', 'aws_secret_access_key', '') server = control.ExperimentServerController(config) launch_in_sandbox_mode = True quiet = False try: shell = PsiturkNetworkShell(config, server, launch_in_sandbox_mode, quiet=quiet) except SystemExit: pass out, err = capfd.readouterr() assert NoMturkConnectionError().message in out
def test_awscreds_notset_shell(capfd, experiment_server_controller, psiturk_shell): from psiturk.psiturk_config import PsiturkConfig from psiturk.psiturk_shell import PsiturkNetworkShell import psiturk.psiturk_shell as ps import psiturk.experiment_server_controller config = PsiturkConfig() config.load_config() config.set('Shell Parameters', 'persistent_history_file', '') config.set('AWS Access', 'aws_access_key_id', 'YourAccessKeyId') config.set('AWS Access', 'aws_secret_access_key', 'YourSecretAccessKey') server = psiturk.experiment_server_controller.ExperimentServerController( config) launch_in_sandbox_mode = True quiet = False try: shell = PsiturkNetworkShell(config, server, launch_in_sandbox_mode, quiet=quiet) except SystemExit: pass out, err = capfd.readouterr() assert AWSAccessKeysNotSetError().message in err
def test_awskeys_invalid_shell(capfd, experiment_server_controller, psiturk_shell): """test_awskeys_invalid_shell.""" import psiturk.experiment_server_controller from psiturk.psiturk_config import PsiturkConfig from psiturk.psiturk_shell import PsiturkNetworkShell config = PsiturkConfig() config.load_config() config.set('Shell Parameters', 'persistent_history_file', '') server = psiturk.experiment_server_controller.ExperimentServerController( config) quiet = False try: shell = PsiturkNetworkShell(config, server, mode='sandbox', quiet=quiet) except SystemExit: pass out, err = capfd.readouterr() assert NoMturkConnectionError().message in err
def deploy(verbose, app): """Deploy app using Heroku to MTurk.""" # Load psiTurk configuration. config = PsiturkConfig() config.load_config() # Set the mode. config.set("Experiment Configuration", "mode", "deploy") config.set("Server Parameters", "logfile", "-") # Ensure that psiTurk is not in sandbox mode. config.set("Shell Parameters", "launch_in_sandbox_mode", "false") # Do shared setup. deploy_sandbox_shared_setup(verbose=verbose, app=app)
def deploy(verbose): """Deploy app using Heroku to MTurk.""" # Load psiTurk configuration. config = PsiturkConfig() config.load_config() # Set the mode. config.set("Experiment Configuration", "mode", "deploy") config.set("Server Parameters", "logfile", "-") # Ensure that psiTurk is not in sandbox mode. config.set("Shell Parameters", "launch_in_sandbox_mode", "false") # Do shared setup. deploy_sandbox_shared_setup(verbose=verbose)
def sandbox(verbose, app): """Deploy app using Heroku to the MTurk Sandbox.""" # Load psiTurk configuration. config = PsiturkConfig() config.load_config() # Set the mode. config.set("Experiment Configuration", "mode", "sandbox") config.set("Server Parameters", "logfile", "-") # Ensure that psiTurk is in sandbox mode. config.set("Shell Parameters", "launch_in_sandbox_mode", "true") # Do shared setup. deploy_sandbox_shared_setup(verbose=verbose, app=app)
def deploy_sandbox_shared_setup(verbose=True, app=None, web_procs=1): """Set up Git, push to Heroku, and launch the app.""" out = None if verbose else open(os.devnull, 'w') (id, tmp) = setup_experiment(debug=False, verbose=verbose, app=app) # Log in to Heroku if we aren't already. log("Making sure that you are logged in to Heroku.") ensure_heroku_logged_in() # Change to temporary directory. cwd = os.getcwd() os.chdir(tmp) # Commit Heroku-specific files to tmp folder's git repo. cmds = [ "git init", "git add --all", 'git commit -m "Experiment ' + id + '"' ] for cmd in cmds: subprocess.call(cmd, stdout=out, shell=True) time.sleep(0.5) # Load psiTurk configuration. config = PsiturkConfig() config.load_config() # Initialize the app on Heroku. log("Initializing app on Heroku...") subprocess.call( "heroku apps:create " + id + " --buildpack https://github.com/thenovices/heroku-buildpack-scipy", stdout=out, shell=True) database_size = config.get('Database Parameters', 'database_size') try: if config.getboolean('Easter eggs', 'whimsical'): whimsical = "true" else: whimsical = "false" except: whimsical = "false" # Set up postgres database and AWS/psiTurk environment variables. cmds = [ "heroku addons:create heroku-postgresql:{}".format(database_size), "heroku pg:wait", "heroku addons:create rediscloud:250", "heroku addons:create papertrail", "heroku config:set HOST=" + id + ".herokuapp.com", "heroku config:set aws_access_key_id=" + config.get('AWS Access', 'aws_access_key_id'), "heroku config:set aws_secret_access_key=" + config.get('AWS Access', 'aws_secret_access_key'), "heroku config:set aws_region=" + config.get('AWS Access', 'aws_region'), "heroku config:set psiturk_access_key_id=" + config.get('psiTurk Access', 'psiturk_access_key_id'), "heroku config:set psiturk_secret_access_id=" + config.get('psiTurk Access', 'psiturk_secret_access_id'), "heroku config:set auto_recruit=" + config.get('Experiment Configuration', 'auto_recruit'), "heroku config:set wallace_email_username="******"heroku config:set wallace_email_key=" + config.get('Email Access', 'wallace_email_password'), "heroku config:set heroku_email_address=" + config.get('Heroku Access', 'heroku_email_address'), "heroku config:set heroku_password="******"heroku config:set whimsical=" + whimsical, ] for cmd in cmds: subprocess.call(cmd + " --app " + id, stdout=out, shell=True) # Set the notification URL in the cofig file to the notifications URL. config.set("Server Parameters", "notification_url", "http://" + id + ".herokuapp.com/notifications") # Set the database URL in the config file to the newly generated one. log("Saving the URL of the postgres database...") db_url = subprocess.check_output("heroku config:get DATABASE_URL --app " + id, shell=True) config.set("Database Parameters", "database_url", db_url.rstrip()) subprocess.call("git add config.txt", stdout=out, shell=True), time.sleep(0.25) subprocess.call('git commit -m "Save URLs for database and notifications"', stdout=out, shell=True) time.sleep(0.25) # Launch the Heroku app. log("Pushing code to Heroku...") subprocess.call("git push heroku HEAD:master", stdout=out, stderr=out, shell=True) scale_up_dynos(id) time.sleep(8) # Launch the experiment. log("Launching the experiment on MTurk...") subprocess.call('curl --data "" http://{}.herokuapp.com/launch'.format(id), shell=True) time.sleep(8) url = subprocess.check_output("heroku logs --app " + id + " | sort | " + "sed -n 's|.*URL:||p'", shell=True) log("URLs:") click.echo(url) # Return to the branch whence we came. os.chdir(cwd) log("Completed deployment of experiment " + id + ".")
def debug(verbose): """Run the experiment locally.""" (id, tmp) = setup_experiment(debug=True, verbose=verbose) # Drop all the tables from the database. db.init_db(drop_all=True) # Switch to the temporary directory. cwd = os.getcwd() os.chdir(tmp) # Load psiTurk configuration. config = PsiturkConfig() config.load_config() # Set the mode to debug. config.set("Experiment Configuration", "mode", "debug") config.set("Shell Parameters", "launch_in_sandbox_mode", "true") config.set("Server Parameters", "logfile", os.path.join(cwd, config.get("Server Parameters", "logfile"))) # Swap in the HotAirRecruiter os.rename("wallace_experiment.py", "wallace_experiment_tmp.py") with open("wallace_experiment_tmp.py", "r+") as f: with open("wallace_experiment.py", "w+") as f2: f2.write("from wallace.recruiters import HotAirRecruiter\n") for idx, line in enumerate(f): if re.search("\s*self.recruiter = (.*)", line): p = line.partition("self.recruiter =") f2.write(p[0] + p[1] + ' HotAirRecruiter\n') else: f2.write(line) os.remove("wallace_experiment_tmp.py") # Set environment variables. aws_vars = ['aws_access_key_id', 'aws_secret_access_key', 'aws_region'] for var in aws_vars: if var not in os.environ: os.environ[var] = config.get('AWS Access', var) pt_vars = ['psiturk_access_key_id', 'psiturk_secret_access_id'] for var in pt_vars: if var not in os.environ: os.environ[var] = config.get('psiTurk Access', var) if "HOST" not in os.environ: os.environ["HOST"] = config.get('Server Parameters', 'host') # Start up the local server log("Starting up the server...") # Try opening the psiTurk shell. try: p = pexpect.spawn("psiturk") p.expect_exact("]$") p.sendline("server on") p.expect_exact("Experiment server launching...") # Launche the experiment. time.sleep(4) host = config.get("Server Parameters", "host") port = config.get("Server Parameters", "port") subprocess.call('curl --data "" http://{}:{}/launch'.format( host, port), shell=True) log("Here's the psiTurk shell...") p.interact() except Exception: click.echo("\nCouldn't open psiTurk shell. Internet connection okay?") log("Completed debugging of experiment " + id + ".") os.chdir(cwd)
def deploy_sandbox_shared_setup(verbose=True, web_procs=1): """Set up Git, push to Heroku, and launch the app.""" if verbose: OUT = None else: OUT = open(os.devnull, 'w') (id, tmp) = setup(debug=False, verbose=verbose) # Log in to Heroku if we aren't already. log("Making sure that you are logged in to Heroku.") ensure_heroku_logged_in() # Change to temporary directory. cwd = os.getcwd() os.chdir(tmp) # Commit Heroku-specific files to tmp folder's git repo. cmds = ["git init", "git add --all", 'git commit -m "Experiment ' + id + '"'] for cmd in cmds: subprocess.call(cmd, stdout=OUT, shell=True) time.sleep(0.5) # Load psiTurk configuration. config = PsiturkConfig() config.load_config() # Initialize the app on Heroku. log("Initializing app on Heroku...") subprocess.call( "heroku apps:create " + id + " --buildpack https://github.com/thenovices/heroku-buildpack-scipy", stdout=OUT, shell=True) database_size = config.get('Database Parameters', 'database_size') # Set up postgres database and AWS/psiTurk environment variables. cmds = [ "heroku addons:create heroku-postgresql:{}".format(database_size), "heroku pg:wait", "heroku addons:create redistogo:small", "heroku addons:create papertrail", "heroku config:set HOST=" + id + ".herokuapp.com", "heroku config:set aws_access_key_id=" + config.get('AWS Access', 'aws_access_key_id'), "heroku config:set aws_secret_access_key=" + config.get('AWS Access', 'aws_secret_access_key'), "heroku config:set aws_region=" + config.get('AWS Access', 'aws_region'), "heroku config:set psiturk_access_key_id=" + config.get('psiTurk Access', 'psiturk_access_key_id'), "heroku config:set psiturk_secret_access_id=" + config.get('psiTurk Access', 'psiturk_secret_access_id'), "heroku config:set auto_recruit=" + config.get('Experiment Configuration', 'auto_recruit'), ] for cmd in cmds: subprocess.call(cmd + " --app " + id, stdout=OUT, shell=True) # Set the notification URL in the cofig file to the notifications URL. config.set( "Server Parameters", "notification_url", "http://" + id + ".herokuapp.com/notifications") # Set the database URL in the config file to the newly generated one. log("Saving the URL of the postgres database...") db_url = subprocess.check_output( "heroku config:get DATABASE_URL --app " + id, shell=True) config.set("Database Parameters", "database_url", db_url.rstrip()) subprocess.call("git add config.txt", stdout=OUT, shell=True), time.sleep(0.25) subprocess.call( 'git commit -m "Save URLs for database and notifications"', stdout=OUT, shell=True) time.sleep(0.25) # Launch the Heroku app. log("Pushing code to Heroku...") subprocess.call("git push heroku HEAD:master", stdout=OUT, stderr=OUT, shell=True) dyno_type = config.get('Server Parameters', 'dyno_type') num_dynos_web = config.get('Server Parameters', 'num_dynos_web') num_dynos_worker = config.get('Server Parameters', 'num_dynos_worker') log("Starting up the web server...") subprocess.call("heroku ps:scale web=" + str(num_dynos_web) + ":" + str(dyno_type) + " --app " + id, stdout=OUT, shell=True) subprocess.call("heroku ps:scale worker=" + str(num_dynos_worker) + ":" + str(dyno_type) + " --app " + id, stdout=OUT, shell=True) time.sleep(8) # Launch the experiment. log("Launching the experiment on MTurk...") subprocess.call( 'curl --data "" http://{}.herokuapp.com/launch'.format(id), shell=True) time.sleep(8) url = subprocess.check_output("heroku logs --app " + id + " | sort | " + "sed -n 's|.*URL:||p'", shell=True) log("URLs:") print url # Return to the branch whence we came. os.chdir(cwd) log("Completed deployment of experiment " + id + ".")
def debug(verbose): """Run the experiment locally.""" (id, tmp) = setup(debug=True, verbose=verbose) # Drop all the tables from the database. db.init_db(drop_all=True) # Switch to the temporary directory. cwd = os.getcwd() os.chdir(tmp) # Load psiTurk configuration. config = PsiturkConfig() config.load_config() # Set the mode to debug. config.set("Experiment Configuration", "mode", "debug") config.set("Shell Parameters", "launch_in_sandbox_mode", "true") config.set( "Server Parameters", "logfile", os.path.join(cwd, config.get("Server Parameters", "logfile"))) # Swap in the HotAirRecruiter os.rename("wallace_experiment.py", "wallace_experiment_tmp.py") with open("wallace_experiment_tmp.py", "r+") as f: with open("wallace_experiment.py", "w+") as f2: f2.write("from wallace.recruiters import HotAirRecruiter\n") for idx, line in enumerate(f): if re.search("\s*self.recruiter = (.*)", line): p = line.partition("self.recruiter =") f2.write(p[0] + p[1] + ' HotAirRecruiter\n') else: f2.write(line) os.remove("wallace_experiment_tmp.py") # Set environment variables. aws_vars = ['aws_access_key_id', 'aws_secret_access_key', 'aws_region'] for var in aws_vars: if var not in os.environ: os.environ[var] = config.get('AWS Access', var) pt_vars = ['psiturk_access_key_id', 'psiturk_secret_access_id'] for var in pt_vars: if var not in os.environ: os.environ[var] = config.get('psiTurk Access', var) if "HOST" not in os.environ: os.environ["HOST"] = config.get('Server Parameters', 'host') # Start up the local server log("Starting up the server...") # Try opening the psiTurk shell. try: p = pexpect.spawn("psiturk") p.expect_exact("]$") p.sendline("server on") p.expect_exact("Experiment server launching...") # Launche the experiment. time.sleep(4) host = config.get("Server Parameters", "host") port = config.get("Server Parameters", "port") subprocess.call( 'curl --data "" http://{}:{}/launch'.format(host, port), shell=True) log("Here's the psiTurk shell...") p.interact() except Exception: print "\nCouldn't open the psiTurk shell. Internet connection okay?" log("Completed debugging of experiment " + id + ".") os.chdir(cwd)