]> git.proxmox.com Git - pve-manager.git/blob - bin/pveceph
a0a9e95e749dd7ef8b9a2ead0f35e1ca4eb2c0b8
[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::CephTools;
20 use PVE::API2::Ceph;
21
22 use PVE::CLIHandler;
23
24 use base qw(PVE::CLIHandler);
25
26 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
27
28 initlog ('pveceph');
29
30 die "please run as root\n" if $> != 0;
31
32 PVE::INotify::inotify_init();
33
34 my $rpcenv = PVE::RPCEnvironment->init('cli');
35
36 $rpcenv->init_request();
37 $rpcenv->set_language($ENV{LANG});
38 $rpcenv->set_user('root@pam');
39
40 my $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
46 my $nodename = PVE::INotify::nodename();
47
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
64 eval {
65 my $rados = PVE::RADOS->new();
66 my $monstat = $rados->mon_command({ prefix => 'mon_status' });
67 };
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?
74 PVE::CephTools::purge_all_ceph_files();
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 => {
87 version => {
88 type => 'string',
89 enum => ['dumpling', 'emperor', 'firefly'],
90 optional => 1,
91 }
92 },
93 },
94 returns => { type => 'null' },
95 code => sub {
96 my ($param) = @_;
97
98 my $cephver = $param->{version} || 'dumpling';
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
124 my $cmddef = {
125 init => [ 'PVE::API2::Ceph', 'init', [], { node => $nodename } ],
126 lspools => [ 'PVE::API2::Ceph', 'lspools', [], { node => $nodename }, sub {
127 my $res = shift;
128
129 printf("%-20s %10s %10s %20s\n", "Name", "size", "pg_num", "used");
130 foreach my $p (sort {$a->{pool_name} cmp $b->{pool_name}} @$res) {
131 printf("%-20s %10d %10d %20d\n", $p->{pool_name}, $p->{size}, $p->{pg_num}, $p->{bytes_used});
132 }
133 }],
134 createpool => [ 'PVE::API2::Ceph', 'createpool', ['name'], { node => $nodename }],
135 destroypool => [ 'PVE::API2::Ceph', 'destroypool', ['name'], { node => $nodename } ],
136 createosd => [ 'PVE::API2::CephOSD', 'createosd', ['dev'], { node => $nodename }, $upid_exit],
137 destroyosd => [ 'PVE::API2::CephOSD', 'destroyosd', ['osdid'], { node => $nodename }, $upid_exit],
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],
142 install => [ __PACKAGE__, 'install', [] ],
143 purge => [ __PACKAGE__, 'purge', [] ],
144 status => [ 'PVE::API2::Ceph', 'status', [], { node => $nodename }, sub {
145 my $res = shift;
146 my $json = JSON->new->allow_nonref;
147 print $json->pretty->encode($res) . "\n";
148 }],
149 };
150
151 my $cmd = shift;
152
153 PVE::CLIHandler::handle_cmd($cmddef, "pveceph", $cmd, \@ARGV, undef, $0);
154
155 exit 0;
156
157 __END__
158
159 =head1 NAME
160
161 pveceph - tool to manage ceph services on pve nodes
162
163 =head1 SYNOPSIS
164
165 =include synopsis
166
167 =head1 DESCRIPTION
168
169 Tool to manage ceph services on pve nodes.
170
171 =include pve_copyright