Coder Social home page Coder Social logo

ljq / vmware-fusion-vmcli-nogui Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 5.0 3.89 MB

vmware fusion shortcut management scripts based on the vmrun cli.

Home Page: https://www.defense.ink

License: MIT License

Shell 100.00%
nogui vmware virtual-machine-instances shortcut-script convenient vmrun

vmware-fusion-vmcli-nogui's Issues

revise fusion-vmcli

#!/usr/bin/env bash
#
# Vmware Funsion Operate Linux (Mode: No Gui)
# 
# Author: jackliu (Jianqiu Liu)
# Site: https://github.com/ljq
# Blog: https://wdft.com
# Email: [email protected]
# Date: 2020-11-29
# Version: 1.0.0

# script version
CLI_VERSION="1.0.1"

# color green
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced
OK="[\033[1;32m✓\033[0m]"
ERROR="[\033[1;31m✗\033[0m]"

# help info
USEAGE="[usage]: [-l | list] [start|stop|suspend|pause|unpause <number>] [-h | -help | --help] [-v | -V | --version]"
HELP_INFO=$(cat <<EOF
[helptext]
    -h|help : Show help info.
    -l|list : default[None parameters] show VMs list and VMs status.
    start|stop|suspend|pause|unpause [number] : 
        Start the corresponding virtual machine [Linux based distribution only].
    
    Notice: default show VMs list.
EOF
)

# ---------------------- Functions -------------------------

# fusion vms source path

