5 require 'inc/init.php';
9 echo "Usage: <this file> <options>\n";
10 echo "\t\t--vdid=<VLAN domain ID>\n";
11 echo "\t\t--mode=pull\n";
12 echo "\t\t--mode=pullall\n";
13 echo "\t\t--mode=push\n";
14 echo "\t\t[--max=<max_to_do>]\n";
15 echo "\t\t[--verbose]\n";
19 $options = getopt ('', array ('vdid:', 'max::', 'mode:', 'verbose'));
20 if (!array_key_exists ('vdid', $options) or !array_key_exists ('mode', $options))
23 switch ($options['mode'])
38 $max = array_key_exists ('max', $options) ?
$options['max'] : 0;
39 $verbose = array_key_exists ('verbose', $options);
43 $mydomain = getVLANDomain ($options['vdid']);
45 catch (RackTablesError
$e)
47 echo "Cannot load domain data with ID ${options['vdid']}\n";
48 echo $e->getMessage() . "\n";
54 'pull' => array ('sync_ready', 'resync_ready'),
55 'push' => array ('sync_ready', 'resync_ready'),
56 'pullall' => array ('sync_ready', 'resync_ready', 'sync_aging', 'resync_aging', 'done'),
59 $filename = '/var/tmp/RackTables-syncdomain-' . $options['vdid'] . '.pid';
60 if (FALSE === $fp = @fopen
($filename, 'x+'))
62 if (FALSE === $pidfile_mtime = filemtime ($filename))
64 echo "Failed to obtain mtime of ${filename}\n";
67 $current_time = time();
68 if ($current_time < $pidfile_mtime)
70 echo "Warning: pidfile ${filename} mtime is in future!\n";
73 // don't indicate failure unless the pidfile is 15 minutes or more old
74 if ($current_time < $pidfile_mtime +
15 * 60)
76 echo "Failed to lock ${filename}, already locked by PID " . mb_substr (file_get_contents ($filename), 0, 6) . "\n";
81 fwrite ($fp, getmypid() . "\n");
84 // fetch all the needed data from DB (preparing for DB connection loss)
85 $switch_queue = array();
86 foreach ($mydomain['switchlist'] as $switch)
87 if (in_array (detectVLANSwitchQueue (getVLANSwitchInfo ($switch['object_id'])), $todo[$options['mode']]))
88 $switch_queue[] = spotEntity ('object', $switch['object_id']);
90 // YOU SHOULD NOT USE DB FUNCTIONS BELOW IN THE PARENT PROCESS
91 // THE PARENT'S DB CONNECTION IS LOST DUE TO RECONNECTING IN THE CHILD
92 $fork_slots = getConfigVar ('SYNCDOMAIN_MAX_PROCESSES');
93 $do_fork = ($fork_slots > 1) and extension_loaded ('pcntl');
94 if ($fork_slots > 1 and ! $do_fork)
95 throw new RackTablesError ('PHP extension \'pcntl\' not found, can not use childs', RackTablesError
::MISCONFIGURED
);
96 $switches_working = 0;
98 foreach ($switch_queue as $object)
102 // wait for the next free slot
103 while ($fork_slots <= $switches_working)
105 pcntl_waitpid (-1, $wait_status);
108 $i_am_child = (0 === $fork_res = pcntl_fork());
110 if (! $do_fork or $i_am_child)
114 // make a separate DB connection for correct concurrent transactions handling
117 $portsdone = exec8021QDeploy ($object['id'], $do_push);
118 if ($portsdone or $verbose)
119 echo "Done '${object['dname']}': ${portsdone}\n";
121 catch (RackTablesError
$e)
123 echo "FAILED '${object['dname']}': " . $e->getMessage() . "\n";
128 if (isset ($fork_res) and $fork_res > 0)
131 if (++
$switchesdone == $max)
134 echo "Maximum of ${max} items reached, terminating\n";
139 // wait for all childs to exit
140 while ($switches_working > 0)
143 pcntl_waitpid (-1, $wait_status);
146 if (FALSE === unlink ($filename))
148 echo "Failed removing pidfile ${filename}\n";