OSDN Git Service

Display of execution status of rest api
authorniwa-hideyuki <niwa.hideyuki@jp.fujitsu.com>
Wed, 22 Oct 2014 04:57:50 +0000 (13:57 +0900)
committerniwa-hideyuki <niwa.hideyuki@jp.fujitsu.com>
Wed, 22 Oct 2014 04:57:50 +0000 (13:57 +0900)
lxcf/rest-api/lxcfv1/api_common.py

index d1134a5..57cb652 100755 (executable)
-#!/usr/bin/python\r
-#-*- coding:utf-8 -*-\r
-\r
-# LXCF - LXC Facility\r
-# Copyright (C) 2014 FUJITSU LIMITED\r
-\r
-# This program is free software; you can redistribute it and/or\r
-# modify it under the terms of the GNU General Public License\r
-# as published by the Free Software Foundation; version 2\r
-# of the License.\r
-#\r
-# This program is distributed in the hope that it will be useful,\r
-# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-# GNU General Public License for more details.\r
-#\r
-# You should have received a copy of the GNU General Public License\r
-# along with this program; if not, write to the Free Software\r
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\r
-# 02110-1301, USA.\r
-\r
-#\r
-# A scripts for creating LXC domain in which some daemons are running.\r
-# running systemd, rsyslog, sshd in a container.\r
-#\r
-\r
-import os, datetime, cgi, StringIO, commands\r
-from wsgiref import util, simple_server\r
-from xml.sax import saxutils\r
-\r
-# sanity check\r
-def sanity_check(parm):\r
-    for c in parm:\r
-        if c.isdigit() or c.isalpha() or (c == '-') or (c == '_') or (c == ' '):\r
-            continue\r
-        else:\r
-            return False\r
-    return True\r
-\r
-# Conversion of character string\r
-def conv_char(str):\r
-    s = ""\r
-    for c in str:\r
-        if (c == '"'):\r
-            c = '&quot;'\r
-        elif (c == '&'):\r
-            c = '&amp;'\r
-        elif (c == '<'):\r
-            c = '&lt;'\r
-        elif (c == '>'):\r
-            c = '&gt;;'\r
-        s = s + c\r
-    return s\r
-\r
-\r
-# API common class\r
-class api_common(object):\r
-    def __init__(self, callcmd):\r
-        self.messages = []\r
-        self.callcmd = callcmd\r
-\r
-    def __call__(self, environ, start_response):\r
-        method = environ['REQUEST_METHOD']\r
-        if method == 'GET':\r
-            return self.get_response(environ, start_response)\r
-        elif method == 'POST':\r
-            return self.post_response(environ, start_response)\r
-        else:\r
-            start_response('501 Not Implemented', [('Content-type', 'text/plain')])\r
-            return 'Not Implemented'\r
-\r
-\r
-    def get_response(self, environ, start_response):\r
-        fp = StringIO.StringIO()\r
-\r
-        fp.write('''<html>\r
-<head><title>LXCF API</title>\r
-<meta http-equiv="Content-type" content="text/html; charset=utf-8">\r
-</head>\r
-<body>\r
-<pre>\r
-''')\r
-       fp.write(conv_char(self.messages))\r
-\r
-        fp.write('''</pre><HR><form action="%s" method="POST" AcceptEncoding="utf-8">\r
-<dl>\r
-<dt>password</dt>\r
-<dd><input type="password" name="password" value=""/></dd>\r
-</dl>\r
-<input type="submit" name="save" value="Execution" />\r
-</form></body></html>''' % util.request_uri(environ))\r
-\r
-        fp.seek(0)\r
-\r
-        start_response('200 OK', [('Content-type', 'text/html; charset=utf-8')])\r
-        return fp\r
-\r
-\r
-    def post_response(self, environ, start_response):\r
-        inpt = environ['wsgi.input']\r
-        length = int(environ.get('CONTENT_LENGTH', 0))\r
-\r
-        query = dict(cgi.parse_qsl(inpt.read(length)))\r
-\r
-       try:\r
-            password = query['password']\r
-        except:\r
-            password = ""\r
-\r
-        path_info = environ['PATH_INFO']\r
-\r
-       cmdline=path_info.split("/")\r
-        cmd = self.callcmd(cmdline)\r
-\r
-       print ("authenticity : "+password+" , "+path_info)\r
-\r
-        if (sanity_check(cmd)):\r
-            # exec command\r
-            check = commands.getoutput("/usr/sbin/lxcf "+cmd)\r
-        else:\r
-            check = "error: sanity error ... "+cmd\r
-\r
-       self.messages = check\r
-\r
-        fp = StringIO.StringIO()\r
-\r
-        fp.write('''<html>\r
-<head><title>LXCF API</title>\r
-<meta http-equiv="Content-type" content="text/html; charset=utf-8">\r
-</head>\r
-<body>\r
-<form action="%s" method="POST" AcceptEncoding="utf-8">\r
-</body></html>''' % util.request_uri(environ))\r
-\r
-        fp.seek(0)\r
-\r
-        start_response('303 See Other', [('Content-type', 'text/plain'),\r
-                                         ('Location', util.request_uri(environ))])\r
-\r
-        return fp\r
-\r
-\r
-# API: start\r
-def call_start(cmdline):\r
-    cmdstr = "start "\r
-    for str in cmdline:\r
-        cmdstr = cmdstr + " " + str\r
-    print cmdstr\r
-    return cmdstr\r
-\r
-class api_start(api_common):\r
-    def __init__(self, callcmd):\r
-        super(api_start, self).__init__(callcmd)\r
-\r
-# API: stop\r
-def call_stop(cmdline):\r
-    cmdstr = "stop "\r
-    for str in cmdline:\r
-        cmdstr = cmdstr + " " + str\r
-    print cmdstr\r
-    return cmdstr\r
-\r
-class api_stop(api_common):\r
-    def __init__(self, callcmd):\r
-        super(api_stop, self).__init__(callcmd)\r
-\r
-# API: list\r
-def call_list(cmdline):\r
-    cmdstr = "list"\r
-    for str in cmdline:\r
-        cmdstr = cmdstr + " " + str\r
-    print cmdstr\r
-    return cmdstr\r
-\r
-class api_list(api_common):\r
-    def __init__(self, callcmd):\r
-        super(api_list, self).__init__(callcmd)\r
-\r
-# API: sysgen\r
-def call_sysgen(cmdline):\r
-    cmdstr = "sysgen"\r
-    for str in cmdline:\r
-        cmdstr = cmdstr + " " + str\r
-    print cmdstr\r
-    return cmdstr\r
-\r
-class api_sysgen(api_common):\r
-    def __init__(self, callcmd):\r
-        super(api_sysgen, self).__init__(callcmd)\r
-\r
-# API: erase\r
-def call_erase(cmdline):\r
-    cmdstr = "erase"\r
-    for str in cmdline:\r
-        cmdstr = cmdstr + " " + str\r
-    print cmdstr\r
-    return cmdstr\r
-\r
-class api_erase(api_common):\r
-    def __init__(self, callcmd):\r
-        super(api_erase, self).__init__(callcmd)\r
-\r
-# API: show\r
-def call_show(cmdline):\r
-    cmdstr = "show"\r
-    for str in cmdline:\r
-        cmdstr = cmdstr + " " + str\r
-    print cmdstr\r
-    return cmdstr\r
-\r
-class api_show(api_common):\r
-    def __init__(self, callcmd):\r
-        super(api_show, self).__init__(callcmd)\r
-\r
-\r
+#!/usr/bin/python
+#-*- coding:utf-8 -*-
+
+# LXCF - LXC Facility
+# Copyright (C) 2014 FUJITSU LIMITED
+
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2
+# of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+#
+# A scripts for creating LXC domain in which some daemons are running.
+# running systemd, rsyslog, sshd in a container.
+#
+
+import os, datetime, cgi, StringIO, commands
+from wsgiref import util, simple_server
+from xml.sax import saxutils
+
+# sanity check
+def sanity_check(parm):
+    for c in parm:
+        if c.isdigit() or c.isalpha() or (c == '-') or (c == '_') or (c == ' '):
+            continue
+        else:
+            return False
+    return True
+
+# Conversion of character string
+def conv_char(str):
+    s = ""
+    for c in str:
+        if (c == '"'):
+            c = '&quot;'
+        elif (c == '&'):
+            c = '&amp;'
+        elif (c == '<'):
+            c = '&lt;'
+        elif (c == '>'):
+            c = '&gt;'
+        s = s + c
+    return s
+
+
+# API common class
+class api_common(object):
+    def __init__(self, callcmd):
+        self.messages = "no operation"
+        self.status = 0
+        self.callcmd = callcmd
+
+    def __call__(self, environ, start_response):
+        method = environ['REQUEST_METHOD']
+        if method == 'GET':
+            return self.get_response(environ, start_response)
+        elif method == 'POST':
+            return self.post_response(environ, start_response)
+        else:
+            start_response('501 Not Implemented', [('Content-type', 'text/plain')])
+            return 'Not Implemented'
+
+
+    def get_response(self, environ, start_response):
+        fp = StringIO.StringIO()
+
+        fp.write('''<html>
+<head><title>LXCF API</title>
+<meta http-equiv="Content-type" content="text/html; charset=utf-8">
+</head>
+<body>
+<pre>
+''')
+        fp.write("status="+str(self.status)+"\n")
+        fp.write(conv_char(self.messages)+"\n")
+        self.status = 0
+       self.messages = "no operation"
+
+        fp.write('''</pre>\n<HR><form action="%s" method="POST" AcceptEncoding="utf-8">
+<dl>
+<dt>password</dt>
+<dd><input type="password" name="password" value=""/></dd>
+</dl>
+<input type="submit" name="save" value="Execution" />
+</form></body></html>''' % util.request_uri(environ))
+
+        fp.seek(0)
+
+        start_response('200 OK', [('Content-type', 'text/html; charset=utf-8')])
+        return fp
+
+
+    def post_response(self, environ, start_response):
+       self.status = 0
+       self.messages = "no operation"
+        inpt = environ['wsgi.input']
+        length = int(environ.get('CONTENT_LENGTH', 0))
+
+        query = dict(cgi.parse_qsl(inpt.read(length)))
+
+       try:
+            password = query['password']
+        except:
+            password = ""
+
+        path_info = environ['PATH_INFO']
+
+       cmdline=path_info.split("/")
+        cmd = self.callcmd(cmdline)
+
+       print ("authenticity : "+password+" , "+path_info)
+
+        if (sanity_check(cmd)):
+            # exec command
+            check = commands.getstatusoutput("/usr/sbin/lxcf "+cmd)
+        else:
+            check = "error: sanity error ... "+cmd
+
+       self.status = check[0]
+        self.messages = check[1]
+
+        fp = StringIO.StringIO()
+
+        fp.write('''<html>
+<head><title>LXCF API</title>
+<meta http-equiv="Content-type" content="text/html; charset=utf-8">
+</head>
+<body>
+<form action="%s" method="POST" AcceptEncoding="utf-8">
+</body></html>''' % util.request_uri(environ))
+
+        fp.seek(0)
+
+        start_response('303 See Other', [('Content-type', 'text/plain'),
+                                         ('Location', util.request_uri(environ))])
+
+        return fp
+
+
+# API: start
+def call_start(cmdline):
+    cmdstr = "start "
+    for str in cmdline:
+        cmdstr = cmdstr + " " + str
+    print cmdstr
+    return cmdstr
+
+class api_start(api_common):
+    def __init__(self, callcmd):
+        super(api_start, self).__init__(callcmd)
+
+# API: stop
+def call_stop(cmdline):
+    cmdstr = "stop "
+    for str in cmdline:
+        cmdstr = cmdstr + " " + str
+    print cmdstr
+    return cmdstr
+
+class api_stop(api_common):
+    def __init__(self, callcmd):
+        super(api_stop, self).__init__(callcmd)
+
+# API: list
+def call_list(cmdline):
+    cmdstr = "list"
+    for str in cmdline:
+        cmdstr = cmdstr + " " + str
+    print cmdstr
+    return cmdstr
+
+class api_list(api_common):
+    def __init__(self, callcmd):
+        super(api_list, self).__init__(callcmd)
+
+# API: sysgen
+def call_sysgen(cmdline):
+    cmdstr = "sysgen"
+    for str in cmdline:
+        cmdstr = cmdstr + " " + str
+    print cmdstr
+    return cmdstr
+
+class api_sysgen(api_common):
+    def __init__(self, callcmd):
+        super(api_sysgen, self).__init__(callcmd)
+
+# API: erase
+def call_erase(cmdline):
+    cmdstr = "erase"
+    for str in cmdline:
+        cmdstr = cmdstr + " " + str
+    print cmdstr
+    return cmdstr
+
+class api_erase(api_common):
+    def __init__(self, callcmd):
+        super(api_erase, self).__init__(callcmd)
+
+# API: show
+def call_show(cmdline):
+    cmdstr = "show"
+    for str in cmdline:
+        cmdstr = cmdstr + " " + str
+    print cmdstr
+    return cmdstr
+
+class api_show(api_common):
+    def __init__(self, callcmd):
+        super(api_show, self).__init__(callcmd)
+
+