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.
Passed
Pull Request — master (#1219)
by Barry vd.
03:44
created

Client::initMultiplexing()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 22
ccs 0
cts 10
cp 0
crap 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Ssh;
9
10
use Deployer\Exception\RuntimeException;
11
use Deployer\Host\Host;
12
use Deployer\Utility\ProcessOutputPrinter;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Process\Process;
15
16
class Client
17
{
18
    /**
19
     * @var OutputInterface
20
     */
21
    private $output;
22
23
    /**
24
     * @var ProcessOutputPrinter
25
     */
26
    private $pop;
27
28
    /**
29
     * @var bool
30
     */
31
    private $multiplexing;
32
33 6
    public function __construct(OutputInterface $output, ProcessOutputPrinter $pop, bool $multiplexing)
34
    {
35 6
        $this->output = $output;
36 6
        $this->pop = $pop;
37 6
        $this->multiplexing = $multiplexing;
38 6
    }
39
40
    /**
41
     * @param Host $host
42
     * @param string $command
43
     * @param array $config
44
     * @return string
45
     * @throws RuntimeException
46
     */
47
    public function run(Host $host, string $command, array $config = [])
48
    {
49
        $hostname = $host->getHostname();
50
        $defaults = [
51
            'timeout' => 300,
52
            'tty' => false,
53
        ];
54
        $config = array_merge($defaults, $config);
55
56
        $this->pop->command($hostname, $command);
57
58
        $sshArguments = $host->getSshArguments();
59
        $become = $host->has('become') ? 'sudo -u ' . $host->get('become') : '';
60
61
        // When tty need to be allocated, don't use multiplexing,
62
        // and pass command without bash allocation on remote host.
63
        if ($config['tty']) {
64
            $this->output->write(''); // Notify OutputWatcher
65
            $sshArguments = $sshArguments->withFlag('-tt');
66
            $command = escapeshellarg($command);
67
68
            $ssh = "ssh $sshArguments $host $command";
69
            $process = new Process($ssh);
70
            $process
71
                ->setTimeout($config['timeout'])
72
                ->setTty(true)
73
                ->mustRun();
74
75
            return $process->getOutput();
76
        }
77
78
        if ($host->isMultiplexing() === null ? $this->multiplexing : $host->isMultiplexing()) {
79
            $sshArguments = $this->initMultiplexing($host);
80
        }
81
82
        $ssh = "ssh $sshArguments $host $become 'bash -s; printf \"[exit_code:%s]\" $?;'";
83
84
        $process = new Process($ssh);
85
        $process
86
            ->setInput($command)
87
            ->setTimeout($config['timeout']);
88
89
        $process->run($this->pop->callback($hostname));
90
91
        $output = $this->pop->filterOutput($process->getOutput());
92
        $exitCode = $this->parseExitStatus($process);
93
94
        if ($exitCode !== 0) {
95
            throw new RuntimeException(
96
                $hostname,
97
                $command,
98
                $exitCode,
99
                $output,
100
                $process->getErrorOutput()
101
            );
102
        }
103
104
        return $output;
105
    }
106
107
    private function parseExitStatus(Process $process)
108
    {
109
        $output = $process->getOutput();
110
        preg_match('/\[exit_code:(.*?)\]/', $output, $match);
111
112
        if (!isset($match[1])) {
113
            return -1;
114
        }
115
116
        $exitCode = (int)$match[1];
117
        return $exitCode;
118
    }
119
120
    private function initMultiplexing(Host $host)
121
    {
122
        $sshArguments = $host->getSshArguments()->withMultiplexing($host);
123
124
        $process = new Process("ssh -O check $sshArguments $host 2>&1");
125
        $process->run();
126
127
        if (!preg_match('/Master running/', $process->getOutput())) {
128
            if ($this->output->isVeryVerbose()) {
129
                $this->pop->writeln(Process::OUT, $host->getHostname(), 'ssh multiplexing initialization');
130
            }
131
            
132
            // Open master connection explicit,
133
            // ControlMaster=auto could not working
134
            (new Process("ssh -M $sshArguments $host"))->start();
135
136
            // Delay to wait connection established
137
            sleep(1);
138
        }
139
140
        return $sshArguments;
141
    }
142
}
143