|
| 1 | +#!/bin/bash -xe |
| 2 | + |
| 3 | +RPM_INSTALL_ARGS="install -y httpd php php-common" |
| 4 | + |
| 5 | +if [ -f "/etc/redhat-release" ]; then |
| 6 | + yum update -y || dnf update -y |
| 7 | + yum $RPM_INSTALL_ARGS || dnf $RPM_INSTALL_ARGS |
| 8 | +else |
| 9 | + apt-get update |
| 10 | + apt-get install -y apache2 libapache2-mod-php |
| 11 | +fi |
| 12 | + |
| 13 | +cat > /var/www/html/index.php <<'EOF' |
| 14 | +<?php |
| 15 | +function metadata_value($value) { |
| 16 | + $opts = array( |
| 17 | + "http" => array( |
| 18 | + "method" => "GET", |
| 19 | + "header" => "Metadata-Flavor: Google" |
| 20 | + ) |
| 21 | + ); |
| 22 | + $context = stream_context_create($opts); |
| 23 | + $content = file_get_contents("http://metadata/computeMetadata/v1/$value", false, $context); |
| 24 | + return $content; |
| 25 | +} |
| 26 | +?> |
| 27 | +
|
| 28 | +<!doctype html> |
| 29 | +<html> |
| 30 | +<head> |
| 31 | +<!-- Compiled and minified CSS --> |
| 32 | +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/css/materialize.min.css"> |
| 33 | +
|
| 34 | +<!-- Compiled and minified JavaScript --> |
| 35 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/js/materialize.min.js"></script> |
| 36 | +<title>Frontend Web Server</title> |
| 37 | +</head> |
| 38 | +<body> |
| 39 | +<div class="container"> |
| 40 | +<div class="row"> |
| 41 | +<div class="col s2"> </div> |
| 42 | +<div class="col s8"> |
| 43 | +
|
| 44 | +
|
| 45 | +<div class="card blue"> |
| 46 | +<div class="card-content white-text"> |
| 47 | +<div class="card-title">Backend that serviced this request</div> |
| 48 | +</div> |
| 49 | +<div class="card-content white"> |
| 50 | +<table class="bordered"> |
| 51 | + <tbody> |
| 52 | + <tr> |
| 53 | + <td>Name</td> |
| 54 | + <td><?php printf(metadata_value("instance/name")) ?></td> |
| 55 | + </tr> |
| 56 | + <tr> |
| 57 | + <td>ID</td> |
| 58 | + <td><?php printf(metadata_value("instance/id")) ?></td> |
| 59 | + </tr> |
| 60 | + <tr> |
| 61 | + <td>Hostname</td> |
| 62 | + <td><?php printf(metadata_value("instance/hostname")) ?></td> |
| 63 | + </tr> |
| 64 | + <tr> |
| 65 | + <td>Zone</td> |
| 66 | + <td><?php printf(metadata_value("instance/zone")) ?></td> |
| 67 | + </tr> |
| 68 | + <tr> |
| 69 | + <td>Machine Type</td> |
| 70 | + <td><?php printf(metadata_value("instance/machine-type")) ?></td> |
| 71 | + </tr> |
| 72 | + <tr> |
| 73 | + <td>Project</td> |
| 74 | + <td><?php printf(metadata_value("project/project-id")) ?></td> |
| 75 | + </tr> |
| 76 | + <tr> |
| 77 | + <td>Internal IP</td> |
| 78 | + <td><?php printf(metadata_value("instance/network-interfaces/0/ip")) ?></td> |
| 79 | + </tr> |
| 80 | + <tr> |
| 81 | + <td>External IP</td> |
| 82 | + <td><?php printf(metadata_value("instance/network-interfaces/0/access-configs/0/external-ip")) ?></td> |
| 83 | + </tr> |
| 84 | + </tbody> |
| 85 | +</table> |
| 86 | +</div> |
| 87 | +</div> |
| 88 | +
|
| 89 | +<div class="card blue"> |
| 90 | +<div class="card-content white-text"> |
| 91 | +<div class="card-title">Proxy that handled this request</div> |
| 92 | +</div> |
| 93 | +<div class="card-content white"> |
| 94 | +<table class="bordered"> |
| 95 | + <tbody> |
| 96 | + <tr> |
| 97 | + <td>Address</td> |
| 98 | + <td><?php printf($_SERVER["HTTP_HOST"]); ?></td> |
| 99 | + </tr> |
| 100 | + </tbody> |
| 101 | +</table> |
| 102 | +</div> |
| 103 | +
|
| 104 | +</div> |
| 105 | +</div> |
| 106 | +<div class="col s2"> </div> |
| 107 | +</div> |
| 108 | +</div> |
| 109 | +</html> |
| 110 | +EOF |
| 111 | + |
| 112 | +mv /var/www/html/index.html /var/www/html/index.html.old || echo "Old index doesn't exist" |
| 113 | + |
| 114 | +[[ -n "${PROXY_PATH}" ]] && mkdir -p /var/www/html/${PROXY_PATH} && cp /var/www/html/index.php /var/www/html/${PROXY_PATH}/index.php |
| 115 | + |
| 116 | +chkconfig httpd on || systemctl enable httpd || systemctl enable apache2 |
| 117 | +service httpd restart || systemctl restart httpd || systemctl restart apache2 |
| 118 | + |
0 commit comments