]> git.proxmox.com Git - pve-manager.git/blobdiff - PVE/API2Tools.pm
update shipped appliance info index
[pve-manager.git] / PVE / API2Tools.pm
index 7b4c425a7555f25996d350bc0cb3e1f156a0b9e5..a56eb73253f736ebb39e95aee49096618cb1bcab 100644 (file)
@@ -2,32 +2,45 @@ package PVE::API2Tools;
 
 use strict;
 use warnings;
+
+use Digest::MD5 qw(md5_hex);
+use File::stat;
 use Net::IP;
+use URI::Escape;
+use URI;
 
+use PVE::Cluster;
+use PVE::DataCenterConfig; # so we can cfs-read datacenter.cfg
 use PVE::Exception qw(raise_param_exc);
-use PVE::Tools;
 use PVE::INotify;
-use PVE::Cluster;
-use Digest::MD5 qw(md5_hex);
-use URI;
-use URI::Escape;
+use PVE::RPCEnvironment;
 use PVE::SafeSyslog;
+use PVE::Storage::Plugin;
+use PVE::Tools;
 
 my $hwaddress;
+my $hwaddress_st = {};
 
 sub get_hwaddress {
-    
-    return $hwaddress if defined ($hwaddress);
-
     my $fn = '/etc/ssh/ssh_host_rsa_key.pub';
+    my $st = stat($fn);
+
+    if (defined($hwaddress)
+       && $hwaddress_st->{mtime} == $st->mtime
+       && $hwaddress_st->{ino} == $st->ino
+       && $hwaddress_st->{dev} == $st->dev) {
+       return $hwaddress;
+    }
+
     my $sshkey = PVE::Tools::file_get_contents($fn);
     $hwaddress = uc(md5_hex($sshkey));
+    $hwaddress_st->@{'mtime', 'ino', 'dev'} = ($st->mtime, $st->ino, $st->dev);
 
     return $hwaddress;
 }
 
 sub extract_node_stats {
-    my ($node, $members, $rrd) = @_;
+    my ($node, $members, $rrd, $exclude_stats) = @_;
 
     my $entry = {
        id => "node/$node",
@@ -37,19 +50,23 @@ sub extract_node_stats {
     };
 
     if (my $d = $rrd->{"pve2-node/$node"}) {
-                   
+
        if (!$members || # no cluster
            ($members->{$node} && $members->{$node}->{online})) {
-           $entry->{uptime} = ($d->[0] || 0) + 0;
-           $entry->{cpu} = ($d->[5] || 0) + 0;
-           $entry->{mem} = ($d->[8] || 0) + 0;
-           $entry->{disk} = ($d->[12] || 0) + 0;
+           if (!$exclude_stats) {
+               $entry->{uptime} = ($d->[0] || 0) + 0;
+               $entry->{cpu} = ($d->[5] || 0) + 0;
+               $entry->{mem} = ($d->[8] || 0) + 0;
+               $entry->{disk} = ($d->[12] || 0) + 0;
+           }
            $entry->{status} = 'online';
        }
        $entry->{level} = $d->[1];
-       $entry->{maxcpu} = ($d->[4] || 0) + 0;
-       $entry->{maxmem} = ($d->[7] || 0) + 0;
-       $entry->{maxdisk} = ($d->[11] || 0) + 0;
+       if (!$exclude_stats) {
+           $entry->{maxcpu} = ($d->[4] || 0) + 0;
+           $entry->{maxmem} = ($d->[7] || 0) + 0;
+           $entry->{maxdisk} = ($d->[11] || 0) + 0;
+       }
     }
 
     if ($members && $members->{$node} &&
@@ -65,7 +82,7 @@ sub extract_vm_stats {
 
     my $entry = {
        id => "$data->{type}/$vmid",
-       vmid => $vmid + 0, 
+       vmid => $vmid + 0,
        node => $data->{node},
        type => $data->{type},
        status => 'unknown',
@@ -88,7 +105,7 @@ sub extract_vm_stats {
        $entry->{netout} = ($d->[10] || 0) + 0;
        $entry->{diskread} = ($d->[11] || 0) + 0;
        $entry->{diskwrite} = ($d->[12] || 0) + 0;
-    
+
     } elsif ($d = $rrd->{"pve2.3-vm/$vmid"}) {
 
        $entry->{uptime} = ($d->[0] || 0) + 0;
@@ -114,13 +131,18 @@ sub extract_vm_stats {
 sub extract_storage_stats {
     my ($storeid, $scfg, $node, $rrd) = @_;
 
+    my $content = PVE::Storage::Plugin::content_hash_to_string($scfg->{content});
+
     my $entry = {
        id => "storage/$node/$storeid",
-       storage => $storeid, 
-       node => $node, 
-       type => 'storage', 
+       storage => $storeid,
+       node => $node,
+       type => 'storage',
+       plugintype => $scfg->{type},
        status => 'unknown',
-    }; 
+       shared => $scfg->{shared} || 0,
+       content => $content,
+    };
 
     if (my $d = $rrd->{"pve2-storage/$node/$storeid"}) {
        $entry->{maxdisk} = ($d->[1] || 0) + 0;
@@ -159,14 +181,14 @@ sub run_spiceterm {
     my $nodename = PVE::INotify::nodename();
     my $family = PVE::Tools::get_host_address_family($nodename);
     my $port = PVE::Tools::next_spice_port($family);
-    
-    my ($ticket, undef, $remote_viewer_config) = 
+
+    my ($ticket, undef, $remote_viewer_config) =
        PVE::AccessControl::remote_viewer_config($authuser, $vmid, $node, $proxy, $title, $port);
 
-    my $timeout = 40; 
+    my $timeout = 40;
 
-    my $cmd = ['/usr/bin/spiceterm', '--port', $port, '--addr', '127.0.0.1',
-              '--timeout', $timeout, '--authpath', $authpath, 
+    my $cmd = ['/usr/bin/spiceterm', '--port', $port, '--addr', 'localhost',
+              '--timeout', $timeout, '--authpath', $authpath,
               '--permissions', $permissions];
 
     my $dcconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
@@ -210,47 +232,6 @@ sub run_spiceterm {
     return $remote_viewer_config;
 }
 
-sub read_proxy_config {
-
-    my $conffile = "/etc/default/pveproxy";
-
-    # Note: evaluate with bash 
-    my $shcmd = ". $conffile;\n";
-    $shcmd .= 'echo \"ALLOW_FROM:\$ALLOW_FROM\";';
-    $shcmd .= 'echo \"DENY_FROM:\$DENY_FROM\";';
-    $shcmd .= 'echo \"POLICY:\$POLICY\";';
-    $shcmd .= 'echo \"CIPHERS:\$CIPHERS\";';
-    $shcmd .= 'echo \"DHPARAMS:\$DHPARAMS\";';
-
-    my $data = -f $conffile ? `bash -c "$shcmd"` : '';
-
-    my $res = {};
-
-    while ($data =~ s/^(.*)\n//) {
-       my ($key, $value) = split(/:/, $1, 2);
-       next if !$value;
-       if ($key eq 'ALLOW_FROM' || $key eq 'DENY_FROM') {
-           my $ips = [];
-           foreach my $ip (split(/,/, $value)) {
-               $ip = "0/0" if $ip eq 'all';
-               push @$ips, Net::IP->new($ip) || die Net::IP::Error() . "\n";
-           }
-           $res->{$key} = $ips;
-       } elsif ($key eq 'POLICY') {
-           die "unknown policy '$value'\n" if $value !~ m/^(allow|deny)$/;
-           $res->{$key} = $value;
-       } elsif ($key eq 'CIPHERS') {
-           $res->{$key} = $value;
-       } elsif ($key eq 'DHPARAMS') {
-           $res->{$key} = $value;
-       } else {
-           # silently skip everythin else?
-       }
-    }
-
-    return $res;
-}
-
 sub resolve_proxyto {
     my ($rpcenv, $proxyto_callback, $proxyto, $uri_param) = @_;
 
@@ -261,10 +242,27 @@ sub resolve_proxyto {
            if !$node;
     } else {
        $node = $uri_param->{$proxyto};
-       raise_param_exc({ $proxyto =>  "proxyto parameter does not exists"})
+       raise_param_exc({ $proxyto =>  "proxyto parameter does not exist"})
            if !$node;
     }
     return $node;
 }
 
+sub get_resource_pool_guest_members {
+    my ($pool) = @_;
+
+    my $usercfg = PVE::Cluster::cfs_read_file("user.cfg");
+
+    my $vmlist = PVE::Cluster::get_vmlist() || {};
+    my $idlist = $vmlist->{ids} || {};
+
+    my $data = $usercfg->{pools}->{$pool};
+
+    die "pool '$pool' does not exist\n" if !$data;
+
+    my $pool_members = [ grep { $idlist->{$_} } keys %{$data->{vms}} ];
+
+    return $pool_members;
+}
+
 1;