]> git.proxmox.com Git - pve-manager.git/blob - bin/pveceph
0def860be9d91a5f4cd4fd8a35a50231fd68e592
[pve-manager.git] / bin / pveceph
1 #!/usr/bin/perl -T
2
3 use strict;
4 use warnings;
5 use Getopt::Long;
6 use Fcntl ':flock';
7 use File::Path;
8 use IO::File;
9 use JSON;
10 use Data::Dumper;
11
12 use PVE::SafeSyslog;
13 use PVE::Cluster;
14 use PVE::INotify;
15 use PVE::RPCEnvironment;
16 use PVE::Storage;
17 use PVE::Tools qw(run_command);
18 use PVE::JSONSchema qw(get_standard_option);
19 use PVE::API2::Ceph;
20
21 use PVE::CLIHandler;
22
23 use base qw(PVE::CLIHandler);
24
25 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
26
27 initlog ('pveceph');
28
29 die "please run as root\n" if $> != 0;
30
31 PVE::INotify::inotify_init();
32
33 my $rpcenv = PVE::RPCEnvironment->init('cli');
34
35 $rpcenv->init_request();
36 $rpcenv->set_language($ENV{LANG});
37 $rpcenv->set_user('root@pam');
38
39 my $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
45 my $nodename = PVE::INotify::nodename();
46
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
63 eval { $monstat = PVE::API2::Ceph::ceph_mon_status(1); };
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?
70 PVE::API2::Ceph::purge_all_ceph_files();
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 => {
83 version => {
84 type => 'string',
85 enum => ['dumpling', 'emperor', 'firefly'],
86 optional => 1,
87 }
88 },
89 },
90 returns => { type => 'null' },
91 code => sub {
92 my ($param) = @_;
93
94 my $cephver = $param->{version} || 'dumpling';
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
120 my $cmddef = {
121 init => [ 'PVE::API2::Ceph', 'init', [], { node => $nodename } ],
122 lspools => [ 'PVE::API2::Ceph', 'lspools', [], { node => $nodename }, sub {
123 my $res = shift;
124
125 printf("%-20s %10s %10s %20s\n", "Name", "size", "pg_num", "used");
126 foreach my $p (sort {$a->{pool_name} cmp $b->{pool_name}} @$res) {
127 printf("%-20s %10d %10d %20d\n", $p->{pool_name}, $p->{size}, $p->{pg_num}, $p->{bytes_used});
128 }
129 }],
130 createpool => [ 'PVE::API2::Ceph', 'createpool', ['name'], { node => $nodename }],
131 destroypool => [ 'PVE::API2::Ceph', 'destroypool', ['name'], { node => $nodename } ],
132 createosd => [ 'PVE::API2::CephOSD', 'createosd', ['dev'], { node => $nodename }, $upid_exit],
133 destroyosd => [ 'PVE::API2::CephOSD', 'destroyosd', ['osdid'], { node => $nodename }, $upid_exit],
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],
138 install => [ __PACKAGE__, 'install', [] ],
139 purge => [ __PACKAGE__, 'purge', [] ],
140 status => [ 'PVE::API2::Ceph', 'status', [], { node => $nodename }, sub {
141 my $res = shift;
142 my $json = JSON->new->allow_nonref;
143 print $json->pretty->encode($res) . "\n";
144 }],
145 };
146
147 my $cmd = shift;
148
149 PVE::CLIHandler::handle_cmd($cmddef, "pveceph", $cmd, \@ARGV, undef, $0);
150
151 exit 0;
152
153 __END__
154
155 =head1 NAME
156
157 pveceph - tool to manage ceph services on pve nodes
158
159 =head1 SYNOPSIS
160
161 =include synopsis
162
163 =head1 DESCRIPTION
164
165 Tool to manage ceph services on pve nodes.
166
167 =include pve_copyright