]> git.proxmox.com Git - pve-manager.git/blob - PVE/CLI/pveceph.pm
pveceph: change status from long JSON to ceph -s
[pve-manager.git] / PVE / CLI / pveceph.pm
1 package PVE::CLI::pveceph;
2
3 use strict;
4 use warnings;
5
6 use Fcntl ':flock';
7 use File::Path;
8 use IO::File;
9 use JSON;
10 use Data::Dumper;
11 use LWP::UserAgent;
12
13 use PVE::SafeSyslog;
14 use PVE::Cluster;
15 use PVE::INotify;
16 use PVE::RPCEnvironment;
17 use PVE::Storage;
18 use PVE::Tools qw(run_command);
19 use PVE::JSONSchema qw(get_standard_option);
20 use PVE::Ceph::Tools;
21 use PVE::Ceph::Services;
22 use PVE::API2::Ceph;
23 use PVE::API2::Ceph::FS;
24 use PVE::API2::Ceph::MDS;
25 use PVE::API2::Ceph::MGR;
26 use PVE::API2::Ceph::MON;
27 use PVE::API2::Ceph::OSD;
28
29 use PVE::CLIHandler;
30
31 use base qw(PVE::CLIHandler);
32
33 my $nodename = PVE::INotify::nodename();
34
35 my $upid_exit = sub {
36 my $upid = shift;
37 my $status = PVE::Tools::upid_read_status($upid);
38 exit($status eq 'OK' ? 0 : -1);
39 };
40
41 sub setup_environment {
42 PVE::RPCEnvironment->setup_default_cli_env();
43 }
44
45 __PACKAGE__->register_method ({
46 name => 'purge',
47 path => 'purge',
48 method => 'POST',
49 description => "Destroy ceph related data and configuration files.",
50 parameters => {
51 additionalProperties => 0,
52 properties => {
53 logs => {
54 description => 'Additionally purge Ceph logs, /var/log/ceph.',
55 type => 'boolean',
56 optional => 1,
57 },
58 crash => {
59 description => 'Additionally purge Ceph crash logs, /var/lib/ceph/crash.',
60 type => 'boolean',
61 optional => 1,
62 },
63 },
64 },
65 returns => { type => 'null' },
66 code => sub {
67 my ($param) = @_;
68
69 my $message;
70 my $pools = [];
71 my $monstat = {};
72 my $mdsstat = {};
73 my $osdstat = [];
74
75 eval {
76 my $rados = PVE::RADOS->new();
77 $pools = PVE::Ceph::Tools::ls_pools(undef, $rados);
78 $monstat = PVE::Ceph::Services::get_services_info('mon', undef, $rados);
79 $mdsstat = PVE::Ceph::Services::get_services_info('mds', undef, $rados);
80 $osdstat = $rados->mon_command({ prefix => 'osd metadata' });
81 };
82 warn "Error gathering ceph info, already purged? Message: $@" if $@;
83
84 my $osd = grep { $_->{hostname} eq $nodename } @$osdstat;
85 my $mds = grep { $mdsstat->{$_}->{host} eq $nodename } keys %$mdsstat;
86 my $mon = grep { $monstat->{$_}->{host} eq $nodename } keys %$monstat;
87
88 # no pools = no data
89 $message .= "- remove pools, this will !!DESTROY DATA!!\n" if @$pools;
90 $message .= "- remove active OSD on $nodename\n" if $osd;
91 $message .= "- remove active MDS on $nodename\n" if $mds;
92 $message .= "- remove other MONs, $nodename is not the last MON\n"
93 if scalar(keys %$monstat) > 1 && $mon;
94
95 # display all steps at once
96 die "Unable to purge Ceph!\n\nTo continue:\n$message" if $message;
97
98 my $services = PVE::Ceph::Services::get_local_services();
99 $services->{mon} = $monstat if $mon;
100 $services->{crash}->{$nodename} = { direxists => 1 } if $param->{crash};
101 $services->{logs}->{$nodename} = { direxists => 1 } if $param->{logs};
102
103 PVE::Ceph::Tools::purge_all_ceph_services($services);
104 PVE::Ceph::Tools::purge_all_ceph_files($services);
105
106 return undef;
107 }});
108
109 __PACKAGE__->register_method ({
110 name => 'install',
111 path => 'install',
112 method => 'POST',
113 description => "Install ceph related packages.",
114 parameters => {
115 additionalProperties => 0,
116 properties => {
117 version => {
118 type => 'string',
119 # for buster, luminous kept for testing/upgrade purposes only! - FIXME: remove with 6.2?
120 enum => ['luminous', 'nautilus', 'octopus'],
121 default => 'nautilus',
122 description => "Ceph version to install.",
123 optional => 1,
124 },
125 'allow-experimental' => {
126 type => 'boolean',
127 default => 0,
128 optional => 1,
129 description => "Allow experimental versions. Use with care!",
130 },
131 },
132 },
133 returns => { type => 'null' },
134 code => sub {
135 my ($param) = @_;
136
137 my $default_vers = 'nautilus';
138 my $cephver = $param->{version} || $default_vers;
139
140 my $repolist;
141 if ($cephver eq 'nautilus') {
142 $repolist = "deb http://download.proxmox.com/debian/ceph-nautilus buster main\n";
143 } elsif ($cephver eq 'luminous') {
144 die "Not allowed to select version '$cephver'\n" if !$param->{'allow-experimental'};
145 $repolist = "deb http://download.proxmox.com/debian/ceph-luminous buster main\n";
146 } elsif ($cephver eq 'octopus') {
147 $repolist = "deb http://download.proxmox.com/debian/ceph-octopus buster main\n";
148 } else {
149 die "not implemented ceph version: $cephver";
150 }
151 PVE::Tools::file_set_contents("/etc/apt/sources.list.d/ceph.list", $repolist);
152
153 warn "WARNING: installing non-default ceph release '$cephver'!\n\n" if $cephver ne $default_vers;
154
155 local $ENV{DEBIAN_FRONTEND} = 'noninteractive';
156 print "update available package list\n";
157 eval { run_command(['apt-get', '-q', 'update'], outfunc => sub {}, errfunc => sub { print STDERR "$_[0]\n" }) };
158
159 my @apt_install = qw(apt-get --no-install-recommends -o Dpkg::Options::=--force-confnew install --);
160 my @ceph_packages = qw(
161 ceph
162 ceph-common
163 ceph-mds
164 ceph-fuse
165 gdisk
166 );
167
168 print "start installation\n";
169 if (system(@apt_install, @ceph_packages) != 0) {
170 die "apt failed during ceph installation ($?)\n";
171 }
172
173 print "\ninstalled ceph $cephver successfully\n";
174
175 return undef;
176 }});
177
178 __PACKAGE__->register_method ({
179 name => 'status',
180 path => 'status',
181 method => 'GET',
182 description => "Get Ceph Status.",
183 parameters => {
184 additionalProperties => 0,
185 },
186 returns => { type => 'null' },
187 code => sub {
188 PVE::Ceph::Tools::check_ceph_inited();
189
190 run_command(
191 ['ceph', '-s'],
192 outfunc => sub { print "$_[0]\n" },
193 errfunc => sub { print STDERR "$_[0]\n" }
194 );
195 return undef;
196 }});
197
198 our $cmddef = {
199 init => [ 'PVE::API2::Ceph', 'init', [], { node => $nodename } ],
200 pool => {
201 ls => [ 'PVE::API2::Ceph', 'lspools', [], { node => $nodename }, sub {
202 my ($data, $schema, $options) = @_;
203 PVE::CLIFormatter::print_api_result($data, $schema,
204 [
205 'pool_name',
206 'size',
207 'min_size',
208 'pg_num',
209 'pg_autoscale_mode',
210 'crush_rule_name',
211 'percent_used',
212 'bytes_used',
213 ],
214 $options);
215 }, $PVE::RESTHandler::standard_output_options],
216 create => [ 'PVE::API2::Ceph', 'createpool', ['name'], { node => $nodename }],
217 destroy => [ 'PVE::API2::Ceph', 'destroypool', ['name'], { node => $nodename } ],
218 set => [ 'PVE::API2::Ceph', 'setpool', ['name'], { node => $nodename } ],
219 },
220 lspools => { alias => 'pool ls' },
221 createpool => { alias => 'pool create' },
222 destroypool => { alias => 'pool destroy' },
223 fs => {
224 create => [ 'PVE::API2::Ceph::FS', 'createfs', [], { node => $nodename }],
225 },
226 osd => {
227 create => [ 'PVE::API2::Ceph::OSD', 'createosd', ['dev'], { node => $nodename }, $upid_exit],
228 destroy => [ 'PVE::API2::Ceph::OSD', 'destroyosd', ['osdid'], { node => $nodename }, $upid_exit],
229 },
230 createosd => { alias => 'osd create' },
231 destroyosd => { alias => 'osd destroy' },
232 mon => {
233 create => [ 'PVE::API2::Ceph::MON', 'createmon', [], { node => $nodename }, $upid_exit],
234 destroy => [ 'PVE::API2::Ceph::MON', 'destroymon', ['monid'], { node => $nodename }, $upid_exit],
235 },
236 createmon => { alias => 'mon create' },
237 destroymon => { alias => 'mon destroy' },
238 mgr => {
239 create => [ 'PVE::API2::Ceph::MGR', 'createmgr', [], { node => $nodename }, $upid_exit],
240 destroy => [ 'PVE::API2::Ceph::MGR', 'destroymgr', ['id'], { node => $nodename }, $upid_exit],
241 },
242 createmgr => { alias => 'mgr create' },
243 destroymgr => { alias => 'mgr destroy' },
244 mds => {
245 create => [ 'PVE::API2::Ceph::MDS', 'createmds', [], { node => $nodename }, $upid_exit],
246 destroy => [ 'PVE::API2::Ceph::MDS', 'destroymds', ['name'], { node => $nodename }, $upid_exit],
247 },
248 start => [ 'PVE::API2::Ceph', 'start', [], { node => $nodename }, $upid_exit],
249 stop => [ 'PVE::API2::Ceph', 'stop', [], { node => $nodename }, $upid_exit],
250 install => [ __PACKAGE__, 'install', [] ],
251 purge => [ __PACKAGE__, 'purge', [] ],
252 status => [ __PACKAGE__, 'status', []],
253 };
254
255 1;