]> git.proxmox.com Git - pve-storage.git/commitdiff
move Storage/CephTools to CephConfig
authorDominik Csapak <d.csapak@proxmox.com>
Wed, 19 Dec 2018 10:24:39 +0000 (11:24 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 20 Dec 2018 08:26:11 +0000 (09:26 +0100)
it is not a storage plugin, and it makes more sense to have it
top-level, but there we cannot name it CephTools because of the
existing ones in pve-manager

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
PVE/CephConfig.pm [new file with mode: 0644]
PVE/Makefile
PVE/Storage/CephFSPlugin.pm
PVE/Storage/CephTools.pm [deleted file]
PVE/Storage/Makefile
PVE/Storage/RBDPlugin.pm

diff --git a/PVE/CephConfig.pm b/PVE/CephConfig.pm
new file mode 100644 (file)
index 0000000..c851ea2
--- /dev/null
@@ -0,0 +1,179 @@
+package PVE::CephConfig;
+
+use strict;
+use warnings;
+use Net::IP;
+use PVE::Tools qw(run_command);
+
+my $parse_ceph_file = sub {
+    my ($filename) = @_;
+
+    my $cfg = {};
+
+    return $cfg if ! -f $filename;
+
+    my $content = PVE::Tools::file_get_contents($filename);
+    my @lines = split /\n/, $content;
+
+    my $section;
+
+    foreach my $line (@lines) {
+       $line =~ s/[;#].*$//;
+       $line =~ s/^\s+//;
+       $line =~ s/\s+$//;
+       next if !$line;
+
+       $section = $1 if $line =~ m/^\[(\S+)\]$/;
+       if (!$section) {
+           warn "no section - skip: $line\n";
+           next;
+       }
+
+       if ($line =~ m/^(.*?\S)\s*=\s*(\S.*)$/) {
+           $cfg->{$section}->{$1} = $2;
+       }
+
+    }
+
+    return $cfg;
+};
+
+my $ceph_get_key = sub {
+    my ($keyfile, $username) = @_;
+
+    my $key = $parse_ceph_file->($keyfile);
+    my $secret = $key->{"client.$username"}->{key};
+
+    return $secret;
+};
+
+sub get_monaddr_list {
+    my ($configfile) = shift;
+
+    my $server;
+
+    if (!defined($configfile)) {
+       warn "No ceph config specified\n";
+       return;
+    }
+
+    my $config = $parse_ceph_file->($configfile);
+    @$server = sort map { $config->{$_}->{'mon addr'} } grep {/mon/} %{$config};
+
+    return join(',', @$server);
+};
+
+sub hostlist {
+    my ($list_text, $separator) = @_;
+
+    my @monhostlist = PVE::Tools::split_list($list_text);
+    return join($separator, map {
+       my ($host, $port) = PVE::Tools::parse_host_and_port($_);
+       $port = defined($port) ? ":$port" : '';
+       $host = "[$host]" if Net::IP::ip_is_ipv6($host);
+       "${host}${port}"
+    } @monhostlist);
+}
+
+my $ceph_check_keyfile = sub {
+    my ($filename, $type) = @_;
+
+    return if ! -f $filename;
+
+    my $content = PVE::Tools::file_get_contents($filename);
+    eval {
+       die if !$content;
+
+       if ($type eq 'rbd') {
+           die if $content !~ /\s*\[\S+\]\s*key\s*=\s*\S+==\s*$/m;
+       } elsif ($type eq 'cephfs') {
+           die if $content !~ /\S+==\s*$/;
+       }
+    };
+    die "Not a proper $type authentication file: $filename\n" if $@;
+
+    return undef;
+};
+
+sub ceph_connect_option {
+    my ($scfg, $storeid, %options) = @_;
+
+    my $cmd_option = {};
+    my $ceph_storeid_conf = "/etc/pve/priv/ceph/${storeid}.conf";
+    my $pveceph_config = '/etc/pve/ceph.conf';
+    my $keyfile = "/etc/pve/priv/ceph/${storeid}.keyring";
+    $keyfile = "/etc/pve/priv/ceph/${storeid}.secret" if ($scfg->{type} eq 'cephfs');
+    my $pveceph_managed = !defined($scfg->{monhost});
+
+    $cmd_option->{ceph_conf} = $pveceph_config if $pveceph_managed;
+
+    $ceph_check_keyfile->($keyfile, $scfg->{type});
+
+    if (-e $ceph_storeid_conf) {
+       if ($pveceph_managed) {
+           warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
+       } else {
+           $cmd_option->{ceph_conf} = $ceph_storeid_conf;
+       }
+    }
+
+    $cmd_option->{keyring} = $keyfile if (-e $keyfile);
+    $cmd_option->{auth_supported} = (defined $cmd_option->{keyring}) ? 'cephx' : 'none';
+    $cmd_option->{userid} =  $scfg->{username} ? $scfg->{username} : 'admin';
+    $cmd_option->{mon_host} = hostlist($scfg->{monhost}, ',') if (defined($scfg->{monhost}));
+
+    if (%options) {
+       foreach my $k (keys %options) {
+           $cmd_option->{$k} = $options{$k};
+       }
+    }
+
+    return $cmd_option;
+
+}
+
+sub ceph_create_keyfile {
+    my ($type, $storeid) = @_;
+
+    my $extension = 'keyring';
+    $extension = 'secret' if ($type eq 'cephfs');
+
+    my $ceph_admin_keyring = '/etc/pve/priv/ceph.client.admin.keyring';
+    my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.$extension";
+
+    die "ceph authx keyring file for storage '$storeid' already exists!\n"
+       if -e $ceph_storage_keyring;
+
+    if (-e $ceph_admin_keyring) {
+       eval {
+           if ($type eq 'rbd') {
+               mkdir '/etc/pve/priv/ceph';
+               PVE::Tools::file_copy($ceph_admin_keyring, $ceph_storage_keyring);
+           } elsif ($type eq 'cephfs') {
+               my $secret = $ceph_get_key->($ceph_admin_keyring, 'admin');
+               mkdir '/etc/pve/priv/ceph';
+               PVE::Tools::file_set_contents($ceph_storage_keyring, $secret, 0400);
+          }
+       };
+       if (my $err = $@) {
+          unlink $ceph_storage_keyring;
+          die "failed to copy ceph authx $extension for storage '$storeid': $err\n";
+       }
+    } else {
+       warn "$ceph_admin_keyring not found, authentication is disabled.\n";
+    }
+}
+
+sub ceph_remove_keyfile {
+    my ($type, $storeid) = @_;
+
+    my $extension = 'keyring';
+    $extension = 'secret' if ($type eq 'cephfs');
+    my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.$extension";
+
+    if (-f $ceph_storage_keyring) {
+       unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";
+    }
+}
+
+1;
index ae2bd35efce19e0d1dc1a0e8b73ad00679502b64..ec7818edb88c232f549203fe92dcc9a34affd7b3 100644 (file)
@@ -4,6 +4,7 @@
 install:
        install -D -m 0644 Storage.pm ${DESTDIR}${PERLDIR}/PVE/Storage.pm
        install -D -m 0644 Diskmanage.pm ${DESTDIR}${PERLDIR}/PVE/Diskmanage.pm
+       install -D -m 0644 CephConfig.pm ${DESTDIR}${PERLDIR}/PVE/CephConfig.pm
        make -C Storage install
        make -C API2 install
        make -C CLI install
index ebd6eec76506b5a85e9bd9193a13405000998b74..f9e5acb5133225a47630a29d5689d7eb859e432a 100644 (file)
@@ -11,16 +11,16 @@ use PVE::Tools qw(run_command);
 use PVE::ProcFSTools;
 use PVE::Storage::Plugin;
 use PVE::JSONSchema qw(get_standard_option);
-use PVE::Storage::CephTools;
+use PVE::CephConfig;
 
 use base qw(PVE::Storage::Plugin);
 
 sub cephfs_is_mounted {
     my ($scfg, $storeid, $mountdata) = @_;
 
-    my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
+    my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
     my $configfile = $cmd_option->{ceph_conf};
-    my $server = $cmd_option->{mon_host} // PVE::Storage::CephTools::get_monaddr_list($configfile);
+    my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile);
 
     my $subdir = $scfg->{subdir} // '/';
     my $mountpoint = $scfg->{path};
@@ -46,10 +46,10 @@ sub cephfs_mount {
     my $mountpoint = $scfg->{path};
     my $subdir = $scfg->{subdir} // '/';
 
-    my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
+    my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
     my $configfile = $cmd_option->{ceph_conf};
     my $secretfile = $cmd_option->{keyring};
-    my $server = $cmd_option->{mon_host} // PVE::Storage::CephTools::get_monaddr_list($configfile);
+    my $server = $cmd_option->{mon_host} // PVE::CephConfig::get_monaddr_list($configfile);
 
     # fuse -> client-enforced quotas (kernel doesn't), updates w/ ceph-fuse pkg
     # kernel -> better performance, less frequent updates
@@ -137,7 +137,7 @@ sub on_add_hook {
 
     return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
 
-    PVE::Storage::CephTools::ceph_create_keyfile($scfg->{type}, $storeid);
+    PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid);
 }
 
 sub on_delete_hook {
@@ -145,7 +145,7 @@ sub on_delete_hook {
 
     return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
 
-    PVE::Storage::CephTools::ceph_remove_keyfile($scfg->{type}, $storeid);
+    PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
 }
 
 sub status {
diff --git a/PVE/Storage/CephTools.pm b/PVE/Storage/CephTools.pm
deleted file mode 100644 (file)
index 7f5260b..0000000
+++ /dev/null
@@ -1,179 +0,0 @@
-package PVE::Storage::CephTools;
-
-use strict;
-use warnings;
-use Net::IP;
-use PVE::Tools qw(run_command);
-
-my $parse_ceph_file = sub {
-    my ($filename) = @_;
-
-    my $cfg = {};
-
-    return $cfg if ! -f $filename;
-
-    my $content = PVE::Tools::file_get_contents($filename);
-    my @lines = split /\n/, $content;
-
-    my $section;
-
-    foreach my $line (@lines) {
-       $line =~ s/[;#].*$//;
-       $line =~ s/^\s+//;
-       $line =~ s/\s+$//;
-       next if !$line;
-
-       $section = $1 if $line =~ m/^\[(\S+)\]$/;
-       if (!$section) {
-           warn "no section - skip: $line\n";
-           next;
-       }
-
-       if ($line =~ m/^(.*?\S)\s*=\s*(\S.*)$/) {
-           $cfg->{$section}->{$1} = $2;
-       }
-
-    }
-
-    return $cfg;
-};
-
-my $ceph_get_key = sub {
-    my ($keyfile, $username) = @_;
-
-    my $key = $parse_ceph_file->($keyfile);
-    my $secret = $key->{"client.$username"}->{key};
-
-    return $secret;
-};
-
-sub get_monaddr_list {
-    my ($configfile) = shift;
-
-    my $server;
-
-    if (!defined($configfile)) {
-       warn "No ceph config specified\n";
-       return;
-    }
-
-    my $config = $parse_ceph_file->($configfile);
-    @$server = sort map { $config->{$_}->{'mon addr'} } grep {/mon/} %{$config};
-
-    return join(',', @$server);
-};
-
-sub hostlist {
-    my ($list_text, $separator) = @_;
-
-    my @monhostlist = PVE::Tools::split_list($list_text);
-    return join($separator, map {
-       my ($host, $port) = PVE::Tools::parse_host_and_port($_);
-       $port = defined($port) ? ":$port" : '';
-       $host = "[$host]" if Net::IP::ip_is_ipv6($host);
-       "${host}${port}"
-    } @monhostlist);
-}
-
-my $ceph_check_keyfile = sub {
-    my ($filename, $type) = @_;
-
-    return if ! -f $filename;
-
-    my $content = PVE::Tools::file_get_contents($filename);
-    eval {
-       die if !$content;
-
-       if ($type eq 'rbd') {
-           die if $content !~ /\s*\[\S+\]\s*key\s*=\s*\S+==\s*$/m;
-       } elsif ($type eq 'cephfs') {
-           die if $content !~ /\S+==\s*$/;
-       }
-    };
-    die "Not a proper $type authentication file: $filename\n" if $@;
-
-    return undef;
-};
-
-sub ceph_connect_option {
-    my ($scfg, $storeid, %options) = @_;
-
-    my $cmd_option = {};
-    my $ceph_storeid_conf = "/etc/pve/priv/ceph/${storeid}.conf";
-    my $pveceph_config = '/etc/pve/ceph.conf';
-    my $keyfile = "/etc/pve/priv/ceph/${storeid}.keyring";
-    $keyfile = "/etc/pve/priv/ceph/${storeid}.secret" if ($scfg->{type} eq 'cephfs');
-    my $pveceph_managed = !defined($scfg->{monhost});
-
-    $cmd_option->{ceph_conf} = $pveceph_config if $pveceph_managed;
-
-    $ceph_check_keyfile->($keyfile, $scfg->{type});
-
-    if (-e $ceph_storeid_conf) {
-       if ($pveceph_managed) {
-           warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n";
-       } else {
-           $cmd_option->{ceph_conf} = $ceph_storeid_conf;
-       }
-    }
-
-    $cmd_option->{keyring} = $keyfile if (-e $keyfile);
-    $cmd_option->{auth_supported} = (defined $cmd_option->{keyring}) ? 'cephx' : 'none';
-    $cmd_option->{userid} =  $scfg->{username} ? $scfg->{username} : 'admin';
-    $cmd_option->{mon_host} = hostlist($scfg->{monhost}, ',') if (defined($scfg->{monhost}));
-
-    if (%options) {
-       foreach my $k (keys %options) {
-           $cmd_option->{$k} = $options{$k};
-       }
-    }
-
-    return $cmd_option;
-
-}
-
-sub ceph_create_keyfile {
-    my ($type, $storeid) = @_;
-
-    my $extension = 'keyring';
-    $extension = 'secret' if ($type eq 'cephfs');
-
-    my $ceph_admin_keyring = '/etc/pve/priv/ceph.client.admin.keyring';
-    my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.$extension";
-
-    die "ceph authx keyring file for storage '$storeid' already exists!\n"
-       if -e $ceph_storage_keyring;
-
-    if (-e $ceph_admin_keyring) {
-       eval {
-           if ($type eq 'rbd') {
-               mkdir '/etc/pve/priv/ceph';
-               PVE::Tools::file_copy($ceph_admin_keyring, $ceph_storage_keyring);
-           } elsif ($type eq 'cephfs') {
-               my $secret = $ceph_get_key->($ceph_admin_keyring, 'admin');
-               mkdir '/etc/pve/priv/ceph';
-               PVE::Tools::file_set_contents($ceph_storage_keyring, $secret, 0400);
-          }
-       };
-       if (my $err = $@) {
-          unlink $ceph_storage_keyring;
-          die "failed to copy ceph authx $extension for storage '$storeid': $err\n";
-       }
-    } else {
-       warn "$ceph_admin_keyring not found, authentication is disabled.\n";
-    }
-}
-
-sub ceph_remove_keyfile {
-    my ($type, $storeid) = @_;
-
-    my $extension = 'keyring';
-    $extension = 'secret' if ($type eq 'cephfs');
-    my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.$extension";
-
-    if (-f $ceph_storage_keyring) {
-       unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";
-    }
-}
-
-1;
index c7f423fc87a6cebd22751dc9f3300446a9556a3f..ad69532eb2f7ae93c6c96561d309705d032efd49 100644 (file)
@@ -1,4 +1,4 @@
-SOURCES=Plugin.pm DirPlugin.pm LVMPlugin.pm NFSPlugin.pm CIFSPlugin.pm ISCSIPlugin.pm CephFSPlugin.pm RBDPlugin.pm CephTools.pm SheepdogPlugin.pm ISCSIDirectPlugin.pm GlusterfsPlugin.pm ZFSPoolPlugin.pm ZFSPlugin.pm DRBDPlugin.pm LvmThinPlugin.pm
+SOURCES=Plugin.pm DirPlugin.pm LVMPlugin.pm NFSPlugin.pm CIFSPlugin.pm ISCSIPlugin.pm CephFSPlugin.pm RBDPlugin.pm SheepdogPlugin.pm ISCSIDirectPlugin.pm GlusterfsPlugin.pm ZFSPoolPlugin.pm ZFSPlugin.pm DRBDPlugin.pm LvmThinPlugin.pm
 
 .PHONY: install
 install:
index c93071cf31512c60e199b0e39ec12570c657be2d..8433715e827e6f416881ce89a8b500ebbc446963 100644 (file)
@@ -8,7 +8,7 @@ use PVE::Tools qw(run_command trim);
 use PVE::Storage::Plugin;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RADOS;
-use PVE::Storage::CephTools;
+use PVE::CephConfig;
 use JSON;
 
 use base qw(PVE::Storage::Plugin);
@@ -30,7 +30,7 @@ my $add_pool_to_disk = sub {
 my $build_cmd = sub {
     my ($binary, $scfg, $storeid, $op, @options) = @_;
 
-    my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
+    my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
     my $pool =  $scfg->{pool} ? $scfg->{pool} : 'rbd';
 
     my $cmd = [$binary, '-p', $pool];
@@ -63,7 +63,7 @@ my $rados_cmd = sub {
 my $librados_connect = sub {
     my ($scfg, $storeid, $options) = @_;
 
-    my $librados_config = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
+    my $librados_config = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
 
     my $rados = PVE::RADOS->new(%$librados_config);
 
@@ -283,7 +283,7 @@ sub on_add_hook {
 
     return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
 
-    PVE::Storage::CephTools::ceph_create_keyfile($scfg->{type}, $storeid);
+    PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid);
 }
 
 sub on_delete_hook {
@@ -291,7 +291,7 @@ sub on_delete_hook {
 
     return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
 
-    PVE::Storage::CephTools::ceph_remove_keyfile($scfg->{type}, $storeid);
+    PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
 }
 
 sub parse_volname {
@@ -307,7 +307,7 @@ sub parse_volname {
 sub path {
     my ($class, $scfg, $volname, $storeid, $snapname) = @_;
 
-    my $cmd_option = PVE::Storage::CephTools::ceph_connect_option($scfg, $storeid);
+    my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
     $name .= '@'.$snapname if $snapname;
 
@@ -318,7 +318,7 @@ sub path {
 
     $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf};
     if (defined($scfg->{monhost})) {
-       my $monhost = PVE::Storage::CephTools::hostlist($scfg->{monhost}, ';');
+       my $monhost = PVE::CephConfig::hostlist($scfg->{monhost}, ';');
        $monhost =~ s/:/\\:/g;
        $path .= ":mon_host=$monhost";
        $path .= ":auth_supported=$cmd_option->{auth_supported}";