Skip to content

Commit c005798

Browse files
committed
init
0 parents  commit c005798

10 files changed

Lines changed: 1022 additions & 0 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
bin
2+
lib
3+
lib64
4+
share
5+
pyvenv.cfg
6+
__pycache__

Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
ARG PYTHON
2+
FROM python:3.8
3+
4+
WORKDIR /jinja101
5+
RUN useradd -m jinja101
6+
7+
ENV PATH="/root/.poetry/bin:$PATH" \
8+
PYTHONDONTWRITEBYTECODE=1 \
9+
PYTHONUNBUFFERED=1
10+
11+
12+
RUN apt-get update && apt-get install curl -y \
13+
&& rm -rf /var/lib/apt/lists/* \
14+
&& curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python \
15+
&& poetry config virtualenvs.create false
16+
17+
COPY --chown=jinja101:jinja101 pyproject.toml .
18+
COPY --chown=jinja101:jinja101 poetry.lock .
19+
20+
# poetry does not support subdirectory yet
21+
RUN pip install "git+https://github.com/StackStorm/st2-rbac-backend.git@master#egg=st2-rbac-backend" \
22+
&& pip install "git+https://github.com/StackStorm/st2.git@v3.3.0#subdirectory=st2common" \
23+
&& poetry install --no-dev --no-interaction --no-ansi
24+
25+
COPY --chown=jinja101:jinja101 . .
26+
27+
USER jinja101
28+
29+
EXPOSE 5000/tcp
30+
31+
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "flask_app:app"]

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# TTP101
2+
3+
Small flask app
4+
5+
## Setup
6+
This repo uses poetry
7+
8+
### Install requirements
9+
10+
```bash
11+
poetry install
12+
```
13+
14+
### Open shell with venv and run app
15+
16+
```bash
17+
poetry shell
18+
```
19+
20+
### Run app (first open shell)
21+
22+
```bash
23+
python flask_app.py
24+
```
25+
26+
### Format python document (first open shell)
27+
28+
```bash
29+
black flask_app.py
30+
```

examples.yaml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
time:
2+
data: |
3+
18:42:41.321 PST Sun Feb 8 2009
4+
12:18:42.123 CET Sun Feb 14 2021
5+
08:15:00.0 PST Mon Okt 31 2020
6+
template: |
7+
Value Year (\d+)
8+
Value MonthDay (\d+)
9+
Value Month (\w+)
10+
Value Timezone (\S+)
11+
Value Time (..:..:..)
12+
13+
Start
14+
^${Time}.* ${Timezone} \w+ ${Month} ${MonthDay} ${Year} -> Record
15+
16+
cdp neighbor:
17+
data: |
18+
Capability Codes: R - Router, T - Trans Bridge, B- Source Route Bridge
19+
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone
20+
21+
Device ID Local Intrfce Holdtme Capability Platform Port ID
22+
R1-PBX Gig 1/0/10 144 R S I 2811 Fas 0/0
23+
TS-1 Gig 1/0/39 122 R 2611 Eth 0/1
24+
Cisco-WAP-N Gig 1/0/1 120 T I AIR-AP125 Gig 0
25+
SEP04FE7F689D33 Gig 1/0/2 125 H P IP Phone Port 1
26+
SEP000DBC50FCD1 Gig 1/0/4 147 H P IP Phone Port 1
27+
SEP00124362C4D2 Gig 1/0/42 147 H P IP Phone Port 1
28+
template: |
29+
Value Required NEIGHBOR (\S+)
30+
Value LOCAL_INTERFACE (\S+(?:\s\S+)?)
31+
Value CAPABILITY ((?:\w\s)*\w)
32+
Value PLATFORM ((?:[IiPp]{2}\s)?\S+)
33+
Value NEIGHBOR_INTERFACE (.+?)
34+
35+
Start
36+
^Device.*ID -> CDP
37+
# Capture time-stamp if vty line has command time-stamping turned on
38+
^Load\s+for\s+
39+
^Time\s+source\s+is
40+
41+
CDP
42+
^${NEIGHBOR}$$
43+
^\s*${LOCAL_INTERFACE}\s+\d+\s+${CAPABILITY}\s+${PLATFORM}\s+${NEIGHBOR_INTERFACE}\s*$$ -> Record
44+
^${NEIGHBOR}\s+${LOCAL_INTERFACE}\s+\d+\s+${CAPABILITY}\s+${PLATFORM}\s+${NEIGHBOR_INTERFACE}\s*$$ -> Record
45+
46+
arp:
47+
data: |
48+
Protocol Address Age(min) Hardware Addr Type Interface
49+
Internet 172.16.233.229 - 0000.0c59.f892 ARPA Ethernet0/0
50+
Internet 172.16.233.218 - 0000.0c07.ac00 ARPA Ethernet0/0
51+
Internet 172.16.233.19 - 0000.0c63.1300 ARPA Ethernet0/0
52+
Internet 172.16.233.209 - 0000.0c36.6965 ARPA Ethernet0/0
53+
Internet 172.16.168.11 - 0000.0c63.1300 ARPA Ethernet0/0
54+
Internet 172.16.168.254 9 0000.0c36.6965 ARPA Ethernet0/0
55+
Internet 10.0.0.0 - aabb.cc03.8200 SRP-A
56+
template: |
57+
Value Required PROTOCOL (\S+)
58+
Value Required ADDRESS (\d+\.\d+\.\d+\.\d+)
59+
Value Required AGE (\S+)
60+
Value Required MAC (\S+)
61+
Value Required TYPE (\S+)
62+
Value INTERFACE (\S+)
63+
64+
Start
65+
^Protocol\s+Address\s+Age\s*\(min\)\s+Hardware Addr\s+Type\s+Interface
66+
^${PROTOCOL}\s+${ADDRESS}\s+${AGE}\s+${MAC}\s+${TYPE}\s+${INTERFACE} -> Record
67+
^${PROTOCOL}\s+${ADDRESS}\s+${AGE}\s+${MAC}\s+${TYPE} -> Record
68+
# Capture time-stamp if vty line has command time-stamping turned on
69+
^Load\s+for\s+
70+
^Time\s+source\s+is
71+
^. -> Error

flask_app.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import io
2+
import yaml
3+
import json
4+
from flask import Flask, render_template, request, Response, url_for, jsonify
5+
from textfsm import TextFSM
6+
7+
app = Flask(__name__, static_url_path="/static")
8+
app.config['JSON_SORT_KEYS'] = False
9+
10+
11+
@app.route("/")
12+
def index():
13+
return render_template("index.html")
14+
15+
16+
@app.route("/examples")
17+
def examples():
18+
try:
19+
with open("examples.yaml", "r") as f:
20+
data = yaml.safe_load(f)
21+
resp = jsonify(data)
22+
except Exception as e:
23+
resp = jsonify({"error": f"Error {type(e).__name__}", "msg": str(e)})
24+
resp.status_code = 400
25+
finally:
26+
return resp
27+
28+
29+
@app.route("/rend", methods=["POST"])
30+
def rend():
31+
data = request.get_json()
32+
if not isinstance(data, dict):
33+
resp = jsonify({"error": "Invalid JSON"})
34+
resp.status_code = 400
35+
return resp
36+
textFSM_template = data.get("template", "")
37+
raw_data = data.get("data")
38+
39+
try:
40+
template = io.StringIO(textFSM_template)
41+
parser = TextFSM(template)
42+
output = parser.ParseTextToDicts(text=raw_data)
43+
44+
resp = jsonify({"result": json.dumps(output, indent=2)})
45+
except Exception as e:
46+
breakpoint()
47+
resp = jsonify({"error": f"Error {type(e).__name__}", "msg": str(e)})
48+
resp.status_code = 400
49+
finally:
50+
return resp
51+
52+
53+
if __name__ == "__main__":
54+
app.run(debug=True)

0 commit comments

Comments
 (0)