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