| 1 | # ex:ts=8 sw=4: | ||
| 2 | # $OpenBSD$ | ||
| 3 | # | ||
| 4 | # Copyright (c) 2024 Dick Olsson <hi@senzilla.io> | ||
| 5 | # | ||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||
| 8 | # copyright notice and this permission notice appear in all copies. | ||
| 9 | # | ||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 17 | |||
| 18 | 1 | ▶ | use v5.36; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | |||
| 20 | package App::FuguVM::Config; | ||
| 21 | our $VERSION = '0.3.1'; | ||
| 22 | |||
| 23 | 0 | ▶ | use Fugu::Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 24 | use Fugu::File; | ||
| 25 | use Fugu::Log; | ||
| 26 | use App::FuguVM::Arch; | ||
| 27 | |||
| 28 | # App::FuguVM::Config - the VM defaults over Fugu::Config. | ||
| 29 | # | ||
| 30 | # The grammar, the tilde expansion and the yes/no spellings come from | ||
| 31 | # Fugu::Config. This file holds only what is true of FuguVM: the | ||
| 32 | # defaults for a machine, the merge of the global and the project | ||
| 33 | # file, and the switch that turns the installed-image cache off. | ||
| 34 | |||
| 35 | use constant { | ||
| 36 | DEFAULT_ARCH => 'arm64', | ||
| 37 | DEFAULT_MEMORY => 2048, | ||
| 38 | DEFAULT_DISK_SIZE => '8G', | ||
| 39 | DEFAULT_SSH_PORT => 2222, | ||
| 40 | DEFAULT_CONSOLE_PORT => 4444, | ||
| 41 | DEFAULT_BIND_ADDRESS => '127.0.0.1', | ||
| 42 | DEFAULT_VERSION => '7.8', | ||
| 43 | |||
| 44 | # The word that makes a port directive take a free host port. | ||
| 45 | # This constant is the one home of the word. | ||
| 46 | AUTO_PORT => 'auto', | ||
| 47 | |||
| 48 | # The size of each automatic port range. A range starts at the | ||
| 49 | # default port of its directive, so the bounds need no constant | ||
| 50 | # of their own. | ||
| 51 | AUTO_PORT_COUNT => 100, | ||
| 52 | |||
| 53 | DATA_DIR => '.fuguvm', | ||
| 54 | GLOBAL_CONFIG => '.fuguvmrc', | ||
| 55 | PROJECT_CONFIG => '.fuguvmrc', | ||
| 56 | }; | ||
| 57 | |||
| 58 | sub new ( $class, $project_root ) | ||
| 59 | { | ||
| 60 | my $self = bless { | ||
| 61 | project_root => $project_root, | ||
| 62 | data_dir => "$project_root/" . DATA_DIR, | ||
| 63 | }, $class; | ||
| 64 | |||
| 65 | $self->_load_configs; | ||
| 66 | |||
| 67 | return $self; | ||
| 68 | } | ||
| 69 | |||
| 70 | # $class->find_project_root: | ||
| 71 | # Walk up to the directory that holds .fuguvmrc. | ||
| 72 | sub find_project_root ($class) | ||
| 73 | { | ||
| 74 | return Fugu::Config->find_project_root(PROJECT_CONFIG); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _load_configs ($self) | ||
| 78 | { | ||
| 79 | my $home = $ENV{HOME} // '/root'; | ||
| 80 | |||
| 81 | $self->{global} = $self->_parse( "$home/" . GLOBAL_CONFIG ); | ||
| 82 | $self->{project} = | ||
| 83 | $self->_parse( "$self->{project_root}/" . PROJECT_CONFIG ); | ||
| 84 | |||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | # $self->_parse($path): | ||
| 89 | # Parse one configuration file. An absent file is normal: a | ||
| 90 | # checkout has no global file, and a global-only setup has no | ||
| 91 | # project file. A file that exists but does not parse is an error | ||
| 92 | # that names the line, and the caller gets empty settings rather | ||
| 93 | # than half of them. | ||
| 94 | sub _parse ( $self, $path ) | ||
| 95 | { | ||
| 96 | return Fugu::Config->new( file => $path ) unless -f $path; | ||
| 97 | |||
| 98 | my $config = Fugu::Config->new( file => $path ); | ||
| 99 | unless ( $config->load ) { | ||
| 100 | Fugu::Log->default->error( '%s', $config->error ); | ||
| 101 | } | ||
| 102 | |||
| 103 | return $config; | ||
| 104 | } | ||
| 105 | |||
| 106 | # $self->_setting($key): | ||
| 107 | # Return a top-level setting. The project file wins over the | ||
| 108 | # global one. | ||
| 109 | sub _setting ( $self, $key ) | ||
| 110 | { | ||
| 111 | return $self->{project}->get($key) // $self->{global}->get($key); | ||
| 112 | } | ||
| 113 | |||
| 114 | # $self->load_vm($name): | ||
| 115 | # Return the merged configuration of one VM. Return undef when | ||
| 116 | # no file declares it, or when a value does not validate. In the | ||
| 117 | # second case, error() reports the reason. | ||
| 118 | sub load_vm ( $self, $name ) | ||
| 119 | { | ||
| 120 | delete $self->{error}; | ||
| 121 | |||
| 122 | # First check for a VM block in the project config. Then check | ||
| 123 | # the global config. | ||
| 124 | my $block = $self->{project}->block( 'vm', $name ) | ||
| 125 | // $self->{global}->block( 'vm', $name ); | ||
| 126 | my $vm = $block ? { %{ $block->{settings} } } : undef; | ||
| 127 | |||
| 128 | # Then look for a file of its own under vms/. This is not a | ||
| 129 | # fallback: 'fuguvm init' writes vms/default.conf, and | ||
| 130 | # fuguvm(1) documents the directory. A vm block in the | ||
| 131 | # project or global config wins over it. | ||
| 132 | if ( !defined $vm ) { | ||
| 133 | my $vm_file = "$self->{data_dir}/vms/$name.conf"; | ||
| 134 | if ( -f $vm_file ) { | ||
| 135 | my $file = $self->_parse($vm_file); | ||
| 136 | $vm = { map { $_ => $file->get($_) } | ||
| 137 | $file->setting_names }; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | return if !defined $vm; | ||
| 142 | |||
| 143 | # Apply the defaults | ||
| 144 | $vm->{name} //= $name; | ||
| 145 | $vm->{arch} //= DEFAULT_ARCH; | ||
| 146 | $vm->{version} //= DEFAULT_VERSION; | ||
| 147 | $vm->{memory} //= DEFAULT_MEMORY; | ||
| 148 | $vm->{disk_size} //= DEFAULT_DISK_SIZE; | ||
| 149 | $vm->{ssh_port} //= DEFAULT_SSH_PORT; | ||
| 150 | $vm->{console_port} //= DEFAULT_CONSOLE_PORT; | ||
| 151 | |||
| 152 | # This loader is the boundary of the arch directive. No module | ||
| 153 | # downstream repeats the check. | ||
| 154 | if ( !App::FuguVM::Arch->new( $vm->{arch} ) ) { | ||
| 155 | $self->{error} = | ||
| 156 | sprintf( "VM '%s': unknown arch '%s' (accepted values: %s)", | ||
| 157 | $name, $vm->{arch}, | ||
| 158 | join( ', ', App::FuguVM::Arch->names ) ); | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | # This loader is also the boundary of the two port directives | ||
| 163 | # and of the bind address. No module downstream repeats these | ||
| 164 | # checks. | ||
| 165 | for my $directive (qw(ssh_port console_port)) { | ||
| 166 | next if _valid_port( $vm->{$directive} ); | ||
| 167 | $self->{error} = sprintf( | ||
| 168 | "VM '%s': %s '%s' is not a port from 1 to 65535" | ||
| 169 | . " and not '%s'", | ||
| 170 | $name, $directive, $vm->{$directive}, AUTO_PORT ); | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | # The bind address follows the same fallback chain as | ||
| 175 | # ssh_pubkey and cache_dir below. | ||
| 176 | $vm->{bind_address} //= $self->bind_address; | ||
| 177 | if ( !_valid_ipv4( $vm->{bind_address} ) ) { | ||
| 178 | $self->{error} = sprintf( | ||
| 179 | "VM '%s': bind_address '%s' is not an IPv4 address" | ||
| 180 | . " in dotted-decimal form", | ||
| 181 | $name, $vm->{bind_address} ); | ||
| 182 | return; | ||
| 183 | } | ||
| 184 | |||
| 185 | # The version gate of the guest reads the directive from the | ||
| 186 | # per-VM config. The directive lives in the enclosing files | ||
| 187 | # only: the pinned binary is a fact of the host, not of one | ||
| 188 | # guest. A pin inside a VM declaration would silently not | ||
| 189 | # apply, so it is an error. | ||
| 190 | if ( exists $vm->{qemu_version} ) { | ||
| 191 | $self->{error} = sprintf( | ||
| 192 | "VM '%s': qemu_version lives in the project or the" | ||
| 193 | . " global .fuguvmrc, not in a VM declaration", | ||
| 194 | $name | ||
| 195 | ); | ||
| 196 | return; | ||
| 197 | } | ||
| 198 | $vm->{qemu_version} = $self->qemu_version; | ||
| 199 | if ( defined $vm->{qemu_version} | ||
| 200 | && $vm->{qemu_version} !~ /^[0-9]+(?:\.[0-9]+)*$/ ) | ||
| 201 | { | ||
| 202 | $self->{error} = | ||
| 203 | sprintf( "VM '%s': qemu_version '%s' is not a version" | ||
| 204 | . " of dot-separated decimal numbers", | ||
| 205 | $name, $vm->{qemu_version} ); | ||
| 206 | return; | ||
| 207 | } | ||
| 208 | |||
| 209 | # Include ssh_pubkey from the global or project config | ||
| 210 | $vm->{ssh_pubkey} //= $self->ssh_pubkey; | ||
| 211 | |||
| 212 | # Include the fixed ports of every VM declaration. The port | ||
| 213 | # probe of the guest skips them, so an automatic port cannot | ||
| 214 | # take the number of a declared sibling, stopped or not. | ||
| 215 | $vm->{declared_ports} = $self->declared_ports; | ||
| 216 | |||
| 217 | # Include the resolved cache_dir. Then the VM operations, the | ||
| 218 | # proxy cache and the installed-image cache, all use the | ||
| 219 | # configured location. Without it, 'fuguvm up' would write its | ||
| 220 | # images under $HOME while the cache subcommands worked on a | ||
| 221 | # different tree. | ||
| 222 | $vm->{cache_dir} //= $self->cache_dir; | ||
| 223 | |||
| 224 | # Normalize the installed-image cache switch, whether it came from | ||
| 225 | # the VM block or the enclosing configuration | ||
| 226 | $vm->{image_cache} = | ||
| 227 | defined $vm->{image_cache} | ||
| 228 | ? $self->_bool( $vm->{image_cache}, 1 ) | ||
| 229 | : $self->image_cache; | ||
| 230 | |||
| 231 | # Normalize the verification switch the same way | ||
| 232 | $vm->{verify} = | ||
| 233 | defined $vm->{verify} | ||
| 234 | ? $self->_bool( $vm->{verify}, 1 ) | ||
| 235 | : $self->verify; | ||
| 236 | |||
| 237 | # Resolve the signify_dir directive, from the VM block or the | ||
| 238 | # enclosing configuration. This loader is the boundary of the | ||
| 239 | # directive: an absent or unreadable directory fails here, not | ||
| 240 | # deep inside an install. | ||
| 241 | $vm->{signify_dir} = | ||
| 242 | defined $vm->{signify_dir} | ||
| 243 | ? $self->_resolve_path( $vm->{signify_dir} ) | ||
| 244 | : $self->signify_dir; | ||
| 245 | if ( defined $vm->{signify_dir} && !-d $vm->{signify_dir} ) { | ||
| 246 | $self->{error} = | ||
| 247 | sprintf( "VM '%s': signify_dir is not a directory: %s", | ||
| 248 | $name, $vm->{signify_dir} ); | ||
| 249 | return; | ||
| 250 | } | ||
| 251 | |||
| 252 | # Include the distfile cap of the project. The distfile tree is | ||
| 253 | # one tree under cache_dir, and each guest of a project shares | ||
| 254 | # it, so the cap is a project fact and not a per-guest | ||
| 255 | # directive. A cap in a VM block would silently not apply, so | ||
| 256 | # the operator hears about it, like an unrecognized switch. | ||
| 257 | Fugu::Log->default->warning( | ||
| 258 | "VM '%s': distfile_cache lives in the project or the" | ||
| 259 | . ' global .fuguvmrc, not in a VM declaration', | ||
| 260 | $name | ||
| 261 | ) if defined $vm->{distfile_cache}; | ||
| 262 | $vm->{distfile_cache} = $self->distfile_cache; | ||
| 263 | |||
| 264 | return if !$self->_validate_origin( $name, $vm ); | ||
| 265 | |||
| 266 | return $vm; | ||
| 267 | } | ||
| 268 | |||
| 269 | # $self->_validate_origin($name, $vm): | ||
| 270 | # Validate the three image-lifecycle directives, and derive | ||
| 271 | # install_mode: expect, autoinstall or import. This loader is the | ||
| 272 | # boundary of each directive, so no module downstream repeats a | ||
| 273 | # check. Return 1 when the configuration loads, and 0 with the | ||
| 274 | # reason in error() otherwise. | ||
| 275 | sub _validate_origin ( $self, $name, $vm ) | ||
| 276 | { | ||
| 277 | if ( defined $vm->{autoinstall} && defined $vm->{base_disk} ) { | ||
| 278 | $self->{error} = sprintf( | ||
| 279 | "VM '%s': autoinstall and base_disk contradict each" | ||
| 280 | . " other; one guest has one origin", | ||
| 281 | $name | ||
| 282 | ); | ||
| 283 | return 0; | ||
| 284 | } | ||
| 285 | |||
| 286 | # Each path directive resolves against the project root, and the | ||
| 287 | # file must be readable now. A path that fails at 'up' time, deep | ||
| 288 | # inside an install, is a path that failed too late. | ||
| 289 | for my $directive (qw(autoinstall base_disk root_password_file)) { | ||
| 290 | next if !defined $vm->{$directive}; | ||
| 291 | my $path = $self->_resolve_path( $vm->{$directive} ); | ||
| 292 | if ( !-f $path || !-r $path ) { | ||
| 293 | $self->{error} = | ||
| 294 | sprintf( "VM '%s': %s file is not readable: %s", | ||
| 295 | $name, $directive, $path ); | ||
| 296 | return 0; | ||
| 297 | } | ||
| 298 | $vm->{$directive} = $path; | ||
| 299 | } | ||
| 300 | |||
| 301 | $vm->{install_mode} = | ||
| 302 | defined $vm->{autoinstall} ? 'autoinstall' | ||
| 303 | : defined $vm->{base_disk} ? 'import' | ||
| 304 | : 'expect'; | ||
| 305 | |||
| 306 | if ( $vm->{install_mode} eq 'import' && !$vm->{image_cache} ) { | ||
| 307 | $self->{error} = sprintf( | ||
| 308 | "VM '%s': base_disk needs the image cache;" | ||
| 309 | . " remove 'image_cache no'", | ||
| 310 | $name | ||
| 311 | ); | ||
| 312 | return 0; | ||
| 313 | } | ||
| 314 | |||
| 315 | # Outside the expect mode the tool does not know the root | ||
| 316 | # password of the image, and it cannot install a key without | ||
| 317 | # one. | ||
| 318 | if ( $vm->{install_mode} ne 'expect' | ||
| 319 | && defined $vm->{ssh_pubkey} | ||
| 320 | && $vm->{ssh_pubkey} ne '' | ||
| 321 | && !defined $vm->{root_password_file} ) | ||
| 322 | { | ||
| 323 | $self->{error} = sprintf( | ||
| 324 | "VM '%s': ssh_pubkey needs root_password_file in the" | ||
| 325 | . " %s mode; add the directive, or unset" | ||
| 326 | . " ssh_pubkey and bake the key into the image", | ||
| 327 | $name, $vm->{install_mode} ); | ||
| 328 | return 0; | ||
| 329 | } | ||
| 330 | |||
| 331 | return 1; | ||
| 332 | } | ||
| 333 | |||
| 334 | # $self->_resolve_path($value): | ||
| 335 | # Expand a leading tilde, then make a relative path absolute | ||
| 336 | # against the project root. The result is always absolute: the | ||
| 337 | # project root itself can be relative, from a --project option, | ||
| 338 | # and a daemonized child can read the path from an other working | ||
| 339 | # directory. | ||
| 340 | sub _resolve_path ( $self, $value ) | ||
| 341 | { | ||
| 342 | my $path = Fugu::File->expand_tilde($value); | ||
| 343 | $path = "$self->{project_root}/$path" if $path !~ m{^/}; | ||
| 344 | |||
| 345 | require File::Spec; | ||
| 346 | return File::Spec->rel2abs($path); | ||
| 347 | } | ||
| 348 | |||
| 349 | # $self->error: | ||
| 350 | # Return the reason of the last failed load_vm, or undef. | ||
| 351 | sub error ($self) | ||
| 352 | { | ||
| 353 | return $self->{error}; | ||
| 354 | } | ||
| 355 | |||
| 356 | # _valid_port($value): | ||
| 357 | # Report if a port directive holds the word AUTO_PORT or a | ||
| 358 | # decimal number from 1 to 65535. | ||
| 359 | sub _valid_port ($value) | ||
| 360 | { | ||
| 361 | return 0 if !defined $value; | ||
| 362 | return 1 if $value eq AUTO_PORT; | ||
| 363 | |||
| 364 | # A leading zero is not decimal, and the string-keyed port sets | ||
| 365 | # of the probe would not match it. | ||
| 366 | return 0 if $value !~ /^[1-9][0-9]*$/; | ||
| 367 | |||
| 368 | return $value <= 65535 ? 1 : 0; | ||
| 369 | } | ||
| 370 | |||
| 371 | # _valid_ipv4($value): | ||
| 372 | # Report if a value is one IPv4 address in dotted-decimal form. | ||
| 373 | # A host name is not: a name resolves once for QEMU and once for | ||
| 374 | # the tool, and the two answers can differ. | ||
| 375 | sub _valid_ipv4 ($value) | ||
| 376 | { | ||
| 377 | return 0 if !defined $value; | ||
| 378 | |||
| 379 | my @parts = split /\./, $value, -1; | ||
| 380 | return 0 if @parts != 4; | ||
| 381 | |||
| 382 | for my $part (@parts) { | ||
| 383 | |||
| 384 | # A leading zero reads as octal in inet_aton, so such a | ||
| 385 | # component is not decimal. | ||
| 386 | return 0 if $part !~ /^(?:0|[1-9][0-9]{0,2})$/; | ||
| 387 | return 0 if $part > 255; | ||
| 388 | } | ||
| 389 | |||
| 390 | return 1; | ||
| 391 | } | ||
| 392 | |||
| 393 | sub cache_dir ($self) | ||
| 394 | { | ||
| 395 | my $dir = $self->_setting('cache_dir') // '~/.cache/fuguvm'; | ||
| 396 | |||
| 397 | return Fugu::File->expand_tilde($dir); | ||
| 398 | } | ||
| 399 | |||
| 400 | # $self->image_cache: | ||
| 401 | # Return whether 'fuguvm up' may use the installed-image cache. | ||
| 402 | # The project configuration wins over the global one. The default | ||
| 403 | # is on. | ||
| 404 | sub image_cache ($self) | ||
| 405 | { | ||
| 406 | my $value = $self->_setting('image_cache'); | ||
| 407 | return 1 if !defined $value; | ||
| 408 | |||
| 409 | return $self->_bool( $value, 1 ); | ||
| 410 | } | ||
| 411 | |||
| 412 | # $self->verify: | ||
| 413 | # Return whether the tool verifies each mirror download. The | ||
| 414 | # project configuration wins over the global one. The default is | ||
| 415 | # on. | ||
| 416 | sub verify ($self) | ||
| 417 | { | ||
| 418 | my $value = $self->_setting('verify'); | ||
| 419 | return 1 if !defined $value; | ||
| 420 | |||
| 421 | return $self->_bool( $value, 1 ); | ||
| 422 | } | ||
| 423 | |||
| 424 | # $self->signify_dir: | ||
| 425 | # Return the resolved directory of the signify public keys, or | ||
| 426 | # undef without the directive. A leading tilde expands, and a | ||
| 427 | # relative path resolves against the project root. | ||
| 428 | sub signify_dir ($self) | ||
| 429 | { | ||
| 430 | my $dir = $self->_setting('signify_dir'); | ||
| 431 | return if !defined $dir; | ||
| 432 | |||
| 433 | return $self->_resolve_path($dir); | ||
| 434 | } | ||
| 435 | |||
| 436 | # $self->distfile_cache: | ||
| 437 | # Return the distfile cap in bytes. The default is 0, and 0 | ||
| 438 | # turns the distfile cache off. An unparsable value gives one | ||
| 439 | # warning and the value 0: an unrecognized spelling must not | ||
| 440 | # silently mean its opposite, and off is the closed state for a | ||
| 441 | # cache. | ||
| 442 | sub distfile_cache ($self) | ||
| 443 | { | ||
| 444 | my $value = $self->_setting('distfile_cache'); | ||
| 445 | return 0 if !defined $value; | ||
| 446 | |||
| 447 | my $bytes = _parse_size($value); | ||
| 448 | if ( !defined $bytes ) { | ||
| 449 | Fugu::Log->default->warning( | ||
| 450 | "distfile_cache '%s' is not a size; the cache is off", | ||
| 451 | $value ); | ||
| 452 | return 0; | ||
| 453 | } | ||
| 454 | |||
| 455 | return $bytes; | ||
| 456 | } | ||
| 457 | |||
| 458 | # _parse_size($value): | ||
| 459 | # Return the byte count of a size, or undef. The value is a bare | ||
| 460 | # number of bytes, or a number with a K, M or G suffix. The | ||
| 461 | # suffix is 1024-based, and the letter case does not matter. | ||
| 462 | sub _parse_size ($value) | ||
| 463 | { | ||
| 464 | my ( $number, $suffix ) = $value =~ /\A([0-9]+)([KkMmGg]?)\z/; | ||
| 465 | return if !defined $number; | ||
| 466 | |||
| 467 | my %scale = ( '' => 1, k => 1024, m => 1024**2, g => 1024**3 ); | ||
| 468 | |||
| 469 | return $number * $scale{ lc $suffix }; | ||
| 470 | } | ||
| 471 | |||
| 472 | # $self->_bool($value, $default): | ||
| 473 | # Read a switch, and report a value that is neither yes nor no. | ||
| 474 | # An unrecognized spelling must not silently mean its opposite, | ||
| 475 | # so the operator hears about it. | ||
| 476 | sub _bool ( $self, $value, $default ) | ||
| 477 | { | ||
| 478 | my $parser = $self->{project}; | ||
| 479 | my $result = $parser->parse_bool( $value, $default ); | ||
| 480 | |||
| 481 | Fugu::Log->default->warning( '%s', $parser->error ) | ||
| 482 | if defined $parser->error; | ||
| 483 | |||
| 484 | return $result; | ||
| 485 | } | ||
| 486 | |||
| 487 | sub state_dir ($self) | ||
| 488 | { | ||
| 489 | my $dir = $self->{project}->get('state_dir') | ||
| 490 | // "$self->{data_dir}/state"; | ||
| 491 | |||
| 492 | # Make relative paths absolute to the project root | ||
| 493 | if ( $dir !~ m{^/} ) { | ||
| 494 | $dir = "$self->{project_root}/$dir"; | ||
| 495 | } | ||
| 496 | |||
| 497 | return $dir; | ||
| 498 | } | ||
| 499 | |||
| 500 | sub default_vm ($self) | ||
| 501 | { | ||
| 502 | return $self->_setting('default_vm') // 'default'; | ||
| 503 | } | ||
| 504 | |||
| 505 | sub ssh_pubkey ($self) | ||
| 506 | { | ||
| 507 | return $self->_setting('ssh_pubkey'); | ||
| 508 | } | ||
| 509 | |||
| 510 | # $self->declared_ports: | ||
| 511 | # Return the fixed ports of every VM declaration of the project, | ||
| 512 | # as a hash reference keyed by port. A declaration that omits a | ||
| 513 | # port directive holds the default port of that directive. The | ||
| 514 | # set covers the vm blocks of both files and the files under | ||
| 515 | # vms/. | ||
| 516 | sub declared_ports ($self) | ||
| 517 | { | ||
| 518 | my @declarations = | ||
| 519 | map { $_->{settings} } | ||
| 520 | ( $self->{project}->blocks('vm'), $self->{global}->blocks('vm') ); | ||
| 521 | |||
| 522 | my $vms_dir = "$self->{data_dir}/vms"; | ||
| 523 | if ( opendir my $dh, $vms_dir ) { | ||
| 524 | for my $file ( sort grep { /\.conf$/ } readdir $dh ) { | ||
| 525 | my $parsed = $self->_parse("$vms_dir/$file"); | ||
| 526 | push @declarations, | ||
| 527 | { map { $_ => $parsed->get($_) } | ||
| 528 | $parsed->setting_names }; | ||
| 529 | } | ||
| 530 | closedir $dh; | ||
| 531 | } | ||
| 532 | |||
| 533 | my %defaults = ( | ||
| 534 | ssh_port => DEFAULT_SSH_PORT, | ||
| 535 | console_port => DEFAULT_CONSOLE_PORT, | ||
| 536 | ); | ||
| 537 | |||
| 538 | my %ports; | ||
| 539 | for my $settings (@declarations) { | ||
| 540 | for my $directive (qw(ssh_port console_port)) { | ||
| 541 | my $value = $settings->{$directive} | ||
| 542 | // $defaults{$directive}; | ||
| 543 | $ports{$value} = 1 if $value =~ /^[0-9]+$/; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | return \%ports; | ||
| 548 | } | ||
| 549 | |||
| 550 | # $self->bind_address: | ||
| 551 | # Return the host address of the forwarded ports. The project | ||
| 552 | # file wins over the global one, and the default is loopback. | ||
| 553 | sub bind_address ($self) | ||
| 554 | { | ||
| 555 | return $self->_setting('bind_address') // DEFAULT_BIND_ADDRESS; | ||
| 556 | } | ||
| 557 | |||
| 558 | # $self->qemu_version: | ||
| 559 | # Return the pinned QEMU version, or undef. With no directive | ||
| 560 | # the tool checks nothing. | ||
| 561 | sub qemu_version ($self) | ||
| 562 | { | ||
| 563 | return $self->_setting('qemu_version'); | ||
| 564 | } | ||
| 565 | |||
| 566 | 1; | ||