django-orchestra/orchestra/contrib/vps/backends.py

136 lines
4.5 KiB
Python
Raw Normal View History

2015-08-05 22:58:35 +00:00
import decimal
import textwrap
from orchestra.contrib.orchestration import ServiceController
2015-04-05 10:46:24 +00:00
from orchestra.contrib.resources import ServiceMonitor
2014-07-09 16:17:43 +00:00
2015-09-21 13:57:15 +00:00
from . import settings
2014-07-09 16:17:43 +00:00
2015-08-05 22:58:35 +00:00
class ProxmoxOVZ(ServiceController):
model = 'vps.VPS'
RESOURCES = (
('memory', 'mem'),
('swap', 'swap'),
('disk', 'disk')
)
GET_PROXMOX_INFO = textwrap.dedent("""
function get_vz_info () {
hostname=$1
version=$(pveversion | cut -d '/' -f2 | cut -d'.' -f1)
if [[ $version -lt 2 ]]; then
conf=$(grep "CID\\|:$hostname:" /var/lib/pve-manager/vzlist | grep -B1 ":$hostname:")
CID=$(echo "$conf" | head -n1 | cut -d':' -f2)
CTID=$(echo "$conf" | tail -n1 | cut -d':' -f1)
node=$(pveca -l | grep "^\\s*$CID\\s*:" | awk {'print $3'})
else
conf=$(grep -r "HOSTNAME=\\"$hostname\\"" /etc/pve/nodes/*/openvz/*.conf)
node=$(echo "${conf}" | cut -d"/" -f5)
CTID=$(echo "${conf}" | cut -d"/" -f7 | cut -d"\\." -f1)
fi
echo $CTID $node
}""")
def prepare(self):
super(ProxmoxOVZ, self).prepare()
self.append(self.GET_PROXMOX_INFO)
2015-09-21 13:57:15 +00:00
def get_vzset_args(self, context):
args = list(settings.VPS_DEFAULT_VZSET_ARGS)
2015-08-05 22:58:35 +00:00
for resource, arg_name in self.RESOURCES:
try:
2015-09-21 13:57:15 +00:00
allocation = context[resource]
except KeyError:
2015-08-05 22:58:35 +00:00
pass
else:
args.append('--%s %i' % (arg_name, allocation))
return ' '.join(args)
2015-09-21 13:57:15 +00:00
def run_ssh_commands(self, ssh_commands):
commands = '\n '.join(ssh_commands)
self.append(textwrap.dedent("""\
cat << EOF | ssh root@${info[1]}
%s
EOF""") % commands
)
2015-08-05 22:58:35 +00:00
def save(self, vps):
2015-09-21 13:57:15 +00:00
# TODO create the container
2015-08-05 22:58:35 +00:00
context = self.get_context(vps)
2015-09-21 13:57:15 +00:00
self.append(textwrap.dedent("""
info=( $(get_vz_info %(hostname)s) )
echo "Managing ${info[@]}"\
""") % context
)
ssh_commands = []
vzset_args = self.get_vzset_args(context)
2015-08-05 22:58:35 +00:00
if vzset_args:
context['vzset_args'] = vzset_args
2015-09-21 13:57:15 +00:00
ssh_commands.append("pvectl vzset ${info[0]} %(vzset_args)s" % context)
2015-08-05 22:58:35 +00:00
if hasattr(vps, 'password'):
context['password'] = vps.password.replace('$', '\\$')
2015-09-21 13:57:15 +00:00
ssh_commands.append(textwrap.dedent("""\
echo 'root:%(password)s' \\
2015-10-09 12:54:30 +00:00
| chroot /var/lib/vz/private/${info[0]} chpasswd -e""") % context
2015-08-05 22:58:35 +00:00
)
2015-09-21 13:57:15 +00:00
self.run_ssh_commands(ssh_commands)
2015-08-05 22:58:35 +00:00
def get_context(self, vps):
2015-09-21 13:57:15 +00:00
context = {
2015-08-05 22:58:35 +00:00
'hostname': vps.hostname,
}
2015-09-21 13:57:15 +00:00
for resource, __ in self.RESOURCES:
try:
allocation = getattr(vps.resources, resource).allocated
except AttributeError:
pass
else:
context[resource] = allocation
return context
2015-08-05 22:58:35 +00:00
# TODO rename to proxmox
2014-07-09 16:17:43 +00:00
class OpenVZTraffic(ServiceMonitor):
model = 'vps.VPS'
resource = ServiceMonitor.TRAFFIC
monthly_sum_old_values = True
2014-07-14 14:56:48 +00:00
2015-08-05 22:58:35 +00:00
def prepare(self):
super(OpenVZTraffic, self).prepare()
self.append(ProxmoxOVZ.GET_PROXMOX_INFO)
self.append(textwrap.dedent("""
function monitor () {
object_id=$1
hostname=$2
info=( $(get_vz_info $hostname) )
cat << EOF | ssh root@${info[1]}
vzctl exec ${info[0]} cat /proc/net/dev \\
| grep venet0 \\
| tr ':' ' ' \\
| awk '{sum=\\$2+\\$10} END {printf ("$object_id %0.0f\\n", sum)}'
EOF
}
""")
)
2014-07-16 15:20:16 +00:00
def process(self, line):
2015-08-05 22:58:35 +00:00
""" diff with last stored state """
object_id, value, state = super(OpenVZTraffic, self).process(line)
value = decimal.Decimal(value)
2014-07-14 14:56:48 +00:00
last = self.get_last_data(object_id)
2015-08-05 22:58:35 +00:00
if not last or last.state > value:
return object_id, value, value
return object_id, value-last.state, value
def monitor(self, vps):
""" Get OpenVZ container traffic on a Proxmox cluster """
context = self.get_context(vps)
self.append('monitor %(object_id)s %(hostname)s' % context)
def get_context(self, vps):
return {
'object_id': vps.id,
'hostname': vps.hostname,
2014-07-14 14:56:48 +00:00
}