GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 31696d...d014e0 )
by Stuart
05:40
created

FromHost::downloadFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * Copyright (c) 2011-present Mediasift Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   Storyplayer/Modules/Host
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://datasift.github.io/storyplayer
42
 */
43
44
namespace Storyplayer\SPv2\Modules\Host;
45
46
use DataSift\Storyplayer\HostLib;
47
use DataSift\Storyplayer\OsLib;
48
49
use DataSift\Stone\DataLib\DataPrinter;
50
use DataSift\Stone\ObjectLib\BaseObject;
51
52
use GanbaroDigital\TextTools\Filters\FilterColumns;
53
use GanbaroDigital\TextTools\Filters\FilterForMatchingRegex;
54
use GanbaroDigital\TextTools\Filters\FilterForMatchingString;
55
56
use Storyplayer\SPv2\Modules\Exceptions;
57
use Storyplayer\SPv2\Modules\Filesystem;
58
use Storyplayer\SPv2\Modules\Screen;
59
60
/**
61
 * get information about a given host
62
 *
63
 * @category  Libraries
64
 * @package   Storyplayer/Modules/Host
65
 * @author    Stuart Herbert <[email protected]>
66
 * @copyright 2011-present Mediasift Ltd www.datasift.com
67
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
68
 * @link      http://datasift.github.io/storyplayer
69
 */
70
class FromHost extends HostAwareModule
71
{
72
    /**
73
     * @return object
74
     */
75 View Code Duplication
    public function getDetails()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        // what are we doing?
78
        $log = usingLog()->startAction("retrieve details for host '{$this->args[0]}'");
79
80
        // get the host details
81
        $hostDetails = $this->getHostDetails();
82
83
        // we already have details - are they valid?
84
        if (isset($hostDetails->invalidHost) && $hostDetails->invalidHost) {
85
            $msg = "there are no details about host '{$hostDetails->hostId}'";
86
            $log->endAction($msg);
87
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
88
        }
89
90
        // return the details
91
        $log->endAction();
92
        return $hostDetails;
93
    }
94
95
    /**
96
     * is a host up and running?
97
     *
98
     * @return boolean
99
     */
100
    public function getHostIsRunning()
101
    {
102
        // what are we doing?
103
        $log = usingLog()->startAction("is host '{$this->args[0]}' running?");
104
105
        // make sure we have valid host details
106
        $hostDetails = $this->getHostDetails();
107
108
        // get an object to talk to this host
109
        $host = HostLib::getHostAdapter($this->st, $hostDetails->type);
110
111
        // if the box is running, it should have a status of 'running'
112
        $result = $host->isRunning($hostDetails);
113
114
        if (!$result) {
115
            $log->endAction("host is not running");
116
            return false;
117
        }
118
119
        // all done
120
        $log->endAction("host is running");
121
        return true;
122
    }
123
124
    /**
125
     * get the hostname for a host
126
     *
127
     * the returned hostname is suitable for use in HTTP/HTTPS URLs
128
     *
129
     * if we have been unable to determine the hostname for the host,
130
     * this will return the host's IP address instead
131
     *
132
     * @return string
133
     */
134
    public function getHostname()
135
    {
136
        // what are we doing?
137
        $log = usingLog()->startAction("get the hostname of host ID '{$this->args[0]}'");
138
139
        // make sure we have valid host details
140
        $hostDetails = $this->getHostDetails();
141
142
        // do we have a hostname?
143
        if (!isset($hostDetails->hostname)) {
144
            throw Exceptions::newActionFailedException(__METHOD__, "no hostname found for host ID '{$this->args[0]}'");
145
        }
146
147
        // all done
148
        $log->endAction("hostname is '{$hostDetails->hostname}'");
149
        return $hostDetails->hostname;
150
    }
151
152
    /**
153
     * get the IP address for a host
154
     *
155
     * @return string
156
     */
157 View Code Duplication
    public function getIpAddress()
158
    {
159
        // what are we doing?
160
        $log = usingLog()->startAction("get IP address of host '{$this->args[0]}'");
161
162
        // make sure we have valid host details
163
        $hostDetails = $this->getHostDetails();
164
165
        // all done
166
        $log->endAction("IP address is '{$hostDetails->ipAddress}'");
167
        return $hostDetails->ipAddress;
168
    }
169
170
    /**
171
     * @param  string $packageName
172
     * @return object
173
     */
174 View Code Duplication
    public function getInstalledPackageDetails($packageName)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        // what are we doing?
177
        $log = usingLog()->startAction("get details for package '{$packageName}' installed on host '{$this->args[0]}'");
178
179
        // make sure we have valid host details
180
        $hostDetails = $this->getHostDetails();
181
182
        // get an object to talk to this host
183
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
184
185
        // get the information
186
        $return = $host->getInstalledPackageDetails($hostDetails, $packageName);
187
188
        // all done
189
        $log->endAction();
190
        return $return;
191
    }
192
193
    /**
194
     * @param  int $pid
195
     * @return bool
196
     */
197 View Code Duplication
    public function getPidIsRunning($pid)
