Skip to content

Commit 725bca9

Browse files
committed
Add exec commands
1 parent dedc587 commit 725bca9

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

src/Client.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,72 @@ public function containerRemove($container, $v = false, $force = false)
215215
return $this->browser->delete($this->url('/containers/%s?v=%u&force=%u', $container, $v, $force))->then(array($this->parser, 'expectEmpty'));
216216
}
217217

218+
/**
219+
* Sets up an exec instance in a running container id
220+
*
221+
* @param string $container container ID
222+
* @param array $config `array('Cmd' => 'date')` (see link)
223+
* @return Promise Promise<array> with exec ID in the form of `array("Id" => $execId)`
224+
* @link https://docs.docker.com/reference/api/docker_remote_api_v1.15/#exec-create
225+
*/
226+
public function execCreate($container, $config)
227+
{
228+
return $this->postJson($this->url('/containers/%s/exec', $container), $config)->then(array($this->parser, 'expectJson'));
229+
}
230+
231+
/**
232+
* Starts a previously set up exec instance id.
233+
*
234+
* If detach is true, this API returns after starting the exec command.
235+
* Otherwise, this API sets up an interactive session with the exec command.
236+
*
237+
* @param string $exec exec ID
238+
* @param array $config (see link)
239+
* @return Promise Promise<array> stream of message objects
240+
* @link https://docs.docker.com/reference/api/docker_remote_api_v1.15/#exec-start
241+
*/
242+
public function execStart($exec, $config)
243+
{
244+
return $this->postJson($this->url('/exec/%s/start', $exec), $config)->then(array($this->parser, 'expectJson'));
245+
}
246+
247+
/**
248+
* Resizes the tty session used by the exec command id.
249+
*
250+
* This API is valid only if tty was specified as part of creating and starting the exec command.
251+
*
252+
* @param string $exec exec ID
253+
* @param int $w TTY width
254+
* @param int $h TTY height
255+
* @return Promise Promise<null>
256+
* @link https://docs.docker.com/reference/api/docker_remote_api_v1.15/#exec-resize
257+
*/
258+
public function execResize($exec, $w, $h)
259+
{
260+
return $this->browser->get($this->url('/exec/%s/resize?w=%u&h=%u', $exec, $w, $h))->then(array($this->parser, 'expectEmpty'));
261+
}
262+
218263
private function url($url)
219264
{
220265
$args = func_get_args();
221266
array_shift($args);
222267

223268
return $this->url . vsprintf($url, $args);
224269
}
270+
271+
private function postJson($url, $data)
272+
{
273+
$body = $this->json($data);
274+
$headers = array('Content-Type' => 'application/json', 'Content-Length' => strlen($body));
275+
276+
return $this->browser->post($url, $headers, $body);
277+
}
278+
279+
private function json($data)
280+
{
281+
if ($data === array()) {
282+
return '{}';
283+
}
284+
return json_encode($data);
285+
}
225286
}

tests/ClientTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,31 @@ public function testContainerResize()
113113
$this->expectPromiseResolveWith('', $this->client->containerResize(123, 800, 600));
114114
}
115115

116+
public function testExecCreate()
117+
{
118+
$json = array();
119+
$config = array();
120+
$this->expectRequestFlow('post', '/containers/123/exec', $this->createResponseJson($json), 'expectJson');
121+
122+
$this->expectPromiseResolveWith($json, $this->client->execCreate(123, $config));
123+
}
124+
125+
public function testExecStart()
126+
{
127+
$json = array();
128+
$config = array();
129+
$this->expectRequestFlow('post', '/exec/123/start', $this->createResponseJson($json), 'expectJson');
130+
131+
$this->expectPromiseResolveWith($json, $this->client->execStart(123, $config));
132+
}
133+
134+
public function testExecResize()
135+
{
136+
$this->expectRequestFlow('get', '/exec/123/resize?w=800&h=600', $this->createResponse(), 'expectEmpty');
137+
138+
$this->expectPromiseResolveWith('', $this->client->execResize(123, 800, 600));
139+
}
140+
116141
private function expectRequestFlow($method, $url, Response $response, $parser)
117142
{
118143
$return = (string)$response->getBody();

0 commit comments

Comments
 (0)