Skip to content

Commit

Permalink
Initial HTTP API for Storm.
Browse files Browse the repository at this point in the history
  • Loading branch information
berkerpeksag committed Nov 13, 2013
1 parent 74faf41 commit abe8dd0
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 7 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
paramiko==1.10.1
termcolor
Flask==0.10.1
11 changes: 9 additions & 2 deletions storm/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import getpass
import __builtin__

from storm import Storm
from storm import __version__
from storm import Storm, __version__
from storm.exceptions import StormValueError
from storm.ssh_uri_parser import parse
from storm import web as _web

from storm.kommandr import *

from termcolor import colored
Expand Down Expand Up @@ -189,6 +190,12 @@ def delete_all():
print get_formatted_message(str(error), 'error')


@command('web')
def web(port=9002, debug=False):
"""Starts the web UI."""
_web.run(port, debug)


if __name__ == '__main__':
sys.exit(main())

3 changes: 2 additions & 1 deletion storm/kommandr.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ def _generate_command(self, func, name=None, **kwargs):
aliases = alias_list
break

func_help = func.__doc__ and func.__doc__.strip()
subparser = self.subparsers.add_parser(name or func.__name__,
aliases=aliases,
help=func.__doc__.strip())
help=func_help)
spec = inspect.getargspec(func)
opts = reversed(list(izip_longest(reversed(spec.args or []),
reversed(spec.defaults or []),
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions resources/web/index.html → storm/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<meta charset="UTF-8">
<title>Storm SSH: Manage your SSH like a boss.</title>

<link rel="stylesheet" href="css/style.css">
<script src="js/core/angular.min.js"></script>
<script src="js/main.js"></script>
<link rel="stylesheet" href="static/css/style.css">
<script src="static/js/core/angular.min.js"></script>
<script src="static/js/main.js"></script>
</head>
<body>
<div id="main" ng-controller="Storm" class="list-{{servers.length}} {{state.position}}">
Expand Down Expand Up @@ -46,4 +46,4 @@ <h2 draggable="true">{{state.action}} ssh entry:</h2>
</footer>
</div>
</body>
</html>
</html>
94 changes: 94 additions & 0 deletions storm/web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import json
import os.path

from flask import Flask, make_response, jsonify, request

from storm import Storm
from storm.ssh_uri_parser import parse
from storm.exceptions import StormValueError

app = Flask(__name__)
storm_ = Storm()


def render(template):
path = os.path.join('storm', 'templates', template)
with open(path) as fobj:
content = fobj.read()
return make_response(content)

@app.route('/')
def index():
return render('index.html')


@app.route('/list', methods=['GET'])
def list_keys():
return json.dumps(storm_.list_entries(True))


@app.route('/add', methods=['POST'])
def add():
try:
name = request.json['name']
connection_uri = request.json['connection_uri']
if 'id_file' in request.json:
id_file = request.json['id_file']
else:
id_file = ''

# validate name
if '@' in name:
return jsonify(message='invalid value: "@" cannot be used in name.'), 400

user, host, port = parse(connection_uri)

storm_.add_entry(name, host, user, port, id_file)

return jsonify(success=True), 201
except (TypeError, StormValueError):
return jsonify(success=False), 400


@app.route('/edit', methods=['PUT'])
def edit():
try:
name = request.json['name']
connection_uri = request.json['connection_uri']
if 'id_file' in request.json:
id_file = request.json['id_file']
else:
id_file = ''

user, host, port = parse(connection_uri)

storm_.edit_entry(name, host, user, port, id_file)

return jsonify(success=True), 200
except StormValueError:
return jsonify(success=False), 404
except TypeError:
return jsonify(success=False), 400


@app.route('/delete', methods=['DELETE'])
def delete():
try:
name = request.json['name']
storm_.delete_entry(name)
return jsonify(success=True), 200
except (TypeError, StormValueError):
return jsonify(success=False), 400


@app.route('/delete_all', methods=['DELETE'])
def delete_all():
try:
storm_.delete_all_entries()
return jsonify(success=True), 200
except StormValueError:
return jsonify(success=False), 400


def run(port, debug):
app.run(port=port, debug=debug)

0 comments on commit abe8dd0

Please sign in to comment.