]> git.proxmox.com Git - pve-manager.git/blob - PVE/CLI/pveceph.pm
followup: code cleanup and print error
[pve-manager.git] / PVE / CLI / pveceph.pm
1 package PVE::CLI::pveceph;
2
3 use strict;
4 use warnings;
5
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::Ceph::Tools;
21 use PVE::API2::Ceph;
22 use PVE::API2::Ceph::FS;
23 use PVE::API2::Ceph::MDS;
24 use PVE::API2::Ceph::MGR;
25 use PVE::API2::Ceph::MON;
26 use PVE::API2::Ceph::OSD;
27
28 use PVE::CLIHandler;
29
30 use base qw(PVE::CLIHandler);
31
32 my $nodename = PVE::INotify::nodename();
33
34 my $upid_exit = sub {
35 my $upid = shift;
36 my $status = PVE::Tools::upid_read_status($upid);
37 exit($status eq 'OK' ? 0 : -1);
38 };
39
40 sub setup_environment {
41 PVE::RPCEnvironment->setup_default_cli_env();
42 }
43
44 __PACKAGE__->register_method ({
45 name => 'purge',
46 path => 'purge',
47 method => 'POST',
48 description => "Destroy ceph related data and configuration files.",
49 parameters => {
50 additionalProperties => 0,
51 properties => {
52 },
53 },
54 returns => { type => 'null' },
55 code => sub {
56 my ($param) = @_;
57
58 my $monstat;
59
60 eval {
61 my $rados = PVE::RADOS->new();
62 my $monstat = $rados->mon_command({ prefix => 'mon_status' });
63 };
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::Ceph::Tools::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 => ['hammer', 'jewel'], # for jessie
86 enum => ['luminous',], # for stretch
87 optional => 1,
88 }
89 },
90 },
91 returns => { type => 'null' },
92 code => sub {
93 my ($param) = @_;
94
95 my $cephver = $param->{version} || 'luminous';
96
97 local $ENV{DEBIAN_FRONTEND} = 'noninteractive';
98
99 if ($cephver eq 'luminous') {
100 PVE::Tools::file_set_contents("/etc/apt/sources.list.d/ceph.list",
101 "deb http://download.proxmox.com/debian/ceph-luminous stretch main\n");
102 } else {
103 # use fixed devel repo for now, because there is no officila repo for jessie
104 my $devrepo = undef;
105
106 my $keyurl = $devrepo ?
107 "https://git.ceph.com/?p=ceph.git;a=blob_plain;f=keys/autobuild.asc" :
108 "https://git.ceph.com/?p=ceph.git;a=blob_plain;f=keys/release.asc";
109
110 print "download and import ceph repository keys\n";
111
112 # Note: wget on Debian wheezy cannot handle new ceph.com certificates, so
113 # we use LWP::UserAgent
114 #system("wget -q -O- '$keyurl'| apt-key add - 2>&1 >/dev/null") == 0 ||
115 #die "unable to download ceph release key\n";
116
117 my $tmp_key_file = "/tmp/ceph-release-keys.asc";
118 my $ua = LWP::UserAgent->new(protocols_allowed => ['http', 'https'], timeout => 120);
119 $ua->env_proxy;
120 my $response = $ua->get($keyurl);
121 if ($response->is_success) {
122 my $data = $response->decoded_content;
123 PVE::Tools::file_set_contents($tmp_key_file, $data);
124 } else {
125 die "unable to download ceph release key: " . $response->status_line . "\n";
126 }
127
128 system("apt-key add $tmp_key_file 2>&1 >/dev/null") == 0 ||
129 die "unable to download ceph release key\n";
130
131 unlink $tmp_key_file;
132
133 my $source = $devrepo ?
134 "deb http://gitbuilder.ceph.com/ceph-deb-jessie-x86_64-basic/ref/$devrepo jessie main\n" :
135 "deb http://download.ceph.com/debian-$cephver jessie main\n";
136
137 PVE::Tools::file_set_contents("/etc/apt/sources.list.d/ceph.list", $source);
138 }
139
140 print "update available package list\n";
141 eval { run_command(['apt-get', '-q', 'update'], outfunc => sub {}, errfunc => sub {}); };
142
143 print "start installation\n";
144 system('apt-get', '--no-install-recommends',
145 '-o', 'Dpkg::Options::=--force-confnew',
146 'install', '--',
147 'ceph', 'ceph-common', 'ceph-mds', 'ceph-fuse', 'gdisk') == 0
148 || die "apt failed during ceph installation ($?)\n";
149
150 print "\ninstalled ceph $cephver successfully\n";
151
152 if (PVE::Ceph::Tools::systemd_managed() && ! -e '/etc/systemd/system/ceph.service') {
153 #to disable old SysV init scripts.
154 print "\nreplacing ceph init script with own ceph.service\n";
155 eval {
156 PVE::Tools::run_command('cp -v /usr/share/doc/pve-manager/examples/ceph.service /etc/systemd/system/ceph.service');
157 PVE::Tools::run_command('systemctl daemon-reload');
158 PVE::Tools::run_command('systemctl enable ceph.service');
159 };
160 if (my $err = $@) {
161 warn "WARNING: could not install ceph.service: $@\n";
162 } else {
163 print "installed ceph.service successfully\n";
164 }
165 }
166
167 return undef;
168 }});
169
170 our $cmddef = {
171 init => [ 'PVE::API2::Ceph', 'init', [], { node => $nodename } ],
172 pool => {
173 ls => [ 'PVE::API2::Ceph', 'lspools', [], { node => $nodename }, sub {
174 my $res = shift;
175
176 printf("%-20s %10s %10s %10s %10s %20s\n", "Name", "size", "min_size",
177 "pg_num", "%-used", "used");
178 foreach my $p (sort {$a->{pool_name} cmp $b->{pool_name}} @$res) {
179 printf("%-20s %10d %10d %10d %10.2f %20d\n", $p->{pool_name},
180 $p->{size}, $p->{min_size}, $p->{pg_num},
181 $p->{percent_used}, $p->{bytes_used});
182 }
183 }],
184 create => [ 'PVE::API2::Ceph', 'createpool', ['name'], { node => $nodename }],
185 destroy => [ 'PVE::API2::Ceph', 'destroypool', ['name'], { node => $nodename } ],
186 },
187 lspools => { alias => 'pool ls' },
188 createpool => { alias => 'pool create' },
189 destroypool => { alias => 'pool destroy' },
190 fs => {
191 create => [ 'PVE::API2::Ceph::FS', 'createfs', [], { node => $nodename }],
192 },
193 osd => {
194 create => [ 'PVE::API2::Ceph::OSD', 'createosd', ['dev'], { node => $nodename }, $upid_exit],
195 destroy => [ 'PVE::API2::Ceph::OSD', 'destroyosd', ['osdid'], { node => $nodename }, $upid_exit],
196 },
197 createosd => { alias => 'osd create' },
198 destroyosd => { alias => 'osd destroy' },
199 mon => {
200 create => [ 'PVE::API2::Ceph::MON', 'createmon', [], { node => $nodename }, $upid_exit],
201 destroy => [ 'PVE::API2::Ceph::MON', 'destroymon', ['monid'], { node => $nodename }, $upid_exit],
202 },
203 createmon => { alias => 'mon create' },
204 destroymon => { alias => 'mon destroy' },
205 mgr => {
206 create => [ 'PVE::API2::Ceph::MGR', 'createmgr', [], { node => $nodename }, $upid_exit],
207 destroy => [ 'PVE::API2::Ceph::MGR', 'destroymgr', ['id'], { node => $nodename }, $upid_exit],
208 },
209 createmgr => { alias => 'mgr create' },
210 destroymgr => { alias => 'mgr destroy' },
211 mds => {
212 create => [ 'PVE::API2::Ceph::MDS', 'createmds', [], { node => $nodename }, $upid_exit],
213 destroy => [ 'PVE::API2::Ceph::MDS', 'destroymds', ['name'], { node => $nodename }, $upid_exit],
214 },
215 start => [ 'PVE::API2::Ceph', 'start', ['service'], { node => $nodename }, $upid_exit],
216 stop => [ 'PVE::API2::Ceph', 'stop', ['service'], { node => $nodename }, $upid_exit],
217 install => [ __PACKAGE__, 'install', [] ],
218 purge => [ __PACKAGE__, 'purge', [] ],
219 status => [ 'PVE::API2::Ceph', 'status', [], { node => $nodename }, sub {
220 my $res = shift;
221 my $json = JSON->new->allow_nonref;
222 print $json->pretty->encode($res) . "\n";
223 }],
224 };
225
226 1;