Skip to content

Commit 4f69303

Browse files
committed
PHPC-180: Replace this Orchestration wrapper with significantly simpler code
We no create a /tmp/PHONGO-SERVERS.json file upon launching the servers which stores the server IDs and connection string (including auth). This makes it much faster to look up the available servers
1 parent aa620d7 commit 4f69303

File tree

8 files changed

+131
-326
lines changed

8 files changed

+131
-326
lines changed

scripts/list-servers.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
<?php
2-
require __DIR__ . "/" . "../tests/utils/orchestration.php";
2+
$FILENAME = sys_get_temp_dir() . "/PHONGO-SERVERS.json";
33

4-
if (!($host = getenv("MONGODB_ORCHESTRATION"))) {
5-
$host = "http://192.168.112.10:8889";
6-
}
7-
8-
$orch = new Mongo\Orchestration($host, getenv("MONGODB_ORCHESTRATION_PRESETS_ROOT"));
9-
if (!$orch->ping()) {
10-
echo "Failed connecting to MO\n";
11-
exit(3);
12-
}
13-
14-
foreach($orch->getAll() as $uri => $server) {
15-
printf("%s:%s%s\n", $server["id"], str_repeat(" ", 40-strlen($server["id"])), $server["mongodb_uri"]);
4+
$json = file_get_contents($FILENAME);
5+
$servers = json_decode($json);
6+
foreach($servers as $serverid => $uri) {
7+
printf("%-20s \t %s\n", $serverid, $uri);
168
}
179

1810

scripts/start-servers.php

Lines changed: 117 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
require __DIR__ . "/" . "../tests/utils/orchestration.php";
2+
$SERVERS = array();
3+
$FILENAME = sys_get_temp_dir() . "/PHONGO-SERVERS.json";
34

45
function lap() {
56
static $then = 0;
@@ -11,47 +12,132 @@ function lap() {
1112
return $ret;
1213
}
1314

14-
if (!($host = getenv("MONGODB_ORCHESTRATION"))) {
15-
$host = "http://192.168.112.10:8889";
15+
16+
if (!($HOST = getenv("MONGODB_ORCHESTRATION_HOST"))) {
17+
$HOST = "192.168.112.10";
18+
}
19+
20+
if (!($PORT = getenv("MONGODB_ORCHESTRATION_PORT"))) {
21+
$PORT = "8889";
22+
}
23+
24+
if (!($BASE = getenv("mongodb_orchestration_base"))) {
25+
$BASE = "/phongo/";
26+
}
27+
28+
$MO = "http://$HOST:$PORT";
29+
30+
$PRESETS = [
31+
"standalone" => [
32+
"scripts/presets/standalone.json",
33+
"scripts/presets/standalone-ssl.json",
34+
"scripts/presets/standalone-auth.json",
35+
"scripts/presets/standalone-x509.json",
36+
"scripts/presets/standalone-plain.json",
37+
],
38+
"replicasets" => [
39+
"scripts/presets/replicaset.json",
40+
],
41+
];
42+
43+
function make_ctx($preset, $method = "POST") {
44+
$opts = [
45+
"http" => [
46+
"timeout" => 30,
47+
"method" => $method,
48+
"header" => "Accept: application/json\r\n" .
49+
"Content-type: application/x-www-form-urlencoded",
50+
"content" => json_encode(array("preset" => $preset)),
51+
"ignore_errors" => true,
52+
],
53+
];
54+
$ctx = stream_context_create($opts);
55+
return $ctx;
1656
}
1757

18-
$orch = new Mongo\Orchestration($host, getenv("MONGODB_ORCHESTRATION_PRESETS_ROOT"));
19-
if (!$orch->ping()) {
20-
var_dump($host);
21-
system("pwd");
22-
system("wget -O - $host");
23-
echo file_get_contents("server.log");
24-
echo "Failed starting MO\n";
25-
exit(3);
58+
function failed($result) {
59+
echo "\n\n";
60+
echo join("\n", $result);
61+
exit();
2662
}
2763

64+
65+
66+
printf("Cleaning out previous processes, if any ");
2867
lap();
29-
$orch->stopAll();
68+
/* Remove all pre-existing ReplicaSets */
69+
$replicasets = file_get_contents($MO . "/replica_sets", false, make_ctx($BASE, "GET"));
70+
$replicasets = json_decode($replicasets, true);
71+
foreach($replicasets["replica_sets"] as $replicaset) {
72+
$uri = $MO . "/replica_sets/" . $replicaset["id"];
73+
file_get_contents($uri, false, make_ctx($BASE, "DELETE"));
74+
echo ".";
75+
}
76+
echo " ";
77+
/* Remove all pre-existing servers */
78+
$servers = file_get_contents($MO . "/servers", false, make_ctx($BASE, "GET"));
79+
$servers = json_decode($servers, true);
80+
foreach($servers["servers"] as $server) {
81+
$uri = $MO . "/servers/" . $server["id"];
82+
file_get_contents($uri, false, make_ctx($BASE, "DELETE"));
83+
echo ".";
84+
}
85+
printf("\t(took: %.2f secs)\n", lap());
86+
87+
foreach($PRESETS["standalone"] as $preset) {
88+
lap();
89+
$json = json_decode(file_get_contents($preset), true);
90+
printf("Starting %-20s ... ", $json["id"]);
3091

31-
$res = $orch->start("standalone.json");
32-
if ($res) {
33-
printf("Standalone running on:\t\t\t(took: %.2f secs)\t%s\n", lap(), $res);
34-
} else {
35-
printf("Failed starting standalone.json after %.2f secs\n", lap());
36-
exit(1);
92+
$result = file_get_contents($MO . "/servers", false, make_ctx($BASE . $preset));
93+
$decode = json_decode($result, true);
94+
if (!isset($decode["id"])) {
95+
failed($decode);
96+
}
97+
98+
$SERVERS[$decode["id"]] = isset($decode["mongodb_auth_uri"]) ? $decode["mongodb_auth_uri"] : $decode["mongodb_uri"];
99+
printf("'%s'\t(took: %.2f secs)\n", $SERVERS[$decode["id"]], lap());
37100
}
101+
echo "---\n";
102+
103+
foreach($PRESETS["replicasets"] as $preset) {
104+
lap();
105+
$json = json_decode(file_get_contents($preset), true);
106+
printf("Starting %-20s ... ", $json["id"]);
38107

39-
if (getenv("TRAVIS")) {
40-
echo "Skipping special nodes on travis\n";
41-
exit;
108+
$result = file_get_contents($MO . "/replica_sets", false, make_ctx($BASE . $preset));
109+
$decode = json_decode($result, true);
110+
if (!isset($decode["id"])) {
111+
failed($decode);
112+
}
113+
$SERVERS[$decode["id"]] = isset($decode["mongodb_auth_uri"]) ? $decode["mongodb_auth_uri"] : $decode["mongodb_uri"];
114+
printf("'%s'\t(took: %.2f secs)\n", $SERVERS[$decode["id"]], lap());
42115
}
43-
$res = $orch->start("standalone-ssl.json");
44-
printf("Standalone SSL running on:\t\t(took: %.2f secs)\t%s\n", lap(), $res);
45116

46-
$res = $orch->start("standalone-auth.json");
47-
printf("Standalone Auth running on:\t\t(took: %.2f secs)\t%s\n", lap(), $res);
117+
file_put_contents($FILENAME, json_encode($SERVERS, JSON_PRETTY_PRINT));
48118

49-
$res = $orch->start("standalone-x509.json");
50-
printf("Standalone X509 Auth running on:\t(took: %.2f secs)\t%s\n", lap(), $res);
119+
/*
120+
wget --body-data='' --method='GET' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers
121+
wget --body-data='' --method='DELETE' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers/STANDALONE-AUTH
122+
wget --body-data='' --method='DELETE' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers/STANDALONE
123+
wget --body-data='' --method='DELETE' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers/RS-two
124+
wget --body-data='' --method='DELETE' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers/RS-arbiter
125+
wget --body-data='' --method='DELETE' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers/STANDALONE-PLAIN
126+
wget --body-data='' --method='DELETE' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers/STANDALONE-X509
127+
wget --body-data='' --method='DELETE' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers/RS-one
128+
wget --body-data='' --method='DELETE' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers/STANDALONE-SSL
51129
52-
$res = $orch->start("standalone-plain.json");
53-
printf("Standalone PLAIN Auth running on:\t(took: %.2f secs)\t%s\n", lap(), $res);
130+
wget --body-data='' --method='GET' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/replica_sets
131+
wget --body-data='' --method='DELETE' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/replica_sets/REPLICASET
54132
55-
$res = $orch->startRS("replicaset.json");
56-
printf("ReplicaSet running on:\t\t\t(took: %.2f secs)\t%s\n", lap(), $res);
133+
wget --body-data='' --method='GET' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/
134+
wget --body-data='' --method='GET' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers
135+
wget --body-data='' --method='GET' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/replica_sets
57136
137+
wget --body-data='{"preset":"\/phongo\/\/scripts\/presets\/standalone.json"}' --method='POST' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers
138+
wget --body-data='{"preset":"\/phongo\/\/scripts\/presets\/standalone-ssl.json"}' --method='POST' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers
139+
wget --body-data='{"preset":"\/phongo\/\/scripts\/presets\/standalone-auth.json"}' --method='POST' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers
140+
wget --body-data='{"preset":"\/phongo\/\/scripts\/presets\/standalone-x509.json"}' --method='POST' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers
141+
wget --body-data='{"preset":"\/phongo\/\/scripts\/presets\/standalone-plain.json"}' --method='POST' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/servers
142+
wget --body-data='{"preset":"\/phongo\/\/scripts\/presets\/replicaset.json"}' --method='POST' --header='Accept: application/json' --header='Content-type: application/x-www-form-urlencoded' http://192.168.112.10:8889/replica_sets
143+
*/

tests/utils/auth-plain-skipif.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

33
require __DIR__ . "/" . "basic-skipif.inc";
4-
$parsed = parse_url(MONGODB_STANDALONE_PLAIN_URI);
4+
$parsed = parse_url(STANDALONE_PLAIN);
55

66
if (!isset($parsed["host"])) {
7-
exit("skip cannot parse uri: '" . MONGODB_STANDALONE_PLAIN_URI. "' maybe PLAIN server not available?");
7+
exit("skip cannot parse uri: '" . STANDALONE_PLAIN. "' maybe PLAIN server not available?");
88
}
99

tests/utils/auth-x509-skipif.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

33
require __DIR__ . "/" . "basic-skipif.inc";
4-
$parsed = parse_url(MONGODB_STANDALONE_X509_URI);
4+
$parsed = parse_url(STANDALONE_X509);
55

66
if (!isset($parsed["host"])) {
7-
exit("skip cannot parse uri: '" . MONGODB_STANDALONE_X509_URI. "' maybe x509 server not available?");
7+
exit("skip cannot parse uri: '" . STANDALONE_X509 . "' maybe x509 server not available?");
88
}
99

tests/utils/basic-skipif.inc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,5 @@ if ( ! extension_loaded("phongo")) {
55
}
66

77
require __DIR__ . "/" . "basic.inc";
8-
$testdir = basename(dirname(realpath($_SERVER["SCRIPT_FILENAME"])));
98

10-
switch($testdir){
11-
case "replicaset":
12-
case "connect":
13-
if (!$orch->ping() || getenv("TRAVIS") || !MONGODB_STANDALONE_AUTH_URI) {
14-
exit("skip these tests in $testdir require Mongo Orchestration + vagrant");
15-
}
16-
break;
17-
}
189

19-
CLEANUP();

tests/utils/basic.inc

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,12 @@
11
<?php
2-
require __DIR__ . "/" . "orchestration.php";
32
require __DIR__ . "/" . "tools.php";
43

54

6-
if (!($host = getenv("MONGODB_ORCHESTRATION"))) {
7-
$host = "http://192.168.112.10:8889";
8-
}
9-
10-
$orch = new Mongo\Orchestration($host, getenv("MONGODB_ORCHESTRATION_PRESETS_ROOT"));
11-
if ($orch->ping()) {
12-
$consts = array(
13-
"MONGODB_URI" => $orch->getURI("standalone.json"),
14-
"MONGODB_CLEANUP_URI" => $orch->getURI("standalone.json"),
15-
"MONGODB_STANDALONE_URI" => $orch->getURI("standalone.json"),
16-
"MONGODB_STANDALONE_AUTH_URI" => $orch->getURI("standalone-auth.json"),
17-
"MONGODB_STANDALONE_SSL_URI" => $orch->getURI("standalone-ssl.json"),
18-
"MONGODB_STANDALONE_PLAIN_URI" => $orch->getURI("standalone-plain.json"),
19-
"MONGODB_STANDALONE_X509_URI" => $orch->getURI("standalone-x509.json"),
20-
"MONGODB_REPLICASET_URI" => $orch->getURI("replicaset.json"),
21-
);
22-
} else {
23-
$consts = array(
24-
"MONGODB_URI" => getenv("MONGODB_TEST_URI"),
25-
"MONGODB_CLEANUP_URI" => getenv("MONGODB_TEST_URI"),
26-
"MONGODB_STANDALONE_URI" => getenv("MONGODB_STANDALONE_URI"),
27-
"MONGODB_STANDALONE_AUTH_URI" => getenv("MONGODB_STANDALONE_AUTH_URI"),
28-
"MONGODB_STANDALONE_SSL_URI" => getenv("MONGODB_STANDALONE_SSL_URI"),
29-
"MONGODB_STANDALONE_PLAIN_URI" => getenv("MONGODB_STANDALONE_PLAIN_URI"),
30-
"MONGODB_STANDALONE_X509_URI" => getenv("MONGODB_STANDALONE_X509_URI"),
31-
"MONGODB_REPLICASET_URI" => getenv("MONGODB_REPLICASET_URI"),
32-
);
33-
34-
}
35-
def($consts);
36-
5+
$FILENAME = sys_get_temp_dir() . "/PHONGO-SERVERS.json";
376

7+
$json = file_get_contents($FILENAME);
8+
$servers = json_decode($json);
9+
def($servers);
3810

3911
$consts = array(
4012
"DATABASE_NAME" => "phongo",

tests/utils/fixtures-users.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require_once __DIR__ . '/../../vendor/autoload.php';
1010
$faker = Faker\Factory::create();
1111
$faker->seed(1234);
1212

13-
$manager = new MongoDB\Driver\Manager(MONGODB_URI);
13+
$manager = new MongoDB\Driver\Manager(STANDALONE);
1414
$bulk = new MongoDB\Driver\BulkWrite;
1515

1616
function createUser($faker)

0 commit comments

Comments
 (0)