def _poweroff(self): """ Log on to remote node and shut it down. :rtype: str :returns: Status message """ host = request.args.get('host') if host is None: message = "Error 'poweroff' must have parameter 'host'!" return self._json_response(message, 400) if host in self.API_POWEROFF_HOSTS.keys(): user = self.API_POWEROFF_HOSTS[host]['user'] else: message = "Error host '{}' has not been configured for 'poweroff'!" message = message.format(host) return self._json_response(message, 400) # Power off node command = ("{}ssh -o 'StrictHostKeyChecking no' -i {} -T {}@{} 'shutdo" "wn -h now'") ssh_key_file = self.SSH_KEY_FILE.replace('$HOST', host) command = command.format(self.SSH_PATH, ssh_key_file, user, host) Debug.debug_print(1, 'Shut down command: {}'.format(command)) p = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, stderr = p.communicate() message = codecs.decode(stdout + stderr, 'utf-8') return self._json_response(message, 200)
def populate_ssh_key_files(): """ Read all ssh key file contents from settings and write ssh key file(s) to disk for SSH to use. """ for host, values in Settings.API_POWEROFF_HOSTS.items(): file_name = Settings.SSH_KEY_FILE.replace('$HOST', host) file_obj = open(file_name, 'w') file_obj.writelines(values['ssh_key']) file_obj.close() command = "chmod 0600 {}".format(file_name) p = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) p.communicate() message = "The following ssh key files have been written to disk:\n{}" Debug.debug_print( 2, message.format(str(Settings.API_POWEROFF_HOSTS.keys())))
def handle_request(self): """Web server function.""" host = request.args.get('host') mac = request.args.get('mac') tab = request.args.get('tab') request_json = request.get_json() Debug.debug_print(3, "request_json: " + pprint.pformat(request_json)) try: message = request.path if request.path == self.WEB_GUI_PATH: settings = self.render_settings_html() active_tab = 'about' if tab is not None: active_tab = tab result = render_template( 'index.html', settings=settings, active_tab=active_tab, restart_ping_path=Settings.WEB_API_PATH + 'restart_ping', poweron_path=Settings.WEB_API_PATH + 'poweron', poweroff_path=Settings.WEB_API_PATH + 'poweroff') elif request.path == self.WEB_API_PATH + 'poweron': message = 'poweron: mac={}' message = message.format(str(mac)) result = self._poweron() elif request.path == self.WEB_API_PATH + 'poweroff': message = 'poweroff: host={}' message = message.format(str(host)) result = self._poweroff() elif request.path == self.WEB_API_PATH + 'restart_ping': message = 'restart_ping' result = self._restart_ping() elif request.path == self.WEB_API_PATH + 'save_settings': message = 'save_settings' result = self._save_settings(request_json) elif request.path == self.WEB_API_PATH + 'delete_param_value': param = None value = None if 'param' in request_json: param = request_json['param'] if 'value' in request_json: value = request_json['value'] message = 'delete_param_value: param={}, value={}' message = message.format(param, value) result = self._delete_param_value(param, value) elif request.path == self.WEB_API_PATH + 'add_param_value': param = None if 'param' in request_json: param = request_json['param'] if 'value' in request_json: value = request_json['value'] message = 'add_param_value: param={}, value={}' message = message.format(param, value) result = self._add_param_value(param, value) except: error_log = LogFile(self.ERROR_LOG) error_log.write([message]) traceback_message = traceback.format_exc() error_log.write([traceback_message], date_time=False) if Debug.debug > 0: Debug.debug_print(1, traceback_message) return self._json_response(traceback_message, 500) return self._json_response('Internal system error!', 500) message_log = LogFile(self.MESSAGE_LOG) message_log.write([message]) return result