File: make-servers.php

package info (click to toggle)
php-mongo 1.4.5-1~bpo70%2B1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 9,828 kB
  • sloc: ansic: 13,275; xml: 1,702; php: 1,625; pascal: 255; makefile: 106; sh: 27
file content (217 lines) | stat: -rw-r--r-- 6,947 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/*
 * The only bootup specific type of servers set
 *      MONGO_SERVER_[SERVER_TYPE]=yes
 * in your environment before running this script.
 *
 * To bootup all exception a specific type of server set
 *      SKIP_MONGO_SERVER_[SERVER_TYPE]=yes
 */
if (!file_exists("tests/utils/cfg.inc")) {
    echo "Please copy tests/utils/cfg.inc.template to tests/utils/cfg.inc\n";
    exit(112);
}
require_once "tests/utils/server.inc";
include "tests/utils/cfg.inc";

function t() {
    static $last;
    if ($last) {
        $current = microtime(true);
        $retval = $current - $last;
        $last = $current;
        return $retval;
    }
    $last = microtime(true);
}
function makeServer($SERVERS, $server, $bit) {
    global $SHELL;

    echo "Making " . $SERVERS[$bit] . ".. ";
    t();
    switch($bit) {
    case STANDALONE:
        $server->makeStandalone(30000);
        $dsn = $server->getStandaloneConfig();
        break;
    case STANDALONE_AUTH:
        $server->makeStandalone(30100, true);
        $dsn = $server->getStandaloneConfig(true);
        break;
    case STANDALONE_BRIDGE:
        $sc = $server->getStandaloneConfig();
        list($shost, $sport) = explode(":", trim($sc));
        try {
            if (strncasecmp(php_uname(), 'win', 3) === 0) {
                // Note: where.exe requires Windows Server 2003 or newer (not XP)
                if (system('where mongobridge.exe', $status) === false || $status !== 0) {
                    throw new DebugException('Could not find mongobridge in $PATH');
                }
            } else {
                if (system('which mongobridge', $status) === false || $status !== 0) {
                    throw new DebugException('Could not find mongobridge in $PATH');
                }
            }

            $server->makeBridge($sport, 1000);
            $dsn = $server->getBridgeConfig();
        } catch(DebugException $e) {
            printf("(%s) %s - Still continuing though, you probably don't have 'mongobridge' installed\n", get_class($e), $e->getMessage());
            //printf("%s\n", $e->getMongoDLog());
            return 0;
        }
        break;
    case MONGOS:
        $rsmembers = array(
            /* Shard 0 */ array(
                array('tags' => array('server' => '0', 'dc' => 'ny')),
                array('tags' => array('server' => '1', 'dc' => 'ny')),
                array('tags' => array('server' => '2', 'dc' => 'sf'), "priority" => 0),
            ),
            /* Shard 1 */ array(
                array('tags' => array('server' => '0', 'dc' => 'ny')),
                array('tags' => array('server' => '1', 'dc' => 'ny', 'theOtherShard' => 'doesntHaveThisTag')),
                array('tags' => array('server' => '2', 'dc' => 'sf'), "priority" => 0),
            ),
        );
        $rssettings = array(
            array(
                "getLastErrorModes" => array(
                    "AnyDC" => array("dc" => 1),
                    "AllDC" => array("dc" => 2),
                    "ALL"   => array("server" => 3),
                ),
            ),
            array(
                "getLastErrorModes" => array(
                    "AnyDC" => array("dc" => 1),
                    "AllDC" => array("dc" => 2),
                    "ALL"   => array("server" => 3),
                    "Broken" => array("theOtherShard" => 1),
                ),
            )
        );
        $retval = $server->makeShard(2, $rsmembers, $rssettings);
        $cfg = $server->getShardConfig();
        $dsn = join(",", $cfg);
        break;
    case REPLICASET:
    case REPLICASET_AUTH:
        $members = array(
            array('tags' => array('server' => '0', 'dc' => 'ny'), "priority" => 42),
            array('tags' => array('server' => '1', 'dc' => 'ny')),
            array('tags' => array('server' => '2', 'dc' => 'sf')),
            array('tags' => array('server' => '3', 'dc' => 'sf'), "priority" => 0),
        );
        $settings = array(
            "getLastErrorModes" => array(
                "AnyDC" => array("dc" => 1),
                "AllDC" => array("dc" => 2),
                "ALL"   => array("server" => 4),
            ),
        );

        if ($bit == REPLICASET) {
            $server->makeReplicaset($members, 30200, $settings);
            $cfg = $server->getReplicaSetConfig();
        } else { /* REPLICASET_AUTH */
            /* Remove the passive server from auth replicasets.
             * We need to have one replicaset without a passive server, see 
             * PHP-829 */
            unset($members[3]);
            $settings["getLastErrorModes"]["ALL"]["server"]--;
            $retval = $server->makeReplicaset($members, 30300, $settings, dirname(__FILE__) . "/keyFile");
            $cfg = $server->getReplicaSetConfig(true);
        }
        $dsn = $cfg["dsn"];
        break;
    default:
        var_dump("No idea what to do about $bit");
        exit(32);
    }
    printf("DONE (%.2f secs): %s\n", t(), $dsn);
}

$SERVERS = array(
    "STANDALONE"        => 0x01,
    "STANDALONE_AUTH"   => 0x02,
    "STANDALONE_BRIDGE" => 0x04,
    "MONGOS"            => 0x08,
    "REPLICASET"        => 0x10,
    "REPLICASET_AUTH"   => 0x20,
);

$ALL_SERVERS = 0;
$BOOTSTRAP = 0;
foreach($SERVERS as $server => $bit) {
    define($server, $bit);
    $ALL_SERVERS |= $bit;
}

foreach($SERVERS as $server => $bit) {
    if (getenv("MONGO_SERVER_$server")) {
        $BOOTSTRAP |= $bit;
    }
}
if (!$BOOTSTRAP) {
    $BOOTSTRAP = $ALL_SERVERS;
    foreach($SERVERS as $server => $bit) {
        if (getenv("SKIP_MONGO_SERVER_$server")) {
            $BOOTSTRAP &= ~$bit;
        }
    }
}
function makeDaemon() {
    $pid = pcntl_fork();
    if ($pid > 0) {
        sleep(1);
        echo "Daemon running..\n";
        return;
    }
    if (!$pid) {
        posix_setsid();
        require_once dirname(__FILE__) . "/daemon.php";
        exit(0);
    }
}

if (!is_dir($DBDIR)) {
    if (!mkdir($DBDIR, 0700, true)) {
        echo "Error creating database directory: $DBDIR\n";
        exit(2);
    }
}

try {
    $server = new MongoShellServer;
} catch(Exception $e) {
    echo "Does't look like the daemon is up and running.. Starting it now\n";
    makeDaemon();
    try {
        $server = new MongoShellServer;
    } catch(Exception $e)  {
        echo $e->getMessage();
        exit(2);
    }
}

foreach($SERVERS as $k => $bit) {
    if ($BOOTSTRAP & $bit) {
        try {
            makeServer(array_flip($SERVERS), $server, $bit);
        } catch(DebugException $e) {
            echo $e->getMessage(), "\n";
            $filename = tempnam(sys_get_temp_dir(), "MONGO-PHP-TESTS");
            file_put_contents($filename, $e->getMongoDLog());
            echo "Debug log from mongod writter to $filename\n";
            $server->close();
            include "tests/utils/teardown-servers.php";
            exit(113);
        }
    }
}

$server->close();

echo "We have liftoff \n";