pveceph: fix purge command
[pve-manager.git] / bin / pveceph
CommitLineData
86ec9322 1#!/usr/bin/perl -T
22415a0c
DM
2
3use strict;
4use warnings;
5use Getopt::Long;
6use Fcntl ':flock';
7use File::Path;
8use IO::File;
9use JSON;
10use Data::Dumper;
11
12use PVE::SafeSyslog;
13use PVE::Cluster;
14use PVE::INotify;
15use PVE::RPCEnvironment;
16use PVE::Storage;
17use PVE::Tools qw(run_command);
18use PVE::JSONSchema qw(get_standard_option);
35a72468 19use PVE::CephTools;
b5e97adc 20use PVE::API2::Ceph;
22415a0c
DM
21
22use PVE::CLIHandler;
23
24use base qw(PVE::CLIHandler);
25
26$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
27
28initlog ('pveceph');
29
30die "please run as root\n" if $> != 0;
31
32PVE::INotify::inotify_init();
33
34my $rpcenv = PVE::RPCEnvironment->init('cli');
35
36$rpcenv->init_request();
37$rpcenv->set_language($ENV{LANG});
38$rpcenv->set_user('root@pam');
39
e67078ee
DM
40my $upid_exit = sub {
41 my $upid = shift;
42 my $status = PVE::Tools::upid_read_status($upid);
43 exit($status eq 'OK' ? 0 : -1);
44};
45
22415a0c
DM
46my $nodename = PVE::INotify::nodename();
47
22415a0c
DM
48__PACKAGE__->register_method ({
49 name => 'purge',
50 path => 'purge',
51 method => 'POST',
52 description => "Destroy ceph related data and configuration files.",
53 parameters => {
54 additionalProperties => 0,
55 properties => {
56 },
57 },
58 returns => { type => 'null' },
59 code => sub {
60 my ($param) = @_;
61
62 my $monstat;
63
35a72468
DM
64 eval {
65 my $rados = PVE::RADOS->new();
66 my $monstat = $rados->mon_command({ prefix => 'mon_status' });
67 };
22415a0c
DM
68 my $err = $@;
69
70 die "detected running ceph services- unable to purge data\n"
71 if !$err;
72
73 # fixme: this is dangerous - should we really support this function?
35a72468 74 PVE::CephTools::purge_all_ceph_files();
22415a0c
DM
75
76 return undef;
77 }});
78
79__PACKAGE__->register_method ({
80 name => 'install',
81 path => 'install',
82 method => 'POST',
83 description => "Install ceph related packages.",
84 parameters => {
85 additionalProperties => 0,
86 properties => {
e69bd5aa
DM
87 version => {
88 type => 'string',
89 enum => ['dumpling', 'emperor', 'firefly'],
90 optional => 1,
91 }
22415a0c
DM
92 },
93 },
94 returns => { type => 'null' },
95 code => sub {
96 my ($param) = @_;
97
e69bd5aa 98 my $cephver = $param->{version} || 'dumpling';
22415a0c
DM
99
100 local $ENV{DEBIAN_FRONTEND} = 'noninteractive';
101
102 my $keyurl = "https://ceph.com/git/?p=ceph.git;a=blob_plain;f=keys/release.asc";
103
104 print "download and import ceph reqpository keys\n";
105 system("wget -q -O- '$keyurl'| apt-key add - 2>&1 >/dev/null") == 0 ||
106 die "unable to download ceph release key\n";
107
108
109 my $source = "deb http://ceph.com/debian-$cephver wheezy main\n";
110
111 PVE::Tools::file_set_contents("/etc/apt/sources.list.d/ceph.list", $source);
112
113 print "update available package list\n";
114 eval { run_command(['apt-get', '-q', 'update'], outfunc => sub {}, errfunc => sub {}); };
115
116 run_command(['apt-get', '-q', '--assume-yes', '--no-install-recommends',
117 '-o', 'Dpkg::Options::=--force-confnew',
118 'install', '--',
119 'ceph', 'ceph-common', 'gdisk']);
120
121 return undef;
122 }});
123
22415a0c 124my $cmddef = {
b613c189 125 init => [ 'PVE::API2::Ceph', 'init', [], { node => $nodename } ],
d48a24f6
DM
126 lspools => [ 'PVE::API2::Ceph', 'lspools', [], { node => $nodename }, sub {
127 my $res = shift;
128
0c7ba3e3 129 printf("%-20s %10s %10s %20s\n", "Name", "size", "pg_num", "used");
d48a24f6 130 foreach my $p (sort {$a->{pool_name} cmp $b->{pool_name}} @$res) {
0c7ba3e3 131 printf("%-20s %10d %10d %20d\n", $p->{pool_name}, $p->{size}, $p->{pg_num}, $p->{bytes_used});
d48a24f6
DM
132 }
133 }],
134 createpool => [ 'PVE::API2::Ceph', 'createpool', ['name'], { node => $nodename }],
135 destroypool => [ 'PVE::API2::Ceph', 'destroypool', ['name'], { node => $nodename } ],
01777b31
DM
136 createosd => [ 'PVE::API2::CephOSD', 'createosd', ['dev'], { node => $nodename }, $upid_exit],
137 destroyosd => [ 'PVE::API2::CephOSD', 'destroyosd', ['osdid'], { node => $nodename }, $upid_exit],
3b51fd13
DM
138 createmon => [ 'PVE::API2::Ceph', 'createmon', [], { node => $nodename }, $upid_exit],
139 destroymon => [ 'PVE::API2::Ceph', 'destroymon', ['monid'], { node => $nodename }, $upid_exit],
140 start => [ 'PVE::API2::Ceph', 'start', ['service'], { node => $nodename }, $upid_exit],
141 stop => [ 'PVE::API2::Ceph', 'stop', ['service'], { node => $nodename }, $upid_exit],
b5e97adc 142 install => [ __PACKAGE__, 'install', [] ],
22415a0c 143 purge => [ __PACKAGE__, 'purge', [] ],
b613c189 144 status => [ 'PVE::API2::Ceph', 'status', [], { node => $nodename }, sub {
22415a0c
DM
145 my $res = shift;
146 my $json = JSON->new->allow_nonref;
147 print $json->pretty->encode($res) . "\n";
148 }],
149};
150
151my $cmd = shift;
152
153PVE::CLIHandler::handle_cmd($cmddef, "pveceph", $cmd, \@ARGV, undef, $0);
154
155exit 0;
156
157__END__
158
159=head1 NAME
160
161pveceph - tool to manage ceph services on pve nodes
162
163=head1 SYNOPSIS
164
165=include synopsis
166
167=head1 DESCRIPTION
168
169Tool to manage ceph services on pve nodes.
170
171=include pve_copyright