From a1db290bd341d2f40633afd8193e4fe5b020427f Mon Sep 17 00:00:00 2001 From: jenkins-x-bot Date: Sat, 12 Sep 2020 23:51:12 +0800 Subject: [PATCH] Updated with correct code --- app.py | 8 +--- app.yaml | 35 ----------------- appengine_config.py | 4 -- forms.py | 8 ++-- main.py | 36 +++++++++++++----- routes/__init__.py | 4 ++ routes/kube-helm-routes.py | 17 --------- routes/kube_helm_routes.py | 36 ++++++++++++++++++ tables.py | 10 +++++ templates/chartRevisionList.html | 26 +++++++++++++ templates/nameChartSelect.html | 65 ++++++++++++++++++++++++++++++++ templates/nameDeploySelect.html | 6 +-- 12 files changed, 174 insertions(+), 81 deletions(-) delete mode 100644 app.yaml delete mode 100644 appengine_config.py create mode 100644 routes/__init__.py delete mode 100644 routes/kube-helm-routes.py create mode 100644 routes/kube_helm_routes.py create mode 100644 tables.py create mode 100644 templates/chartRevisionList.html create mode 100644 templates/nameChartSelect.html diff --git a/app.py b/app.py index 88af264..958e1cb 100644 --- a/app.py +++ b/app.py @@ -1,13 +1,7 @@ # encoding=utf-8 -import random -import string from flask import Flask from flask_bootstrap import Bootstrap app = Flask(__name__) app.secret_key = "4pFwRNNXs+xQSOEaHrq4iSBwl+mq1UTdRuxqhM+RQpo=" -Bootstrap(app) - -def randomUrlSuffix(stringLength=6): - lettersAndDigits = string.ascii_letters + string.digits - return ''.join(random.choice(lettersAndDigits) for i in range(stringLength)) +Bootstrap(app) \ No newline at end of file diff --git a/app.yaml b/app.yaml deleted file mode 100644 index d548f3b..0000000 --- a/app.yaml +++ /dev/null @@ -1,35 +0,0 @@ -runtime: python27 -api_version: 1 -threadsafe: true - -handlers: -- url: /static - static_dir: static -- url: /.* - script: main.app - secure: always - - -libraries: -- name: ssl - version: "latest" -- name: MySQLdb - version: "1.2.5" -- name: flask - version: latest - - -env_variables: - CLOUDSQL_CONNECTION_NAME: hk-hockey:asia-east2:hk-hockey-sql - CLOUDSQL_USER: root - CLOUDSQL_WRITE_USER: hockeyWrite - CLOUDSQL_READ_USER: hockeyRead - CLOUDSQL_PASSWORD: P8P1YopMlwg8TxhE - CLOUDSQL_WRITE_PASSWORD: 1URYcxXXlQ6xOWgj - CLOUDSQL_READ_PASSWORD: o4GWrbbkBKy3oR6u - CLOUDSQL_DATABASE: 2019_hockeyResults - CLOUDSQL_DATABASE_STATIC: hockeyResults - CLOUDSQL_CHARSET: utf8 - BASIC_AUTH_USERNAME: admin - BASIC_AUTH_PASSWORD: hATnNYriYN5ZrHvXcaImtXJ8SGMonNym - diff --git a/appengine_config.py b/appengine_config.py deleted file mode 100644 index 1a4ad37..0000000 --- a/appengine_config.py +++ /dev/null @@ -1,4 +0,0 @@ -from google.appengine.ext import vendor - -# Add any libraries installed in the "lib" folder. -vendor.add('venv/lib') diff --git a/forms.py b/forms.py index d1602e5..e9a7874 100644 --- a/forms.py +++ b/forms.py @@ -3,15 +3,13 @@ from app import app from flask_wtf import FlaskForm from wtforms import BooleanField, StringField, PasswordField, TextField, IntegerField, TextAreaField, SubmitField, RadioField, SelectField from wtforms.fields.html5 import DateField -from wtforms_components import read_only from wtforms import validators, ValidationError from wtforms.validators import InputRequired, Email, Length -from readSettings import mySettings from flask_bootstrap import Bootstrap class deploySelectForm(FlaskForm): - namespace = SelectField('namespace', validators=[InputRequired(), Length(min=4, max=15)]) - deployment = SelectField('password', validators=[InputRequired(), Length(min=4, max=80)]) + namespace = SelectField('namespace', choices=[], coerce=str) + chart = SelectField('chart', choices=[]) version = SelectField('remember me') - + submitButton = SubmitField("Submit") \ No newline at end of file diff --git a/main.py b/main.py index 511f8a2..aae7b26 100644 --- a/main.py +++ b/main.py @@ -13,7 +13,8 @@ from routes import * from logging import error, info from subprocess import STDOUT, CalledProcessError, check_output from itertools import islice -import json + +app.register_blueprint(routes) def get_namespaces(): command = "/usr/local/bin/kubectl get ns -ojson" @@ -39,16 +40,31 @@ def get_deployments(namespace): data = json.loads(output) return data -@app.route('/', methods=['GET', 'POST']) -def namespacesForm(): - form = deploySelectForm() - namespaces = get_namespaces() - print('Here we are') - if form.validate_on_submit(): - return redirect(url_for('/')) -# return '

Something went wrong there

