Skip to content

Commit

Permalink
Merge pull request #942 from andrzej-jankowski/proxmox
Browse files Browse the repository at this point in the history
LGFM
  • Loading branch information
zefciu committed Jun 25, 2014
2 parents 488dc67 + 8e74ca4 commit da9e37c
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 15 deletions.
73 changes: 58 additions & 15 deletions src/ralph/scan/plugins/ssh_proxmox.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,36 @@ def _connect_ssh(ip_address, user, password):
return network.connect_ssh(ip_address, user, password)


def _get_master_ip_address(ssh, ip_address, cluster_cfg=None):
def _add_prefix(command, proxmox_version):
if proxmox_version == 2:
return "sudo -u www-data %s" % command
return "sudo %s" % command


def _get_master_ip_address(
ssh, ip_address, cluster_cfg=None, proxmox_version=1
):
if not cluster_cfg:
stdin, stdout, stderr = ssh.exec_command("cat /etc/pve/cluster.cfg")
stdin, stdout, stderr = ssh.exec_command(
_add_prefix("/bin/cat /etc/pve/cluster.cfg", proxmox_version)
)
data = stdout.read()
else:
data = cluster_cfg
if not data:
stdin, stdout, stderr = ssh.exec_command("pvesh get /nodes")
stdin, stdout, stderr = ssh.exec_command(
_add_prefix("/usr/bin/pvesh get /nodes", proxmox_version)
)
data = stdout.read()
if data:
for node in json.loads(data):
stdin, stdout, stderr = ssh.exec_command(
'pvesh get "/nodes/%s/dns"' % node['node'],
_add_prefix(
'/usr/bin/pvesh get "/nodes/%s/dns"' % (
node['node']
),
proxmox_version
)
)
dns_data = stdout.read()
if not dns_data:
Expand Down Expand Up @@ -107,15 +124,22 @@ def _get_virtual_machine_info(
master_ip_address,
storages,
hypervisor_ip_address,
proxmox_version=1,
):
stdin, stdout, stderr = ssh.exec_command(
"cat /etc/qemu-server/%d.conf" % vmid,
_add_prefix(
"/bin/cat /etc/qemu-server/%d.conf" % vmid,
proxmox_version
),
)
lines = stdout.readlines()
if not lines:
# Proxmox 2 uses a different directory structure
stdin, stdout, stderr = ssh.exec_command(
"cat /etc/pve/nodes/*/qemu-server/%d.conf" % vmid,
"for i in `sudo -u www-data ls /etc/pve/nodes/`; do "
"if [ -f /etc/pve/nodes/$i/qemu-server/%d.conf ]; then "
"sudo -u www-data cat /etc/pve/nodes/$i/qemu-server/%d.conf; fi; "
"done" % (vmid, vmid)
)
lines = stdout.readlines()
disks = {}
Expand Down Expand Up @@ -216,11 +240,13 @@ def _get_virtual_machine_info(
return device_info


def _get_virtual_machines(ssh, master_ip_address, hypervisor_ip_address):
def _get_virtual_machines(
ssh, master_ip_address, hypervisor_ip_address, proxmox_version=1
):
detected_machines = []
storages = get_disk_shares(ssh, include_logical_volumes=True)
stdin, stdout, stderr = ssh.exec_command("qm list")
for line in stdout:
stdin, stdout, stderr = ssh.exec_command("sudo /usr/sbin/qm list")
for line in stdout.readlines():
line = line.strip()
if line.startswith('VMID'):
continue
Expand All @@ -237,6 +263,7 @@ def _get_virtual_machines(ssh, master_ip_address, hypervisor_ip_address):
master_ip_address,
storages,
hypervisor_ip_address,
proxmox_version=1,
)
except NoLanError as e:
logger.warning(unicode(e))
Expand All @@ -245,20 +272,34 @@ def _get_virtual_machines(ssh, master_ip_address, hypervisor_ip_address):
return detected_machines


def _get_proxmox_version(ssh):
stdin, stdout, stderr = ssh.exec_command("lsb_release -a")
for line in stdout.readlines():
line = line.lower()
if "codename" in line and "lenny" in line:
return 1
return 2


def _ssh_proxmox(ip_address, user, password):
ssh = _connect_ssh(ip_address, user, password)
proxmox_version = _get_proxmox_version(ssh)
try:
cluster_cfg = None
for command in (
'cat /etc/pve/cluster.cfg',
'cat /etc/pve/cluster.conf',
'cat /etc/pve/storage.cfg',
'pvecm help',
'/bin/cat /etc/pve/cluster.cfg',
'/bin/cat /etc/pve/cluster.conf',
'/bin/cat /etc/pve/storage.cfg',
'/usr/bin/pvecm help',
):
stdin, stdout, stderr = ssh.exec_command(command)
stdin, stdout, stderr = ssh.exec_command(
_add_prefix(command, proxmox_version)
if 'pvecm help' not in command
else command
)
data = stdout.read()
if data != '':
if command == 'cat /etc/pve/cluster.cfg':
if command == '/bin/cat /etc/pve/cluster.cfg':
cluster_cfg = data
break
else:
Expand All @@ -267,12 +308,14 @@ def _ssh_proxmox(ip_address, user, password):
ssh,
ip_address,
cluster_cfg,
proxmox_version=proxmox_version
)
cluster_member = _get_cluster_member(ssh, ip_address)
subdevices = _get_virtual_machines(
ssh,
master_ip_address,
ip_address,
proxmox_version=proxmox_version
)
if subdevices:
cluster_member['subdevices'] = subdevices
Expand Down
53 changes: 53 additions & 0 deletions src/ralph/scan/tests/plugins/test_ssh_proxmox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from django.test import TestCase

from ralph.discovery.tests.util import MockSSH
from ralph.scan.plugins.ssh_proxmox import (
_add_prefix,
_get_proxmox_version,
)


LSB_RELEASE_1 = """
Distributor ID: Debian
Description: Debian GNU/Linux 5.0.0 (lenny)
Release: 5.0.0
Codename: lenny
"""
LSB_RELEASE_2 = """
Distributor ID: Debian
Description: Debian GNU/Linux 6.0.0 (squeeze)
Release: 6.0.0
Codename: squeeze
"""


class SshProxmoxPluginTest(TestCase):

def test_add_prefix(self):
self.assertEqual(
_add_prefix('command123', 1),
'sudo command123'
)
self.assertEqual(
_add_prefix('command321', 2),
'sudo -u www-data command321'
)

def test_get_proxmox_version(self):
ssh = MockSSH([("lsb_release -a", LSB_RELEASE_1)])
self.assertEqual(
_get_proxmox_version(ssh),
1
)
ssh = MockSSH([("lsb_release -a", LSB_RELEASE_2)])
self.assertEqual(
_get_proxmox_version(ssh),
2
)

0 comments on commit da9e37c

Please sign in to comment.