198
    {
199
        // what are we doing?
200
        $log = usingLog()->startAction("is process PID '{$pid}' running on host '{$this->args[0]}'?");
201
202
        // make sure we have valid host details
203
        $hostDetails = $this->getHostDetails();
204
205
        // get an object to talk to this host
206
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
207
208
        // get the information
209
        $return = $host->getPidIsRunning($hostDetails, $pid);
210
211
        // did it work?
212
        if ($return) {
213
            $log->endAction("'{$pid}' is running");
214
            return true;
215
        }
216
217
        $log->endAction("'{$pid}' is not running");
218
        return false;
219
    }
220
221
    /**
222
     * @param  string $processName
223
     * @return bool
224
     */
225 View Code Duplication
    public function getProcessIsRunning($processName)
226
    {
227
        // what are we doing?
228
        $log = usingLog()->startAction("is process '{$processName}' running on host '{$this->args[0]}'?");
229
230
        // make sure we have valid host details
231
        $hostDetails = $this->getHostDetails();
232
233
        // get an object to talk to this host
234
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
235
236
        // get the information
237
        $return = $host->getProcessIsRunning($hostDetails, $processName);
238
239
        // did it work?
240
        if ($return) {
241
            $log->endAction("'{$processName}' is running");
242
            return true;
243
        }
244
245
        $log->endAction("'{$processName}' is not running");
246
        return false;
247
    }
248
249
    /**
250
     * @param  string $processName
251
     * @return int
252
     */
253
    public function getPid($processName)
254
    {
255
        // log some info to the user
256
        $log = usingLog()->startAction("get id of process '{$processName}' running on VM '{$this->args[0]}'");
257
258
        // make sure we have valid host details
259
        $hostDetails = $this->getHostDetails();
260
261
        // get an object to talk to this host
262
        $host = OsLib::getHostAdapter($this->st, $hostDetails->osName);
263
264
        // get the information
265
        $return = (int)$host->getPid($hostDetails, $processName);
266
267
        // success
268
        $log->endAction("pid is '{$return}'");
269
        return $return;
270
    }
271
272
    /**
273
     * @return string
274
     */
275 View Code Duplication
    public function getSshUsername()
276
    {
277
        // what are we doing?
278
        $log = usingLog()->startAction("get username to use with SSH to host '{$this->args[0]}'");
279
280
        // make sure we have valid host details
281
        $hostDetails = $this->getHostDetails();
282
283
        // get the information
284
        $return = $hostDetails->sshUsername;
285
286
        // all done
287
        $log->endAction("username is '{$return}'");
288
        return $return;
289
    }
290
291
    /**
292
     * @return string
293
     */
294 View Code Duplication
    public function getSshKeyFile()
295
    {
296
        // what are we doing?
297
        $log = usingLog()->startAction("get key file to use with SSH to host '{$this->args[0]}'");
298
299
        // make sure we have valid host details
300
        $hostDetails = $this->getHostDetails();
301
302
        // get the information
303
        $return = $hostDetails->sshKeyFile;
304
305
        // all done
306
        $log->endAction("key file is '{$return}'");
307
        return $return;
308
    }
309
310
    /**
311
     * @param  string $sessionName
312
     * @return bool
313
     */
314
    public function getIsScreenRunning($sessionName)
315
    {
316
        return $this->getScreenIsRunning($sessionName);
0 ignored issues
show
Deprecated Code introduced by
The method Storyplayer\SPv2\Modules...t::getScreenIsRunning() has been deprecated with message: since v2.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
317
    }
318
319
    /**
320
     * @deprecated since v2.4.0
321
     */
322
    public function getScreenIsRunning($sessionName)
323
    {
324
        return Screen::fromHost($this->args[0])->getScreenIsRunning($sessionName);
325
    }
326
327
    /**
328
     * @deprecated since v2.4.0
329
     */
330
    public function getScreenSessionDetails($sessionName)
331
    {
332
        return Screen::fromHost($this->args[0])->getScreenSessionDetails($sessionName);
333
    }
334
335
    /**
336
     * @deprecated since v2.4.0
337
     */
338
    public function getAllScreenSessions()
339
    {
340
        return Screen::fromHost($this->args[0])->getAllScreenSessions();
341
    }
342
343
    /**
344
     * @param  string $appName
345
     * @return mixed
346
     */
347
    public function getAppSettings($appName)
348
    {
349
        // what are we doing?
350
        $log = usingLog()->startAction("get settings for '{$appName}' from host '{$this->args[0]}'");
351
352
        // make sure we have valid host details
353
        $hostDetails = $this->getHostDetails();
354
355
        // do we have any app settings?
356
        if (!isset($hostDetails->appSettings, $hostDetails->appSettings->$appName)) {
357
            $log->endAction("setting does not exist :(");
358
            throw Exceptions::newActionFailedException(__METHOD__);
359
        }
360
361
        // yes we do
362
        $value = $hostDetails->appSettings->$appName;
363
364
        // log the settings
365
        $printer  = new DataPrinter();
366
        $logValue = $printer->convertToString($value);
367
        $log->endAction("settings for '{$appName}' are '{$logValue}'");
368
369
        // all done
370
        return $value;
371
    }
