|
1
|
|
|
<?php namespace G2R\Rundeck; |
|
2
|
|
|
|
|
3
|
|
|
use G2R\Config\RundeckConfig; |
|
4
|
|
|
use G2R\Exception\Exception; |
|
5
|
|
|
|
|
6
|
|
|
class JobRunner |
|
7
|
|
|
{ |
|
8
|
|
|
protected $config; |
|
9
|
|
|
|
|
10
|
4 |
|
public function __construct(RundeckConfig $config) |
|
11
|
|
|
{ |
|
12
|
4 |
|
$this->config = $config; |
|
13
|
4 |
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* return the Rundeck configuration |
|
17
|
|
|
* |
|
18
|
|
|
* @return RundeckConfig |
|
19
|
|
|
*/ |
|
20
|
2 |
|
public function getConfig() |
|
21
|
|
|
{ |
|
22
|
2 |
|
return $this->config; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Send the request to rundesk |
|
27
|
|
|
* |
|
28
|
|
|
* @param $jobId |
|
29
|
|
|
* @param array $parameters |
|
30
|
|
|
*/ |
|
31
|
|
|
public function run($jobId, array $parameters = []) |
|
32
|
|
|
{ |
|
33
|
|
|
$method = (empty($parameters)) ? 'GET' : 'POST'; |
|
34
|
|
|
|
|
35
|
|
|
$options = [ |
|
36
|
|
|
'http' => [ |
|
37
|
|
|
'header' => "X-Rundeck-Auth-Token: {$this->config['token']}\r\n", |
|
38
|
|
|
'method' => $method |
|
39
|
|
|
] |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
if ($method == 'POST') { |
|
43
|
|
|
$options ['http']['header'] .= "Content-type: application/x-www-form-urlencoded\r\n"; |
|
44
|
|
|
$options ['http']['content'] = http_build_query($parameters); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$context = stream_context_create($options); |
|
48
|
|
|
|
|
49
|
|
|
return file_get_contents($this->getApiUrl($jobId), false, $context); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Return the formatted url for a GET or POST rundeck request to run a job |
|
54
|
|
|
* |
|
55
|
|
|
* http://rundeck.org/docs/api/index.html#running-a-job |
|
56
|
|
|
* GET /api/1/job/[ID]/run |
|
57
|
|
|
* POST /api/12/job/[ID]/executions |
|
58
|
|
|
* |
|
59
|
|
|
* @param $jobId |
|
60
|
|
|
* @param string $method |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
* @throws Exception |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function getApiUrl($jobId, $method = 'GET') |
|
66
|
|
|
{ |
|
67
|
|
|
return |
|
68
|
2 |
|
"http" . (($this->config['ssl']) ? 's' : '') . "://" . |
|
69
|
2 |
|
$this->config['host'] . ':' . $this->config['port'] . '/api/' . |
|
70
|
2 |
|
$this->config['api_version'] . '/job/' . $jobId . '/' . |
|
71
|
2 |
|
((strcasecmp($method, 'get') == 0) ? 'run' : 'executions'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|