FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
fossjobs.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2012 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 ***********************************************************/
20 
45 /**********************************************************************
46  **********************************************************************
47 SUPPORT FUNCTIONS
48 **********************************************************************
49 **********************************************************************/
58 function list_agents()
59 {
60  $agent_list = menu_find("Agents", $depth);
61  return ($agent_list);
62 }
63 
68 require_once("$MODDIR/lib/php/common-cli.php");
69 
70 global $Plugins;
71 global $PERM_NAMES;
72 global $SysConf;
73 global $PG_CONN;
74 
75 /**********************************************************************
76  **********************************************************************
77 INITIALIZE THIS INTERFACE
78 **********************************************************************
79 **********************************************************************/
80 $usage = basename($argv[0]) . " [options]
81  Options:
82  -h :: help, this message
83  -v :: verbose output
84  -a :: list available agent tasks
85  -A string :: specify agent to schedule (default is everything from -a)
86  The string can be a comma-separated list of agent tasks.
87  -u :: list available upload ids
88  -U upload :: the upload identifier for scheduling agent tasks
89  The string can be a comma-separated list of upload ids.
90  Or, use 'ALL' to specify all upload ids.
91  -D upload :: the upload identifier for scheduling delete tasks
92  The string can either be 'ALL', a string (the upload_pk),
93  or an array of upload_pk's if multiple -D's were specified.
94  --username string :: user name
95  --password string :: password
96  --groupname string :: group name
97  -c string :: Specify the directory for the system configuration
98 ";
99 //process parameters, see usage above
100 $longopts = array("username:", "password:", "groupname:");
101 $options = getopt("c:haA:P:uU:D:v", $longopts);
102 //print_r($options);
103 if (empty($options)) {
104  echo $usage;
105  exit(1);
106 }
107 if (array_key_exists("h", $options)) {
108  echo $usage;
109  exit(0);
110 }
111 
112 /**********************************************************************
113  **********************************************************************
114 PROCESS COMMAND LINE SELECTION
115 **********************************************************************
116 **********************************************************************/
117 $user = "";
118 $passwd = "";
119 $group = "";
120 if (array_key_exists("username", $options)) {
121  $user = $options["username"];
122 }
123 
124 if (array_key_exists("groupname", $options)) {
125  $group = $options["groupname"];
126 }
127 
128 if (array_key_exists("password", $options)) {
129  $passwd = $options["password"];
130 }
131 
132 account_check($user, $passwd, $group); // check username/password
133 $groupId = Auth::getGroupId();
134 
135 /* init plugins */
136 cli_Init();
137 
138 $Verbose = 0;
139 if (array_key_exists("v", $options)) {
140  $Verbose = 1;
141 }
142 
143 // Get the list of registered agents
144 $agent_list = list_agents();
145 if (empty($agent_list)) {
146  echo "ERROR! could not get list of agents\n";
147  echo "Are Plugins configured?\n";
148  exit(1);
149 }
150 
151 /* List available agents */
152 if (array_key_exists("a", $options)) {
153  if (empty($agent_list)) {
154  echo "No agents configured\n";
155  } else {
156  echo "The available agents are:\n";
157  $agent_count = count($agent_list);
158  for ($ac = 0;$ac < $agent_count;$ac++) {
159  $agent = ($agent_list[$ac]->URI);
160  if (!empty($agent)) {
161  echo " $agent\n";
162  }
163  }
164  }
165 }
166 
167 /* Hide agents that aren't related to data scans */
168 $Skip = array("agent_unpack", "agent_adj2nest", "wget_agent");
169 for ($ac=0; !empty($agent_list[$ac]->URI); $ac++) {
170  if (array_search($agent_list[$ac]->URI, $Skip) !== false) {
171  unset($agent_list[$ac]);
172  }
173 }
174 
175 /* If the user specified a list, then disable every agent not in the list */
176 $Skip = array("agent_unpack", "agent_adj2nest", "wget_agent");
177 if (array_key_exists("A", $options)) {
178  $agent_count = count($agent_list);
179  for ($ac = 0;$ac < $agent_count;$ac++) {
180  $Found = 0;
181  foreach (explode(',', $options["A"]) as $Val) {
182  if (!strcmp($Val, $agent_list[$ac]->URI)) {
183  $Found = 1;
184  }
185  }
186  if ($Found == 0) {
187  $agent_list[$ac]->URI = null;
188  }
189  }
190 }
191 
192 /* List available uploads */
193 if (array_key_exists("u", $options)) {
194  $root_folder_pk = GetUserRootFolder();
195  $FolderPath = null;
196  $FolderList = FolderListUploadsRecurse($root_folder_pk, $FolderPath, Auth::PERM_WRITE);
197 
198  print "# The following uploads are available (upload id: name)\n";
199  foreach ($FolderList as $Folder) {
200  $Label = $Folder['name'] . " (" . $Folder['upload_desc'] . ')';
201  print $Folder['upload_pk'] . ": $Label\n";
202  }
203  exit(0);
204 }
205 
206 /* @var $uploadDao UploadDao */
207 $uploadDao = $GLOBALS['container']->get('dao.upload');
208 
209 if (array_key_exists("U", $options)) {
210  /* $options['U'] can either be 'ALL', a string (the upload_pk),
211  or an array of upload_pk's if multiple -U's were specified.
212  */
213  $upload_options = $options['U'];
214  $upload_pk_array = array();
215  if ($upload_options == 'ALL') {
216  $SQL = "SELECT upload_pk,upload_desc,upload_filename FROM upload ORDER BY upload_pk;";
217  $result = pg_query($PG_CONN, $SQL);
218  DBCheckResult($result, $SQL, __FILE__, __LINE__);
219  while ($row = pg_fetch_assoc($result) && !empty($row['upload_pk'])) {
220  $upload_pk_array[] = $row['upload_pk'];
221  }
222  pg_free_result($result);
223  } else if (is_array($upload_options)) {
224  $upload_pk_array = $upload_options;
225  } else {
226  $upload_pk_array[] = $upload_options;
227  }
228 
229  /* check permissions */
230  $checked_list = array();
231  foreach ($upload_pk_array as $upload_pk) {
232  if (!$uploadDao->isEditable($upload_pk, $groupId)) {
233  print "You have no permission to queue agents for upload $upload_pk in group $groupId\n";
234  continue;
235  }
236  $checked_list[] = $upload_pk;
237  }
238  $checked_list_str = implode(",", $checked_list);
239 
241  QueueUploadsOnAgents($checked_list_str, $agent_list, $Verbose);
242 }
243 
244 if (array_key_exists("D", $options)) {
245  /* $options['D'] can either be 'ALL', a string (the upload_pk),
246  or an array of upload_pk's if multiple -D's were specified.
247  */
248  $upload_options = $options['D'];
249  $upload_pk_array = array();
250  if ($upload_options == 'ALL') {
251  $SQL = "SELECT upload_pk,upload_desc,upload_filename FROM upload ORDER BY upload_pk;";
252  $result = pg_query($PG_CONN, $SQL);
253  DBCheckResult($result, $SQL, __FILE__, __LINE__);
254  while ($row = pg_fetch_assoc($result) && !empty($row['upload_pk'])) {
255  $upload_pk_array[] = $row['upload_pk'];
256  }
257  pg_free_result($result);
258  } else if (is_array($upload_options)) {
259  $upload_pk_array = $upload_options;
260  } else {
261  $upload_pk_array[] = $upload_options;
262  }
263  /* check permissions */
264  $checked_list = array();
265  foreach ($upload_pk_array as $upload_pk) {
266  if (!$uploadDao->isEditable($upload_pk, $groupId)) {
267  print "You have no permission to delete upload " . $upload_pk . "\n";
268  continue;
269  }
270  $checked_list[] = $upload_pk;
271  }
272  $checked_list_str = implode(",", $checked_list);
273 
275  QueueUploadsOnDelagents($checked_list_str);
276 }
account_check(&$user, &$passwd, &$group="")
check if this account is correct
Definition: common-auth.php:87
QueueUploadsOnAgents($upload_pk_list, $agent_list, $Verbose)
Schedule agent tasks on upload ids.
Definition: common-job.php:270
menu_find($Name, &$MaxDepth, $Menu=NULL)
Given a top-level menu name, find the list of sub-menus below it and max depth of menu...
GetUserRootFolder()
Get the top-of-tree folder_pk for the current user. Fail if there is no user session.
cli_Init()
Initialize the fossology environment for CLI use. This routine loads the plugins so they can be use b...
Definition: common-cli.php:36
FolderListUploadsRecurse($ParentFolder=-1, $FolderPath= '', $perm=Auth::PERM_READ)
Get uploads and folder info, starting from $ParentFolder.
global $Plugins
Definition: fossjobs.php:68
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
QueueUploadsOnDelagents($upload_pk_list)
Schedule delagent on upload ids.
Definition: common-job.php:335
list_agents()
list agents
Definition: fossjobs.php:58
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45