aboutsummaryrefslogtreecommitdiff
path: root/kvmx-supervise
blob: 441c506526a7eafd0c16d13b3b6b61e49cb8e6d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env bash
#
# kvmx-supervise handles all registered kvmx instances in a system
#
# Copyright (C) 2017 Silvio Rhatto - rhatto at riseup.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Parameters
BASENAME="`basename $0`"
DIRNAME="`dirname $0`"
INSTANCES="`ls -1 /home/*/.config/kvmx/*`"
ACTION="$1"

# Load basic functions
export APP_BASE="`$DIRNAME/kvmx app_base`"
source $APP_BASE/lib/kvmx/functions || exit 1

# Calculate version
VERSION="`$APP_BASE/kvmx version`"

# Initialize
function __kvmx_supervise_initialize {
  #if [ "`whoami`" != "root" ]; then
  #  echo "$BASENAME: needs root privileges"
  #  exit 1
  #fi

  # Put startup code here
  true
}

# Display usage
function kvmx_supervise_usage {
  echo "$BASENAME $VERSION - virtual machine supervisor"
  echo ""
  echo "usage: $BASENAME <action> [options]"
  echo ""
  echo "available actions:"
  echo ""
  grep "^function kvmx_supervise_" $0 | cut -d ' ' -f 2 | \
    sed -e 's/kvmx_supervise_//' | sort | xargs -L 6 | column -t -c 6 | sed -e 's/^/\t/'
  echo ""

  #local list="`kvmx_supervise_list`"

  #if [ ! -z "$list" ]; then
  #  echo "available virtual machines:"
  #  echo ""
  #  echo "$list" | sed -e 's/^/\t/'
  #  echo ""
  #fi

  exit 1
}

# Call
function kvmx_supervise_call {
  local user="$1"
  local vm="$2"

  shift 2
  local params=($*)

  # Check parameters
  if [ -z "$vm" ]; then
    kvmx_supervise_usage
    exit 1
  fi

  # Check for existing VM
  if [ ! -e "/home/$user/.config/kvmx/$2" ]; then
    kvmx_supervise_usage
    exit 1
  fi

  # When running as root, operate only with VMs configured with supervise_manage=1
  # Specify /bin/bash to avoid restricted shells like kvmx-shell
  if [ "`whoami`" == "root" ]; then
    supervise_manage="`su $user -s /bin/bash -c "kvmx config $vm supervise_manage"`"
  else
    supervise_manage="1"
  fi

  # Dispatch
  if [ "$supervise_manage" == "1" ]; then
    if [ "`whoami`" == "root" ]; then
      su $user -s /bin/bash -c "kvmx ${params[0]} $vm ${params[@]:1}"
    else
      if [ "`whoami`" == "$user" ]; then
        kvmx ${params[0]} $vm ${params[@]:1}
      fi
    fi
  fi
}

# Foreach
function kvmx_supervise_foreach {
  local user
  local vm

  for instance in $INSTANCES; do
    user="`echo $instance | cut -d / -f 3`"
    vm="`basename $instance`"

    #echo "$BASENAME: $vm guest..."
    kvmx_supervise_call $user $vm $*
  done
}

# Status
function kvmx_supervise_status {
  kvmx_supervise_foreach status
}

# Start
function kvmx_supervise_start {
  kvmx_supervise_foreach start
}

# Stop
function kvmx_supervise_poweroff {
  kvmx_supervise_foreach poweroff
}

# Restart
function kvmx_supervise_restart {
  kvmx_supervise_foreach restart
}

# Dispatch
if type kvmx_supervise_$ACTION 2> /dev/null | grep -q "kvmx_supervise_$ACTION ()"; then
  __kvmx_supervise_initialize $*

  shift
  kvmx_supervise_$ACTION $*
else
  kvmx_supervise_usage
fi