2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2024-11-24 16:46:24 +00:00

run/hetzner: upload using sparsecat and run e2fsck

docs: add demo scripts

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-09-10 18:15:22 +02:00
parent 6c93c8be56
commit d97b58159c
Signed by: adphi
GPG Key ID: 46BE4062DB2397FF
4 changed files with 360 additions and 6 deletions

View File

@ -243,12 +243,7 @@ func runHetzner(ctx context.Context, imgPath string, stdin io.Reader, stderr io.
}
}
}()
var cmd string
if runtime.GOOS == "linux" {
cmd = fmt.Sprintf("%s -r -disable-sparse-target -of %s", sparsecatPath, vmBlockPath)
} else {
cmd = fmt.Sprintf("dd of=%s", vmBlockPath)
}
cmd := fmt.Sprintf("%s -r -disable-sparse-target -of %s", sparsecatPath, vmBlockPath)
logrus.Debugf("$ %s", cmd)
if b, err := wses.CombinedOutput(cmd); err != nil {
return fmt.Errorf("%v: %s", err, string(b))
@ -281,6 +276,19 @@ func runHetzner(ctx context.Context, imgPath string, stdin io.Reader, stderr io.
} else {
logrus.Debugf(string(b))
}
cses, err := sc.NewSession()
if err != nil {
return err
}
defer cses.Close()
logrus.Infof("checking disk partition")
cmd = fmt.Sprintf("e2fsck -yf %s1", vmBlockPath)
logrus.Debugf("$ %s", cmd)
if b, err := cses.CombinedOutput(cmd); err != nil {
return fmt.Errorf("%v: %s", err, string(b))
} else {
logrus.Debugf(string(b))
}
eses, err := sc.NewSession()
if err != nil {
return err

75
scripts/demo Executable file
View File

@ -0,0 +1,75 @@
#!/usr/bin/env bash
dir="$PWD"
scripts_dir="scripts"
if [ "$(basename $PWD)" == "$scripts_dir" ]; then
cd ..
fi
. ./$scripts_dir/demo-magic
TYPE_SPEED=20
EXEC_WAIT=1
DEMO_PROMPT="${CYAN}➜ ${CYAN}\W "
clear
IMAGE="./images/workstation.qcow2"
PROMPT_TIMEOUT=1
print_prompt
wait
pei "# Let's create a virtual machine from a Dockerfile"
wait
DOCKERFILE="examples/full/Dockerfile"
pei "cat $DOCKERFILE"
cp scripts/demo-magic examples/full
cp scripts/inside examples/full
cat <<EOF >> $DOCKERFILE
COPY demo-magic /home/adphi/demo-magic
COPY inside /home/adphi/inside
RUN sudo chmod +x /home/adphi/inside && echo /home/adphi/inside >> /home/adphi/.zshrc && sudo apt install -y pv
EOF
PROMPT_TIMEOUT=5
wait
PROMPT_TIMEOUT=0
EXEC_WAIT=2
pei "export PASSWORD=\"Don'tThinkTh4tIReallyUseThisPassword:)\""
pei "sudo d2vm build -s 10G -o $IMAGE --force --build-arg USER=adphi --build-arg PASSWORD=\$PASSWORD -p \$PASSWORD -v --time=relative examples/full"
rm examples/full/{demo-magic,inside}
git checkout examples/ &> /dev/null
PROMPT_TIMEOUT=1
wait
PROMPT_TIMEOUT=2
EXEC_WAIT=1
pei "# Now let's run this image"
wait
EXEC_WAIT=2
pei "sudo d2vm run qemu --cpus 4 --mem 4096 --networking default $IMAGE"
# demo continues inside the vm is soon as the boot completes
wait
EXEC_WAIT=1
pei "# Let's try to run it on a cloud provider: Hetzner..."
EXEC_WAIT=2
pei "sudo -E d2vm run hetzner --rm -v --time=relative -u adphi -i ~/.ssh/id_rsa $IMAGE"
# demo continues inside the vm is soon as the boot completes
pei "# Pretty cool rigth ? :)"
wait
cd $dir

220
scripts/demo-magic Normal file
View File

@ -0,0 +1,220 @@
#!/usr/bin/env bash
###############################################################################
#
# demo-magic.sh
#
# Copyright (c) 2015 Paxton Hare
#
# This script lets you script demos in bash. It runs through your demo script when you press
# ENTER. It simulates typing and runs commands.
#
###############################################################################
# the speed to "type" the text
TYPE_SPEED=20
# no wait after "p" or "pe"
NO_WAIT=false
# if > 0, will pause for this amount of seconds before automatically proceeding with any p or pe
PROMPT_TIMEOUT=0
# don't show command number unless user specifies it
SHOW_CMD_NUMS=false
EXEC_WAIT=1
# handy color vars for pretty prompts
BLACK="\033[0;30m"
BLUE="\033[0;34m"
GREEN="\033[0;32m"
GREY="\033[0;90m"
CYAN="\033[0;36m"
RED="\033[0;31m"
PURPLE="\033[0;35m"
BROWN="\033[0;33m"
WHITE="\033[1;37m"
COLOR_RESET="\033[0m"
C_NUM=0
# prompt and command color which can be overriden
DEMO_PROMPT="$ "
DEMO_CMD_COLOR=$WHITE
DEMO_COMMENT_COLOR=$GREY
##
# prints the script usage
##
function usage() {
echo -e ""
echo -e "Usage: $0 [options]"
echo -e ""
echo -e "\tWhere options is one or more of:"
echo -e "\t-h\tPrints Help text"
echo -e "\t-d\tDebug mode. Disables simulated typing"
echo -e "\t-n\tNo wait"
echo -e "\t-w\tWaits max the given amount of seconds before proceeding with demo (e.g. '-w5')"
echo -e ""
}
##
# wait for user to press ENTER
# if $PROMPT_TIMEOUT > 0 this will be used as the max time for proceeding automatically
##
function wait() {
if [[ "$PROMPT_TIMEOUT" == "0" ]]; then
read -rs
else
read -rst "$PROMPT_TIMEOUT"
fi
}
print_prompt() {
# render the prompt
x=$(PS1="$DEMO_PROMPT" "$BASH" --norc -i </dev/null 2>&1 | sed -n '${s/^\(.*\)exit$/\1/p;}')
# show command number is selected
if $SHOW_CMD_NUMS; then
printf "[$((++C_NUM))] $x"
else
printf "$x"
fi
}
##
# print command only. Useful for when you want to pretend to run a command
#
# takes 1 param - the string command to print
#
# usage: p "ls -l"
#
##
function p() {
if [[ ${1:0:1} == "#" ]]; then
cmd=$DEMO_COMMENT_COLOR$1$COLOR_RESET
else
cmd=$DEMO_CMD_COLOR$1$COLOR_RESET
fi
# wait for the user to press a key before typing the command
if [ $NO_WAIT = false ]; then
wait
fi
if [[ -z $TYPE_SPEED ]]; then
echo -en "$cmd"
else
echo -en "$cmd" | pv -qL $[$TYPE_SPEED+(-2 + RANDOM%5)];
fi
# wait for the user to press a key before moving on
if [ $NO_WAIT = false ]; then
wait
fi
sleep $EXEC_WAIT
echo ""
}
##
# Prints and executes a command
#
# takes 1 parameter - the string command to run
#
# usage: pe "ls -l"
#
##
function pe() {
# print the command
p "$@"
run_cmd "$@"
}
##
# print and executes a command immediately
#
# takes 1 parameter - the string command to run
#
# usage: pei "ls -l"
#
##
function pei {
NO_WAIT=true pe "$@"
}
##
# Enters script into interactive mode
#
# and allows newly typed commands to be executed within the script
#
# usage : cmd
#
##
function cmd() {
# render the prompt
x=$(PS1="$DEMO_PROMPT" "$BASH" --norc -i </dev/null 2>&1 | sed -n '${s/^\(.*\)exit$/\1/p;}')
printf "$x\033[0m"
read command
run_cmd "${command}"
}
function run_cmd() {
function handle_cancel() {
printf ""
}
trap handle_cancel SIGINT
stty -echoctl
eval "$@"
stty echoctl
trap - SIGINT
print_prompt
}
function check_pv() {
command -v pv >/dev/null 2>&1 || {
echo ""
echo -e "${RED}##############################################################"
echo "# HOLD IT!! I require pv but it's not installed. Aborting." >&2;
echo -e "${RED}##############################################################"
echo ""
echo -e "${COLOR_RESET}Installing pv:"
echo ""
echo -e "${BLUE}Mac:${COLOR_RESET} $ brew install pv"
echo ""
echo -e "${BLUE}Other:${COLOR_RESET} http://www.ivarch.com/programs/pv.shtml"
echo -e "${COLOR_RESET}"
exit 1;
}
}
check_pv
#
# handle some default params
# -h for help
# -d for disabling simulated typing
#
while getopts ":dhncw:" opt; do
case $opt in
h)
usage
exit 1
;;
d)
unset TYPE_SPEED
;;
n)
NO_WAIT=true
;;
c)
SHOW_CMD_NUMS=true
;;
w)
PROMPT_TIMEOUT=$OPTARG
;;
esac
done

51
scripts/inside Normal file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
. $HOME/demo-magic
TYPE_SPEED=20
EXEC_WAIT=1
DEMO_PROMPT="${PURPLE}➜ ${PURPLE}\W "
defer_kill_htop() {
sleep 8
pkill htop
}
resize
print_prompt
sleep 2
pei "# Nice auto login ;)"
PROMPT_TIMEOUT=1
wait
if ! $(ps aux|grep -e "sshd: adphi" | grep -v grep &> /dev/null); then
pei "# Is the network configured ?"
pei "ip a"
pei "# But is it trully working ?"
pei "ping -c 5 linka.cloud"
fi
pei "# Now let's take a look at CPU and Memory usage..."
wait
defer_kill_htop &
pei "htop"
pei "# Let's see disk usage..."
PROMPT_TIMEOUT=3
pei "df -hT"
wait
pei "# Pretty small right ? ;)"
PROMPT_TIMEOUT=1
wait
pei "sudo poweroff"