|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Mongo; |
| 5 | +class Orchestration { |
| 6 | + protected $baseuri; |
| 7 | + protected $conf; |
| 8 | + |
| 9 | + function __construct($baseuri) { |
| 10 | + $this->baseuri = $baseuri; |
| 11 | + $this->conf = array( |
| 12 | + "timeout" => 35, |
| 13 | + ); |
| 14 | + } |
| 15 | + |
| 16 | + function ping() { |
| 17 | + try { |
| 18 | + $data = $this->get(""); |
| 19 | + } catch(\Exception $e) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + function stopAll() { |
| 25 | + $servers = $this->get("servers"); |
| 26 | + foreach($servers["servers"] as $server) { |
| 27 | + $this->stopServer($server["id"]); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + function hasStandalone() { |
| 32 | + try { |
| 33 | + $data = $this->get("servers/STANDALONE"); |
| 34 | + } catch(\Exception $e) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + return $data["mongodb_uri"]; |
| 38 | + } |
| 39 | + |
| 40 | + function startStandalone() { |
| 41 | + $retval = $this->post("servers", ["name" => "mongod", "id" => "STANDALONE"]); |
| 42 | + if ($retval["procInfo"]["alive"]) { |
| 43 | + return $retval["mongodb_uri"]; |
| 44 | + } |
| 45 | + |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + function stopStandalone() { |
| 50 | + return $this->stopServer("STANDALONE"); |
| 51 | + } |
| 52 | + |
| 53 | + function getServerInfo($id) { |
| 54 | + $data = $this->get("servers/" . $id); |
| 55 | + return $data; |
| 56 | + } |
| 57 | + |
| 58 | + function stopServer($id) { |
| 59 | + try { |
| 60 | + $retval = $this->delete("servers/$id"); |
| 61 | + return true; |
| 62 | + } catch(\Exception $e) { |
| 63 | + if ($e->getCode() == 204) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + function getTimeout() { |
| 71 | + return $this->conf["timeout"]; |
| 72 | + } |
| 73 | + |
| 74 | + function delete($target) { |
| 75 | + $opts = [ |
| 76 | + "http" => [ |
| 77 | + "timeout" => $this->getTimeout(), |
| 78 | + "method" => "DELETE", |
| 79 | + "header" => "Accept: application/json\r\n" . |
| 80 | + "Content-type: application/x-www-form-urlencoded", |
| 81 | + "ignore_errors" => true, |
| 82 | + ], |
| 83 | + ]; |
| 84 | + |
| 85 | + $data = $this->_sendAndReceive($target, $opts); |
| 86 | + return $data; |
| 87 | + } |
| 88 | + |
| 89 | + function get($target) { |
| 90 | + $opts = [ |
| 91 | + "http" => [ |
| 92 | + "timeout" => $this->getTimeout(), |
| 93 | + "method" => "GET", |
| 94 | + "header" => "Accept: application/json\r\n" . |
| 95 | + "Content-type: application/x-www-form-urlencoded", |
| 96 | + "ignore_errors" => true, |
| 97 | + ], |
| 98 | + ]; |
| 99 | + |
| 100 | + $data = $this->_sendAndReceive($target, $opts); |
| 101 | + return $data; |
| 102 | + } |
| 103 | + |
| 104 | + function post($target, $array) { |
| 105 | + $postdata = json_encode($array); |
| 106 | + |
| 107 | + $opts = [ |
| 108 | + "http" => [ |
| 109 | + "timeout" => $this->getTimeout(), |
| 110 | + "method" => "POST", |
| 111 | + "header" => "Accept: application/json\r\n" . |
| 112 | + "Content-type: application/x-www-form-urlencoded", |
| 113 | + "content" => $postdata, |
| 114 | + "ignore_errors" => true, |
| 115 | + ], |
| 116 | + ]; |
| 117 | + |
| 118 | + return $this->_sendAndReceive($target, $opts); |
| 119 | + } |
| 120 | + |
| 121 | + protected function _sendAndReceive($target, $opts) { |
| 122 | + $context = stream_context_create($opts); |
| 123 | + $url = $this->baseuri . "/" . $target; |
| 124 | + |
| 125 | + $http = $opts["http"]; |
| 126 | + |
| 127 | + $hdr = ""; |
| 128 | + foreach(explode("\r\n", $http["header"]) as $header) { |
| 129 | + $hdr .= "--header='{$header}' "; |
| 130 | + } |
| 131 | + |
| 132 | + $debug = sprintf("wget --body-data='%s' --method='%s' %s %s\n", |
| 133 | + isset($http["content"]) ? $http["content"] : "", |
| 134 | + $http["method"], |
| 135 | + $hdr, |
| 136 | + $url |
| 137 | + ); |
| 138 | + if (defined("DUMP_WGET") && DUMP_WGET) { |
| 139 | + echo $debug; |
| 140 | + } |
| 141 | + |
| 142 | + $data = @file_get_contents($url, false, $context); |
| 143 | + if ($data) { |
| 144 | + return json_decode($data, true); |
| 145 | + } |
| 146 | + if (!empty($http_response_header)) { |
| 147 | + sscanf($http_response_header[0], "HTTP/%f %i %s", $proto, $code, $desc); |
| 148 | + throw new \RuntimeException(json_encode($http_response_header), $code); |
| 149 | + } |
| 150 | + |
| 151 | + throw new \RuntimeException(error_get_last()["message"]); |
| 152 | + |
| 153 | + } |
| 154 | +} |
| 155 | + |
0 commit comments