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 (#1220)
by Barry vd.
03:05
created

Client::initMultiplexing()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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