This program is supposed to trigger a script when a fail over from an LB hits it.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123 lignes
4.2KB

  1. import arsd.cgi;
  2. //import arsd.sqlite;
  3. import std.file;
  4. import std.conv;
  5. import std.process;
  6. string host_port = ":9000";
  7. string allowed_host = "";
  8. string fileToString(string path){
  9. auto file = File(path, "r");
  10. string contents = "";
  11. while (!file.eof()) {
  12. contents ~= file.readln();
  13. }
  14. file.close();
  15. return contents;
  16. }
  17. string get_header(){
  18. return fileToString("public/header.html");
  19. }
  20. string get_footer(){
  21. return fileToString("public/footer.html");
  22. }
  23. bool run_script(string script_path){
  24. try{
  25. string script_command = "chmod +x " ~ script_path ~ " && bash -c "~ script_path;
  26. writeln("calling::: " ~ script_command);
  27. auto cont = executeShell(script_command);
  28. if(cont.status != 0) {
  29. writeln("failed to run script command");
  30. //return "Failed";
  31. return false;
  32. } else {
  33. writeln("script output " ~ cont.output);
  34. return true;
  35. }
  36. } catch (Exception e) {
  37. return false;
  38. }
  39. }
  40. void handler(Cgi cgi) {
  41. try{
  42. string host = cgi.host.replace(host_port, "");
  43. string path = cgi.pathInfo;
  44. if(path == "/favicon.ico"){
  45. return;
  46. }
  47. if(cgi.remoteAddress ~= allowed_host.split(":"[0])){ // ignore the port.
  48. cgi.write("<html> Connections are not allowed from: " ~ cgi.remoteAddress ~ "</html>");
  49. return;
  50. }
  51. writeln("Trying to access: " ~host ~path);
  52. string script_path = getcwd() ~"/" ~ host ~ "/" ~ "run.sh";
  53. writeln("script_path: " ~ script_path);
  54. if(script_path.exists){
  55. writeln("script_path exists: Trying to access: " ~host);
  56. //Not needed currently. auto fail_script = fileToString(script_path);
  57. writeln("Running script: \r\n" ~ script_path);
  58. auto result = run_script(script_path);
  59. if(result){
  60. cgi.write("<html> successfully ran script.</html>");
  61. } else {
  62. cgi.write("<html> Failed to run script.</html>");
  63. }
  64. writeln("done with script");
  65. } else {
  66. writeln("site path doesn't exist.");
  67. cgi.write("<html> Hello " ~ cgi.remoteAddress ~ " The host: " ~ host ~ " is not configured.</html>");
  68. }
  69. } catch (Exception e){
  70. writeln("ʕノ•ᴥ•ʔノ ︵ ┻━┻ Exception thrown: "~ e.toString());
  71. //throw o;
  72. cgi.setResponseStatus("500 Internal Server Error");
  73. cgi.write("ʕノ•ᴥ•ʔノ ︵ ┻━┻ 500 Internal Server Error.", true);
  74. }
  75. }
  76. //mixin GenericMain!handler;
  77. void main(string[] args) {
  78. //TODO: Fix this to load the acceptable host from an argument, or config file.
  79. allowed_host = "127.0.0.1";
  80. //TODO: Figure out a better way of setting defaults, this is pretty dumb.
  81. string[] newargs;
  82. writeln(to!string(args));
  83. args = [];
  84. writeln(to!string(args));
  85. newargs ~= "./main";
  86. newargs ~= "--listen";
  87. newargs ~= "http://localhost" ~ host_port;
  88. args = newargs;
  89. writeln(to!string(args));
  90. // this is all the content of [cgiMainImpl] which you can also call
  91. // cgi.d embeds a few add on functions like real time event forwarders
  92. // and session servers it can run in other processes. this spawns them, if needed.
  93. if(tryAddonServers(args))
  94. return;
  95. // cgi.d allows you to easily simulate http requests from the command line,
  96. // without actually starting a server. this function will do that.
  97. if(trySimulatedRequest!(handler, Cgi)(args))
  98. return;
  99. RequestServer server;
  100. // you can change the default port here if you like
  101. // server.listeningPort = 9000;
  102. // then call this to let the command line args override your default
  103. server.configureFromCommandLine(args);
  104. // here is where you could print out the listeningPort to the user if you wanted
  105. // and serve the request(s) according to the compile configuration
  106. server.serve!(handler)();
  107. // or you could explicitly choose a serve mode like this:
  108. // server.serveEmbeddedHttp!requestHandler();
  109. }