|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +preflight_with_failure=" |
| 5 | +apiVersion: troubleshoot.sh/v1beta2 |
| 6 | +kind: HostPreflight |
| 7 | +spec: |
| 8 | + collectors: |
| 9 | + - tcpPortStatus: |
| 10 | + collectorName: Port 24 |
| 11 | + port: 24 |
| 12 | + - tcpPortStatus: |
| 13 | + collectorName: Port 22 |
| 14 | + port: 22 |
| 15 | + analyzers: |
| 16 | + - tcpPortStatus: |
| 17 | + checkName: Port 24 |
| 18 | + collectorName: Port 24 |
| 19 | + outcomes: |
| 20 | + - fail: |
| 21 | + when: connection-refused |
| 22 | + message: Connection to port 24 was refused. |
| 23 | + - warn: |
| 24 | + when: address-in-use |
| 25 | + message: Another process was already listening on port 24. |
| 26 | + - fail: |
| 27 | + when: connection-timeout |
| 28 | + message: Timed out connecting to port 24. |
| 29 | + - fail: |
| 30 | + when: error |
| 31 | + message: Unexpected port status |
| 32 | + - pass: |
| 33 | + when: connected |
| 34 | + message: Port 24 is available |
| 35 | + - warn: |
| 36 | + message: Unexpected port status |
| 37 | + - tcpPortStatus: |
| 38 | + checkName: Port 22 |
| 39 | + collectorName: Port 22 |
| 40 | + outcomes: |
| 41 | + - fail: |
| 42 | + when: connection-refused |
| 43 | + message: Connection to port 22 was refused. |
| 44 | + - fail: |
| 45 | + when: address-in-use |
| 46 | + message: Another process was already listening on port 22. |
| 47 | + - fail: |
| 48 | + when: connection-timeout |
| 49 | + message: Timed out connecting to port 22. |
| 50 | + - fail: |
| 51 | + when: error |
| 52 | + message: Unexpected port status |
| 53 | + - pass: |
| 54 | + when: connected |
| 55 | + message: Port 22 is available |
| 56 | + - warn: |
| 57 | + message: Unexpected port status |
| 58 | +" |
| 59 | + |
| 60 | +preflight_with_warning=" |
| 61 | +apiVersion: troubleshoot.sh/v1beta2 |
| 62 | +kind: HostPreflight |
| 63 | +spec: |
| 64 | + collectors: |
| 65 | + - tcpPortStatus: |
| 66 | + collectorName: Port 24 |
| 67 | + port: 24 |
| 68 | + - tcpPortStatus: |
| 69 | + collectorName: Port 22 |
| 70 | + port: 22 |
| 71 | + analyzers: |
| 72 | + - tcpPortStatus: |
| 73 | + checkName: Port 24 |
| 74 | + collectorName: Port 24 |
| 75 | + outcomes: |
| 76 | + - fail: |
| 77 | + when: connection-refused |
| 78 | + message: Connection to port 24 was refused. |
| 79 | + - warn: |
| 80 | + when: address-in-use |
| 81 | + message: Another process was already listening on port 24. |
| 82 | + - fail: |
| 83 | + when: connection-timeout |
| 84 | + message: Timed out connecting to port 24. |
| 85 | + - fail: |
| 86 | + when: error |
| 87 | + message: Unexpected port status |
| 88 | + - pass: |
| 89 | + when: connected |
| 90 | + message: Port 24 is available |
| 91 | + - warn: |
| 92 | + message: Unexpected port status |
| 93 | + - tcpPortStatus: |
| 94 | + checkName: Port 22 |
| 95 | + collectorName: Port 22 |
| 96 | + outcomes: |
| 97 | + - fail: |
| 98 | + when: connection-refused |
| 99 | + message: Connection to port 22 was refused. |
| 100 | + - warn: |
| 101 | + when: address-in-use |
| 102 | + message: Another process was already listening on port 22. |
| 103 | + - fail: |
| 104 | + when: connection-timeout |
| 105 | + message: Timed out connecting to port 22. |
| 106 | + - fail: |
| 107 | + when: error |
| 108 | + message: Unexpected port status |
| 109 | + - pass: |
| 110 | + when: connected |
| 111 | + message: Port 22 is available |
| 112 | + - warn: |
| 113 | + message: Unexpected port status |
| 114 | +" |
| 115 | + |
| 116 | +embed_preflight() { |
| 117 | + content="$1" |
| 118 | + rm -rf /root/preflight* |
| 119 | + echo "$content" > /root/preflight.yaml |
| 120 | + tar -czvf /root/preflight.tar.gz /root/preflight.yaml |
| 121 | + objcopy --input-target binary --output-target binary --rename-section .data=sec_bundle /root/preflight.tar.gz /root/preflight.o |
| 122 | + rm -rf /usr/local/bin/helmvm |
| 123 | + cp -Rfp /usr/local/bin/helmvm-copy /usr/local/bin/helmvm |
| 124 | + objcopy --add-section sec_bundle=/root/preflight.o /usr/local/bin/helmvm |
| 125 | +} |
| 126 | + |
| 127 | +has_applied_host_preflight() { |
| 128 | + if ! grep -q "Port 24 is available" /tmp/log ; then |
| 129 | + return 1 |
| 130 | + fi |
| 131 | + if ! grep -q "Another process was already listening on port 22" /tmp/log ; then |
| 132 | + return 1 |
| 133 | + fi |
| 134 | +} |
| 135 | + |
| 136 | +wait_for_healthy_node() { |
| 137 | + ready=$(kubectl get nodes | grep -v NotReady | grep -c Ready || true) |
| 138 | + counter=0 |
| 139 | + while [ "$ready" -lt "1" ]; do |
| 140 | + if [ "$counter" -gt 36 ]; then |
| 141 | + return 1 |
| 142 | + fi |
| 143 | + sleep 5 |
| 144 | + counter=$((counter+1)) |
| 145 | + echo "Waiting for node to be ready" |
| 146 | + ready=$(kubectl get nodes | grep -v NotReady | grep -c Ready || true) |
| 147 | + kubectl get nodes || true |
| 148 | + done |
| 149 | + return 0 |
| 150 | +} |
| 151 | + |
| 152 | +main() { |
| 153 | + cp -Rfp /usr/local/bin/helmvm /usr/local/bin/helmvm-copy |
| 154 | + embed_preflight "$preflight_with_failure" |
| 155 | + if helmvm install --no-prompt 2>&1 | tee /tmp/log ; then |
| 156 | + cat /tmp/log |
| 157 | + echo "Expected installation to fail" |
| 158 | + exit 1 |
| 159 | + fi |
| 160 | + if ! has_applied_host_preflight; then |
| 161 | + echo "Install hasn't applied host preflight" |
| 162 | + cat /tmp/log |
| 163 | + exit 1 |
| 164 | + fi |
| 165 | + mv /tmp/log /tmp/log-failure |
| 166 | + embed_preflight "$preflight_with_warning" |
| 167 | + if ! helmvm install --no-prompt 2>&1 | tee /tmp/log ; then |
| 168 | + cat /etc/os-release |
| 169 | + echo "Failed to install helmvm" |
| 170 | + exit 1 |
| 171 | + fi |
| 172 | + if ! grep -q "You can now access your cluster" /tmp/log; then |
| 173 | + echo "Failed to install helmvm" |
| 174 | + exit 1 |
| 175 | + fi |
| 176 | + if ! has_applied_host_preflight; then |
| 177 | + echo "Install hasn't applied host preflight" |
| 178 | + cat /tmp/log |
| 179 | + exit 1 |
| 180 | + fi |
| 181 | + if ! wait_for_healthy_node; then |
| 182 | + echo "Failed to install helmvm" |
| 183 | + exit 1 |
| 184 | + fi |
| 185 | +} |
| 186 | + |
| 187 | +export KUBECONFIG=/root/.helmvm/etc/kubeconfig |
| 188 | +export PATH=$PATH:/root/.helmvm/bin |
| 189 | +main |
0 commit comments