]> git.proxmox.com Git - pve-manager.git/blob - bin/pve-init-ceph-crash
update shipped appliance info index
[pve-manager.git] / bin / pve-init-ceph-crash
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use List::Util qw(first);
7
8 use PVE::Ceph::Tools;
9 use PVE::Cluster;
10 use PVE::RADOS;
11 use PVE::RPCEnvironment;
12
13 my $ceph_cfg_file = 'ceph.conf';
14 my $keyring_value = '/etc/pve/ceph/$cluster.$name.keyring';
15
16
17 sub try_adapt_cfg {
18 my ($cfg) = @_;
19
20 my $entity = 'client.crash';
21 my $removed_key = 0;
22
23 print("Checking whether the configuration for '$entity' needs to be updated.\n");
24
25 my $add_keyring = sub {
26 print("Setting keyring path to '$keyring_value'.\n");
27 $cfg->{$entity}->{keyring} = $keyring_value;
28 };
29
30 if (!exists($cfg->{$entity})) {
31 print("Adding missing section for '$entity'.\n");
32 $add_keyring->();
33 return 1;
34 }
35
36 if (exists($cfg->{$entity}->{key})) {
37 print("Removing existing usage of key.\n");
38 delete($cfg->{$entity}->{key});
39 $removed_key = 1;
40 }
41
42 if (!exists($cfg->{$entity}->{keyring})) {
43 print("Keyring path is missing from configuration.\n");
44 $add_keyring->();
45 return 1;
46 }
47
48 my $current_keyring_value = $cfg->{$entity}->{keyring};
49 if ($current_keyring_value ne $keyring_value) {
50 print("Current keyring path differs from expected path.\n");
51 $add_keyring->();
52 return 1;
53 }
54
55 return $removed_key;
56 }
57
58
59 sub main {
60 # PVE::RADOS expects an active RPC Environment because it forks itself
61 # and may want to clean up after
62 my $rpcenv = PVE::RPCEnvironment->setup_default_cli_env();
63
64 if (!PVE::Ceph::Tools::check_ceph_installed('ceph_bin', 1)) {
65 print("Ceph is not installed. No action required.\n");
66 exit 0;
67 }
68
69 my $ceph_cfg_path = PVE::Ceph::Tools::get_config('pve_ceph_cfgpath');
70 if (PVE::Ceph::Tools::check_ceph_installed('ceph_mon', 1) && -f $ceph_cfg_path) {
71 my $pve_ceph_cfgdir = PVE::Ceph::Tools::get_config('pve_ceph_cfgdir');
72 if (! -d $pve_ceph_cfgdir) {
73 File::Path::make_path($pve_ceph_cfgdir);
74 }
75 }
76
77 eval {
78 PVE::Ceph::Tools::check_ceph_inited();
79 };
80 if ($@) {
81 print("Ceph is not initialized. No action required.\n");
82 exit 0;
83 }
84
85 my $rados = eval { PVE::RADOS->new() };
86 my $ceph_crash_key_path = PVE::Ceph::Tools::get_config('pve_ceph_crash_key_path');
87
88 my $inner_err = '';
89
90 my $rval = PVE::Cluster::cfs_lock_file($ceph_cfg_file, undef, sub {
91 eval {
92 my $cfg = PVE::Cluster::cfs_read_file($ceph_cfg_file);
93
94 if (!defined($rados)) {
95 my $has_mon_host = defined($cfg->{global}) && defined($cfg->{global}->{mon_host});
96 if ($has_mon_host && $cfg->{global}->{mon_host} ne '') {
97 die "Connection to RADOS failed even though a monitor is configured.\n" .
98 "Please verify whether your configuration in '$ceph_cfg_file' is correct.\n"
99 }
100
101 print(
102 "Connection to RADOS failed and no monitor is configured in '$ceph_cfg_file'.\n".
103 "Assuming that things are fine. No action required.\n"
104 );
105 return;
106 }
107
108 my $updated_keyring = PVE::Ceph::Tools::create_or_update_crash_keyring_file($rados);
109
110 if ($updated_keyring) {
111 print("Keyring file '$ceph_crash_key_path' was updated.\n");
112 }
113
114 my $changed = try_adapt_cfg($cfg);
115
116 if ($changed) {
117 print("Committing updated configuration to '$ceph_cfg_file'.\n");
118 PVE::Cluster::cfs_write_file($ceph_cfg_file, $cfg);
119 print("Successfully updated configuration for 'ceph-crash.service'.\n");
120 } else {
121 print("Configuration in '$ceph_cfg_file' does not need to be updated.\n");
122 }
123 };
124 $inner_err = $@;
125
126 return 1;
127 });
128
129 # cfs_lock_file sets $@ explicitly to undef
130 my $err = $@ // '';
131
132 my $has_err = !defined($rval) || $inner_err || $err;
133
134 if ($has_err) {
135 $err =~ s/\n*$//;
136 $inner_err =~ s/\n*$//;
137
138 if (!defined($rval)) {
139 warn("Error while acquiring or releasing lock for '$ceph_cfg_file'.\n");
140 warn("Error: $err\n") if $err ne '';
141 }
142
143 warn("Failed to configure keyring for 'ceph-crash.service'.\nError: $inner_err\n")
144 if $inner_err ne '';
145
146 exit 1;
147 }
148 }
149
150 main();