Browse Source

cli: Get env vars from env, simplify everything else

Now commands such as rebuild_cache can be used from commandline, and
display a useful help message when there are missing environment
variables
templates_refactor
dequis 6 years ago
parent
commit
415dae483e
5 changed files with 74 additions and 41 deletions
  1. +2
    -3
      board.py
  2. +59
    -21
      cli.py
  3. +4
    -14
      interboard.py
  4. +9
    -0
      util.py
  5. +0
    -3
      wakarimasen.py

+ 2
- 3
board.py View File

@@ -200,9 +200,8 @@ class Board(object):
self.check_access(task_data.user)
task_data.contents.append(self.name)

Popen([sys.executable, sys.argv[0], 'rebuild_cache', self.name,
local.environ['DOCUMENT_ROOT'], local.environ['SCRIPT_NAME'],
local.environ['SERVER_NAME']])
Popen([sys.executable, sys.argv[0], 'rebuild_cache', self.name],
env=util.proxy_environ())

return util.make_http_forward(
misc.make_script_url(task='mpanel', board=self.name),


+ 59
- 21
cli.py View File

@@ -6,8 +6,8 @@ import werkzeug
import util
import fcgi
import config
import board
import interboard
from board import Board
from util import local

COMMANDS = {}
@@ -22,40 +22,38 @@ def need_application(f):
f.need_application = True
return f

# commands
def need_environment(f):
f.need_environment = True
return f

def set_env_vars(*args):
(local.environ['DOCUMENT_ROOT'],
local.environ['SCRIPT_NAME'],
local.environ['SERVER_NAME']) = args[:3]
# commands

@command
def rebuild_cache(board_name, *env_vars):
@need_environment
def rebuild_cache(board_name):
"""
$0 rebuild_cache board_name document_root script_name server_name
$0 rebuild_cache board_name
"""
set_env_vars(*env_vars)

board = Board(board_name)
local.environ['waka.board'] = board
board.rebuild_cache()
this_board = board.Board(board_name)
local.environ['waka.board'] = this_board
this_board.rebuild_cache()

@command
def delete_by_ip(ip, boards, *env_vars):
@need_environment
def delete_by_ip(ip, boards, ):
"""
$0 delete_by_ip ip boards document_root script_name server_name
$0 delete_by_ip ip boards
"""
boards = boards.split(",")
set_env_vars(*env_vars)

interboard.process_global_delete_by_ip(ip, boards)

@command
def rebuild_global_cache(*env_vars):
@need_environment
def rebuild_global_cache():
"""
$0 document_root script_name server_name
$0
"""
set_env_vars(*env_vars)

interboard.global_cache_rebuild()

@@ -121,18 +119,58 @@ def help(command=None):
docstring = str(f.__doc__).replace("$0", sys.argv[0])
print "Usage:", docstring

def init_environ():
"""
Initialize a wsgi-ish environment for commands.

Tries to get variables from the OS env and checks for required ones.
"""

ENV_VAR_HELP = """
Environment variables explanation:
- DOCUMENT_ROOT: full filesystem path to html files
ex: /srv/http/imageboard.example.com/
- SCRIPT_NAME: url to wakarimasen.py without host part
ex: /wakarimasen.py
- SERVER_NAME: hostname of the webserver
ex: imageboard.example.com
- SERVER_PORT: port of the webserver (optional)
ex: 80
"""

local.environ.update(os.environ)
werkzeug.BaseRequest(local.environ)

local.environ.setdefault('waka.rootpath',
os.path.join('/', config.BOARD_DIR, ''))
local.environ.setdefault('wsgi.url_scheme', 'http')
local.environ.setdefault('SERVER_PORT', '80')

required_vars = ['DOCUMENT_ROOT', 'SCRIPT_NAME', 'SERVER_NAME']

for var in required_vars:
if var not in local.environ:
print "Error: %s not in environment" % (var,)
print ENV_VAR_HELP
sys.exit(1)

def handle_command(args, application):
name = args.pop(0)
f = COMMANDS.get(name, help)

if getattr(f, 'need_application', False):
if hasattr(f, 'need_application'):
args.insert(0, application)

if hasattr(f, 'need_environment'):
# Initialize environment
init_environ()

try:
# attempt to call function with specified arguments
inspect.getcallargs(f, *args)
except TypeError:
# it doesn't fit
return help(name)
help(name)
sys.exit(1)

f(*args)

+ 4
- 14
interboard.py View File

@@ -91,12 +91,8 @@ def global_cache_rebuild():
def global_cache_rebuild_proxy(task_data):
if task_data.user.account != staff.ADMIN:
raise WakaError(strings.INUSUFFICENTPRIVLEDGES)
Popen(
[sys.executable, sys.argv[0], 'rebuild_global_cache',
local.environ['DOCUMENT_ROOT'],
local.environ['SCRIPT_NAME'],
local.environ['SERVER_NAME']]
)
Popen([sys.executable, sys.argv[0], 'rebuild_global_cache'],
env=util.proxy_environ())
referer = local.environ['HTTP_REFERER']
task_data.contents.append(referer)
return util.make_http_forward(referer, config.ALTERNATE_REDIRECT)
@@ -448,14 +444,8 @@ def delete_by_ip(task_data, ip, mask='255.255.255.255'):
else:
reign = [x['board_entry'] for x in get_all_boards()]

Popen(
[sys.executable, sys.argv[0], 'delete_by_ip',
ip,
','.join(reign),
local.environ['DOCUMENT_ROOT'],
local.environ['SCRIPT_NAME'],
local.environ['SERVER_NAME']]
)
Popen([sys.executable, sys.argv[0], 'delete_by_ip', ip, ','.join(reign)],
env=util.proxy_environ())

board_name = local.environ['waka.board'].name
redir = misc.make_script_url(task='mpanel', board=board_name)


+ 9
- 0
util.py View File

@@ -229,3 +229,12 @@ class FileLock(object):
lying around.
"""
self.release()

def proxy_environ():
"""Returns a cleaned up version of the environment for proxied calls"""

environ = {}
for key, value in local.environ.iteritems():
if isinstance(value, basestring):
environ[key] = value
return environ

+ 0
- 3
wakarimasen.py View File

@@ -80,9 +80,6 @@ def cleanup(*args, **kwargs):
application = util.cleanup(application, cleanup)

def main():
# Set up tentative environment variables.
local.environ['waka.rootpath'] \
= os.path.join('/', config.BOARD_DIR, '')
try:
app.init_database()
except model.OperationalError, e:


Loading…
Cancel
Save