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