diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..30a9ca2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.travis.yml +.git +.gitignore +docker-compose.yml +Dockerfile diff --git a/.gitignore b/.gitignore index e5d9ef6..d95c119 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ ve/ share/vim/.vim/ share/vim/.viminfo typescript +venv/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..88c15cf --- /dev/null +++ b/.travis.yml @@ -0,0 +1,24 @@ +dist: trusty +sudo: required + +language: + - generic + +services: + - docker + +env: + DOCKER_COMPOSE_VERSION: 1.22.0 + +addons: + apt: + packages: + - docker-ce + +before_install: + - sudo rm /usr/local/bin/docker-compose + - curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose + - chmod +x docker-compose + - sudo mv docker-compose /usr/local/bin + - docker-compose up -d + - docker ps \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..95c26a2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM alpine:latest +WORKDIR /app +COPY . /app +RUN apk add --update --no-cache python2 py2-pip py2-gevent \ + py2-flask py2-requests py2-pygments py2-redis \ + py2-cffi py2-icu bash vim gawk sed \ + && apk add --no-cache --virtual build-deps python2-dev \ + build-base git \ + && pip install -r requirements.txt \ + && sh share/scripts/get-sheets.sh \ + && apk del build-deps +ENTRYPOINT ["python2"] +CMD ["bin/srv.py"] diff --git a/README.md b/README.md index 582daab..f06a499 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ What features should it have? Such a thing exists. +[![Build Status](https://travis-ci.org/bglopez/cheat.sh.svg?branch=master)](https://travis-ci.org/bglopez/cheat.sh) + ## Features **cheat.sh** @@ -43,6 +45,8 @@ Such a thing exists. * [Client usage](#client-usage) * [Tab-completion](#tab-completion) * [Stealth mode](#stealth-mode) +* [Self-Hosting](#self-hosting) + * [Docker](#docker) * [Editors integration](#editors-integration) * [Vim](#vim) * [Emacs](#emacs) @@ -405,6 +409,13 @@ because you know what happens when you do. ![when you lie in your interview](http://cheat.sh/files/when-you-lie-katze.png) +## Self-Hosting + +### Docker + +Currently the easiest way to get a self-hosted instance running is by using the docker-compose.yml file provided in the extra/docker folder. +This pulls down the latest image with baked in cheatsheets and starts the app and a Redis instance to back it, making the service available on port 8002 of the local host. This is currently an early implementation and should probably not be used for anything outside of internal/dev/personal use right now. + ## Editors integration You can use *cheat.sh* directly from the editor diff --git a/bin/srv.py b/bin/srv.py index ce646aa..858cf3b 100644 --- a/bin/srv.py +++ b/bin/srv.py @@ -6,8 +6,9 @@ Main server program. """ from __future__ import print_function -from gevent.pywsgi import WSGIServer from gevent.monkey import patch_all +from gevent.pywsgi import WSGIServer + patch_all() # pylint: disable=wrong-import-position,wrong-import-order diff --git a/docker-compose.prebuilt.yml b/docker-compose.prebuilt.yml new file mode 100644 index 0000000..898d9f2 --- /dev/null +++ b/docker-compose.prebuilt.yml @@ -0,0 +1,14 @@ +version: '2' +services: + app: + image: bglopez/cheat.sh + depends_on: + - redis + ports: + - "8002:8002" + redis: + image: redis:4-alpine + volumes: + - redis_data:/data +volumes: + redis_data: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..589033c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +version: '2' +services: + app: + build: + context: . + depends_on: + - redis + ports: + - "8002:8002" + redis: + image: redis:4-alpine + volumes: + - redis_data:/data +volumes: + redis_data: diff --git a/lib/beautifier.py b/lib/beautifier.py index 3a257d2..22eb33f 100644 --- a/lib/beautifier.py +++ b/lib/beautifier.py @@ -36,10 +36,10 @@ import redis MYDIR = os.path.abspath(os.path.join(__file__, '..', '..')) sys.path.append("%s/lib/" % MYDIR) from languages_data import VIM_NAME -from globals import PATH_VIM_ENVIRONMENT +from globals import PATH_VIM_ENVIRONMENT, REDISHOST # pylint: enable=wrong-import-position,wrong-import-order -REDIS = redis.StrictRedis(host='localhost', port=6379, db=1) +REDIS = redis.StrictRedis(host=REDISHOST, port=6379, db=1) FNULL = open(os.devnull, 'w') TEXT = 0 CODE = 1 @@ -150,14 +150,10 @@ def _classify_lines(lines): return line_types def _unindent_code(line, shift=0): - #if line.startswith(' '): - # return line[4:] - if shift == -1 and line != '': return ' ' + line - if shift > 0: - if line.startswith(' '*shift): + if shift > 0 and line.startswith(' '*shift): return line[shift:] return line @@ -245,8 +241,6 @@ def _beautify(text, filetype, add_comments=False, remove_text=False): lines = _cleanup_lines(lines) lines_classes = zip(_classify_lines(lines), lines) lines_classes = _wrap_lines(lines_classes, unindent_code=unindent_code) - #for x,y in lines_classes: - # print "%s: %s" % (x, y) if remove_text: lines = [line[1] for line in lines_classes if line[0] == 1] diff --git a/lib/get_answer.py b/lib/get_answer.py index 61e4c70..babf8ed 100644 --- a/lib/get_answer.py +++ b/lib/get_answer.py @@ -25,13 +25,13 @@ from polyglot.detect.base import UnknownLanguage import time import beautifier -from globals import MYDIR, PATH_TLDR_PAGES, PATH_CHEAT_PAGES, PATH_CHEAT_SHEETS, COLOR_STYLES +from globals import MYDIR, PATH_TLDR_PAGES, PATH_CHEAT_PAGES, PATH_CHEAT_SHEETS, COLOR_STYLES, REDISHOST from adapter_learnxiny import get_learnxiny, get_learnxiny_list, is_valid_learnxy from languages_data import LANGUAGE_ALIAS, SO_NAME, rewrite_editor_section_name from colorize_internal import colorize_internal # pylint: enable=wrong-import-position,wrong-import-order -REDIS = redis.StrictRedis(host='localhost', port=6379, db=0) +REDIS = redis.StrictRedis(host=REDISHOST, port=6379, db=0) MAX_SEARCH_LEN = 20 @@ -170,13 +170,10 @@ def get_topic_type(topic): # pylint: disable=too-many-locals,too-many-branches,t if '+' in topic_name: result = 'question' else: - #if topic_type in _get_topics_dirs() and topic_name in [':list']: - if topic_name in [':list']: + if (topic_name in [':list']) or (topic_name in [':learn']): result = "internal" elif is_valid_learnxy(topic): result = 'learnxiny' - elif topic_name in [':learn']: - result = "internal" else: # let us activate the 'question' feature for all subsections result = 'question' @@ -198,7 +195,6 @@ def get_topic_type(topic): # pylint: disable=too-many-locals,too-many-branches,t TOPIC_TYPE_CACHE[topic] = result - #print topic, " ", result return result # @@ -241,10 +237,10 @@ def _get_tldr(topic): line = line[2:] if line.startswith('-'): line = '# '+line[2:] - elif line == "": - pass elif not line.startswith(' '): line = "# "+line + else: + pass fixed_answer.append(line) @@ -472,7 +468,6 @@ def get_answer(topic, keyword, options="", request_options=None): # pylint: disa # if answer was not found in the cache # try to find it in one of the repositories if not answer: - #topic_type = get_topic_type(topic) for topic_getter_type, topic_getter in TOPIC_GETTERS: if topic_type == topic_getter_type: diff --git a/lib/globals.py b/lib/globals.py index c285369..3c3b700 100644 --- a/lib/globals.py +++ b/lib/globals.py @@ -9,6 +9,7 @@ import os from pygments.styles import get_all_styles MYDIR = os.path.abspath(os.path.join(__file__, '..', '..')) +REDISHOST = 'redis' ANSI2HTML = os.path.join(MYDIR, "share/ansi2html.sh") @@ -18,7 +19,7 @@ TEMPLATES = os.path.join(MYDIR, 'share/templates') STATIC = os.path.join(MYDIR, 'share/static') PATH_VIM_ENVIRONMENT = os.path.join(MYDIR, 'share/vim') -USE_OS_PACKAGES = True # change it False if you pull cheat sheets repositories from GitHub +USE_OS_PACKAGES = False # change it False if you pull cheat sheets repositories from GitHub if USE_OS_PACKAGES: PATH_TLDR_PAGES = "/home/igor/.tldr/cache/pages/*/*.md" PATH_CHEAT_PAGES = "/usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/*" diff --git a/lib/stateful_queries.py b/lib/stateful_queries.py index 424072a..578e2e2 100644 --- a/lib/stateful_queries.py +++ b/lib/stateful_queries.py @@ -3,8 +3,9 @@ Support for the stateful queries """ import redis +from globals import REDISHOST -REDIS = redis.StrictRedis(host='localhost', port=6379, db=1) +REDIS = redis.StrictRedis(host=REDISHOST, port=6379, db=1) def save_query(client_id, query): """ diff --git a/share/scripts/get-sheets.sh b/share/scripts/get-sheets.sh index 7145e54..c72184b 100644 --- a/share/scripts/get-sheets.sh +++ b/share/scripts/get-sheets.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh ## this doesn't error check, if it breaks and destroys things I'm sorry