diff options
author | Suren A. Chilingaryan <csa@suren.me> | 2018-07-04 23:08:02 +0200 |
---|---|---|
committer | Suren A. Chilingaryan <csa@suren.me> | 2018-07-04 23:08:02 +0200 |
commit | 127a9061b2af28e3cb59f67b5cf7268b59d312cd (patch) | |
tree | d8382b48f764181aaf87bfcbd741b0a3596fb4c8 /index.php | |
download | kickstart-127a9061b2af28e3cb59f67b5cf7268b59d312cd.tar.gz kickstart-127a9061b2af28e3cb59f67b5cf7268b59d312cd.tar.bz2 kickstart-127a9061b2af28e3cb59f67b5cf7268b59d312cd.tar.xz kickstart-127a9061b2af28e3cb59f67b5cf7268b59d312cd.zip |
Initial release
Diffstat (limited to 'index.php')
-rw-r--r-- | index.php | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/index.php b/index.php new file mode 100644 index 0000000..61f4650 --- /dev/null +++ b/index.php @@ -0,0 +1,226 @@ +<?php +#$VERBOSE = 1; + +require "cidr.php"; +require "config.php"; + +function get_templates(&$server) { + $templates = array(); + foreach (glob("{$server['templates']}/*") as $ffn) { + $fn = basename($ffn); + $templates[$fn] = file_get_contents($ffn); + } + + if ((isset($server["disk2"]))&&($server["raid"] !== false)) { + $raid = $server["raid"]; + if (preg_match("/RAID(\d+)/i", $raid, $m)) $raid = $m[1]; + $templates['storage_config'] = $templates["raid{$raid}"]; + } else { + $templates['storage_config'] = $templates['hdd']; + } + + unset($server['templates']); + + return $templates; +} + +function get_server($srvid) { + global $SERVER_LIST; + global $NETMASK; + + $server = $SERVER_LIST[$srvid]; + + if (!isset($server["fqdn"])) + $server["fqdn"] = "{$srvid}.{$server['domain']}"; + + if (!isset($server["ip"])) { + if (isset($server["base_ip"])&&($server["base_net"])) { + if (preg_match("/(\d+)$/", $srvid, $m)) { + $server["ip"] = $server["base_net"] . ($server["base_ip"] + $m[1]); + } else { + $server["ip"] = false; + } + } else { + $ip = gethostbyname($server["fqdn"]); + if ($ip == $server["fqdn"]) + $server["ip"] = false; + else + $server["ip"] = $ip; + } + } + + if ($server["ip"]) { + $server["skip_ip"] = false; + } else { + $server["skip_ip"] = true; + } + + if (!isset($server["netmask"])) { + if (isset($server["cidr"])) { + $server["netmask"] = $NETMASK[$server["cidr"]]; + } + } + + $eths = $server["eth"]; + if ((isset($eths[0]))&&(!isset($server["ethdev"]))) + $server["ethdev"] = $eths[0]; + if ((isset($eths[1]))&&(!isset($server["ethdev2"]))) + $server["ethdev2"] = $eths[1]; + + $disks = explode(",", $server["sysdisks"]); + if (!isset($server["bootdisk"])) + $server["bootdisk"] = $disks[0]; + + if (!isset($server["disk1"])) + $server["disk1"] = $disks[0]; + + if ((isset($disks[1]))&&(!isset($server["disk2"]))) + $server["disk2"] = $disks[1]; + + unset($server["eths"]); + unset($server["macs"]); + unset($server["headers"]); + + return $server; +} + +function find_mac($macs, $mac_header) { + if (!is_array($macs)) $macs = array($macs); + + foreach ($macs as $mac) { + if (preg_match("/$mac/", $mac_header)) + return true; + } + return false; +} + +function find_server_by_mac($mac_header) { + global $SERVER_LIST; + + foreach ($SERVER_LIST as $srvid => $server) { + if (find_mac($server['macs'], $mac_header)) + return get_server($srvid); + } + return false; +} + +function find_server_by_header($http_header, $value) { + global $SERVER_LIST; + + foreach ($SERVER_LIST as $srvid => $server) { + if ((is_array($server["headers"]))&&(isset($server["headers"][$http_header]))) { + $expected = $server["headers"][$http_header]; + if (!is_array($expected)) $expected = array($expected); + foreach ($expected as $re) { + if (preg_match("/$re/", $value)) + return get_server($srvid); + } + } + } + return false; +} + +function find_server() { + global $DEFAULT_SERVER; + global $_SERVER; + + $headers = getallheaders(); + for ($i = 0; $i < 10; $i++) { + $if = "X-RHN-Provisioning-MAC-$i"; + if (!isset($headers[$if])) break; + + $server = find_server_by_mac($headers[$if]); + if ($server) return $server; + } + + foreach ($_SERVER as $header => $value) { + $server = find_server_by_header($header, $value); + if ($server) return $server; + } + + if (isset($DEFAULT_SERVER)) { + return get_server($DEFAULT_SERVER); + } + + return false; +} + + + + +$server = find_server(); + +$f = fopen("/srv/www/htdocs/ands/logs/kickstart.log", "a+"); +if ($f) { + fprintf($f, "%s %s\n", $_SERVER["REMOTE_ADDR"], ($server?$server["fqdn"]:"-")); + fclose($f); +} + +if (!$server) { + $f = fopen("/srv/www/htdocs/ands/logs/kickstart-new.log", "a+"); + if ($f) { + fwrite($f, print_r($_SERVER, true)); + fwrite($f, print_r(getallheaders(), true)); + fclose($f); + } + return; +} + +$templates = get_templates($server); + +$ks = file_get_contents($server["ks"]); + + +$patterns=array(); $values=array(); +foreach ($templates as $key => $val) { + array_push($patterns, "/@" . strtoupper($key) . "@/"); + array_push($values, $val); +} +$ks = preg_replace($patterns, $values, $ks); + + +$patterns=array(); $values=array(); +foreach ($server as $key => $val) { + array_push($patterns, "/@" . strtoupper($key) . "@/"); + if (is_bool($val)) { + if (preg_match("/^skip_/", $key)) { + if ($val) $val = "#"; + else $val = ""; + } elseif (preg_match("/^enable_/", $key)) { + if ($val) $val = ""; + else $val = "#"; + } else { + if ($val) $val = 1; + else $val = 0; + } + } + array_push($values, $val); +} + +foreach ($DISTRIBUTIONS as $distrib) { + array_push($patterns, "/@" . strtoupper($distrib) . "@/"); + array_push($values, (strcasecmp($distrib, $DISTRIBUTION)?"#":"")); +} + +//print_r($patterns); + +$ks = preg_replace($patterns, $values, $ks); + +if ($VERBOSE) { + $f = fopen("/srv/www/htdocs/ands/logs/kickstart-verbose.log", "a+"); + if ($f) { + fwrite($f, "-----------------------------------------------------\n"); + fwrite($f, print_r($server, true)); + fwrite($f, print_r($_SERVER, true)); + fwrite($f, print_r(getallheaders(), true)); + fwrite($f, "-----------------------------------------------------\n"); + fwrite($f, $ks); + fwrite($f, "=====================================================\n"); + fclose($f); + } +} + +header("Content-type: text/plain"); +echo $ks; + +?> |