Skip to content

Commit e4ee51e

Browse files
committed
template & cors
1 parent d510130 commit e4ee51e

File tree

3 files changed

+104
-21
lines changed

3 files changed

+104
-21
lines changed

app.py

+9-21
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,18 @@
55
from time import sleep
66
import subprocess
77
import tempfile
8-
from flask import Flask, request
8+
from flask import Flask, request, render_template, jsonify
9+
from flask_cors import CORS
910
import os
1011

1112

1213
def create_app():
1314
app = Flask(__name__)
15+
CORS(app)
1416

1517
@app.route("/")
1618
def index():
17-
return f"""<h1>Create lxc container on proxmox via http api</h1>
18-
<p>Specify</p>
19-
<ul>
20-
<li>Ram</li>
21-
<li>Hostname</li>
22-
<li>Public ssh key (so you can login)</li>
23-
</ul>
24-
<p>You get:</p>
25-
<ul>
26-
<li>ipv6 public address for each container</li>
27-
<li>You can ssh into the container</li>
28-
</ul>
29-
<h2>Usage:</h2>
30-
<p><b>Note:</b>The `ssh_public_keys` is just a copy/paste `cat` of your id_rsa.pub</p>
31-
<pre>curl {request.host_url}container -d '{{"hostname":123, "memory": 1024, "ssh_public_keys": "<string-of-your-public-key>", "network": {{}}}}'</pre>\n
32-
<p>Then ssh into your container: ssh root@&lt;public_ip&gt; using the public key you provided.</p>
33-
<br /><a href="/openapi">View Openapi</a>"""
19+
return render_template("index.html")
3420

3521
@app.route("/container", methods=["POST"])
3622
def create_container():
@@ -68,14 +54,16 @@ def get_ip():
6854
attempts = max_attempts
6955

7056
return (
71-
{
72-
"msg": f"""Container created! Probably.\n
57+
jsonify(
58+
{
59+
"msg": f"""Container created! Probably.\n
7360
ssh into it with: `ssh root@{public_ip}` using your public key\n
7461
You might need to wait a minute or two before the public address is
7562
reachable. Not 100% sure why.\n
7663
You can expediate it by traceroute6{public_ip} will add your route to
7764
routing tables along the way."""
78-
},
65+
}
66+
),
7967
201,
8068
)
8169

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Optional gunicorn==20.1.0
22
Flask==1.1.2
33
pydantic==1.8.1
4+
Flask-Cors==3.0.1

templates/index.html

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<!-- Required meta tags -->
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
8+
<!-- Bootstrap CSS -->
9+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
10+
<title>Hello, world!</title>
11+
</head>
12+
<body>
13+
<div class="container">
14+
<div class="row">
15+
<div class="col-sm-12">
16+
<div class="jumbotron">
17+
<h1 class="display-4">Create a server quickly</h1>
18+
<p class="lead">You get a server with an ipv6 public address, which you can login to using ssh.</p>
19+
<hr class="my-4">
20+
<p><em>You can also create a server using curl:</em><br />
21+
<span style="background:white; padding: 4px;">curl {{ request.host_url }}container -d '{"hostname":123, "memory": 1024, "ssh_public_keys": "<string-of-your-public-key>", "network": {}}'</span></p>
22+
<a href="/openapi">View Openapi</a>
23+
</div>
24+
</div>
25+
</div>
26+
27+
<div class="row" id="start">
28+
<div class="col-sm-12">
29+
<form action="#" method="POST" id="serverForm">
30+
<div class="form-group">
31+
<label for="hostname">Hostname</label>
32+
<input type="hostname" name="hostname" class="form-control" id="hostname" placeholder="example.com" aria-describedby="hostnameHelp" required>
33+
<small id="hostnameHelp" class="form-text text-muted">e.g. example.com</small>
34+
</div>
35+
<div class="form-group">Ram
36+
<label for="exampleInput1">Ram</label>
37+
<input type="number" name="memory" step="1" class="form-control" value="1024" id="exampleInputRam1" disabled>
38+
<small class="form-text text-muted">You can increase this later.</small>
39+
</div>
40+
<div class="form-group">
41+
<label for="ssh_public_keys">Public SSH Key (so you can login)</label>
42+
<textarea class="form-control" name="ssh_public_keys" id="ssh_public_keys" rows="3" Required
43+
placeholder="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC71TlW7HzAUkiLN7GuP6p/IRivXxu0nXvhE4snnKxwpaWod8jPUJCYb8ISt4hhmuGCzHSKz4piOtdYHHUiaLWPZHGTUlkwCqbTYlfiz+YxWsF117I8iOjLGQuqn6f1pgZA+BE7YB2l3sUPNwBoCr5yyycIENFR6euDbCym0kODFhIQA5j2FyMI5QuwXWKj8P4t/pthBgqyyUsah47q51IWz7FVTHup4FIj/JPgqu/X/ZEkH+DHh8qv7kwl5uSBb6evctv7JqqNz2xbGCli+dDLvL0zgsFtPLjycK/KUN5qWHTjNvpE/CPzat2InI4wXYuX11Y9Dfah+jMrR7mb+ahRu0Zbvsn/RIpybgydMahBVYs1SAFtmzbGnnJODlag7vr2D/Jdi3E0LPliPqGQB3zCAINaQ00BAHg4ERxK4eS4TGOmqQVSYG4Y+8YhOBtEcvIGTA5/kfjAL/o69oUsC6x+tWPlzGFB5kQkS2ALs1C+S97F1Iu2yx45ZPfSmdtgGr0= bob@example"></textarea>
44+
<small class="form-text text-muted">Don't know what this is? It's better than a password. <a href="https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key" target="_blank">How to generate an ssh key</a></small>
45+
</div>
46+
<button type="submit" id="save" class="btn btn-primary btn-lg" href="#start" role="button">Start a Server</button>
47+
</form>
48+
</div>
49+
</div>
50+
</div><!-- end container -->
51+
52+
<!-- Optional JavaScript -->
53+
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
54+
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous">
55+
</script>
56+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous">
57+
</script>
58+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous">
59+
</script>
60+
<script>
61+
form = document.getElementById('save');
62+
63+
form.addEventListener("click", function(e) {
64+
e.preventDefault();
65+
console.log("OK");
66+
form.innerText = "Starting server..."
67+
form.disabled = true;
68+
69+
const formData = new FormData(document.getElementById('serverForm'));
70+
var object = {};
71+
formData.forEach((value, key) => object[key] = value);
72+
73+
object['memory'] = 512
74+
object['network'] = {}
75+
76+
var json = JSON.stringify(object);
77+
fetch('http://another-web-service.pcpink.co.uk/container', {
78+
method: 'POST',
79+
body: json
80+
})
81+
.then(response => response.json())
82+
.then(data => {
83+
console.log('Success:', data);
84+
document.write(data.msg);
85+
})
86+
.catch((error) => {
87+
console.error('Error:', error);
88+
});
89+
90+
})
91+
</script>
92+
</body>
93+
</html>
94+

0 commit comments

Comments
 (0)