# etc conf
function etc_ini(){
    item=$1
    section="BASE"
    ini_file="/usr/local/etc/fusion-vmcli.conf"
    if [ ! -f "$ini_file" ]; then
        return 0
    fi
    ini_options=`awk -F '=' '/'$section'/{a=1}a==1&&$1~/'${item}'/{print $2;exit}' ${ini_file}`
    ini_options=${ini_options//\"/}
    echo ${ini_options}
}

# Show list vm nodes
function show_vms(){
    local vms=($1)
    local total=$2
    local num=1
    echo -e "${OK} VM List (by Linux distribution)"
    for dirname in ${vms[@]}
    do
        #filter Win
        dirname=${dirname//.vmwarevm/}
        if [[ "${dirname}" =~ "Win" ]]; then
            continue
        else
            # escape filename
            esc_dirname=${dirname//#/\\ }
            echo -e "  [${num}] ${esc_dirname}"
            ((num=num+1))
        fi
    done
    echo -e "${OK} By linux distribution total:${total} vm nodes."
}

# get vm total number
function vm_nodes_total(){
    local vms=($1)
    local total=0
    for dirname in ${vms[@]}
    do
        #filter Win
        if [[ "${dirname}" =~ "Win" ]]; then
            continue
        else
            ((total=total+1))
        fi
    done
    echo $total
}

# Task exec
function task_exec(){
    # check params
    local operate=$1
    local serial_num=$2
    local vms=($3)
    local vm_path=$4
    local list_total=$5

    if [[ -z $operate || -z $serial_num ]]; then
        echo -e "${ERROR} missing parameter: operate or serial_num."
        exit;
    fi

    local vmrun_params=("start" "stop" "suspend" "pause" "unpause")
    if [[ ${vmrun_params[@]/${operate}/} == ${vmrun_params[@]} ]]; then
        echo -e "${ERROR} input invalid parameter: operate."
        exit
    fi

    if [[ $serial_num -le 0 || $serial_num -gt ${list_total} ]]; then
        echo -e "${ERROR} serial_num is not range (1-${list_total})"
        exit
    fi
    
    #vm nodes
    local vm_nodes=()
    local num=1
    for dirname in ${vms[@]}
    do
        #filter Win
        dirname=${dirname//.vmwarevm/}
        if [[ "${dirname}" =~ "Win" ]]; then
            continue
        else
            # escape filename
            esc_dirname=${dirname//#/\\ }
            vm_nodes[$num]=$esc_dirname
            ((num=num+1))
        fi
    done
    
    # select node machine
    local vm_num_machine=${vm_nodes[${serial_num}]}
    echo -e "${OK} VM machine (${vm_num_machine}) $operate ..."
    # task exec
    #echo "$(which vmrun) $operate $vm_path/$vm_num_machine.vmwarevm/$vm_num_machine.vmx nogui"
    output="$("$(which vmrun)" $operate "${vm_path}/${vm_num_machine}.vmwarevm/${vm_num_machine}.vmx" nogui)"

    if [ -n "$output" ]; then
        echo -e  "[\033[1;33m~\033[0m] ${output}"
    fi
    echo -e "${OK} VM machine(${vm_num_machine}) $operate Finished."
}

# Show vm nodes running
function vms_runing(){
    echo -e "${OK} VM Runing List"
    printf "$("$(which vmrun)" list | sed "s/^/  [\\\033[1;33m~\\\033[0m] /")"
    echo ""
}


# ---------------------- Task Process -------------------------

# check vmware fusion installed status
if which vmrun >/dev/null 2>&1; then
    vmrun_version=$(vmrun | grep "vmrun version")
    echo -e "${OK} ${vmrun_version}."
else
    echo -e "${ERROR} vmrun is not installd."
fi

# help
case $1 in
    "-v"|"-V"|"--version")
        echo -e "cli script version:${CLI_VERSION}."
        exit
        ;;
    "-h"|"-help"|"--help") 
        echo -e "${HELP_INFO}"
        exit
        ;;
    "-l"|"list") 
        ;;
esac

# virtual machines path config (custom)
vm_path="${HOME}/Virtual Machines.localized"
# list dir and process filename space
list_vms=($(ls "${vm_path}" | tr " " "\#"))
list_total=$(vm_nodes_total "${list_vms[*]}")
# vmrun params exec
if [[ ! -z $1 && ! -z $2 ]]; then
    # direct exec
    task_exec $1 $2 "${list_vms[*]}" "$vm_path" $list_total
    vms_runing
else
    # show list
    show_vms "${list_vms[*]}" $list_total
    vms_runing
    # interactive exec
    printf "${OK} (start|stop|suspend|pause|unpause) num: \033[1;32m"
    read operate serial_num 
    printf "\033[0m"
    # cli interactive
    task_exec $operate $serial_num "${list_vms[*]}" "$vm_path" $list_total
    vms_runing
fi
echo -e "${OK} Task process is done."
exit
➜  ~ fusion-vmcli stop 1
[✓] vmrun version 1.17.0 build-23298085.
[✓] VM machine (OpenWrt) stop ...
[~] Error: The virtual machine is not powered on: /Users/xr/Virtual Machines.localized/OpenWrt.vmwarevm/OpenWrt.vmx
[✓] VM machine(OpenWrt) stop Finished.
[✓] VM Runing List
  [~] Total running VMs: 0
[✓] Task process is done.
➜  ~ fusion-vmcli start 1
[✓] vmrun version 1.17.0 build-23298085.
[✓] VM machine (OpenWrt) start ...
[~] 2024-03-22T17:37:53.897| ServiceImpl_Opener: PID 31617
[✓] VM machine(OpenWrt) start Finished.
[✓] VM Runing List
  [~] Total running VMs: 1
  [~] /Users/xr/Virtual Machines.localized/OpenWrt.vmwarevm/OpenWrt.vmx
[✓] Task process is done.
➜  ~ fusion-vmcli
[✓] vmrun version 1.17.0 build-23298085.
[✓] VM List (by Linux distribution)
  [1] OpenWrt
[✓] By linux distribution total:1 vm nodes.
[✓] VM Runing List
  [~] Total running VMs: 1
  [~] /Users/xr/Virtual Machines.localized/OpenWrt.vmwarevm/OpenWrt.vmx
[✓] (start|stop|suspend|pause|unpause) num: stop 1
[✓] VM machine (OpenWrt) stop ...
[✓] VM machine(OpenWrt) stop Finished.
[✓] VM Runing List
  [~] Total running VMs: 0
[✓] Task process is done.
➜  ~ fusion-vmcli -l
[✓] vmrun version 1.17.0 build-23298085.
[✓] VM List (by Linux distribution)
  [1] OpenWrt
[✓] By linux distribution total:1 vm nodes.
[✓] VM Runing List
  [~] Total running VMs: 0
[✓] (start|stop|suspend|pause|unpause) num: start 1
[✓] VM machine (OpenWrt) start ...
[~] 2024-03-22T17:38:09.409| ServiceImpl_Opener: PID 31785
[✓] VM machine(OpenWrt) start Finished.
[✓] VM Runing List
  [~] Total running VMs: 1
  [~] /Users/xr/Virtual Machines.localized/OpenWrt.vmwarevm/OpenWrt.vmx
[✓] Task process is done.
➜  ~

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.