Răsfoiți Sursa

bugfixing and add dhcp wan configuring

andre 5 ani în urmă
părinte
comite
91301bf6e0
1 a modificat fișierele cu 37 adăugiri și 14 ștergeri
  1. 37 14
      proxmox_cloud-init.php

+ 37 - 14
proxmox_cloud-init.php

@@ -54,8 +54,13 @@ function createDir( $path) {
 }
 /**
  * restoreConfiguration removes all local cloudinit files and starts a reset to factory defaults
+ * 
+ * @param string $cloudInitLocalPath
+ * @param array $cloudInitFiles
+ * @param string $cloudInitControl
+ * @param string $cloudInitResetControl
  */
-function restoreConfiguration() {
+function restoreConfiguration( $cloudInitLocalPath, $cloudInitFiles, $cloudInitControl, $cloudInitResetControl) {
 	unlink("$cloudInitLocalPath/$cloudInitControl");
 	unlink("$cloudInitLocalPath/$cloudInitResetControl");
 	foreach ( $cloudInitFiles as $cloudInitFile) {
@@ -69,11 +74,15 @@ function restoreConfiguration() {
  * 
  * $cloudInitControl contains "init" after a change run -> controlEmergencyRun outs the ciúrrent timestamp into $cloudInitControl
  * 
+ * @param string $cloudInitLocalPath
+ * @param string $cloudInitControl
+ * @param string $cloudInitResetControl
+ * 
  * @return int 0 to trigger a skip run
  * @return int 1 to trigger an emergency run (set again all cloudinit settings)
  * @return int 3 to trigger a reset to factory defaults run (reset all cloudinit & pfSense changes)
  */
-function controlEmergencyRun() {
+function controlEmergencyRun( $cloudInitLocalPath, $cloudInitControl, $cloudInitResetControl) {
 	if ((file_get_contents( "$cloudInitLocalPath/$cloudInitControl")) == "init") {
 		file_put_contents( "$cloudInitLocalPath/$cloudInitControl", time());
 		return 0;
@@ -104,9 +113,13 @@ function controlEmergencyRun() {
  * update the local copy in case of changes
  * or if the local copy does not exist
  * 
+ * @param string $cloudInitMountPoint
+ * @param string $cloudInitLocalPath
+ * @param array $cloudInitFiles
+ * 
  * @return bool true in case of updates, false in case of all is up to date 
  */
-function updateCloudInitFiles() {
+function updateCloudInitFiles( $cloudInitMountPoint, $cloudInitLocalPath, $cloudInitFiles) {
 	$cloudInitFileDiff = false;
 	// check for updated config files and update the local copy in case of differs
 	foreach ( $cloudInitFiles as $cloudInitFile ) {
@@ -125,11 +138,14 @@ function updateCloudInitFiles() {
 /**
  * checkCloudInitFiles probes existence of all necessary cloudinit files
  * 
- * @return bool true in case of all files are in place or false if something missing
+ * @param string $cloudInitMountPoint
+ * @param array $cloudInitFiles
+ * 
+ * @return bool true in case of all files are in place or false if someone missing
  */
-function checkCloudInitFiles() {
+function checkCloudInitFiles( $cloudInitMountPoint, $cloudInitFiles) {
 	foreach($cloudInitFiles as $cloudInitFile) {
-		if (!file_exists("$cloudInitPath/$cloudInitFile")) {
+		if (!file_exists("$cloudInitMountPoint/$cloudInitFile")) {
 			return false;
 		}
 	}
@@ -141,13 +157,16 @@ function checkCloudInitFiles() {
  * probes any attached cd device for existing cloudinit files
  * mounts the drive to /mnt/cloudinit and probes that all neccessary cloudinit files exist
  * 
+ * @param string $cloudInitMountPoint
+ * @param array $cloudInitFiles
+ * 
  * @return bool true in case of success, fals in case of no cloudinit drive could found
  */
-function checkCloudInitDevice() {
+function checkCloudInitDevice( $cloudInitMountPoint, $cloudInitFiles) {
 	// get attached cd devices
 	preg_match_all( "/.*cd[0-9] /", file_get_contents('/var/run/dmesg.boot'), $cdDeviceList);
 	if (empty($cdDeviceList[0])) {
-		syslog(LOG_ERR,"cloudinit failed: No cloudinit drive found");
+		syslog(LOG_ERR,"cloudinit: no cloudinit drive found");
 		exit(1);
 	}
 	// check cloudinit iso is mounted or try to mount the cloudinit medium
@@ -158,7 +177,7 @@ function checkCloudInitDevice() {
 			// try to mount the cd
 			$mount_err = exec("mount_cd9660 /dev/$cdDevice $cloudInitMountPoint", $sys_msg, $sys_err_no);
 			if (!$sys_err_no) {
-				if (checkCloudInitFiles( $cloudInitFiles, $cloudInitMountPoint)) {
+				if (checkCloudInitFiles( $cloudInitMountPoint, $cloudInitFiles)) {
 					syslog(LOG_INFO,"cloudinit: found cloud init drive on $cdDevice mounted at $cloudInitMountPoint/");
 					return true;
 				} else {
@@ -170,7 +189,7 @@ function checkCloudInitDevice() {
 				}
 			}
 		} else { //already mounted (but not by us)
-			if (checkCloudInitFiles( $cloudInitFiles, $cloudInitMountPoint)) {
+			if (checkCloudInitFiles( $cloudInitMountPoint, $cloudInitFiles)) {
 				syslog(LOG_INFO,"cloudinit: found cloud init drive on $cdDevice at $cloudInitMountPoint/");
 				return true;
 			} else {
@@ -202,13 +221,13 @@ createDir( $cloudInitMountPoint);
 // create local folder for config & control files
 createDir( $cloudInitLocalPath);
 // search and mount the cloudinit image or exit 1
-if (!checkCloudInitDevice()) {
+if (!checkCloudInitDevice( $cloudInitMountPoint, $cloudInitFiles)) {
 	syslog(LOG_ERR,"cloudinit: no cloud init drive available, skipping...\n");
 	exit(1);
 }
 // probe for special run modes
-if (!updateCloudInitFiles()) {
-	switch (controlEmergencyRun()) {
+if (!updateCloudInitFiles( $cloudInitMountPoint, $cloudInitLocalPath, $cloudInitFiles)) {
+	switch (controlEmergencyRun( $cloudInitLocalPath, $cloudInitControl, $cloudInitResetControl)) {
 		case 0:
 			syslog(LOG_INFO,"cloudinit: cloud init files up to date, skipping...\n");
 			exit(0);
@@ -218,7 +237,7 @@ if (!updateCloudInitFiles()) {
 		break;
 		case 2:
 			syslog(LOG_INFO,"cloudinit: reset run triggered, restore cloudinit default configuration!\n");
-			restoreConfiguration();
+			restoreConfiguration( $cloudInitLocalPath, $cloudInitFiles, $cloudInitControl, $cloudInitResetControl);
 		break;
 	}
 }
@@ -249,6 +268,10 @@ if ($netData['config'][0][0]['type'] == 'static') {
 	$config['interfaces']['wan']['ipaddr'] = $netData['config'][0][0]['address'];
 	$config['interfaces']['wan']['subnet'] = calcCIDR( $netData['config'][0][0]['netmask']);
 	$config['interfaces']['wan']['gateway'] = $netData['config'][0][0]['gateway'];
+} elseif ($netData['config'][0][0]['type'] == 'dhcp4') {
+	$config['interfaces']['wan']['ipaddr'] = 'dhcp';
+	unset( $config['interfaces']['wan']['subnet']);
+	unset( $config['interfaces']['wan']['gateway']);
 }
 // configure primary LAN device
 $lanDevice = searchIfDevice( $netData['config'][1]['mac_address']);