From 3307bf281c0660eb839c9a7d8fae084f3de58dea Mon Sep 17 00:00:00 2001 From: Rui Carmo Date: Mon, 15 May 2017 21:37:14 +0100 Subject: [PATCH] REPL --- README.md | 1 + DESIGN.md => docs/DESIGN.md | 0 ENV.md => docs/ENV.md | 0 INSTALL.md => docs/INSTALL.md | 4 ++-- .../RASPBERRY_PI_QUICKSTART.md | 2 +- SCRIPTS.md => docs/SCRIPTS.md | 2 +- piku.py | 23 +++++++++++++------ 7 files changed, 21 insertions(+), 11 deletions(-) rename DESIGN.md => docs/DESIGN.md (100%) rename ENV.md => docs/ENV.md (100%) rename INSTALL.md => docs/INSTALL.md (99%) rename RASPBERRY_PI_QUICKSTART.md => docs/RASPBERRY_PI_QUICKSTART.md (99%) rename SCRIPTS.md => docs/SCRIPTS.md (97%) mode change 100644 => 100755 piku.py diff --git a/README.md b/README.md index 455aa33..85a886e 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ From the bottom up: - [ ] nginx SSL optimization/cypher suites, own certificates - [ ] Let's Encrypt support - [ ] Review docs/CLI command documentation +- [x] (experimental) REPL - [x] Python 3 support through `PYTHON_VERSION = 3` - [x] static URL mapping to arbitrary paths (hat tip to @carlosefr for `nginx` tuning) - [x] remote CLI (requires `ssh -t`) diff --git a/DESIGN.md b/docs/DESIGN.md similarity index 100% rename from DESIGN.md rename to docs/DESIGN.md diff --git a/ENV.md b/docs/ENV.md similarity index 100% rename from ENV.md rename to docs/ENV.md diff --git a/INSTALL.md b/docs/INSTALL.md similarity index 99% rename from INSTALL.md rename to docs/INSTALL.md index 3f45bf7..ac79a4a 100644 --- a/INSTALL.md +++ b/docs/INSTALL.md @@ -38,7 +38,7 @@ Before running `piku` for the first time, you need to install the following Pyth ```bash sudo apt-get install git python-virtualenv python-pip -sudo pip install -U click +sudo pip install -U click click-repl ``` ### Raspbian Wheezy @@ -46,7 +46,7 @@ sudo pip install -U click ```bash sudo apt-get install git python2.7 sudo easy_install -U pip -sudo pip install -U click virtualenv +sudo pip install -U click click-repl virtualenv ``` These may or may not be installed already (`click` usually isn't). For Raspbian Wheezy this is the preferred approach, since current `apt` packages are fairly outdated. diff --git a/RASPBERRY_PI_QUICKSTART.md b/docs/RASPBERRY_PI_QUICKSTART.md similarity index 99% rename from RASPBERRY_PI_QUICKSTART.md rename to docs/RASPBERRY_PI_QUICKSTART.md index c67a8fa..6cf3d43 100644 --- a/RASPBERRY_PI_QUICKSTART.md +++ b/docs/RASPBERRY_PI_QUICKSTART.md @@ -49,7 +49,7 @@ As of April 2016, the shipping versions with Raspbian are recent enough to run ` ```bash # as 'pi' user sudo apt install -y python-virtualenv python-pip git uwsgi uwsgi-plugin-python incron nginx -sudo pip install -U click +sudo pip install -U click click-repl sudo reboot ``` diff --git a/SCRIPTS.md b/docs/SCRIPTS.md similarity index 97% rename from SCRIPTS.md rename to docs/SCRIPTS.md index 16d4abd..83b8f60 100644 --- a/SCRIPTS.md +++ b/docs/SCRIPTS.md @@ -7,7 +7,7 @@ sudo apt-get update sudo apt-get -y dist-upgrade sudo apt-get -y autoremove sudo apt-get install -y tmux vim htop fail2ban uwsgi uwsgi-plugin-python uwsgi-plugin-python3 uwsgi-plugin-asyncio-python3 uwsgi-plugin-gevent-python uwsgi-plugin-tornado-python nginx incron libxml2-dev libxslt1-dev python-dev zlib1g-dev build-essential git python-virtualenv python-pip -sudo pip install -U click pip +sudo pip install -U click click-repl pip sudo adduser --disabled-password --gecos 'PaaS access' --ingroup www-data piku # move to /tmp and grab our distribution files diff --git a/piku.py b/piku.py old mode 100644 new mode 100755 index e4bcd05..a69ba2d --- a/piku.py +++ b/piku.py @@ -2,7 +2,8 @@ "Piku Micro-PaaS" -from click import argument, command, group, option, secho as echo +from click import argument, command, group, get_current_context, option, secho as echo +from click_repl import repl from collections import defaultdict, deque from datetime import datetime from fcntl import fcntl, F_SETFL, F_GETFL @@ -12,6 +13,7 @@ from json import loads from multiprocessing import cpu_count from os import chmod, unlink, remove, stat, listdir, environ, makedirs, O_NONBLOCK from os.path import abspath, basename, dirname, exists, getmtime, join, realpath, splitext +from prompt_toolkit.history import FileHistory from re import sub from shutil import copyfile, rmtree from socket import socket, AF_INET, SOCK_STREAM @@ -953,15 +955,15 @@ def git_hook(app): @argument('app') def receive(app): """INTERNAL: Handle git pushes for an app""" - + app = sanitize_app_name(app) hook_path = join(GIT_ROOT, app, 'hooks', 'post-receive') - + if not exists(hook_path): makedirs(dirname(hook_path)) # Initialize the repository with a hook to this script call("git init --quiet --bare " + app, cwd=GIT_ROOT, shell=True) - with open(hook_path,'w') as h: + with open(hook_path, 'w') as h: h.write("""#!/usr/bin/env bash set -e; set -o pipefail; cat | PIKU_ROOT="%s" %s git-hook %s""" % (PIKU_ROOT, realpath(__file__), app)) @@ -969,7 +971,14 @@ cat | PIKU_ROOT="%s" %s git-hook %s""" % (PIKU_ROOT, realpath(__file__), app)) chmod(hook_path, stat(hook_path).st_mode | S_IXUSR) # Handle the actual receive. We'll be called with 'git-hook' after it happens call('git-shell -c "%s" ' % (argv[1] + " '%s'" % app), cwd=GIT_ROOT, shell=True) - - + + +@piku.command() +def prompt(): + """Starts an interactive prompt""" + repl(get_current_context(), + prompt_kwargs={'history': FileHistory(join(dirname(realpath(__file__)), '.piku_history'))}, + allow_system_commands=False) + if __name__ == '__main__': - piku() \ No newline at end of file + piku()