' +def get_charts(namespace): + command = "/usr/bin/helm -n " + namespace + " list -ojson" + info(f"Running command: {command}") + try: + output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") + except CalledProcessError as err: + error(err.output.decode("utf-8")) + raise err + info(f"Output from command:\n{output}") + data = json.loads(output) + return data - return render_template('index.html', namespaces=namespaces, form=form) +def get_chartdata(namespace, chart): + command = "/usr//bin/helm -n " + namespace + " history " + chart + " -ojson" + info(f"Running command: {command}") + print(f"Running command: {command}") + try: + output = check_output(command.split(" "), stderr=STDOUT).decode("utf-8") + except CalledProcessError as err: + error(err.output.decode("utf-8")) + raise err + info(f"Output from command:\n{output}") + data = json.loads(output) + print(data) + return data if __name__ == "__main__": app.run(host='0.0.0.0', port=3000, debug=True) diff --git a/routes/__init__.py b/routes/__init__.py new file mode 100644 index 0000000..2d021d9 --- /dev/null +++ b/routes/__init__.py @@ -0,0 +1,4 @@ +from flask import Blueprint +routes = Blueprint('routes', __name__) + +from .kube_helm_routes import * \ No newline at end of file diff --git a/routes/kube-helm-routes.py b/routes/kube-helm-routes.py deleted file mode 100644 index ba628bf..0000000 --- a/routes/kube-helm-routes.py +++ /dev/null @@ -1,17 +0,0 @@ -from flask import render_template, request -from forms import searchForm, playerRecordsForm, teamRecordsForm, clubPlayingRecordsForm -from . import routes -from tables import Results, playerResults, teamResults, clubPlayingRecord -import datetime -from datetime import date - -@routes.route('/') -def deploySelect(): - namespaces = - sql2 = "SELECT nextClub, oppoLogo FROM hkfcDAdminSettings" - clubs = sql_read_static(sql) - settings = sql_read_static(sql2) - form = teamRecordsForm() - form.clubName.choices = [(name['hockeyClub'], name['hockeyClub']) for name in clubs] - clubLogo = settings[0]['oppoLogo'] - return render_template('_teamRecords.html', data=clubs, selectedClubLogo=clubLogo, form=form) \ No newline at end of file diff --git a/routes/kube_helm_routes.py b/routes/kube_helm_routes.py new file mode 100644 index 0000000..ada0ce4 --- /dev/null +++ b/routes/kube_helm_routes.py @@ -0,0 +1,36 @@ +from flask import render_template, request, jsonify +from forms import deploySelectForm +from main import get_namespaces, get_charts, get_chartdata +from . import routes +from tables import chartVersionTable +import json + +@routes.route('/', methods=['GET', 'POST']) +def index(): + namespaces = get_namespaces() + print(namespaces) + form = deploySelectForm() + form.namespace.choices = [(name['metadata']['name'], name['metadata']['name']) for name in namespaces['items']] + return render_template('nameChartSelect.html', namespaces=namespaces, form=form) + +@routes.route('/chartSelect', methods=['POST']) +def chartVersions(): + namespace = request.form['namespace'] + chart = request.form['chart'] + chartVersions = get_chartdata(namespace, chart) + print(chartVersions) + table = chartVersionTable(chartVersions) + table.border = True + table.classes = ['table-striped', 'table-condensed', 'table-hover'] + return render_template('chartRevisionList.html', table=table, namespace=namespace, chart=chart) + + +@routes.route('/nsLookup/') +def namespaceLookup(namespace): + charts = get_charts(namespace) + return jsonify(charts) + +@routes.route('/deployChartRevision/') +def deployChartRevision(revision): + charts = get_charts(namespace) + return jsonify(charts) \ No newline at end of file diff --git a/tables.py b/tables.py new file mode 100644 index 0000000..3167480 --- /dev/null +++ b/tables.py @@ -0,0 +1,10 @@ +from flask_table import Table, Col, LinkCol, ButtonCol + +class chartVersionTable(Table): + revision = Col('Chart Revision') + updated = Col('Updated') + status = Col('Status') + chart = Col('Chart Version') + app_version = Col('Application Version') + description = Col('Description') + deploy = ButtonCol('Deploy', 'routes.deployChartRevision', url_kwargs=dict(revision='revision'), button_attrs={"type" : "submit", "class" : "btn btn-danger"}) diff --git a/templates/chartRevisionList.html b/templates/chartRevisionList.html new file mode 100644 index 0000000..afd34d1 --- /dev/null +++ b/templates/chartRevisionList.html @@ -0,0 +1,26 @@ + + + List of deployed chart revisions + + + + + + + +

+ {% with messages = get_flashed_messages() %} + {% if messages %} +

    + {% for message in messages %} +
  • {{ message }}
  • + {% endfor %} +
+ {% endif %} + {% endwith %} +

+ + {{ table }} +
+ Home + \ No newline at end of file diff --git a/templates/nameChartSelect.html b/templates/nameChartSelect.html new file mode 100644 index 0000000..9b97fa5 --- /dev/null +++ b/templates/nameChartSelect.html @@ -0,0 +1,65 @@ + + + Select Namespace and Chart *TESTING* + + + + + + +

Select both a namespace and chart

+ +
+

+ {{ form.csrf_token }} +

+
+
+
+
+
+ Namespace: + {{ form.namespace(class_="form-control") }} +
+
+
+
+ Chart: + {{ form.chart(class_="form-control") }} +
+
+
+

+ {{ form.submitButton(class_="btn btn-success") }} + Cancel +

+
+
+
+

+
+ + + + diff --git a/templates/nameDeploySelect.html b/templates/nameDeploySelect.html index 67fdf94..78c3275 100644 --- a/templates/nameDeploySelect.html +++ b/templates/nameDeploySelect.html @@ -22,7 +22,7 @@ {{ form.namespace(class_="form-control") }} -
+
Deployment: {{ form.deployment(class_="form-control") }} @@ -52,8 +52,8 @@ fetch('/nsLookup/' + namespace).then(function(response) { response.json().then(function(data) { var optionHTML = ''; - for (var deployment of data) { - optionHTML += ''; + for (var deployment of data.items) { + optionHTML += ''; } deploymentSelect.innerHTML = optionHTML; })