]> git.proxmox.com Git - pve-manager.git/blob - PVE/CLI/pveceph.pm
pveceph: octopus is OK and wanted to be installed/used
[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 = qr/^(?:nautilus|octopus)$/;
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"
154 if $cephver !~ $default_vers;
155
156 local $ENV{DEBIAN_FRONTEND} = 'noninteractive';
157 print "update available package list\n";
158 eval {
159 run_command(
160 ['apt-get', '-q', 'update'],
161 outfunc => sub {},
162 errfunc => sub { print STDERR "$_[0]\n" },
163 )
164 };
165
166 my @apt_install = qw(apt-get --no-install-recommends -o Dpkg::Options::=--force-confnew install --);
167 my @ceph_packages = qw(
168 ceph
169 ceph-common
170 ceph-mds
171 ceph-fuse
172 gdisk
173 );
174
175 print "start installation\n";
176 if (system(@apt_install, @ceph_packages) != 0) {
177 die "apt failed during ceph installation ($?)\n";
178 }
179
180 print "\ninstalled ceph $cephver successfully\n";
181
182 return undef;
183 }});
184
185 __PACKAGE__->register_method ({
186 name => 'status',
187 path => 'status',
188 method => 'GET',
189 description => "Get Ceph Status.",
190 parameters => {
191 additionalProperties => 0,
192 },
193 returns => { type => 'null' },
194 code => sub {
195 PVE::Ceph::Tools::check_ceph_inited();
196
197 run_command(
198 ['ceph', '-s'],
199 outfunc => sub { print "$_[0]\n" },
200 errfunc => sub { print STDERR "$_[0]\n" },
201 timeout => 15,
202 );
203 return undef;
204 }});
205
206 our $cmddef = {
207 init => [ 'PVE::API2::Ceph', 'init', [], { node => $nodename } ],
208 pool => {
209 ls => [ 'PVE::API2::Ceph::Pools', 'lspools', [], { node => $nodename }, sub {
210 my ($data, $schema, $options) = @_;
211 PVE::CLIFormatter::print_api_result($data, $schema,
212 [
213 'pool_name',
214 'size',
215 'min_size',
216 'pg_num',
217 'pg_num_min',
218 'pg_num_final',
219 'pg_autoscale_mode',
220 'target_size',
221 'target_size_ratio',
222 'crush_rule_name',
223 'percent_used',
224 'bytes_used',
225 ],
226 $options);
227 }, $PVE::RESTHandler::standard_output_options],
228 create => [ 'PVE::API2::Ceph::Pools', 'createpool', ['name'], { node => $nodename }],
229 destroy => [ 'PVE::API2::Ceph::Pools', 'destroypool', ['name'], { node => $nodename } ],
230 set => [ 'PVE::API2::Ceph::Pools', 'setpool', ['name'], { node => $nodename } ],
231 get => [ 'PVE::API2::Ceph::Pools', 'getpool', ['name'], { node => $nodename }, sub {
232 my ($data, $schema, $options) = @_;
233 PVE::CLIFormatter::print_api_result($data, $schema, undef, $options);
234 }, $PVE::RESTHandler::standard_output_options],
235 },
236 lspools => { alias => 'pool ls' },
237 createpool => { alias => 'pool create' },
238 destroypool => { alias => 'pool destroy' },
239 fs => {
240 create => [ 'PVE::API2::Ceph::FS', 'createfs', [], { node => $nodename }],
241 },
242 osd => {
243 create => [ 'PVE::API2::Ceph::OSD', 'createosd', ['dev'], { node => $nodename }, $upid_exit],
244 destroy => [ 'PVE::API2::Ceph::OSD', 'destroyosd', ['osdid'], { node => $nodename }, $upid_exit],
245 },
246 createosd => { alias => 'osd create' },
247 destroyosd => { alias => 'osd destroy' },
248 mon => {
249 create => [ 'PVE::API2::Ceph::MON', 'createmon', [], { node => $nodename }, $upid_exit],
250 destroy => [ 'PVE::API2::Ceph::MON', 'destroymon', ['monid'], { node => $nodename }, $upid_exit],
251 },
252 createmon => { alias => 'mon create' },
253 destroymon => { alias => 'mon destroy' },
254 mgr => {
255 create => [ 'PVE::API2::Ceph::MGR', 'createmgr', [], { node => $nodename }, $upid_exit],
256 destroy => [ 'PVE::API2::Ceph::MGR', 'destroymgr', ['id'], { node => $nodename }, $upid_exit],
257 },
258 createmgr => { alias => 'mgr create' },
259 destroymgr => { alias => 'mgr destroy' },
260 mds => {
261 create => [ 'PVE::API2::Ceph::MDS', 'createmds', [], { node => $nodename }, $upid_exit],
262 destroy => [ 'PVE::API2::Ceph::MDS', 'destroymds', ['name'], { node => $nodename }, $upid_exit],
263 },
264 start => [ 'PVE::API2::Ceph', 'start', [], { node => $nodename }, $upid_exit],
265 stop => [ 'PVE::API2::Ceph', 'stop', [], { node => $nodename }, $upid_exit],
266 install => [ __PACKAGE__, 'install', [] ],
267 purge => [ __PACKAGE__, 'purge', [] ],
268 status => [ __PACKAGE__, 'status', []],
269 };
270
271 1;