|
|
@@ -24,6 +24,7 @@ require_once('system.inc'); /
|
|
|
$cloudInitFiles = array('meta-data', 'network-config', 'user-data'); // provided by Proxmox
|
|
|
$cloudInitLocalPath = '/etc/cloud-init';
|
|
|
$cloudInitMountPoint = '/mnt/cloud-init';
|
|
|
+$changesDetected = false;
|
|
|
/**
|
|
|
* compares the cloud-init files
|
|
|
*
|
|
|
@@ -148,28 +149,34 @@ function searchIfDevice( $mac) {
|
|
|
}
|
|
|
// search and mount the cloud-init image or exit 1
|
|
|
if (!checkCloudInitDevice( $cloudInitFiles, $cloudInitMountPoint)) {
|
|
|
- syslog(LOG_ERR,"cloud-init: no cloud init drive available, skipping...\n");
|
|
|
+ syslog(LOG_ERR,"cloud-init: no cloud init drive available, skipping");
|
|
|
exit(1);
|
|
|
}
|
|
|
-// update the local copy of cloud-init files if there are any changes or exit 0
|
|
|
-if (!(updateCloudInitFiles( $cloudInitFiles, $cloudInitLocalPath, $cloudInitMountPoint))) {
|
|
|
- syslog(LOG_INFO,"cloud-init: cloud init files up to date, skipping...\n");
|
|
|
- exit(0);
|
|
|
+// update the local copy of cloud-init files if there are any changes
|
|
|
+if (updateCloudInitFiles( $cloudInitFiles, $cloudInitLocalPath, $cloudInitMountPoint)) {
|
|
|
+ syslog(LOG_INFO,"cloud-init: cloud init files updated");
|
|
|
+ $changesDetected = true;
|
|
|
}
|
|
|
// parse cloud init configurations
|
|
|
-// $metaData = Spyc::YAMLLoad("$cloudInitLocalPath/$cloudInitFiles[0]"); // meta-data (actually not in use)
|
|
|
-$netData = Spyc::YAMLLoad("$cloudInitLocalPath/$cloudInitFiles[1]"); // network-config
|
|
|
-$userData = Spyc::YAMLLoad("$cloudInitLocalPath/$cloudInitFiles[2]"); // user-data
|
|
|
+// $metaData = Spyc::YAMLLoad("$cloudInitLocalPath/$cloudInitFiles[0]"); // meta-data (actually not in use)
|
|
|
+$netData = Spyc::YAMLLoad("$cloudInitLocalPath/$cloudInitFiles[1]"); // network-config
|
|
|
+$userData = Spyc::YAMLLoad("$cloudInitLocalPath/$cloudInitFiles[2]"); // user-data
|
|
|
// configure nameserver if set
|
|
|
-$ifLastNr=(count($netData['config'])-1); // the YAML parser reurns a crappy array like this
|
|
|
-if (reset($netData['config'][$ifLastNr]) == 'nameserver') { // (
|
|
|
- next($netData['config'][$ifLastNr]); // [type] => nameserver
|
|
|
- $dnsServerCount = 0; // [address] =>
|
|
|
- while($nameserverIP=next($netData['config'][$ifLastNr])) { // [0] => 1.2.3.4
|
|
|
- $config['system']['dnsserver'][$dnsServerCount] = $nameserverIP; // [1] => 4.3.2.1
|
|
|
- $dnsServerCount++; // [search] =>
|
|
|
- } // [2] => mydomain.local
|
|
|
- $config['system']['domain'] = next($netData['config'][$ifLastNr]); // )
|
|
|
+$ifLastNr=(count($netData['config'])-1); // the YAML parser reurns a crappy array like this
|
|
|
+if (reset($netData['config'][$ifLastNr]) == 'nameserver') { // (
|
|
|
+ next($netData['config'][$ifLastNr]); // [type] => nameserver
|
|
|
+ $dnsServerCount = 0; // [address] =>
|
|
|
+ while($nameserverIP=next($netData['config'][$ifLastNr])) { // [0] => 1.2.3.4
|
|
|
+ if ($nameserverIP != $config['system']['dnsserver'][$dnsServerCount]) { // [1] => 4.3.2.1
|
|
|
+ $config['system']['dnsserver'][$dnsServerCount] = $nameserverIP; // [search] =>
|
|
|
+ $changesDetected = true; // [2] => mydomain.local
|
|
|
+ } // )
|
|
|
+ $dnsServerCount++;
|
|
|
+ }
|
|
|
+ if (next($netData['config'][$ifLastNr]) != $config['system']['domain']) {
|
|
|
+ $config['system']['domain'] = current($netData['config'][$ifLastNr]);
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
}
|
|
|
// configure WAN interface
|
|
|
$wanDevice = searchIfDevice( $netData['config'][0]['mac_address']);
|
|
|
@@ -177,12 +184,24 @@ if (!$wanDevice) {
|
|
|
syslog(LOG_ERR,"cloud-init: no WAN device found");
|
|
|
exit(1);
|
|
|
} else {
|
|
|
- $config['interfaces']['wan']['if'] = $wanDevice;
|
|
|
+ if ($wanDevice != $config['interfaces']['wan']['if']) {
|
|
|
+ $config['interfaces']['wan']['if'] = $wanDevice;
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
}
|
|
|
if ($netData['config'][0][0]['type'] == 'static') {
|
|
|
- $config['interfaces']['wan']['ipaddr'] = $netData['config'][0][0]['address'];
|
|
|
- $config['interfaces']['wan']['subnet'] = 32 - log((ip2long($netData['config'][0][0]['netmask']) ^ ip2long('255.255.255.255')) + 1 ,2);
|
|
|
- $config['interfaces']['wan']['gateway'] = $netData['config'][0][0]['gateway'];
|
|
|
+ if ($netData['config'][0][0]['address'] != $config['interfaces']['wan']['ipaddr']) {
|
|
|
+ $config['interfaces']['wan']['ipaddr'] = $netData['config'][0][0]['address'];
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
+ if ((32 - log((ip2long($netData['config'][0][0]['netmask']) ^ ip2long('255.255.255.255')) + 1 ,2)) != $config['interfaces']['wan']['subnet']) {
|
|
|
+ $config['interfaces']['wan']['subnet'] = 32 - log((ip2long($netData['config'][0][0]['netmask']) ^ ip2long('255.255.255.255')) + 1 ,2);
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
+ if ($netData['config'][0][0]['gateway'] != $config['interfaces']['wan']['gateway']) {
|
|
|
+ $config['interfaces']['wan']['gateway'] = $netData['config'][0][0]['gateway'];
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
}
|
|
|
// configure primary LAN device
|
|
|
$lanDevice = searchIfDevice( $netData['config'][1]['mac_address']);
|
|
|
@@ -190,12 +209,24 @@ if (!$lanDevice) {
|
|
|
syslog(LOG_ERR,"cloud-init: no LAN device found");
|
|
|
exit(1);
|
|
|
} else {
|
|
|
- $config['interfaces']['lan']['if'] = $lanDevice;
|
|
|
+ if ($lanDevice != $config['interfaces']['lan']['if']) {
|
|
|
+ $config['interfaces']['lan']['if'] = $lanDevice;
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
}
|
|
|
if ($netData['config'][1][0]['type'] == 'static') {
|
|
|
- $config['interfaces']['lan']['ipaddr'] = $netData['config'][1][0]['address'];
|
|
|
- $config['interfaces']['lan']['subnet'] = 32 - log((ip2long($netData['config'][1][0]['netmask']) ^ ip2long('255.255.255.255')) + 1 ,2);
|
|
|
- $config['interfaces']['lan']['gateway'] = $netData['config'][1][0]['gateway'];
|
|
|
+ if ($netData['config'][1][0]['address'] != $config['interfaces']['lan']['ipaddr']) {
|
|
|
+ $config['interfaces']['lan']['ipaddr'] = $netData['config'][1][0]['address'];
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
+ if ((32 - log((ip2long($netData['config'][1][0]['netmask']) ^ ip2long('255.255.255.255')) + 1 ,2)) != $config['interfaces']['lan']['subnet']) {
|
|
|
+ $config['interfaces']['lan']['subnet'] = 32 - log((ip2long($netData['config'][1][0]['netmask']) ^ ip2long('255.255.255.255')) + 1 ,2);
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
+ if ($netData['config'][1][0]['gateway'] != $config['interfaces']['lan']['gateway']) {
|
|
|
+ $config['interfaces']['lan']['gateway'] = $netData['config'][1][0]['gateway'];
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
}
|
|
|
// configure additional network devices
|
|
|
if ($ifLastNr > 2) {
|
|
|
@@ -206,12 +237,24 @@ if ($ifLastNr > 2) {
|
|
|
syslog(LOG_WARN,"cloud-init: given network device {$netData['config'][$ifNr]['mac_address']} not found");
|
|
|
break;
|
|
|
} else {
|
|
|
- $config['interfaces'][$optDeviceName]['if'] = $optDevice;
|
|
|
+ if ($optDevice != $config['interfaces'][$optDeviceName]['if']) {
|
|
|
+ $config['interfaces'][$optDeviceName]['if'] = $optDevice;
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
}
|
|
|
if ($netData['config'][$ifNr][0]['type'] == 'static') {
|
|
|
- $config['interfaces'][$optDeviceName]['ipaddr'] = $netData['config'][$ifNr][0]['address'];
|
|
|
- $config['interfaces'][$optDeviceName]['subnet'] = 32 - log((ip2long($netData['config'][$ifNr][0]['netmask']) ^ ip2long('255.255.255.255')) + 1 ,2);
|
|
|
- $config['interfaces'][$optDeviceName]['gateway'] = $netData['config'][$ifNr][0]['gateway'];
|
|
|
+ if ($netData['config'][$ifNr][0]['address'] != $config['interfaces'][$optDeviceName]['ipaddr']) {
|
|
|
+ $config['interfaces'][$optDeviceName]['ipaddr'] = $netData['config'][$ifNr][0]['address'];
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
+ if ((32 - log((ip2long($netData['config'][$ifNr][0]['netmask']) ^ ip2long('255.255.255.255')) + 1 ,2)) != $config['interfaces'][$optDeviceName]['subnet']) {
|
|
|
+ $config['interfaces'][$optDeviceName]['subnet'] = 32 - log((ip2long($netData['config'][$ifNr][0]['netmask']) ^ ip2long('255.255.255.255')) + 1 ,2);
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
+ if ($netData['config'][$ifNr][0]['gateway'] != $config['interfaces'][$optDeviceName]['gateway']) {
|
|
|
+ $config['interfaces'][$optDeviceName]['gateway'] = $netData['config'][$ifNr][0]['gateway'];
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -220,11 +263,24 @@ if (isset($userData['ssh_authorized_keys'])) {
|
|
|
foreach ($userData[ssh_authorized_keys] as $sshKey) {
|
|
|
$sshKeys .= "$sshKey\n";
|
|
|
}
|
|
|
- $config['system']['user'][0]['authorizedkeys'] = base64_encode("$sshKeys");
|
|
|
+ if ((base64_encode("$sshKeys")) != $config['system']['user'][0]['authorizedkeys']) {
|
|
|
+ $config['system']['user'][0]['authorizedkeys'] = base64_encode("$sshKeys");
|
|
|
+ $changesDetected = true;
|
|
|
+ }
|
|
|
+}
|
|
|
+if ($userData['hostname'] != $config['system']['hostname']) {
|
|
|
+ $config['system']['hostname'] = $userData['hostname'];
|
|
|
+ $changesDetected = true;
|
|
|
+}
|
|
|
+if ($userData['password'] != $config['system']['user'][0]['bcrypt-hash']) {
|
|
|
+ $config['system']['user'][0]['bcrypt-hash'] = $userData['password'];
|
|
|
+ $changesDetected = true;
|
|
|
+}
|
|
|
+if ($changesDetected == true) {
|
|
|
+ // write the configuration
|
|
|
+ write_config();
|
|
|
+ // finally reboot the system
|
|
|
+ system_reboot_sync();
|
|
|
+} else {
|
|
|
+ syslog(LOG_INFO,"cloud-init: no changes detected");
|
|
|
}
|
|
|
-$config['system']['hostname'] = $userData['hostname'];
|
|
|
-$config['system']['user'][0]['bcrypt-hash'] = $userData['password'];
|
|
|
-// write the configuration
|
|
|
-write_config();
|
|
|
-// finally reboot the system
|
|
|
-system_reboot_sync();
|