1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dock\Docker\Machine; |
4
|
|
|
|
5
|
|
|
use Dock\IO\ProcessRunner; |
6
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
7
|
|
|
|
8
|
|
|
class DockerMachineCli implements Machine |
9
|
|
|
{ |
10
|
|
|
const DEFAULT_MACHINE_NAME = 'inviqa-dock-cli'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var ProcessRunner |
14
|
|
|
*/ |
15
|
|
|
private $processRunner; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param ProcessRunner $processRunner |
24
|
|
|
* @param string $name |
25
|
|
|
*/ |
26
|
|
|
public function __construct(ProcessRunner $processRunner, $name) |
27
|
|
|
{ |
28
|
|
|
$this->processRunner = $processRunner; |
29
|
|
|
$this->name = $name; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function isRunning() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$process = $this->processRunner->run('docker-machine status '.$this->name, false); |
38
|
|
|
if (!$process->isSuccessful()) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return strpos($process->getOutput(), 'Running') === 0; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function start() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
try { |
51
|
|
|
$this->processRunner->run('docker-machine start '.$this->name); |
52
|
|
|
} catch (ProcessFailedException $e) { |
53
|
|
|
throw new MachineException($e->getMessage(), $e->getCode(), $e); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function stop() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
$this->processRunner->run('docker-machine stop '.$this->name); |
64
|
|
|
} catch (ProcessFailedException $e) { |
65
|
|
|
throw new MachineException($e->getMessage(), $e->getCode(), $e); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function getIp() |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
|
|
$ip = $this->processRunner->run('docker-machine ip '.$this->name)->getOutput(); |
76
|
|
|
} catch (ProcessFailedException $e) { |
77
|
|
|
throw new MachineException($e->getMessage(), $e->getCode(), $e); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$ip = trim($ip); |
81
|
|
|
if (strpos($ip, 'not running') !== false) { |
82
|
|
|
throw new MachineException($ip); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $ip; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function isCreated() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$process = $this->processRunner->run('docker-machine status '.$this->name, false); |
94
|
|
|
if(!$process->isSuccessful() || strpos($process->getOutput(), 'Error') === 0) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function create() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
try { |
107
|
|
|
$this->processRunner->run('docker-machine create --driver virtualbox '.$this->name); |
108
|
|
|
} catch (ProcessFailedException $e) { |
109
|
|
|
throw new MachineException($e->getMessage(), $e->getCode(), $e); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getEnvironmentDeclaration() |
117
|
|
|
{ |
118
|
|
|
return sprintf('eval "$(docker-machine env %s)"', $this->name).PHP_EOL; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.