<s> """<STR_LIT>""" <EOL> import json <EOL> import socket <EOL> import yaml <EOL> import time <EOL> from flask import Flask <EOL> from flask import request <EOL> import simoorg . Api . ApiConstants as ApiConstants <EOL> import uuid <EOL> import os <EOL> import signal <EOL> class MoiraiApiServer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config_dir ) : <EOL> self . app = Flask ( __name__ ) <EOL> self . service_map = { } <EOL> self . event_map = { } <EOL> signal . signal ( signal . SIGTERM , self . term_handler ) <EOL> signal . signal ( signal . SIGQUIT , self . term_handler ) <EOL> signal . signal ( signal . SIGINT , self . term_handler ) <EOL> with open ( config_dir ) as config_fd : <EOL> self . api_config = yaml . load ( config_fd ) <EOL> self . api_fifo_name = str ( uuid . uuid4 ( ) ) + '<STR_LIT>' <EOL> self . api_fifo_path = os . path . join ( ApiConstants . API_PIPE_DIR , <EOL> self . api_fifo_name ) <EOL> os . mkfifo ( self . api_fifo_path ) <EOL> try : <EOL> self . api_fifo_fd = os . open ( self . api_fifo_path , os . O_NONBLOCK ) <EOL> self . api_fifo_file = os . fdopen ( self . api_fifo_fd ) <EOL> except ( IOError , OSError ) as exc : <EOL> print ( "<STR_LIT>" <EOL> . format ( exc ) ) <EOL> raise <EOL> if not os . path . exists ( self . api_config [ '<STR_LIT>' ] ) : <EOL> os . mkfifo ( self . api_config [ '<STR_LIT>' ] ) <EOL> try : <EOL> self . moirai_fifo_fd = os . open ( self . api_config [ '<STR_LIT>' ] , <EOL> os . O_WRONLY | os . O_NONBLOCK ) <EOL> self . moirai_fifo_file = os . fdopen ( self . moirai_fifo_fd , '<STR_LIT:w>' ) <EOL> except ( IOError , OSError ) as exc : <EOL> print "<STR_LIT>" <EOL> self . moirai_fifo_fd = None <EOL> self . setup_routes ( ) <EOL> self . command_id = <NUM_LIT:0> <EOL> def term_handler ( self , recvd_signal , frame ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> try : <EOL> self . api_fifo_file . close ( ) <EOL> except Exception , exc : <EOL> print "<STR_LIT>" . format ( exc ) <EOL> try : <EOL> self . moirai_fifo_file . close ( ) <EOL> except Exception , exc : <EOL> print "<STR_LIT>" . format ( exc ) <EOL> try : <EOL> os . close ( self . api_fifo_fd ) <EOL> except Exception , exc : <EOL> print "<STR_LIT>" . format ( exc ) <EOL> try : <EOL> os . close ( self . moirai_fifo_fd ) <EOL> except Exception , exc : <EOL> print ( "<STR_LIT>" <EOL> . format ( exc ) ) <EOL> exit ( <NUM_LIT:0> ) <EOL> def app_run ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . app . run ( ) <EOL> except socket . error , msg : <EOL> print "<STR_LIT>" . format ( msg [ <NUM_LIT:1> ] ) <EOL> print "<STR_LIT>" <EOL> def fetch_app ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . app <EOL> def setup_routes ( self ) : <EOL> """<STR_LIT>""" <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> def list_services ( command ) : <EOL> """<STR_LIT>""" <EOL> args = { } <EOL> op_msg = self . execute_command ( command , args , request . method ) <EOL> return op_msg <EOL> @ self . app . route ( '<STR_LIT>' ) <EOL> def get_plan ( service_name , command ) : <EOL> """<STR_LIT>""" <EOL> args = { '<STR_LIT>' : service_name } <EOL> op_msg = self . execute_command ( command , args , request . method ) <EOL> return op_msg <EOL> def execute_command ( self , command , args , method ) : <EOL> """<STR_LIT>""" <EOL> if self . moirai_fifo_fd is None : <EOL> try : <EOL> self . moirai_fifo_fd = os . open ( <EOL> self . api_config [ '<STR_LIT>' ] , <EOL> os . O_WRONLY | os . O_NONBLOCK ) <EOL> self . moirai_fifo_file = os . fdopen ( self . moirai_fifo_fd , '<STR_LIT:w>' ) <EOL> except : <EOL> self . moirai_fifo_fd = None <EOL> return "<STR_LIT>" <EOL> self . command_id = self . command_id + <NUM_LIT:1> <EOL> command_map = { ApiConstants . FIFO_ENDPOINT_KEY : self . api_fifo_path , <EOL> ApiConstants . COMMAND_ID_KEY : self . command_id , <EOL> ApiConstants . COMMAND_KEY : command , <EOL> ApiConstants . ARGS_KEY : args , <EOL> ApiConstants . METHOD_KEY : method } <EOL> command_string = json . dumps ( command_map ) + '<STR_LIT:\n>' <EOL> try : <EOL> self . moirai_fifo_file . write ( command_string ) <EOL> self . moirai_fifo_file . flush ( ) <EOL> except IOError : <EOL> op_msg = ( "<STR_LIT>" ) <EOL> print "<STR_LIT>" + op_msg <EOL> return op_msg <EOL> except OSError : <EOL> self . moirai_fifo_fd = None <EOL> op_msg = ( "<STR_LIT>" ) <EOL> print "<STR_LIT>" + op_msg <EOL> return op_msg <EOL> start_time = time . time ( ) <EOL> output_string = '<STR_LIT>' <EOL> while output_string == '<STR_LIT>' : <EOL> try : <EOL> output_string = self . api_fifo_file . read ( ) <EOL> except IOError : <EOL> pass <EOL> current_time = time . time ( ) <EOL> if current_time - start_time > ApiConstants . READ_TIMEOUT_SECS : <EOL> op_msg = ( "<STR_LIT>" ) <EOL> print "<STR_LIT>" + op_msg <EOL> return op_msg <EOL> try : <EOL> output_obj = json . loads ( output_string ) <EOL> except ValueError : <EOL> op_msg = "<STR_LIT>" <EOL> print "<STR_LIT>" + op_msg <EOL> return op_msg <EOL> if output_obj [ ApiConstants . COMMAND_ID_KEY ] != self . command_id : <EOL> op_msg = ( "<STR_LIT>" ) <EOL> print "<STR_LIT>" + op_msg <EOL> return op_msg <EOL> return output_obj [ ApiConstants . COMMAND_OUTPUT_KEY ] <EOL> def create_app ( config_path ) : <EOL> """<STR_LIT>""" <EOL> api = MoiraiApiServer ( config_path ) <EOL> application = api . fetch_app ( ) <EOL> return application </s>
