Odyssey
remote-hcu-services.prg
1 #!/usr/local/bin/php
2 <?php
3  /*
4  * Filename: hcu-services
5  *
6  * mws 4/15/2013
7  *
8  * Purpose: This script is intended to be executed from the command line.
9  * It will allow the user to turn enable/disable HomeCU Services.
10  * This is done by making changes to the cutrustedetail record where
11  * trustedid = 'HOMECUSERVICES' and cu = 'HOMECU'
12  *
13  * Command Line Arguments:
14  * -s {Services} that will be affected by the script value
15  * Options are ALL, or a list of services
16  * USAGE:
17  * -s ALL
18  * OR
19  * for one service
20  * -s IPAY
21  * for multiple services
22  * -s IPAY -s ENSENTA
23  *
24  * -a {Action} {on/off} this argument determines what the purpose of
25  * the script should
26  * USAGE:
27  * -a on for On
28  * -a off for Off
29  *
30  * -q {quiet} flag is set when listed. When set, the program should NOT
31  * print any output
32  */
33 
34  $suppressPrint = false;
35  $serviceAction = ""; // ** Service actions {on/off}
36  $serviceCodes = ""; // ** The services that will be affected {ALL for all services} or individual OR array of list
37  $argsFound = true; // ** Were ALL the necessary arguments found?
38 
39  // ** INCLUDE NEEDED SCRIPT
40  require('remote-hcu-services.conf');
41  require('cutrusted.i');
42  require('dbenv.i');
43  require('dbfunc.i');
44  // ** Command line does NOT set _SERVER['SERVER_NAME'] -- use homecuServer from the remote-hcu-services.conf file
45  $dbhost = ($homecuServer == 'monitor' ? 'localhost' : "db-" . $homecuServer);
46 
47 
48  // ** Declare argument option list
49  $options = Array("s:" => "required:",
50  "a:" => "required:",
51  "q" => "optional");
52  $opts = getopt(implode('', array_keys($options)), $options);
53 
54  // ** validate options
55  // ** ACTION
56  if (array_key_exists('a', $opts)) {
57  if (strtolower($opts['a']) == 'on' || strtolower($opts['a']) == 'off') {
58  $serviceAction = strtolower($opts['a']);
59  } else {
60  // ** Incorrect Syntact for -a
61  $argsFound = false;
62  }
63  } else {
64  // ** NO -a found
65  $argsFound = false;
66  }
67 
68  if (array_key_exists('s', $opts)) {
69  $serviceCodes = $opts['s'];
70  } else {
71  // ** -s options NOT FOUND
72  $argsFound = false;
73  }
74  if (array_key_exists('q', $opts)) {
75  $suppressPrint = true;
76  }
77 
78  if (!$argsFound) {
79  print "
80 usage: hcu-services -a action -s service [-q]
81  ";
82  exit;
83  }
84 
85 
86 
87 
88 
89  $dbh = db_pconnect();
90 
91  // ** Retrieve the HomeCU Services Record
92  $trustSettings = Array('Cu' => prep_save($homecuTrustCU, 12), 'trustedid' => prep_save($homecuTrustID, 20));
93  $retResp = cutd_read($dbh, $trustSettings);
94  if ($retResp['status']['Response']) {
95  // ** Verify the data
96  if ($retResp['data']["{$homecuTrustCU}|{$homecuTrustID}"]) {
97  if (is_array($retResp['data']["{$homecuTrustCU}|{$homecuTrustID}"])) {
98  $homecuServices = $retResp['data']["{$homecuTrustCU}|{$homecuTrustID}"];
99  /* homecuServices
100  * This should now be an array of key/value sets.
101  *
102  * key is the homecuService ie IPAY,ENSENTA
103  *
104  * value {Y, N, U}
105  *
106  * N - Service is ONLINE
107  * Y - Service is OFFLINE - and stays down, if the script is set to enable services, this service will NOT change
108  * U - Service is OFFLINE - and if the script is set to enable services, the service value is set to N
109  *
110  * NOW -- LOOP through all the key/value sets
111  * when action is set to Off -a off, then turn all occurrences of N to U
112  * when action is set to On -a on, then turn all occurrencens of U to N
113  * if -s ALL is set, then all services will be affected
114  * if -s is an array of services, only those listed services should be affected
115  *
116  */
117  foreach ($homecuServices as $itemService => $itemValue) {
118  // ** First determine if we are on a correct service
119  $foundService = false;
120  if (is_array($serviceCodes)) {
121  // ** Command line service codes is an ARRAY -- look up using in_array
122  if (in_array($itemService, $serviceCodes)) {
123  // ** CUTD Service was found in list of command line services to alter
124  $foundService = true;
125  }
126  } elseif ((strtoupper($serviceCodes) == 'ALL') || ($serviceCodes == trim($itemService))) {
127  // ** Command line service code is ALL -- Attempt to alter ALL services
128  // ** OR
129  // ** Command line referenced ONE code -- and it is a match
130  $foundService = true;
131  }
132  if ($foundService) {
133  // ** Now depending on the action -- perform update
134  if ($serviceAction == 'on') {
135  // * ONLY Update if current itemValue is U
136  if ($itemValue == 'U') {
137  $homecuServices[$itemService] = 'N';
138  }
139  } elseif ($serviceAction == 'off') {
140  // * ONLY Update if current itemValue is N
141  if ($itemValue == 'N') {
142  $homecuServices[$itemService] = 'U';
143  }
144  }
145  }
146 
147  }
148 
149  // ** AFTER SETTING ALL THE SERVICES
150  // ** I need to resave the trusted detail record with the new values
151  $trustSettings = Array('Cu' => prep_save($homecuTrustCU, 12), 'trustedid' => prep_save($homecuTrustID, 20), 'fields' => $homecuServices);
152  $retResp = cutd_write($dbh, $trustSettings);
153  if ($retResp['status']['Response']) {
154  } else {
155  // ** ERROR - when updating the record
156  if (!$suppressPrint) {print "\nError when updating the services.";}
157  }
158  } else {
159  if (!$suppressPrint) {print "\nHomeCU Services Found. Data Not Valid";}
160  }
161  } else {
162  if (!$suppressPrint) {print "\nHomeCU Services Found. No Services Data Found";}
163  }
164  } else {
165  if (!$suppressPrint) {print "\nNo HomeCU Services Record Found.";}
166  }
167 
168 
169 ?>