"Fossies" - the Fresh Open Source Software Archive 
Member "fogproject-1.5.9/packages/web/lib/reg-task/taskqueue.class.php" (13 Sep 2020, 16513 Bytes) of package /linux/misc/fogproject-1.5.9.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the latest
Fossies "Diffs" side-by-side code changes report for "taskqueue.class.php":
1.5.8_vs_1.5.9.
1 <?php
2 /**
3 * The queue handling system for FOG's checkin/checkout processes.
4 *
5 * PHP version 5
6 *
7 * @category TaskQueue
8 * @package FOGProject
9 * @author Tom Elliott <tommygunsster@gmail.com>
10 * @license http://opensource.org/licenses/gpl-3.0 GPLv3
11 * @link https://fogproject.org
12 */
13 /**
14 * The queue handling system for FOG's checkin/checkout processes.
15 *
16 * @category TaskQueue
17 * @package FOGProject
18 * @author Tom Elliott <tommygunsster@gmail.com>
19 * @license http://opensource.org/licenses/gpl-3.0 GPLv3
20 * @link https://fogproject.org
21 */
22 class TaskQueue extends TaskingElement
23 {
24 /**
25 * Handles task checkin
26 *
27 * @throws Exception
28 * @return void
29 */
30 public function checkIn()
31 {
32 try {
33 self::randWait();
34 $this->Task
35 ->set('stateID', self::getCheckedInState())
36 ->set('checkinTime', self::formatTime('now', 'Y-m-d H:i:s'))
37 ->save();
38 if (!$this->Task->save()) {
39 throw new Exception(_('Failed to update task'));
40 }
41 if ($this->imagingTask) {
42 if ($this->Task->isMulticast()) {
43 $msID = @min(
44 self::getSubObjectIDs(
45 'MulticastSessionAssociation',
46 array(
47 'taskID' => $this->Task->get('id')
48 ),
49 'msID'
50 )
51 );
52 $MulticastSession = self::getClass(
53 'MulticastSession',
54 $msID
55 );
56 if (!$MulticastSession->isValid()) {
57 throw new Exception(_('Invalid Multicast Session'));
58 }
59 if ($MulticastSession->get('clients') < 0) {
60 $clients = 1;
61 } else {
62 $clients = $MulticastSession->get('clients') + 1;
63 }
64 $MulticastSession
65 ->set('clients', $clients);
66 if (!$MulticastSession->save()) {
67 throw new Exception(_('Failed to update Session'));
68 }
69 if (self::$Host->isValid()) {
70 self::$Host
71 ->set(
72 'imageID',
73 $MulticastSession->get('image')
74 );
75 }
76
77 $this->StorageNode = self::nodeFail(
78 self::getClass('StorageNode', $this->Task->get('storagenodeID')),
79 self::$Host->get('id')
80 );
81 if ($MulticastSession->get('stateID') == 1) {
82 $msg = sprintf(
83 '%s, %s %d %s.',
84 _('No open slots'),
85 _('There are'),
86 $inFront,
87 _('before me')
88 );
89 throw new Exception($msg);
90 }
91 } elseif ($this->Task->isForced()) {
92 self::$HookManager->processEvent(
93 'TASK_GROUP',
94 array(
95 'StorageGroup' => &$this->StorageGroup,
96 'Host' => &self::$Host
97 )
98 );
99 $this->StorageNode = null;
100 self::$HookManager->processEvent(
101 'TASK_NODE',
102 array(
103 'StorageNode' => &$this->StorageNode,
104 'Host' => &self::$Host
105 )
106 );
107 $method = 'getOptimalStorageNode';
108 if ($this->Task->isCapture()
109 || $this->Task->isMulticast()
110 ) {
111 $method = 'getMasterStorageNode';
112 }
113 if (!$this->StorageNode || !$this->StorageNode->isValid()) {
114 $this->StorageNode = $this->Image
115 ->getStorageGroup()
116 ->{$method}();
117 }
118 } else {
119 $this->StorageNode = self::nodeFail(
120 self::getClass(
121 'StorageNode',
122 $this->Task->get('storagenodeID')
123 ),
124 self::$Host->get('id')
125 );
126 $nodeTest = $this->StorageNode instanceof StorageNode &&
127 $this->StorageNode->isValid();
128
129 if (!$nodeTest) {
130 $msg = sprintf(
131 '%s %s. %s %s.',
132 _('The node trying to be used is currently'),
133 _('unavailable'),
134 _('On reboot we will try to find a new node'),
135 _('automatically')
136 );
137 throw new Exception($msg);
138 }
139 $totalSlots = $this->StorageNode->get('maxClients');
140 $usedSlots = $this->StorageNode->getUsedSlotCount();
141 $inFront = $this->Task->getInFrontOfHostCount();
142 $groupOpenSlots = $totalSlots - $usedSlots;
143 if ($groupOpenSlots < 1) {
144 $msg = sprintf(
145 '%s, %s %d %s.',
146 _('No open slots'),
147 _('There are'),
148 $inFront,
149 _('before me')
150 );
151 throw new Exception($msg);
152 }
153 if ($groupOpenSlots <= $inFront) {
154 $msg = sprintf(
155 '%s, %s %d %s.',
156 _('There are open slots'),
157 _('but'),
158 $inFront,
159 _('before me on this node')
160 );
161 throw new Exception($msg);
162 }
163 }
164 if ($this->Task->isCapture()) {
165 $this->Task->getImage()->set('size', '')->save();
166 }
167 $this->Task
168 ->set(
169 'storagenodeID',
170 $this->StorageNode->get('id')
171 );
172 if (!$this->imageLog(true)) {
173 throw new Exception(_('Failed to update/create image log'));
174 }
175 }
176 $this->Task
177 ->set('stateID', self::getProgressState())
178 ->set('checkInTime', self::formatTime('now', 'Y-m-d H:i:s'));
179 if (!$this->Task->save()) {
180 throw new Exception(_('Failed to update Task'));
181 }
182 if (!$this->taskLog()) {
183 throw new Exception(_('Failed to update/create task log'));
184 }
185 self::$EventManager->notify(
186 'HOST_CHECKIN',
187 array(
188 'Host' => &self::$Host
189 )
190 );
191 echo '##@GO';
192 } catch (Exception $e) {
193 echo $e->getMessage();
194 }
195 }
196 /**
197 * Handles the email sending.
198 *
199 * @return void
200 */
201 private function _email()
202 {
203 list(
204 $emailAction,
205 $emailAddress,
206 $emailBinary,
207 $fromEmail
208 ) = self::getSubObjectIDs(
209 'Service',
210 array(
211 'name' => array(
212 'FOG_EMAIL_ACTION',
213 'FOG_EMAIL_ADDRESS',
214 'FOG_EMAIL_BINARY',
215 'FOG_FROM_EMAIL'
216 )
217 ),
218 'value',
219 false,
220 'AND',
221 'name',
222 false,
223 false
224 );
225 if (!$emailAction || !$emailAddress) {
226 return;
227 }
228 if (!self::$Host->get('inventory')->isValid()) {
229 return;
230 }
231 $SnapinJob = self::$Host->get('snapinjob');
232 $SnapinTasks = self::getSubObjectIDs(
233 'SnapinTask',
234 array(
235 'stateID' => self::getQueuedStates(),
236 'jobID' => $SnapinJob->get('id')
237 ),
238 'snapinID'
239 );
240 $SnapinNames = array();
241 if ($SnapinJob->isValid()) {
242 $SnapinNames = self::getSubObjectIDs(
243 'Snapin',
244 array(
245 'id' => $SnapinTasks,
246 ),
247 'name'
248 );
249 }
250 if (!$emailBinary) {
251 $emailBinary = '/usr/sbin/sendmail -t -f noreply@fogserver.com -i';
252 }
253 $reg = '#\$\{server-name\}#';
254 $nodeName = 'fogserver';
255 if ($this->StorageNode->isValid()) {
256 $nodeName = $this->StorageNode->get('name');
257 }
258 $emailBinary = preg_replace(
259 $reg,
260 $nodeName,
261 $emailBinary
262 );
263 if (!$fromEmail) {
264 $fromEmail = 'noreply@fogserver.com';
265 }
266 $fromEmail = preg_replace(
267 $reg,
268 $nodeName,
269 $fromEmail
270 );
271 $headers = sprintf(
272 "From: %s\r\nX-Mailer: PHP/%s",
273 $fromEmail,
274 phpversion()
275 );
276 $engineer = ucwords(
277 $this->Task->get('createdBy')
278 );
279 $primaryUser = ucwords(
280 self::$Host->get('inventory')->get('primaryUser')
281 );
282 $replaceUser = '#\$\{user-name\}#';
283 $emailAddress = preg_replace(
284 $replaceUser,
285 lcfirst($engineer),
286 $emailAddress
287 );
288 $emailAddress = preg_replace(
289 $reg,
290 $nodeName,
291 $emailAddress
292 );
293 $Inventory = self::$Host->get('inventory');
294 $mac = self::$Host->get('mac')->__toString();
295 $ImageName = $this->Task->getImage()->get('name');
296 $Snapins = implode(',', (array)$SnapinNames);
297 $email = array(
298 sprintf("%s:-\n", _('Machine Details')) => '',
299 sprintf("\n%s: ", _('Host Name')) => self::$Host->get('name'),
300 sprintf("\n%s: ", _('Computer Model')) => $Inventory->get('sysproduct'),
301 sprintf("\n%s: ", _('Serial Number')) => $Inventory->get('sysserial'),
302 sprintf("\n%s: ", _('MAC Address')) => $mac,
303 "\n" => '',
304 sprintf("\n%s: ", _('Image Used')) => $ImageName,
305 sprintf("\n%s: ", _('Snapin Used')) => $Snapins,
306 "\n" => '',
307 sprintf("\n%s: ", _('Imaged By')) => $engineer,
308 sprintf("\n%s: ", _('Imaged For')) => $primaryUser
309 );
310 self::$HookManager->processEvent(
311 'EMAIL_ITEMS',
312 array(
313 'email' => &$email,
314 'Host' => &self::$Host
315 )
316 );
317 ob_start();
318 foreach ((array)$email as $key => &$val) {
319 printf('%s%s', $key, $val);
320 unset($key, $val);
321 }
322 $emailMe = ob_get_clean();
323 $stat = sprintf(
324 '%s - %s',
325 self::$Host->get('name'),
326 _('Image Task Completed')
327 );
328 if ($Inventory->get('other1')) {
329 mail(
330 $emailAddress,
331 sprintf(
332 'ISSUE=%s PROJ=1',
333 $Inventory->get('other1')
334 ),
335 $emailMe,
336 $headers
337 );
338 $emailMe .= sprintf(
339 "\n%s (%s): %s",
340 _('Imaged For'),
341 _('Call'),
342 $Inventory->get('other1')
343 );
344 //$Inventory->set('other1', '')->save();
345 }
346 mail(
347 $emailAddress,
348 $stat,
349 $emailMe,
350 $headers
351 );
352 }
353 /**
354 * Function moves the images from dev into root when upload
355 * tasking is finished.
356 *
357 * @throws Exception
358 * @return void
359 */
360 private function _moveUpload()
361 {
362 if (!$this->Task->isCapture()) {
363 return;
364 }
365 if (!(isset($_REQUEST['mac'])
366 && is_string($_REQUEST['mac']))
367 ) {
368 return;
369 }
370 $macftp = strtolower(
371 str_replace(
372 array(
373 ':',
374 '-',
375 '.'
376 ),
377 '',
378 basename($_REQUEST['mac'])
379 )
380 );
381 $src = sprintf(
382 '%s/dev/%s',
383 $this->StorageNode->get('ftppath'),
384 $macftp
385 );
386 $dest = sprintf(
387 '%s/%s',
388 $this->StorageNode->get('ftppath'),
389 $this->Image->get('path')
390 );
391 self::$FOGFTP
392 ->set('host', $this->StorageNode->get('ip'))
393 ->set('username', $this->StorageNode->get('user'))
394 ->set('password', $this->StorageNode->get('pass'))
395 ->connect()
396 ->delete($dest)
397 ->rename($src, $dest)
398 ->chmod(0777, $dest)
399 ->close();
400 if ($this->Image->get('format') == 1) {
401 $this->Image
402 ->set('format', 0)
403 ->set('srvsize', self::getFilesize($dest));
404 }
405 $this->Image
406 ->set(
407 'deployed',
408 self::niceDate()->format('Y-m-d H:i:s')
409 )->save();
410 }
411 /**
412 * Handles task checkout
413 *
414 * @throws Exception
415 * @return void
416 */
417 public function checkout()
418 {
419 self::randWait();
420 if ($this->Task->isSnapinTasking()) {
421 die('##');
422 }
423 try {
424 if ($this->Task->isMulticast()) {
425 $MCTask = self::getClass('MulticastSessionAssociation')
426 ->set(
427 'taskID',
428 $this->Task->get('id')
429 )->load('taskID');
430 $MulticastSession = $MCTask->getMulticastSession();
431 if ($MulticastSession->get('clients') < 0) {
432 $clients = 1;
433 } else {
434 $clients = $MulticastSession->get('clients') - 1;
435 }
436 $MulticastSession
437 ->set('clients', $clients)
438 ->save();
439 }
440 self::$Host
441 ->set('pub_key', '')
442 ->set('sec_tok', '');
443 if ($this->Task->isDeploy()) {
444 self::$Host
445 ->set('deployed', self::niceDate()->format('Y-m-d H:i:s'));
446 $this->_email();
447 } elseif ($this->Task->isCapture()) {
448 $this->_moveUpload();
449 }
450 $this->Task
451 ->set('pct', 100)
452 ->set('percent', 100)
453 ->set('stateID', self::getCompleteState());
454 if (!self::$Host->save()) {
455 throw new Exception(_('Failed to update Host'));
456 }
457 if (!$this->Task->save()) {
458 throw new Exception(_('Failed to update Task'));
459 }
460 self::$HookManager
461 ->processEvent(
462 'HOST_TASKING_COMPLETE',
463 array(
464 'Host' => &self::$Host,
465 'Task' => &$this->Task
466 )
467 );
468 if (!$this->taskLog()) {
469 throw new Exception(_('Failed to update task log'));
470 }
471 if ($this->imagingTask) {
472 if (!$this->imageLog(false)) {
473 throw new Exception(_('Failed to update imaging log'));
474 }
475 }
476 self::$EventManager->notify(
477 'HOST_IMAGE_COMPLETE',
478 array(
479 'HostName' => self::$Host->get('name')
480 )
481 );
482 echo '##';
483 } catch (Exception $e) {
484 echo $e->getMessage();
485 }
486 }
487 }