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