372
373
    /**
374
     * @param  string $path
375
     * @param  string|null $settingName
376
     * @return mixed
377
     */
378
    public function getAppSetting($path, $settingName = null)
379
    {
380
        // are we operating in legacy mode (for DataSift), or are we using
381
        // the new dot.notation.support that we want everywhere in v2?
382
        $parts = explode(".", $path);
383
        if (count($parts) === 1 && $settingName !== null) {
384
            return $this->getLegacyAppSetting($path, $settingName);
385
        }
386
387
        // if we get here, then we are using the new dot.notation.support
388
389
        // what are we doing?
390
        $log = usingLog()->startAction("get appSetting '{$path}' from host '{$this->args[0]}'");
391
392
        // make sure we have valid host details
393
        $hostDetails = $this->getHostDetails();
394
395
        // do we have any app settings?
396 View Code Duplication
        if (!isset($hostDetails->appSettings)) {
397
            $msg = "host has no appSettings at all";
398
            $log->endAction($msg);
399
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
400
        }
401
402
        // do we have the setting that we want?
403
        if (!$hostDetails->appSettings->hasData($path)) {
404
            $msg = "host does not have appSetting '{$path}'";
405
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
406
        }
407
408
        // success
409
        $data = $hostDetails->appSettings->getData($path);
410
411
        // all done
412
        $log->endAction([ "setting is", $data ]);
413
        return $data;
414
    }
415
416
    /**
417
     * @param  string $appName
418
     * @param  string $settingName
419
     * @return mixed
420
     */
421
    protected function getLegacyAppSetting($appName, $settingName)
422
    {
423
        // what are we doing?
424
        $log = usingLog()->startAction("get $settingName for '{$appName}' from host '{$this->args[0]}'");
425
426
        // make sure we have valid host details
427
        $hostDetails = $this->getHostDetails();
428
429
        // do we have any app settings?
430
        if (!isset($hostDetails->appSettings)) {
431
            $log->endAction("host has no appSettings at all");
432
            throw Exceptions::newActionFailedException(__METHOD__);
433
        }
434
        if (!isset($hostDetails->appSettings->$appName)) {
435
            $log->endAction("host has no appSettings for {$appName}");
436
            throw Exceptions::newActionFailedException(__METHOD__);
437
        }
438 View Code Duplication
        if (!isset($hostDetails->appSettings->$appName->$settingName)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
439
            $log->endAction("host has no appSetting '{$settingName}' for {$appName}");
440
            throw Exceptions::newActionFailedException(__METHOD__);
441
        }
442
443
        // yes we do
444
        $value = $hostDetails->appSettings->$appName->$settingName;
445
446
        // log the settings
447
        $printer  = new DataPrinter();
448
        $logValue = $printer->convertToString($value);
449
        $log->endAction("setting for '{$appName}' is '{$logValue}'");
450
451
        // all done
452
        return $value;
453
    }
454
455
    /**
456
     * @param  string $path
457
     * @return mixed
458
     */
459
    public function getStorySetting($path)
460
    {
461
        // what are we doing?
462
        $log = usingLog()->startAction("get storySetting '{$path}' from host '{$this->args[0]}'");
463
464
        // make sure we have valid host details
465
        $hostDetails = $this->getHostDetails();
466
467
        // do we have any app settings?
468 View Code Duplication
        if (!isset($hostDetails->storySettings)) {
469
            $msg = "host has no storySettings at all";
470
            $log->endAction($msg);
471
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
472
        }
473
474
        // do we have the setting that we want?
475
        if (!$hostDetails->storySettings->hasData($path)) {
476
            $msg = "host does not have storySetting '{$path}'";
477
            throw Exceptions::newActionFailedException(__METHOD__, $msg);
478
        }
479
480
        // success
481
        $data = $hostDetails->storySettings->getData($path);
482
483
        // all done
484
        $log->endAction([ "setting is", $data ]);
485
        return $data;
486
    }
487
488
    /**
489
     * @deprecated since v2.4.0
490
     */
491
    public function downloadFile($sourceFilename, $destFilename)
492
    {
493
        return Filesystem::fromHost($this->args[0])->downloadFile($sourceFilename, $destFilename);
494
    }
495
496
    /**
497
     * @deprecated since v2.4.0
498
     */
499
    public function getFileDetails($filename)
500
    {
501
        return Filesystem::fromHost($this->args[0])->getFileDetails($filename);
502
    }
503
504
    /**
505
     * which local folder do we need to be in when working with this host?
506
     *
507
     * @return string
508
     */
509 View Code Duplication
    public function getLocalFolder()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
510
    {
511
        // what are we doing?
512
        $log = usingLog()->startAction("get local folder for host '{$this->args[0]}'");
513
514
        // make sure we have valid host details
515
        $hostDetails = $this->getHostDetails();
516
517
        // does it have a folder defined?
518
        if (isset($hostDetails->dir)) {
519
            $retval = $hostDetails->dir;
520
        }
521
        else {
522
            $retval = getcwd();
523
        }
524
525
        // all done
526
        $log->endAction($retval);
527
        return $retval;
528
    }
529
}
530