|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +PROJECT_BASE="$(pwd)" |
| 6 | +CONFIG_FILE=${PROJECT_BASE}/.dev-config |
| 7 | +STORAGE_DIRVER=${STORAGE_DIRVER:-'virtiofs'} # virtiofs | sshfs | 9p | nfs |
| 8 | +CODE_SRC_ROOT_DIR=${CODE_SRC_ROOT_DIR:-'/home/code/src/'} |
| 9 | + |
| 10 | +get_script_dir() { |
| 11 | + SOURCE="${BASH_SOURCE[0]}" |
| 12 | + while [ -h "$SOURCE" ]; do |
| 13 | + DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" |
| 14 | + SOURCE="$(readlink "$SOURCE")" |
| 15 | + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" |
| 16 | + done |
| 17 | + SCRIPT_PATH="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )" |
| 18 | + echo "$SCRIPT_PATH" |
| 19 | +} |
| 20 | + |
| 21 | +DEV_STACK_BASE="${PROJECT_BASE}/vendor/solcloud/dev-stack" |
| 22 | +if ! [ -d "$DEV_STACK_BASE" ]; then |
| 23 | + DEV_STACK_BASE=$(realpath "$(get_script_dir)/../") |
| 24 | + if ! [ -d "$DEV_STACK_BASE" ]; then |
| 25 | + echo "Cannot decide script path" |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | +fi |
| 29 | + |
| 30 | +if [ "$1" ] && [ "$1" == "init" ]; then |
| 31 | + cp -i ${DEV_STACK_BASE}/src/.dev-config.example $CONFIG_FILE |
| 32 | + echo "Example config file created $CONFIG_FILE" |
| 33 | + exit 0 |
| 34 | +fi |
| 35 | + |
| 36 | +LOCAL_PROXY_PORT=${LOCAL_PROXY_PORT:-1122} |
| 37 | +REMOTE_PROXY_PORT=${REMOTE_PROXY_PORT:-80} |
| 38 | + |
| 39 | +REMOTE_SYSTEM=${REMOTE_SYSTEM:-'qemu'} # qemu | real |
| 40 | +REMOTE_DEV_STACK_BIN=${REMOTE_DEV_STACK_BIN:-'dev'} |
| 41 | +REMOTE_USER=${REMOTE_USER:-'code'} |
| 42 | +REMOTE_USER_UID=${REMOTE_USER_UID:-1007} |
| 43 | +REMOTE_USER_GID=${REMOTE_USER_GID:-1007} |
| 44 | +REMOTE_IP=${REMOTE_IP:-'virtual'} # routable IP from host perspective |
| 45 | +REMOTE_IP_REAL=${REMOTE_IP_REAL:-'10.0.2.15'} # real interface ip from remote perspective |
| 46 | +REMOTE_PORT=${REMOTE_PORT:-2201} |
| 47 | + |
| 48 | +remote() { |
| 49 | + ssh -t -q -p $REMOTE_PORT $REMOTE_USER@$REMOTE_IP "export REMOTE_PROXY_PORT=$REMOTE_PROXY_PORT ; export PROXY_PORT=$LOCAL_PROXY_PORT ; $1" || echo "Remote command return error" |
| 50 | +} |
| 51 | +singleton_bg() { |
| 52 | + if [ $(ps aux | grep -- "$1" | wc -l) == 1 ] ; then |
| 53 | + $1 & |
| 54 | + fi |
| 55 | +} |
| 56 | +remote_dev_stack() { |
| 57 | + remote "cd $PROJECT_BASE && $REMOTE_DEV_STACK_BIN $@" |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +########## |
| 62 | +########## |
| 63 | + |
| 64 | +if [ "$1" ] && [ "$1" == "machine" ]; then |
| 65 | + if [ $REMOTE_SYSTEM == "qemu" ]; then |
| 66 | + if [ "$2" ] && [ "$2" == "up" ]; then |
| 67 | + if [ "$(ps aux | grep 'dev_stack=machine_qemu' | wc -l)" = "2" ]; then |
| 68 | + echo "Machine is already up" |
| 69 | + exit 0 |
| 70 | + fi |
| 71 | + QEMU_OPTS="-nographic" |
| 72 | + if [ $STORAGE_DIRVER == "virtiofs" ]; then |
| 73 | + virtiofsd-rs --socket /tmp/dev-stack-qemu-virtiofs --shared-dir $CODE_SRC_ROOT_DIR --disable-xattr --sandbox none --no-announce-submounts & |
| 74 | + QEMU_OPTS="$QEMU_OPTS -chardev socket,id=char0,path=/tmp/dev-stack-qemu-virtiofs -device vhost-user-fs-pci,queue-size=1024,chardev=char0,tag=tag -object memory-backend-file,id=mem,size=${QEMU_RAM:-4G},mem-path=/dev/shm,share=on -numa node,memdev=mem" |
| 75 | + fi |
| 76 | + qemu-system-x86_64 -cpu host -smp ${QEMU_NPROC:-1} -m ${QEMU_RAM:-4G} -enable-kvm -device ahci,id=ahci -usb -device usb-tablet \ |
| 77 | + -kernel "${QEMU_KERNEL:-/data/store/dev-machine/kernel}" -append "mitigations=off console=ttyS0 dev_stack=machine_qemu root=/dev/sda2 init=/init" \ |
| 78 | + -drive id=disk0,file="${QEMU_HDA:-/data/store/dev-machine/sda.img}",if=none,format=raw -device ide-hd,drive=disk0,bus=ahci.0 \ |
| 79 | + -drive id=disk1,file="${QEMU_HDA_1:-/data/store/dev-machine/sdb.img}",if=none,format=raw -device ide-hd,drive=disk1,bus=ahci.1 \ |
| 80 | + -net user,hostfwd=tcp::$REMOTE_PORT-:22 -net nic,model=virtio-net-pci $QEMU_OPTS |
| 81 | + elif [ "$2" ] && [ "$2" == "down" ]; then |
| 82 | + remote "poweroff" |
| 83 | + fi |
| 84 | + else |
| 85 | + echo "Unknown REMOTE_SYSTEM" |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | + exit 0 |
| 89 | +elif [ "$1" ] && [ "$1" == "ssh" ]; then |
| 90 | + CHANGE_CWD=$PWD ssh -p $REMOTE_PORT $REMOTE_USER@$REMOTE_IP -o SendEnv=CHANGE_CWD |
| 91 | + exit 0 |
| 92 | +elif [ "$1" ] && [ "$1" == "forward" ]; then |
| 93 | + if [ "$4" ]; then |
| 94 | + echo "Forwarding local port $2 to remote IP $3:$4" |
| 95 | + ssh -N -L 127.0.0.1:$2:$3:$4 -p $REMOTE_PORT $REMOTE_USER@$REMOTE_IP |
| 96 | + else |
| 97 | + echo "Usage: forward LOCAL_PORT REMOTE_IP REMOTE_PORT" |
| 98 | + fi |
| 99 | + exit 0 |
| 100 | +fi |
| 101 | + |
| 102 | +if [[ ! -f $CONFIG_FILE ]]; then |
| 103 | + echo "No config file found, run init command or create it manually" |
| 104 | + exit 1 |
| 105 | +fi |
| 106 | + |
| 107 | +{ |
| 108 | + # Init deamons |
| 109 | + |
| 110 | + # Proxy docker jwilder forward from host |
| 111 | + singleton_bg "ssh -q -N -L 127.0.0.1:$LOCAL_PROXY_PORT:127.0.0.1:$REMOTE_PROXY_PORT -p $REMOTE_PORT $REMOTE_USER@$REMOTE_IP" |
| 112 | + |
| 113 | + # Ports forwarding from remote (NEEDS sshd_config: GatewayPorts clientspecified or yes) |
| 114 | + singleton_bg "ssh -q -N -R $REMOTE_IP_REAL:9000:127.0.0.1:9000 -p $REMOTE_PORT $REMOTE_USER@$REMOTE_IP > /dev/null 2>& 1" # xdebug 2 9000 port forward |
| 115 | + #singleton_bg "ssh -q -N -R $REMOTE_IP_REAL:9003:$127.0.0.1:9003 -p $REMOTE_PORT $REMOTE_USER@$REMOTE_IP > /dev/null 2>& 1" # xdebug 3 9003 port forward |
| 116 | +} |
| 117 | + |
| 118 | +# Params |
| 119 | +if [ "$1" ] && [ "$1" == "up" ]; then |
| 120 | + # Setup directory and shares |
| 121 | + if [ $STORAGE_DIRVER == "sshfs" ]; then |
| 122 | + HOST_USER=${HOST_USER:-'code'} |
| 123 | + HOST_IP=${HOST_IP:-'10.0.2.2'} |
| 124 | + HOST_PORT=${HOST_PORT:-22} |
| 125 | + remote "mkdir -p $PROJECT_BASE ; mount | grep -- '$PROJECT_BASE' > /dev/null 2> /dev/null || sshfs -o uid=${REMOTE_USER_UID},gid=${REMOTE_USER_GID},direct_io,kernel_cache,ciphers=aes128-gcm@openssh.com,allow_root,default_permissions,compression=no,cache=no,reconnect,disable_hardlink,max_conns=12 -p $HOST_PORT $HOST_USER@$HOST_IP:$PROJECT_BASE $PROJECT_BASE" |
| 126 | + fi |
| 127 | + # Run remote dev-stack |
| 128 | + remote_dev_stack "$*" |
| 129 | +elif [ "$1" ] && [ "$1" == "down" ]; then |
| 130 | + remote_dev_stack "$*" |
| 131 | + if [ $STORAGE_DIRVER == "sshfs" ]; then |
| 132 | + remote "cd /tmp && fusermount3 -u $PROJECT_BASE" |
| 133 | + fi |
| 134 | +elif [ "$1" ] && [ "$1" == "composerssh" ]; then |
| 135 | + eval $(ssh-agent) |
| 136 | + ssh-add -t 300 ${PRIVATE_KEY:-~/.ssh/id_rsa} |
| 137 | + ssh -A -p $REMOTE_PORT $REMOTE_USER@$REMOTE_IP "cd $PROJECT_BASE && export SSH_AUTH_SOCK_PATH=\$(echo \$SSH_AUTH_SOCK | sed s@/tmp/@/share/@) ; $REMOTE_DEV_STACK_BIN $*" |
| 138 | + eval $(ssh-agent -k) |
| 139 | +elif [ "$1" ] && [ "$1" == "greenmail" ]; then |
| 140 | + for port in 3025 3110; do |
| 141 | + ssh -N -L 127.0.0.1:${port}:127.0.0.1:${port} -p $REMOTE_PORT $REMOTE_USER@$REMOTE_IP & |
| 142 | + done; |
| 143 | + remote_dev_stack "$*" |
| 144 | +else |
| 145 | + # Run remote dev-stack |
| 146 | + remote_dev_stack "$*" |
| 147 | +fi |
0 commit comments