5 # Copyright (C) 2014 FUJITSU LIMITED
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
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.
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
23 # A scripts for creating LXC domain in which some daemons are running.
24 # running systemd, rsyslog, sshd in a container.
27 import os, datetime, cgi, StringIO, commands
28 from wsgiref import util, simple_server
29 from xml.sax import saxutils
32 def sanity_check(parm):
34 if c.isdigit() or c.isalpha() or (c == '-') or (c == '_') or (c == ' '):
40 # Conversion of character string
57 class api_common(object):
58 def __init__(self, callcmd):
59 self.messages = "no operation"
61 self.callcmd = callcmd
63 def __call__(self, environ, start_response):
64 method = environ['REQUEST_METHOD']
66 return self.get_response(environ, start_response)
67 elif method == 'POST':
68 return self.post_response(environ, start_response)
70 start_response('501 Not Implemented', [('Content-type', 'text/plain')])
71 return 'Not Implemented'
74 def get_response(self, environ, start_response):
75 fp = StringIO.StringIO()
78 <head><title>LXCF API</title>
79 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
84 fp.write("status="+str(self.status)+"\n")
85 fp.write(conv_char(self.messages)+"\n")
87 self.messages = "no operation"
89 fp.write('''</pre>\n<HR><form action="%s" method="POST" AcceptEncoding="utf-8">
92 <dd><input type="password" name="password" value=""/></dd>
94 <input type="submit" name="save" value="Execution" />
95 </form></body></html>''' % util.request_uri(environ))
99 start_response('200 OK', [('Content-type', 'text/html; charset=utf-8')])
103 def post_response(self, environ, start_response):
105 self.messages = "no operation"
106 inpt = environ['wsgi.input']
107 length = int(environ.get('CONTENT_LENGTH', 0))
109 query = dict(cgi.parse_qsl(inpt.read(length)))
112 password = query['password']
116 path_info = environ['PATH_INFO']
118 cmdline=path_info.split("/")
119 cmd = self.callcmd(cmdline)
121 print ("authenticity : "+password+" , "+path_info)
123 if (sanity_check(cmd)):
125 check = commands.getstatusoutput("/usr/sbin/lxcf "+cmd)
127 check = "error: sanity error ... "+cmd
129 self.status = check[0]
130 self.messages = check[1]
132 fp = StringIO.StringIO()
135 <head><title>LXCF API</title>
136 <meta http-equiv="Content-type" content="text/html; charset=utf-8">
139 <form action="%s" method="POST" AcceptEncoding="utf-8">
140 </body></html>''' % util.request_uri(environ))
144 start_response('303 See Other', [('Content-type', 'text/plain'),
145 ('Location', util.request_uri(environ))])
151 def call_start(cmdline):
154 cmdstr = cmdstr + " " + str
158 class api_start(api_common):
159 def __init__(self, callcmd):
160 super(api_start, self).__init__(callcmd)
163 def call_stop(cmdline):
166 cmdstr = cmdstr + " " + str
170 class api_stop(api_common):
171 def __init__(self, callcmd):
172 super(api_stop, self).__init__(callcmd)
175 def call_list(cmdline):
178 cmdstr = cmdstr + " " + str
182 class api_list(api_common):
183 def __init__(self, callcmd):
184 super(api_list, self).__init__(callcmd)
187 def call_sysgen(cmdline):
190 cmdstr = cmdstr + " " + str
194 class api_sysgen(api_common):
195 def __init__(self, callcmd):
196 super(api_sysgen, self).__init__(callcmd)
199 def call_erase(cmdline):
202 cmdstr = cmdstr + " " + str
206 class api_erase(api_common):
207 def __init__(self, callcmd):
208 super(api_erase, self).__init__(callcmd)
211 def call_show(cmdline):
214 cmdstr = cmdstr + " " + str
218 class api_show(api_common):
219 def __init__(self, callcmd):
220 super(api_show, self).__init__(callcmd)