r3813 note the last bugfix
[racktables] / syncdomain.php
1 #!/usr/local/bin/php
2 <?php
3
4 $script_mode = TRUE;
5 require 'inc/init.php';
6
7 function usage()
8 {
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";
16 exit (1);
17 }
18
19 $options = getopt ('', array ('vdid:', 'max::', 'mode:', 'verbose'));
20 if (!array_key_exists ('vdid', $options) or !array_key_exists ('mode', $options))
21 usage();
22
23 switch ($options['mode'])
24 {
25 case 'pullall':
26 $do_push = FALSE;
27 break;
28 case 'pull':
29 $do_push = FALSE;
30 break;
31 case 'push':
32 $do_push = TRUE;
33 break;
34 default:
35 usage();
36 }
37
38 $max = array_key_exists ('max', $options) ? $options['max'] : 0;
39 $verbose = array_key_exists ('verbose', $options);
40
41 if (NULL === $mydomain = getVLANDomain ($options['vdid']))
42 {
43 echo "Cannot load domain data with ID ${options['vdid']}\n";
44 exit (1);
45 }
46
47 $todo = array
48 (
49 'pull' => array ('sync_ready', 'resync_ready'),
50 'push' => array ('sync_ready'),
51 'pullall' => array ('sync_ready', 'resync_ready', 'sync_aging', 'resync_aging', 'done'),
52 );
53
54 $filename = '/var/tmp/RackTables-syncdomain-' . $options['vdid'] . '.pid';
55 if (FALSE === $fp = @fopen ($filename, 'x+'))
56 {
57 echo "Failed to lock ${filename}, already locked by PID " . mb_substr (file_get_contents ($filename), 0, 6);
58 exit (1);
59 }
60
61 ftruncate ($fp, 0);
62 fwrite ($fp, getmypid() . "\n");
63 fclose ($fp);
64
65 $switchesdone = 0;
66 foreach ($mydomain['switchlist'] as $switch)
67 if (in_array (detectVLANSwitchQueue (getVLANSwitchInfo ($switch['object_id'])), $todo[$options['mode']]))
68 {
69 $object = spotEntity ('object', $switch['object_id']);
70 $portsdone = exec8021QDeploy ($switch['object_id'], $do_push);
71 if ($portsdone or $verbose)
72 echo "Done '${object['dname']}': ${portsdone}\n";
73 if (++$switchesdone == $max)
74 {
75 if ($verbose)
76 echo "Maximum of ${max} items reached, terminating\n";
77 break;
78 }
79 }
80
81 if (FALSE === unlink ($filename))
82 {
83 echo "Failed removing pidfile ${filename}\n";
84 exit (1);
85 }
86 exit (0);
87 ?>