-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCobblerApiClient.php
439 lines (361 loc) · 12.7 KB
/
CobblerApiClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
#
# CobblerApiClient.php
#
# Created by Jorge Serrano on 28-04-2015.
# Copyright 2015, Fubra Limited. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# You may obtain a copy of the License at:
# http://www.gnu.org/licenses/gpl-2.0.txt
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
/*****************************
*
* Cobbler PHP API client.
* PHP Client for accesing the Cobbler XMLRPC API, based on the
* Incatuio XMLRPC client library
* Check https://fedorahosted.org/cobbler/wiki/CobblerXmlrpc
* Check also http://scripts.incutio.com/xmlrpc/
*
******************************/
include ('IXRLibrary.php');
class CobblerApiClient {
/**
* Client to the xmlrpc Cobbler API, would be the object handling all the requests to the API
*/
protected $_ixrClient;
/**
* Cobbler user, needed for authenticated calls to the API
*/
protected $_user;
/**
* Cobbler password, needed for authenticated calls to the API
*/
protected $_pass;
/**
* Constructor. Gets the cobbler api parameters, builds the ixr client and stores user and
* password for future auth operations.
*
* @access public
* @param string $host Cobbler hostname or ip address
* @param string $port Cobbler secure/SSL port
* @param string $path Cobbler API path
* @param string $user Cobbler user
* @param string $pass Cobbler password
* @param string $debug (optional) Wether we want to print out stuff from the ixr client or not
*/
public function __construct($host, $port, $path, $user, $pass, $debug=false){
//Check if the Cobbler server is available
$address = gethostbyname($host);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, $address, $port);
if ($result === false) {
throw new \Exception('The Cobbler server is not available, check if the parameters are correct');
}
//Create the secure IXR client and store user/pass for auth operations
$this->_ixrClient = new IXR_ClientSSL($host, $path, $port);
$this->_ixrClient->debug = $debug;
$this->_user = $user;
$this->_pass = $pass;
}
/**
* Performs an authentication request to the Cobbler API and retrieves a token from it.
*
* @access protected
* @return string Auth token that can be used in other requests to the API
*/
protected function auth(){
$this->_ixrClient->query('login',$this->_user,$this->_pass);
$response = $this->_ixrClient->getResponse();
if (is_array($response)) {
throw new Exception('The credentials provided to the client are not right');
}
return $response;
}
/**
* Request the API a system handler, efectively, an identifier to perform updates on existing systems.
*
* @access protected
* @param string $token Auth token, required to validate our request to the API
* @param string $system_name Name of the system we want to manage
* @return string System handle
*/
protected function getSystemHandle($token, $system_name){
$this->_ixrClient->query('get_system_handle', $system_name, $token);
return $this->_ixrClient->getResponse();
}
/**
* Generic method to upgrade kickstart metadata on the system
*
* @access protected
* @param string $token Auth token, required to validate our request to the API
* @param string $system_name Name of the system we want to edit
* @param string $key Name of the metadata item to edit
* @param string $value Value of the metadata item to edit
* @return boolean It returns true if everything went fine
*/
protected function updateMetadata($token, $system_name, $key, $value){
$this->_ixrClient->query('get_system', $system_name);
$system = $this->_ixrClient->getResponse();
if (!is_array($system)) {
throw new Exception('System not found');
}
$metadata = $system['ks_meta'];
$metadata[$key] = $value;
$metadata_string = implode(' ', array_map(function ($v, $k) { return $k . '=' . $v; }, $metadata, array_keys($metadata)));
$handle = $this->getSystemHandle($token, $system_name);
$this->_ixrClient->query('modify_system', $handle,'ks_meta', $metadata_string, $token);
$this->_ixrClient->query('save_system', $handle, $token);
if ($this->_ixrClient->isError()) {
throw new Exception($this->_ixrClient->getErrorMessage());
}
return true;
}
/**
* Generic method for finding systems in Cobbler using one of his attributes
*
* @access protected
* @param string $key Name of the attribute
* @param string $value Value of the attribute
* @return array List of all the systems in Cobbler matching the params
*/
protected function findSystem($key, $value){
$this->_ixrClient->query('find_system', array($key => $value));
return $this->_ixrClient->getResponse();
}
/**
* Determine if there is a system in Cobbler with some specific attribute value
*
* @access protected
* @param string $key Name of the attribute
* @param string $value Value of the attribute
* @return boolean True if it can find a system matching those params, false otherwise
*/
protected function existsSystem($key, $value){
$systems = $this->findSystem($key, $value);
return sizeof($systems) > 0;
}
/**
* Request Cobbler API for list of systems
*
* @access public
* @return array List all the systems in Cobbler
*/
public function listSystems(){
$token = $this->auth();
$this->_ixrClient->query('get_systems');
if ($this->_ixrClient->isError()) {
throw new Exception($this->_ixrClient->getErrorMessage());
}
return $this->_ixrClient->getResponse();
}
/**
* Request Cobbler API for list of systems
*
* @access public
* @return array List all the systems in Cobbler
*/
public function listDistros(){
$token = $this->auth();
$this->_ixrClient->query('get_distros');
if ($this->_ixrClient->isError()) {
throw new Exception($this->_ixrClient->getErrorMessage());
}
return $this->_ixrClient->getResponse();
}
/**
* Request Cobbler API for list of profiles
*
* @access public
* @return array List all the profiles in Cobbler
*/
public function listProfiles(){
$token = $this->auth();
$this->_ixrClient->query('get_profiles');
if ($this->_ixrClient->isError()) {
throw new Exception($this->_ixrClient->getErrorMessage());
}
return $this->_ixrClient->getResponse();
}
/**
* Request Cobbler API for list of images
*
* @access public
* @return array List all the images in Cobbler
*/
public function listImages(){
$token = $this->auth();
$this->_ixrClient->query('get_images');
if ($this->_ixrClient->isError()) {
throw new Exception($this->_ixrClient->getErrorMessage());
}
return $this->_ixrClient->getResponse();
}
/**
* Create a new system in Cobbler
*
* @access public
* @param $params Array of parameters of the systems, name, host, mac and profile are compulsory
* @return string The id of the new system
*/
public function createSystem($params = array()){
$token = $this->auth();
//Check the compulsory fields
if (empty($params['name'])) {
throw new \Exception('Missing argument: `name` is required.');
}
if (empty($params['host'])) {
throw new \Exception('Missing argument: `host` is required.');
}
if (empty($params['mac'])) {
throw new \Exception('Missing argument: `mac` is required.');
}
if (empty($params['profile'])) {
throw new \Exception('Missing argument: `profile` is required.');
}
$name = $params['name'];
$host = $params['host'];
$mac = $params['mac'];
$profile = $params['profile'];
if (empty($params['interface'])) {
$interfaceName = 'eth0';
}else{
$interfaceName = $params['interface'];
}
$ip = isset($params['ip']) ? $params['ip'] : '';
$gateway = isset($params['gateway']) ? $params['gateway'] : '';
$dnsnames = isset($params['dnsnames']) ? $params['dnsnames'] : '';
//Check the unique fields
if ($this->existsSystem('name',$name)){
throw new Exception('There is already a system using that name');
}
if ($this->existsSystem('hostname',$host)){
throw new Exception('There is already a system using that hostname');
}
if ($this->existsSystem('mac_address',$mac)){
throw new Exception('There is already a system using that mac address');
}
$this->_ixrClient->query('new_system',$token);
$system_id = $this->_ixrClient->getResponse();
$this->_ixrClient->query('modify_system', $system_id, 'name', $name, $token);
$this->_ixrClient->query('modify_system', $system_id, 'hostname', $host, $token);
$this->_ixrClient->query('modify_system', $system_id, 'profile', $profile, $token);
$interface = array();
$interface['macaddress-'.$interfaceName] = $mac;
$interface['ipaddress-'.$interfaceName] = $ip;
$interface['gateway-'.$interfaceName] = $gateway;
$interface['dnsname-'.$interfaceName] = $dnsnames;
$this->_ixrClient->query('modify_system', $system_id, 'modify_interface', $interface, $token);
$this->_ixrClient->query('save_system', $system_id, $token);
if ($this->_ixrClient->isError()) {
$this->deleteSystem($name);
throw new Exception($this->_ixrClient->getErrorMessage());
}
return $system_id;
}
/**
* Request Cobbler API to delete an existing system
*
* @access public
* @param string $system_name Name of the system to delete
* @return boolean True if everything goes fine, false otherwise
*/
public function deleteSystem($system_name){
$token = $this->auth();
$this->_ixrClient->query('remove_system', $system_name, $token);
if ($this->_ixrClient->isError()) {
throw new Exception($this->_ixrClient->getErrorMessage());
}
return true;
}
/**
* Request Cobbler API to enable netbooting on an existing system
*
* @access public
* @param string $system_name Name of the system
* @return boolean True if everything goes fine, false otherwise
*/
public function enableNetboot($system_name){
$token = $this->auth();
$handle = $this->getSystemHandle($token, $system_name);
$this->_ixrClient->query('modify_system', $handle,'netboot_enabled', True, $token);
$this->_ixrClient->query('save_system', $handle, $token);
if ($this->_ixrClient->isError()) {
throw new Exception($this->_ixrClient->getErrorMessage());
}
return true;
}
/**
* Request Cobbler API to disable netbooting on an existing system
*
* @access public
* @param string $system_name Name of the system
* @return boolean True if everything goes fine, false otherwise
*/
public function disableNetboot($system_name){
$token = $this->auth();
$handle = $this->getSystemHandle($token, $system_name);
$this->_ixrClient->query('modify_system', $handle,'netboot_enabled', False, $token);
$this->_ixrClient->query('save_system', $handle, $token);
if ($this->_ixrClient->isError()) {
throw new Exception($this->_ixrClient->getErrorMessage());
}
return true;
}
/**
* Request Cobbler API to set up a SSH key for an exiting system. If the system has already a SSH key,
* this will update it and change it in the next reprovision operation.
*
* @access public
* @param string $system_name Name of the system
* @param string $key SSH key to add to the system, without comments or spaces.
* @return boolean True if everything goes fine, false otherwise
*/
public function setSSHKey($system_name, $key) {
$token = $this->auth();
$this->updateMetadata($token, $system_name, 'ssh_key', trim($key));
return true;
}
/**
* Request Cobbler API to set up a password for the root user on an exiting system.
*
* @access public
* @param string $system_name Name of the system
* @param string $password Plain text password to add to the system
* @return boolean True if everything goes fine, false otherwise
*/
public function setPassword($system_name, $password) {
$token = $this->auth();
$password_crypted = crypt($password);
$this->updateMetadata($token, $system_name, 'custom_password', $password_crypted);
return true;
}
/**
* Request Cobbler API for the current status of the server, to check if is active or being installed
*
* @access public
* @param string $ip IP assigned to the server
* @return string State of the server
*/
public function getStatus($ip) {
$token = $this->auth();
$this->_ixrClient->query('get_status' ,'normal', $token);
$status = $this->_ixrClient->getResponse();
if ($this->_ixrClient->isError()) {
throw new Exception($this->_ixrClient->getErrorMessage());
}
if (array_key_exists($ip, $status)){
return end ($status[$ip]);
}else{
return 'unknown';
}
}
}