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