Skip to content

Commit

Permalink
add: nice style and auto start/stop at
Browse files Browse the repository at this point in the history
  • Loading branch information
sasanquaneuf committed Feb 4, 2021
1 parent 3df0818 commit d1e45e4
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 15 deletions.
7 changes: 6 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from logging.handlers import RotatingFileHandler

import cherrypy
from flask import Flask, request, session, redirect
from flask import Flask, request, session, redirect, send_from_directory
from flask_babel import Babel
from flask_wtf.csrf import CSRFProtect
from paste.translogger import TransLogger
Expand Down Expand Up @@ -56,6 +56,11 @@ def ssl_redirect():
return redirect('://'.join(url), 301)


@app.route('/static/<path:path>', methods=['GET', 'POST'])
def send_static_cdn(path):
return send_from_directory('static/', path)


if __name__ == '__main__':
random.seed()
log_dir = 'log/'
Expand Down
20 changes: 16 additions & 4 deletions controller/server.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json
from typing import Optional

from boto3.session import Session as BotoSession
from flask import Blueprint, render_template, request

from basic import need_basic_auth
from config import webapp_settings
from model import Server
from formatter import safe_strftime
from model import Server, HideServer
from mysql_dbcon import Connection
from service.pull import GitHubConnector

Expand All @@ -28,20 +30,30 @@ def server_list():
instance_response = ec2.describe_instances()

with Connection() as cn:
db_instance_id_list = [server.instance_id for server in cn.s.query(Server).all()]
server_list = cn.s.query(Server).all()
db_instance_id_dict = {server.instance_id: server for server in server_list}
hide_server_name_set = {hide_server.name for hide_server in cn.s.query(HideServer).all()}

instance_list = []
# TODO non-reserved instance
for r in instance_response['Reservations']:
for instance in r['Instances']:
name_tag = [tag for tag in instance['Tags'] if tag['Key'] == 'Name']
name = name_tag[0]['Value'] if name_tag else ''
if name in hide_server_name_set:
continue
server: Optional[Server] = db_instance_id_dict.get(instance['InstanceId'])
instance_list.append({
'InstanceId': instance['InstanceId'],
'State': instance['State']['Name'],
'Name': name_tag[0]['Value'] if name_tag else '',
'Name': name,
'PrivateIpAddress': instance['NetworkInterfaces'][0]['PrivateIpAddress']
if instance['NetworkInterfaces'] else '',
'Registered': instance['InstanceId'] in db_instance_id_list,
'Registered': instance['InstanceId'] in db_instance_id_dict,
'Staging': server and server.is_staging,
'AutoStartAt': safe_strftime(server and server.auto_start_at),
'AutoStopAt': safe_strftime(server and server.auto_stop_at),
'Id': server and server.id,
})
instance_list.sort(key=lambda x: x['Name'] + '____' + x['InstanceId'])
return render_template('server/list.html', instance_list=instance_list)
Expand Down
5 changes: 5 additions & 0 deletions formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def safe_strftime(d, format_str='%Y-%m-%d %H:%M:%S') -> str:
try:
return d.strftime(format_str)
except Exception:
return ''
11 changes: 11 additions & 0 deletions model/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,21 @@ class Server(BaseObject):
db_schema = Column(Text)
check_url = Column(Text)
is_staging = Column(Integer)
auto_start_at = Column(DateTime)
auto_stop_at = Column(DateTime)

pull_requests = relationship('PullRequest', back_populates='server')


class HideServer(BaseObject):
__tablename__ = 'hide_servers'

id = Column(BigInteger, primary_key=True, autoincrement=True)
created_at = Column(DateTime, default=current_timestamp(), nullable=False)
updated_at = Column(DateTime, default=current_timestamp(), onupdate=current_timestamp(), nullable=False)
name = Column(Text)


class PullRequest(BaseObject):
__tablename__ = 'pull_requests'

Expand Down
51 changes: 51 additions & 0 deletions static/simple.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
body {
color: #555;
}

table {
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}

thead tr {
background-color: #009879;
color: #ffffff;
text-align: left;
}

th, td {
padding: 12px 15px;
}

tbody tr {
border-bottom: 1px solid #dddddd;
}

tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}

tbody tr:last-of-type {
border-bottom: 2px solid #009879;
}

button {
background-color: #555;
border: none;
color: #fff;
font-size: 120%;
padding: 12px 26px;
text-align: center;
text-decoration: none;
display: inline-block;
cursor: pointer;
transition: background-color 0.5s;
}

button:hover {
background-color: #999;
}
41 changes: 31 additions & 10 deletions templates/server/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,58 @@
<head>
<meta charset="UTF-8">
<title>pullre-kun</title>
<link rel="stylesheet" href="/static/simple.css">
</head>
<body>
<table>
<thead>
<tr>
<td>use</td>
<td>name</td>
<td>id</td>
<td>state</td>
<td>auto start at(UTC)</td>
<td>auto stop at(UTC)</td>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
{% for instance in instance_list %}
<tr>
<td>{% if instance['Staging'] %}staging{% endif %}</td>
<td>{{ instance['Name'] }}</td>
<td>{{ instance['InstanceId'] }}</td>
<td>{{ instance['State'] }}</td>
<td>{{ instance['AutoStartAt'] }}</td>
<td>{{ instance['AutoStopAt'] }}</td>
<td>
<form action="/server/register" method="post">
<input type="hidden" name="InstanceId" value="{{ instance['InstanceId'] }}" />
<input type="hidden" name="Name" value="{{ instance['Name'] }}" />
<input type="hidden" name="PrivateIpAddress" value="{{ instance['PrivateIpAddress'] }}" />
{% if not instance['Registered'] %}
<input type="submit" value="register" />
{% endif %}
</form>
{% if not instance['Registered'] %}
<form action="/server/register" method="post">
<input type="hidden" name="InstanceId" value="{{ instance['InstanceId'] }}" />
<input type="hidden" name="Name" value="{{ instance['Name'] }}" />
<input type="hidden" name="PrivateIpAddress" value="{{ instance['PrivateIpAddress'] }}" />
<button type="submit">register</button>
</form>
{% else %}
<form action="/master/servers/{{ instance['Id'] }}/update" method="get">
<button type="submit">edit</button>
</form>
{% endif %}
</td>
<td>
<form action="/server/start" method="post">
<input type="hidden" name="InstanceId" value="{{ instance['InstanceId'] }}" />
{% if instance['State'] != 'running' %}
<input type="submit" value="start" />
<button type="submit">start</button>
{% endif %}
</form>
</td>
<td>
<form action="/server/stop" method="post">
<input type="hidden" name="InstanceId" value="{{ instance['InstanceId'] }}" />
{% if instance['State'] != 'stopped' %}
<input type="submit" value="stop" />
<button type="submit">stop</button>
{% endif %}
</form>
</td>
Expand Down

0 comments on commit d1e45e4

Please sign in to comment.