OSDN Git Service

Display of execution status of rest api
[lxcf/lxcf.git] / lxcf / rest-api / lxcfv1 / api_common.py
1 #!/usr/bin/python
2 #-*- coding:utf-8 -*-
3
4 # LXCF - LXC Facility
5 # Copyright (C) 2014 FUJITSU LIMITED
6
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; version 2
10 # of the License.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301, USA.
21
22 #
23 # A scripts for creating LXC domain in which some daemons are running.
24 # running systemd, rsyslog, sshd in a container.
25 #
26
27 import os, datetime, cgi, StringIO, commands
28 from wsgiref import util, simple_server
29 from xml.sax import saxutils
30
31 # sanity check
32 def sanity_check(parm):
33     for c in parm:
34         if c.isdigit() or c.isalpha() or (c == '-') or (c == '_') or (c == ' '):
35             continue
36         else:
37             return False
38     return True
39
40 # Conversion of character string
41 def conv_char(str):
42     s = ""
43     for c in str:
44         if (c == '"'):
45             c = '"'
46         elif (c == '&'):
47             c = '&'
48         elif (c == '<'):
49             c = '&lt;'
50         elif (c == '>'):
51             c = '&gt;'
52         s = s + c
53     return s
54
55
56 # API common class
57 class api_common(object):
58     def __init__(self, callcmd):
59         self.messages = "no operation"
60         self.status = 0
61         self.callcmd = callcmd
62
63     def __call__(self, environ, start_response):
64         method = environ['REQUEST_METHOD']
65         if method == 'GET':
66             return self.get_response(environ, start_response)
67         elif method == 'POST':
68             return self.post_response(environ, start_response)
69         else:
70             start_response('501 Not Implemented', [('Content-type', 'text/plain')])
71             return 'Not Implemented'
72
73
74     def get_response(self, environ, start_response):
75         fp = StringIO.StringIO()
76
77         fp.write('''<html>
78 <head><title>LXCF API</title>
79 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
80 </head>
81 <body>
82 <pre>
83 ''')
84         fp.write("status="+str(self.status)+"\n")
85         fp.write(conv_char(self.messages)+"\n")
86         self.status = 0
87         self.messages = "no operation"
88
89         fp.write('''</pre>\n<HR><form action="%s" method="POST" AcceptEncoding="utf-8">
90 <dl>
91 <dt>password</dt>
92 <dd><input type="password" name="password" value=""/></dd>
93 </dl>
94 <input type="submit" name="save" value="Execution" />
95 </form></body></html>''' % util.request_uri(environ))
96
97         fp.seek(0)
98
99         start_response('200 OK', [('Content-type', 'text/html; charset=utf-8')])
100         return fp
101
102
103     def post_response(self, environ, start_response):
104         self.status = 0
105         self.messages = "no operation"
106         inpt = environ['wsgi.input']
107         length = int(environ.get('CONTENT_LENGTH', 0))
108
109         query = dict(cgi.parse_qsl(inpt.read(length)))
110
111         try:
112             password = query['password']
113         except:
114             password = ""
115
116         path_info = environ['PATH_INFO']
117
118         cmdline=path_info.split("/")
119         cmd = self.callcmd(cmdline)
120
121         print ("authenticity : "+password+" , "+path_info)
122
123         if (sanity_check(cmd)):
124             # exec command
125             check = commands.getstatusoutput("/usr/sbin/lxcf "+cmd)
126         else:
127             check = "error: sanity error ... "+cmd
128
129         self.status = check[0]
130         self.messages = check[1]
131
132         fp = StringIO.StringIO()
133
134         fp.write('''<html>
135 <head><title>LXCF API</title>
136 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
137 </head>
138 <body>
139 <form action="%s" method="POST" AcceptEncoding="utf-8">
140 </body></html>''' % util.request_uri(environ))
141
142         fp.seek(0)
143
144         start_response('303 See Other', [('Content-type', 'text/plain'),
145                                          ('Location', util.request_uri(environ))])
146
147         return fp
148
149
150 # API: start
151 def call_start(cmdline):
152     cmdstr = "start "
153     for str in cmdline:
154         cmdstr = cmdstr + " " + str
155     print cmdstr
156     return cmdstr
157
158 class api_start(api_common):
159     def __init__(self, callcmd):
160         super(api_start, self).__init__(callcmd)
161
162 # API: stop
163 def call_stop(cmdline):
164     cmdstr = "stop "
165     for str in cmdline:
166         cmdstr = cmdstr + " " + str
167     print cmdstr
168     return cmdstr
169
170 class api_stop(api_common):
171     def __init__(self, callcmd):
172         super(api_stop, self).__init__(callcmd)
173
174 # API: list
175 def call_list(cmdline):
176     cmdstr = "list"
177     for str in cmdline:
178         cmdstr = cmdstr + " " + str
179     print cmdstr
180     return cmdstr
181
182 class api_list(api_common):
183     def __init__(self, callcmd):
184         super(api_list, self).__init__(callcmd)
185
186 # API: sysgen
187 def call_sysgen(cmdline):
188     cmdstr = "sysgen"
189     for str in cmdline:
190         cmdstr = cmdstr + " " + str
191     print cmdstr
192     return cmdstr
193
194 class api_sysgen(api_common):
195     def __init__(self, callcmd):
196         super(api_sysgen, self).__init__(callcmd)
197
198 # API: erase
199 def call_erase(cmdline):
200     cmdstr = "erase"
201     for str in cmdline:
202         cmdstr = cmdstr + " " + str
203     print cmdstr
204     return cmdstr
205
206 class api_erase(api_common):
207     def __init__(self, callcmd):
208         super(api_erase, self).__init__(callcmd)
209
210 # API: show
211 def call_show(cmdline):
212     cmdstr = "show"
213     for str in cmdline:
214         cmdstr = cmdstr + " " + str
215     print cmdstr
216     return cmdstr
217
218 class api_show(api_common):
219     def __init__(self, callcmd):
220         super(api_show, self).__init__(callcmd)
221
222