|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2023 Ericsson AB |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +has_sudo_password=false |
| 19 | +sudo_password="" |
| 20 | +has_ssh_password=false |
| 21 | +ssh_password="" |
| 22 | + |
| 23 | +while getopts ":s:p:n" option; do |
| 24 | + case "${option}" in |
| 25 | + s) |
| 26 | + has_sudo_password=true |
| 27 | + sudo_password="$OPTARG" |
| 28 | + ;; |
| 29 | + p) |
| 30 | + has_ssh_password=true |
| 31 | + ssh_password="$OPTARG" |
| 32 | + ;; |
| 33 | + n) |
| 34 | + has_sudo_password=true |
| 35 | + sudo_password="" |
| 36 | + ;; |
| 37 | + *) |
| 38 | + echo "Usage: $0 -s <sudo password> -p <ssh password> -n" |
| 39 | + echo "Parameters:" |
| 40 | + echo " -s: Set sudo password to argument" |
| 41 | + echo " -p: Set ssh password to argument (if sshpass is installed)" |
| 42 | + echo " -n: Skip sudo password prompt" |
| 43 | + exit 0 |
| 44 | + ;; |
| 45 | + esac |
| 46 | +done |
| 47 | + |
| 48 | +name="$(hostname -f)/hydragen-emulator" |
| 49 | +image="$(docker images $name --format '{{.Repository}}:{{.Tag}}')" |
| 50 | + |
| 51 | +cd "$(git rev-parse --show-toplevel)/generator/k8s" |
| 52 | +contexts="$(echo *)" |
| 53 | +#contexts="$(kubectl config get-contexts --output=name | tr '\n' ' ')" |
| 54 | + |
| 55 | +echo "Contexts: $contexts" |
| 56 | +echo "Trying to discover all nodes that need an updated image..." |
| 57 | +echo "" |
| 58 | + |
| 59 | +nodes=() |
| 60 | + |
| 61 | +# Try every context |
| 62 | +# TODO: Does not check for the "node" property in configmap |
| 63 | +for ctx in $contexts; do |
| 64 | + echo "Trying to access context $ctx" |
| 65 | + cmd="kubectl get nodes -o custom-columns=:metadata.name,:spec.taints[].effect --no-headers --context $ctx" |
| 66 | + output="$($cmd 2>&1)" |
| 67 | + if [[ $? == 0 ]]; then |
| 68 | + echo " Kubectl returned nodes: $(echo $output | tr '\n' ' ')" |
| 69 | + ctxnodes="$(echo "$output" | grep -v 'NoSchedule' | cut -d ' ' -f 1 | tr '\n' ' ')" |
| 70 | + echo " Nodes that can have pods scheduled: $ctxnodes" |
| 71 | + for node in $ctxnodes; do |
| 72 | + nodes+=("$ctx/$node") |
| 73 | + done |
| 74 | + else |
| 75 | + echo " Command failed (exit status $?): $(echo $output | tr '\n' ' ')" |
| 76 | + fi |
| 77 | + echo "" |
| 78 | +done |
| 79 | + |
| 80 | +echo "Nodes: ${nodes[@]}" |
| 81 | + |
| 82 | +if [[ $has_sudo_password == false ]]; then |
| 83 | + read -s -p "Sudo password (leave blank if '$(whoami)' has administrative access to containerd): " sudo_password |
| 84 | + if [[ -z "$sudo_password" ]]; then |
| 85 | + echo -n "(not using sudo)" |
| 86 | + fi |
| 87 | + echo "" |
| 88 | +fi |
| 89 | + |
| 90 | +for node in "${nodes[@]}"; do |
| 91 | + IFS="/" read -r ctx name <<< $node |
| 92 | + # https://kubernetes.io/docs/reference/kubectl/cheatsheet/ |
| 93 | + jsonpath="{.status.addresses[?(@.type=='InternalIP')].address}" |
| 94 | + ip="$(kubectl get nodes $name --context $ctx -o jsonpath=$jsonpath)" |
| 95 | + file="/tmp/containerd-import-image.sh" |
| 96 | + |
| 97 | + # Start ssh in background |
| 98 | + if [[ $has_ssh_password == true ]]; then |
| 99 | + sshpass -p "$ssh_password" ssh -M -S /tmp/containerd-import-ssh-socket -fnNT "$(whoami)@$ip" |
| 100 | + else |
| 101 | + ssh -M -S /tmp/containerd-import-ssh-socket -fnNT "$(whoami)@$ip" |
| 102 | + fi |
| 103 | + |
| 104 | + # Copy script to remote machine |
| 105 | + scp -o "ControlPath=/tmp/containerd-import-ssh-socket" ../../community/containerd-import-image.sh "$(whoami)@$ip:/tmp/containerd-import-image.sh" |
| 106 | + # Execute script with archive coming from stdin |
| 107 | + ssh -S /tmp/containerd-import-ssh-socket "$(whoami)@$ip" "chmod +x /tmp/containerd-import-image.sh" |
| 108 | + # Add space at the start to prevent password from being saved in bash history |
| 109 | + cat ../generated/hydragen-emulator.tar | ssh -S /tmp/containerd-import-ssh-socket -C "$(whoami)@$ip" " /tmp/containerd-import-image.sh "$sudo_password"" |
| 110 | + ssh -S /tmp/containerd-import-ssh-socket "$(whoami)@$ip" "rm /tmp/containerd-import-image.sh" |
| 111 | + # Close ssh session |
| 112 | + ssh -S /tmp/containerd-import-ssh-socket -O exit "$(whoami)@$ip" |
| 113 | +done |
0 commit comments