tarbell-project / tarbell

A Flask-based static site authoring tool.
https://tarbell.readthedocs.io/en/latest/index.html
BSD 3-Clause "New" or "Revised" License
165 stars 32 forks source link

virtualenv tarbell via user cron #420

Closed jheasly closed 8 years ago

jheasly commented 8 years ago

Are there any examples of how to set up a virtualenv'd tarbell cron? The problem seems to be some kind of environmental/path issue that keeps the Google OAuth client_secrets.json authorization from being loaded. When I run the script from the command line it works fine. When I run it from that same user's cron it tries to do the Web browser launch thing (" ... browser tab to walk through the OAuth 2.0 process ... ") 'cause apparently the client_secrets.json load failed.

Seems to be falling down around get_drive_api_from_client_secrets(): https://github.com/tarbell-project/tarbell/blob/master/tarbell/oauth.py#L37

Here's the error:

Enter verification code: Traceback (most recent call last):
 File "/Users/someuser/.virtualenvs/tarbell_env/bin/tarbell", line 9, in <module>
   load_entry_point('tarbell==1.0.5', 'console_scripts', 'tarbell')()
 File "/Users/someuser/.virtualenvs/tarbell_env/lib/python2.7/site-packages/tarbell/cli.py", line 74, in main
   command.__call__(command, args)
 File "/Users/someuser/.virtualenvs/tarbell_env/lib/python2.7/site-packages/tarbell/cli.py", line 931, in __call__
   return self.fn(*args, **kw_args)
 File "/Users/someuser/.virtualenvs/tarbell_env/lib/python2.7/site-packages/tarbell/cli.py", line 336, in tarbell_publish
   with ensure_settings(command, args) as settings, ensure_project(command, args) as site:
 File "/Users/someuser/.virtualenvs/tarbell_env/lib/python2.7/site-packages/tarbell/contextmanagers.py", line 58, in __enter__
   return self.ensure_site()
 File "/Users/someuser/.virtualenvs/tarbell_env/lib/python2.7/site-packages/tarbell/contextmanagers.py", line 79, in ensure_site
   site = TarbellSite(path)
 File "/Users/someuser/.virtualenvs/tarbell_env/lib/python2.7/site-packages/tarbell/app.py", line 279, in __init__
   self.project, self.base = self.load_project(path)
 File "/Users/someuser/.virtualenvs/tarbell_env/lib/python2.7/site-packages/tarbell/app.py", line 407, in load_project
   self.client = get_drive_api()
 File "/Users/someuser/.virtualenvs//lib/python2.7/site-packages/tarbell/oauth.py", line 34, in get_drive_api
   return get_drive_api_from_client_secrets(settings.client_secrets_path)
 File "/Users/someuser/.virtualenvs/tarbell_env/lib/python2.7/site-packages/tarbell/oauth.py", line 50, in get_drive_api_from_client_secrets
   credentials = tools.run_flow(flow, storage, flags)
 File "/Users/someuser/.virtualenvs/tarbell_env/lib/python2.7/site-packages/oauth2client/util.py", line 140, in positional_wrapper
   return wrapped(*args, **kwargs)
 File "/Users/someuser/.virtualenvs/tarbell_env/lib/python2.7/site-packages/oauth2client/tools.py", line 228, in run_flow
   code = input('Enter verification code: ').strip()
EOFError: EOF when reading a line

Here's the script:

#!/bin/bash

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Development
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
source /usr/local/bin/virtualenvwrapper.sh

workon tarbell_env
cd /Users/someuser/Development/tarbell/discovery/
tarbell publish production
eyeseast commented 8 years ago

I've dealt with this, but I think it was on an old server I just killed. Oops. I usually use the full path to the virtualenv executable:

0 12 * * * cd /home/me/.virtualenvs/my-project && /home/me/.virtualenvs/my-project/bin/tarbell publish production

That should get all the right Python paths and config stuff. The other thing you can do is set CREDENTIALS_PATH in your tarbell_config.py. I think I've done that before.

jheasly commented 8 years ago

Hrm. In addition to setting the CREDENTIALS_PATH I tried the cron one-liner (where the first arg is the location of the tarbell project and the second is the virtualenv'd path to tarbell, right?) and I'm still getting the same EOFError: EOF when reading a line error.

For some reason, the return get_drive_api_from_client_secrets(settings.client_secrets_path) is not coming back with the oauth object. I've compared my and the shell's sys.path (identical) and environmental variables and am not seeing any difference of consequence, but I guess, by definition, there is/are ...

jheasly commented 8 years ago

Aha! ⚡ So it seems, if you're running from cron from anywhere, even if from your local development server, you must do the tarbell credentials > ~/.tarbell/credentials.json bit. Is this the case? It seems to be. Once I did that, my cron setup worked fine.
If this is indeed the case, I suggest a note be added to the Remote configuration page, and I'm perfectly willing to do a PR for that. Oy ...

eyeseast commented 8 years ago

Yes, PR would be awesome. Thanks!