Virt.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package Trog::DataSource::Virt;
  2. use v5.36;
  3. use re '/aa';
  4. use File::Path();
  5. use Sys::Hostname::FQDN();
  6. use Sys::Virt();
  7. use Trog::Config();
  8. use Trog::Log qw{WARN INFO};
  9. =head1 Trog::DataSource::Virt
  10. Builds posts out of libvirt guests rather than out of the datastore.
  11. A post type says it wants this by naming it in its sidecar:
  12. "x-tcms-datasource": "Trog::DataSource::Virt"
  13. A series of that type then lists the guests of whichever hypervisor posts its
  14. relations pulled in, instead of listing posts somebody wrote. Nothing here is
  15. ever written to the datastore -- the guests are the source of truth, and the
  16. posts are rebuilt from them on every view.
  17. =head2 EDITABLE
  18. False. A guest is built from libvirt every time the page is drawn and has
  19. nothing behind it to save, so a post type backed by this wants no editor: a
  20. form posting to /post/save would write a real post into the datastore which
  21. would then sit alongside, and collide with, the synthesized ones.
  22. =cut
  23. use constant EDITABLE => 0;
  24. =head2 A NOTE ON SAFETY
  25. libvirt forbids virDomainScreenshot on a read-only connection, so the read path
  26. cannot protect itself by connecting read-only -- it holds a connection which is
  27. perfectly capable of destroying a guest.
  28. The discipline is therefore in the code: posts() and screenshot() call nothing
  29. but accessors, and every operation that changes a guest lives in act(), which
  30. is reached only from an explicit admin-gated POST. Keep it that way.
  31. =cut
  32. # How long a cached screenshot is considered fresh. These are console
  33. # captures, so they change slowly and are cheap to refetch.
  34. our $screenshot_ttl = 30;
  35. # Under www/assets like every other asset, in its own subtree because these are
  36. # not uploads somebody made -- they are cached datasource output, and anything
  37. # in here can be deleted at any time and will simply be refetched.
  38. #
  39. # Under private/ specifically because a console capture can show a login
  40. # prompt, a hostname, whatever the last command printed. That is the same
  41. # reasoning _handle_upload uses to put a private post's assets there, and the
  42. # screenshot route serves these to admins itself rather than relying on
  43. # anything about how www/assets is exposed.
  44. our $screenshot_dir = 'www/assets/private/datasource/virt';
  45. # Reconnecting per guest would be absurd -- one ssh handshake per hypervisor
  46. # per process is enough. Not a state var: a connection that has gone away
  47. # should be retried rather than remembered.
  48. sub _connect ( $uri, $cache = {} ) {
  49. return $cache->{$uri} if $cache->{$uri};
  50. local $@;
  51. my $conn = eval { Sys::Virt->new( uri => $uri ) };
  52. if ( !$conn ) {
  53. my $why = "$@";
  54. $why =~ s/\n.*//s;
  55. WARN("Could not connect to hypervisor '$uri': $why");
  56. return ( undef, $why );
  57. }
  58. $cache->{$uri} = $conn;
  59. return $conn;
  60. }
  61. # libvirt's numeric domain states, in the order the constants define them.
  62. my @states = (
  63. 'no state', 'running', 'blocked', 'paused',
  64. 'shutting down', 'shut off', 'crashed', 'suspended',
  65. );
  66. sub _state_name ($state) {
  67. return $states[$state] // 'unknown';
  68. }
  69. # What this machine calls itself. Not a state var so that a test can pretend
  70. # to be a different host without having to lie to Sys::Hostname.
  71. our $hostname;
  72. sub _hostname {
  73. return $hostname if defined $hostname;
  74. local $@;
  75. # Sys::Hostname croaks rather than returning undef when it cannot tell, and
  76. # the configured hostname is the next best thing we know about ourselves.
  77. $hostname = eval { Sys::Hostname::FQDN::fqdn() }
  78. || eval { Trog::Config->get()->param('general.hostname') }
  79. || '';
  80. return $hostname;
  81. }
  82. =head2 _is_self($name)
  83. Whether a guest by this name is the machine we are running on.
  84. Such a guest is acting as dom0: it is serving the page the guest list appears
  85. on, so powering it off or destroying it takes the site down along with whatever
  86. asked for that.
  87. Returns true if the guest name is the same as the fqdn of this machine.
  88. Returns false otherwise.
  89. =cut
  90. sub _is_self ($name) {
  91. my $me = lc( _hostname() );
  92. $name = lc( $name // '' );
  93. print("Name comp: $me $name\n");
  94. return 1 if $name eq $me;
  95. return 0;
  96. }
  97. =head2 posts($series, $query) = @posts
  98. One post per guest, across every hypervisor the series' relations pulled in.
  99. A hypervisor we cannot reach becomes a single post saying so, rather than
  100. vanishing -- a page that silently omits half your infrastructure is worse than
  101. one that tells you the connection is broken.
  102. =cut
  103. sub posts ( $series, $query ) {
  104. my $hypervisors = $series->{ $series->{'x-tcms-datasource-relation'} // 'hypervisors' };
  105. return () unless ref $hypervisors eq 'ARRAY';
  106. my $form = $series->{child_form} // '';
  107. my %cache;
  108. my @out;
  109. foreach my $hypervisor (@$hypervisors) {
  110. my $uri = $hypervisor->{conn_uri};
  111. next unless $uri;
  112. my ( $conn, $why ) = _connect( $uri, \%cache );
  113. if ( !$conn ) {
  114. push( @out, _unreachable_post( $hypervisor, $series, $form, $why ) );
  115. next;
  116. }
  117. local $@;
  118. my @domains = eval { $conn->list_all_domains() };
  119. if ($@) {
  120. my $err = "$@";
  121. $err =~ s/\n.*//s;
  122. push( @out, _unreachable_post( $hypervisor, $series, $form, $err ) );
  123. next;
  124. }
  125. push( @out, map { _post_for( $_, $hypervisor, $series, $form ) } @domains );
  126. }
  127. return @out;
  128. }
  129. sub _post_for ( $domain, $hypervisor, $series, $form ) {
  130. my $name = $domain->get_name();
  131. my $info = $domain->get_info();
  132. return {
  133. # The guest's own UUID: stable across renders, and what the control
  134. # routes look it back up by.
  135. id => $domain->get_uuid_string(),
  136. title => $name,
  137. form => $form,
  138. data => '',
  139. domain => $name,
  140. state => _state_name( $info->{state} ),
  141. is_active => $domain->is_active() ? 1 : 0,
  142. is_self => _is_self($name),
  143. memory => $info->{memory},
  144. vcpus => $info->{nrVirtCpu},
  145. hypervisor => $hypervisor->{id},
  146. hypervisor_title => $hypervisor->{title},
  147. # Fetched by the browser rather than inline, so ten guests don't mean
  148. # ten screenshots taken serially before the page can render.
  149. preview => "/guest/screenshot/$hypervisor->{id}/$name",
  150. # Enough of a post to survive being rendered like one.
  151. local_href => "/guest/$hypervisor->{id}/$name",
  152. href => '',
  153. tags => $series->{tags} // [],
  154. visibility => $series->{visibility} // 'private',
  155. created => time(),
  156. version => 0,
  157. user => $series->{user},
  158. method => 'GET',
  159. };
  160. }
  161. sub _unreachable_post ( $hypervisor, $series, $form, $why ) {
  162. return {
  163. id => "unreachable-$hypervisor->{id}",
  164. title => $hypervisor->{title} // 'hypervisor',
  165. form => $form,
  166. data => '',
  167. state => "unreachable: $why",
  168. is_active => 0,
  169. unreachable => 1,
  170. hypervisor => $hypervisor->{id},
  171. hypervisor_title => $hypervisor->{title},
  172. preview => '',
  173. local_href => '',
  174. href => '',
  175. tags => $series->{tags} // [],
  176. visibility => $series->{visibility} // 'private',
  177. created => time(),
  178. version => 0,
  179. user => $series->{user},
  180. method => 'GET',
  181. };
  182. }
  183. =head2 screenshot($hypervisor, $guest_name) = ($path, $error)
  184. Path to a reasonably fresh PNG of the guest's console.
  185. Cached on disk, because a page of guests means one of these per guest and they
  186. are only interesting to about the nearest half minute. libvirt hands back the
  187. mimetype; qemu gives us PNG already, so there is nothing to convert.
  188. Keyed by hypervisor as well as by guest -- guest names are only unique within
  189. one hypervisor, and two of them called 'staging' would otherwise show each
  190. other's console.
  191. =cut
  192. sub screenshot ( $hypervisor, $guest_name ) {
  193. my ($safe) = ( $guest_name // '' ) =~ m/^([A-Za-z0-9._-]+)$/;
  194. return ( undef, 'bad guest name' ) unless $safe;
  195. my ($host) = ( $hypervisor->{id} // '' ) =~ m/^([A-Za-z0-9._-]+)$/;
  196. return ( undef, 'bad hypervisor id' ) unless $host;
  197. my $conn_uri = $hypervisor->{conn_uri};
  198. return ( undef, 'hypervisor has no connection uri' ) unless $conn_uri;
  199. my $dir = "$screenshot_dir/$host";
  200. my $path = "$dir/$safe.png";
  201. return ( $path, undef ) if -f $path && ( time() - ( stat($path) )[9] ) < $screenshot_ttl;
  202. my ( $conn, $why ) = _connect($conn_uri);
  203. return ( undef, $why ) unless $conn;
  204. local $@;
  205. my $data = eval {
  206. my ($domain) = grep { $_->get_name() eq $safe } $conn->list_all_domains();
  207. die "no such guest\n" unless $domain;
  208. my $stream = $conn->new_stream();
  209. $domain->screenshot( $stream, 0, 0 );
  210. my $buffer = '';
  211. $stream->recv_all( sub { my ( undef, $chunk ) = @_; $buffer .= $chunk; return length($chunk) } );
  212. $stream->finish();
  213. $buffer;
  214. };
  215. if ( !$data ) {
  216. my $err = "$@";
  217. $err =~ s/\n.*//s;
  218. return ( undef, $err );
  219. }
  220. File::Path::make_path($dir);
  221. open( my $fh, '>', $path ) or return ( undef, "could not cache the screenshot: $!" );
  222. binmode $fh;
  223. print {$fh} $data;
  224. close $fh;
  225. return ( $path, undef );
  226. }
  227. =head2 act($action, $conn_uri, $domain_name, $user) = ($ok, $message)
  228. The operations which change a guest.
  229. Deliberately the only sub here that does, and deliberately not reachable from
  230. the render path -- see the safety note at the top. Everything it does is
  231. logged with the user who asked for it.
  232. =cut
  233. sub act ( $action, $conn_uri, $domain_name, $user ) {
  234. my ($safe) = $domain_name =~ m/^([A-Za-z0-9._-]+)$/;
  235. return ( 0, 'bad guest name' ) unless $safe;
  236. # The template draws neither of these buttons for this guest; this is what
  237. # actually stops it happening. /guest/act is a POST anybody holding the
  238. # admin acl can craft by hand, and if it went through there would be
  239. # nothing left running to report the mistake.
  240. if ( _is_self($safe) && ( $action // '' ) =~ m/^(?:poweroff|destroy)$/ ) {
  241. WARN("Refused guest action '$action' on '$safe': that is this server");
  242. return ( 0, "'$safe' is this server -- refusing to $action it" );
  243. }
  244. my ( $conn, $why ) = _connect($conn_uri);
  245. return ( 0, "could not reach the hypervisor: $why" ) unless $conn;
  246. local $@;
  247. my ($domain) = eval {
  248. grep { $_->get_name() eq $safe } $conn->list_all_domains();
  249. };
  250. return ( 0, "no guest called '$safe' on that hypervisor" ) unless $domain;
  251. INFO("Guest action '$action' on '$safe' requested by $user");
  252. my %actions = (
  253. poweron => sub { $_[0]->create() },
  254. poweroff => sub { $_[0]->shutdown() },
  255. snapshot => sub { $_[0]->create_snapshot() },
  256. destroy => sub { $_[0]->destroy() },
  257. );
  258. my $doit = $actions{ $action // '' };
  259. return ( 0, "'$action' is not something we do to a guest" ) unless $doit;
  260. eval { $doit->($domain); 1 } or do {
  261. my $err = "$@";
  262. $err =~ s/\n.*//s;
  263. WARN("Guest action '$action' on '$safe' failed: $err");
  264. return ( 0, $err );
  265. };
  266. return ( 1, "$action: ok" );
  267. }
  268. 1;