| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Net::OpenNebula::RPC; | ||
| 5 | $Net::OpenNebula::RPC::VERSION = '0.317.0'; | ||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | use constant ONERPC => 'rpc'; | ||
| 9 | use constant ONEPOOLKEY => undef; | ||
| 10 | use constant NAME_FROM_TEMPLATE => 0; | ||
| 11 | |||
| 12 | |||
| 13 | # If cacche->{add} attibute is true, add the cache for the following method | ||
| 14 | # If cacche->{remove} attribute is true, remove the cache for the following method | ||
| 15 | # The cache is removed before it is added (e.g. in case you want to refresh) | ||
| 16 | sub new { | ||
| 17 | my $that = shift; | ||
| 18 | my $proto = ref($that) || $that; | ||
| 19 | my $self = { @_ }; | ||
| 20 | |||
| 21 | $self->{ONERPC} = $proto->ONERPC; | ||
| 22 | |||
| 23 | $self->{cache}->{add} = 0; | ||
| 24 | $self->{cache}->{remove} = 0; | ||
| 25 | |||
| 26 | bless($self, $proto); | ||
| 27 | |||
| 28 | return $self; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _onerpc { | ||
| 32 | my ($self, $method, @args) = @_; | ||
| 33 | |||
| 34 | my $onemethod = "one.$self->{ONERPC}.$method"; | ||
| 35 | |||
| 36 | if ($self->{cache}->{remove}) { | ||
| 37 | $self->{rpc}->remove_cache_method($onemethod); | ||
| 38 | $self->{cache}->{remove} = 0; | ||
| 39 | } | ||
| 40 | |||
| 41 | if ($self->{cache}->{add}) { | ||
| 42 | $self->{rpc}->add_cache_method($onemethod); | ||
| 43 | $self->{cache}->{add} = 0; | ||
| 44 | } | ||
| 45 | |||
| 46 | return $self->{rpc}->_rpc($onemethod, @args); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _onerpc_id { | ||
| 50 | my ($self, $method) = @_; | ||
| 51 | |||
| 52 | $self->has_id("_onerpc_id") || return; | ||
| 53 | |||
| 54 | return $self->_onerpc($method, | ||
| 55 | [ int => $self->id ], | ||
| 56 | ); | ||
| 57 | |||
| 58 | }; | ||
| 59 | |||
| 60 | sub _onerpc_simple { | ||
| 61 | my ($self, $method, $arg) = @_; | ||
| 62 | |||
| 63 | $self->has_id("_onerpc_simple") || return; | ||
| 64 | |||
| 65 | return $self->_onerpc($method, | ||
| 66 | [ string => "$arg" ], | ||
| 67 | [ int => $self->id ], | ||
| 68 | ); | ||
| 69 | }; | ||
| 70 | |||
| 71 | |||
| 72 | # return info call | ||
| 73 | # opts | ||
| 74 | # clearcache: if set to 1, clears the cache and queries again | ||
| 75 | # id: get info for other id (if missing, use $self->id) | ||
| 76 | # Return extended_data (and if not existing, rerieves and sets extended_data) | ||
| 77 | sub _get_info { | ||
| 78 | my ($self, %option) = @_; | ||
| 79 | |||
| 80 | my $id; | ||
| 81 | if (exists $option{id}) { | ||
| 82 | $id = $option{id} ; | ||
| 83 | } else { | ||
| 84 | $self->has_id("_get_info") || return; | ||
| 85 | $id = $self->id; | ||
| 86 | } | ||
| 87 | |||
| 88 | if(! exists $self->{extended_data} || (exists $option{clearcache} && $option{clearcache} == 1)) { | ||
| 89 | $self->{extended_data} = $self->_onerpc("info", [ int => $id ]); | ||
| 90 | } | ||
| 91 | |||
| 92 | return $self->{extended_data}; | ||
| 93 | } | ||
| 94 | |||
| 95 | # Similar to _get_info, but will try with with clearcache if C<entry> can't be | ||
| 96 | # found in extended_data and it returns the entry in extended_data. | ||
| 97 | sub _get_info_extended { | ||
| 98 | my ($self, $entry) = @_; | ||
| 99 | $self->_get_info(); | ||
| 100 | |||
| 101 | if(! exists $self->{extended_data}->{$entry}) { | ||
| 102 | $self->_get_info(clearcache => 1); | ||
| 103 | } | ||
| 104 | |||
| 105 | if(exists $self->{extended_data}->{$entry}) { | ||
| 106 | $self->debug(2, "Entry $entry present in extended_data"); | ||
| 107 | return $self->{extended_data}->{$entry}; | ||
| 108 | } else { | ||
| 109 | $self->debug(2, "Entry $entry still not present in extended_data"); | ||
| 110 | return []; # empty array ref | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub name { | ||
| 116 | my ($self) = @_; | ||
| 117 | |||
| 118 | my $name; | ||
| 119 | |||
| 120 | if (NAME_FROM_TEMPLATE) { | ||
| 121 | my $ext_name = $self->_get_info_extended('NAME'); | ||
| 122 | |||
| 123 | $name = $ext_name->[0]; | ||
| 124 | } | ||
| 125 | |||
| 126 | if (!defined($name)) { | ||
| 127 | $name = $self->{data}->{NAME}->[0]; | ||
| 128 | if (! $name) { | ||
| 129 | my $template = $self->_get_info_extended('TEMPLATE'); | ||
| 130 | # if vm NAME is set, use that instead of template NAME | ||
| 131 | $name = $template->[0]->{NAME}->[0]; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | return $name; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub id { | ||
| 139 | my ($self) = @_; | ||
| 140 | return $self->{data}->{ID}->[0]; | ||
| 141 | } | ||
| 142 | |||
| 143 | # just check if the id is valid or not (returned result to be used as boolean) | ||
| 144 | sub has_id { | ||
| 145 | my ($self, $msg) = @_; | ||
| 146 | my $id = $self->id; | ||
| 147 | |||
| 148 | if (defined($id)) { | ||
| 149 | return 1; | ||
| 150 | } else { | ||
| 151 | $self->error("$self->{ONERPC}: no valid id ($msg)"); | ||
| 152 | return 0; | ||
| 153 | } | ||
| 154 | }; | ||
| 155 | |||
| 156 | sub dump { | ||
| 157 | my $self = shift; | ||
| 158 | return Dumper($self); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _allocate { | ||
| 162 | my ($self, @args) = @_; | ||
| 163 | |||
| 164 | my $id = $self->_onerpc("allocate", @args); | ||
| 165 | |||
| 166 | if (! defined($id)) { | ||
| 167 | my $args_txt = $self->{rpc}->_rpc_args_to_txt(@args); | ||
| 168 | $self->error("$self->{ONERPC}: _allocate failed, no id returned (arguments $args_txt)."); | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | $self->debug(1, "$self->{ONERPC} allocate returned id $id"); | ||
| 173 | |||
| 174 | my $data = $self->_get_info(id => $id); | ||
| 175 | if (defined($data)) { | ||
| 176 | $self->{data} = $data; | ||
| 177 | $self->debug(3, "$self->{ONERPC} allocate updated data for id $id"); | ||
| 178 | return $id; | ||
| 179 | } else { | ||
| 180 | $self->error("$self->{ONERPC} allocate updated data failed for id $id"); | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | sub delete { | ||
| 186 | my ($self) = @_; | ||
| 187 | |||
| 188 | $self->has_id("delete") || return; | ||
| 189 | |||
| 190 | $self->debug(1, "$self->{ONERPC} delete id ".$self->id); | ||
| 191 | return $self->_onerpc_id("delete"); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub update { | ||
| 195 | my ($self, $tpl, $merge) = @_; | ||
| 196 | |||
| 197 | # reset $merge to integer value; undef implies merge = 0 | ||
| 198 | if ($merge) { | ||
| 199 | $merge = 1; | ||
| 200 | } else { | ||
| 201 | $merge = 0; | ||
| 202 | } | ||
| 203 | |||
| 204 | $self->has_id("update") || return; | ||
| 205 | |||
| 206 | return $self->_onerpc("update", | ||
| 207 | [ int => $self->id ], | ||
| 208 | [ string => $tpl ], | ||
| 209 | [ int => $merge ] | ||
| 210 | ); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _lookup | ||
| 214 | { | ||
| 215 | my ($self, $type, $id, $one) = @_; | ||
| 216 | |||
| 217 | $id = -1 if ! defined($id); | ||
| 218 | |||
| 219 | my $err; | ||
| 220 | if ($id !~ m/^-?\d+$/) { | ||
| 221 | my $msg = "Non-integer ${type}name $id"; | ||
| 222 | if ($one) { | ||
| 223 | my $method = "get_${type}s"; | ||
| 224 | $self->debug(1, "$msg, looking up id using $method"); | ||
| 225 | my @found = $one->$method($id); | ||
| 226 | if (@found) { | ||
| 227 | if (scalar @found > 1) { | ||
| 228 | $self->warn("$msg and more than one $type found: ".join(", ", @found)); | ||
| 229 | } | ||
| 230 | $id = $found[0]->id(); | ||
| 231 | $self->debug(1, "$msg resolved to id $id"); | ||
| 232 | } else { | ||
| 233 | $err = "$msg and no matching ${type}s found."; | ||
| 234 | } | ||
| 235 | } else { | ||
| 236 | $err = "$msg and no ONE instance provided"; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | if ($err) { | ||
| 241 | $self->error($err); | ||
| 242 | return; | ||
| 243 | } else { | ||
| 244 | return $id; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | # Chown | ||
| 250 | # options | ||
| 251 | # uid : uid to use | ||
| 252 | # gid : gid to use | ||
| 253 | # one : Net::OpenNebula instance to use for user/groupname lookup | ||
| 254 | # (i.e. when uid/gid is not a integer) | ||
| 255 | # It wil use $one->get_users and/or $one->get_groups methods. | ||
| 256 | |||
| 257 | sub chown | ||
| 258 | { | ||
| 259 | my ($self, %opts) = @_; | ||
| 260 | |||
| 261 | $self->has_id("chown") || return; | ||
| 262 | |||
| 263 | my $uid = $self->_lookup('user', $opts{uid}, $opts{one}); | ||
| 264 | return if ! defined($uid); | ||
| 265 | |||
| 266 | my $gid = $self->_lookup('group', $opts{gid}, $opts{one}); | ||
| 267 | return if ! defined($gid); | ||
| 268 | |||
| 269 | return $self->_onerpc("chown", | ||
| 270 | [ int => $self->id ], | ||
| 271 | [ int => $uid ], | ||
| 272 | [ int => $gid ] | ||
| 273 | ); | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | # Mode can be | ||
| 278 | # an integer, typical in octal mode 0664, but can be decimal or whatever | ||
| 279 | sub chmod | ||
| 280 | { | ||
| 281 | my ($self, $mode) = @_; | ||
| 282 | |||
| 283 | $self->has_id("chmod") || return; | ||
| 284 | |||
| 285 | my @bits; | ||
| 286 | if ($mode =~ m/^\d+/) { | ||
| 287 | my $bin = sprintf("%b", $mode & oct("777")); | ||
| 288 | @bits = split('', $bin); | ||
| 289 | } else { | ||
| 290 | $self->error("chown cannot handle mode $mode"); | ||
| 291 | return; | ||
| 292 | }; | ||
| 293 | |||
| 294 | if((scalar @bits) == 9) { | ||
| 295 | my @allargs = qw(chmod); | ||
| 296 | push(@allargs, [ int => $self->id ], map {[ int => $_ ]} @bits); | ||
| 297 | return $self->_onerpc(@allargs); | ||
| 298 | } else { | ||
| 299 | $self->error("9 permissions bits required, got ".join(",", @bits)); | ||
| 300 | return; | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | # When C<nameregex> is defined, only instances with name matching | ||
| 306 | # the regular expression are returned (if any). | ||
| 307 | # C<nameregex> is a compiled regular expression (e.g. qr{^somename$}). | ||
| 308 | sub _get_instances { | ||
| 309 | my ($self, $nameregex, @args) = @_; | ||
| 310 | |||
| 311 | my $class = ref $self; | ||
| 312 | my $pool = $class->ONERPC . "pool"; | ||
| 313 | my $key = $class->ONEPOOLKEY || uc($class->ONERPC); | ||
| 314 | |||
| 315 | my @ret = (); | ||
| 316 | my $info = "info"; | ||
| 317 | |||
| 318 | # Change VM info by new infoextended if version >= 5.8.x | ||
| 319 | if (($self->{rpc}->version() >= version->new('5.8.0')) and ($pool eq "vmpool")) { | ||
| 320 | $info = 'infoextended'; | ||
| 321 | push(@args, [ string => "" ]); | ||
| 322 | }; | ||
| 323 | |||
| 324 | my $reply = $self->{rpc}->_rpc("one.$pool.$info", @args); | ||
| 325 | |||
| 326 | foreach my $data (@{ $reply->{$key} }) { | ||
| 327 | # This is data from pool.info, so it is as complete as individual info | ||
| 328 | # so we can set it as extended_data | ||
| 329 | my $inst = $self->new(rpc => $self->{rpc}, data => $data, extended_data => $data); | ||
| 330 | if (! defined($nameregex) || ($inst->name && $inst->name =~ $nameregex) ) { | ||
| 331 | push(@ret, $inst); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | return @ret; | ||
| 336 | } | ||
| 337 | |||
| 338 | # Given state, wait until the state is reached. | ||
| 339 | # Between each check, sleep number of seconds; | ||
| 340 | # and there's a maximum number of iterations to try. | ||
| 341 | # Return 1 if the state is reached, 0 otherwise. | ||
| 342 | # state: the state (in text) to wait for | ||
| 343 | # opts: | ||
| 344 | # sleep: sleep per interval | ||
| 345 | # max_iter: maximum iterations (if 0, no sleep) | ||
| 346 | sub wait_for_state { | ||
| 347 | my ($self, $state, %opts) = @_; | ||
| 348 | |||
| 349 | my $sleep = 5; # in seconds | ||
| 350 | my $max_iter = 200; # approx 15 minutes with default sleep | ||
| 351 | $sleep = $opts{sleep} if defined($opts{sleep}); | ||
| 352 | $max_iter = $opts{max_iter} if defined($opts{max_iter}); | ||
| 353 | |||
| 354 | my $currentstate = $state eq $self->state; | ||
| 355 | my $ind = 1; # first state fetched, no sleep involved | ||
| 356 | while ($ind < $max_iter && ! $currentstate) { | ||
| 357 | sleep($sleep); | ||
| 358 | $currentstate = $state eq $self->state; | ||
| 359 | $ind +=1; | ||
| 360 | } | ||
| 361 | |||
| 362 | return $currentstate; | ||
| 363 | |||
| 364 | } | ||
| 365 | |||
| 366 | # add logging shortcuts | ||
| 367 | no strict 'refs'; ## no critic | ||
| 368 | foreach my $i (qw(error warn info verbose debug)) { | ||
| 369 | *{$i} = sub { | ||
| 370 | my ($self, @args) = @_; | ||
| 371 | return $self->{rpc}->{log}->$i(@args); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | use strict 'refs'; | ||
| 375 | |||
| 376 | 1; |