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