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

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
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\InitializationException;
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
            $process = new Process("ssh -M $sshArguments $host");
133
            $process->start();
134
135
            $attempts = 0;
136
            while (!$this->isMultiplexingInitialized($host, $sshArguments)) {
137
                if ($attempts++ > 30) {
138
                    throw new InitializationException('Wait time exceeded for ssh multiplexing initialization');
139
                }
140
141
                if (!$process->isRunning()) {
142
                    throw new InitializationException('Failed to initialize ssh multiplexing');
143
                }
144
145
                // Delay to check again if the connection is established
146
                sleep(1);
147
            }
148
        }
149
150
        return $sshArguments;
151
    }
152
153
    private function isMultiplexingInitialized(Host $host, Arguments $sshArguments)
154
    {
155
        $process = new Process("ssh -O check $sshArguments $host 2>&1");
156
        $process->run();
157
158
        return (bool) preg_match('/Master running/', $process->getOutput());
159
    }
160
}
161