Host.pm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: Host.pm,v 1.7 2015/05/13 12:21:11 espie Exp $
  3. #
  4. # Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
  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. use strict;
  18. use warnings;
  19. # we have unique objects for hosts, so we can put properties in there.
  20. package DPB::Host;
  21. my $hosts = {};
  22. sub shell
  23. {
  24. my $self = shift;
  25. return $self->{shell};
  26. }
  27. sub new
  28. {
  29. my ($class, $name, $prop) = @_;
  30. if ($class->name_is_localhost($name)) {
  31. $class = "DPB::Host::Localhost";
  32. $name = 'localhost';
  33. $prop->{build_user}->enforce_local
  34. if defined $prop->{build_user};
  35. } else {
  36. require DPB::Core::Distant;
  37. $class = "DPB::Host::Distant";
  38. }
  39. if (!defined $hosts->{$name}) {
  40. my $h = bless {host => $name,
  41. prop => $prop }, $class;
  42. # XXX have to register *before* creating the shell
  43. $hosts->{$name} = $h;
  44. $h->{shell} = $h->shellclass->new($h);
  45. }
  46. return $hosts->{$name};
  47. }
  48. sub name
  49. {
  50. my $self = shift;
  51. return $self->{host};
  52. }
  53. sub fullname
  54. {
  55. my $self = shift;
  56. my $name = $self->name;
  57. if (defined $self->{prop}->{jobs}) {
  58. $name .= "/$self->{prop}->{jobs}";
  59. }
  60. return $name;
  61. }
  62. sub name_is_localhost
  63. {
  64. my ($class, $host) = @_;
  65. if ($host eq "localhost" or $host eq DPB::Core::Local->hostname) {
  66. return 1;
  67. } else {
  68. return 0;
  69. }
  70. }
  71. package DPB::Host::Localhost;
  72. our @ISA = qw(DPB::Host);
  73. sub is_localhost
  74. {
  75. return 1;
  76. }
  77. sub is_alive
  78. {
  79. return 1;
  80. }
  81. sub shellclass
  82. {
  83. my $self = shift;
  84. if ($self->{prop}{iamroot}) {
  85. return "DPB::Shell::Local::Root";
  86. } elsif ($self->{prop}{chroot}) {
  87. return "DPB::Shell::Local::Chroot";
  88. } else {
  89. return "DPB::Shell::Local";
  90. }
  91. }
  92. # XXX this is a "quicky" shell before we set up hosts properly
  93. sub getshell
  94. {
  95. my ($class, $state) = @_;
  96. my $prop;
  97. if ($state->{default_prop}) {
  98. $prop = $state->{default_prop};
  99. } else {
  100. $prop = {};
  101. if ($state->{chroot}) {
  102. $prop->{chroot} = $state->{chroot};
  103. }
  104. }
  105. $prop->{iamroot} = $< == 0;
  106. my $h = bless { prop => $prop }, $class;
  107. return $h->shellclass->new($h);
  108. }
  109. 1;