| 1 | # Linux::LXC - Manage LXC containers. | ||
| 2 | # Copyright (C) 2020 Spydemon <jsaipakoimetr@spyzone.fr> | ||
| 3 | # | ||
| 4 | # This program is free software: you can redistribute it and/or modify | ||
| 5 | # it under the terms of the GNU General Public License as published by | ||
| 6 | # the Free Software Foundation, either version 3 of the License, or | ||
| 7 | # (at your option) any later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, | ||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | # GNU General Public License for more details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License | ||
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package Linux::LXC; | ||
| 18 | $Linux::LXC::VERSION = '1.0004'; | ||
| 19 | |||
| 20 | use v5.0; | ||
| 21 | |||
| 22 | use Backticks; | ||
| 23 | use Carp; | ||
| 24 | use Exporter qw(import); | ||
| 25 | use File::Copy qw(move); | ||
| 26 | use File::Temp; | ||
| 27 | use Moo; | ||
| 28 | use IPC::Run qw(run); | ||
| 29 | |||
| 30 | use constant ALLOW_UNDEF => 0x01; | ||
| 31 | use constant ERASING_MODE => 0x01; | ||
| 32 | use constant ADDITION_MODE => 0x02; | ||
| 33 | our @EXPORT_OK = ('ALLOW_UNDEF', 'ERASING_MODE', 'ADDITION_MODE'); | ||
| 34 | |||
| 35 | $Backticks::autodie = 1; | ||
| 36 | |||
| 37 | BEGIN { | ||
| 38 | my $lxc = `lxc-start --version`; | ||
| 39 | die ("LXC seems to not be installed and is needed for Linux::LXC.\n") unless $lxc; | ||
| 40 | } | ||
| 41 | |||
| 42 | ###################### | ||
| 43 | # Module subroutines # | ||
| 44 | ###################### | ||
| 45 | sub get_existing_containers { | ||
| 46 | split("\n", `lxc-ls -1`); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub get_running_containers { | ||
| 50 | split("\n", `lxc-ls -1 --running`); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub get_stopped_containers { | ||
| 54 | split("\n", `lxc-ls -1 --stopped`); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub get_version { | ||
| 58 | my $version = `lxc-start --version`; | ||
| 59 | chomp($version); | ||
| 60 | return $version; | ||
| 61 | } | ||
| 62 | |||
| 63 | ####################### | ||
| 64 | # Objects subroutines # | ||
| 65 | ####################### | ||
| 66 | has utsname => ( | ||
| 67 | 'is' => 'ro', | ||
| 68 | 'required' => 1, | ||
| 69 | 'reader' => 'get_utsname' | ||
| 70 | ); | ||
| 71 | |||
| 72 | has template => ( | ||
| 73 | 'is' => 'rw', | ||
| 74 | 'reader' => '_get_template', | ||
| 75 | 'writer' => 'set_template' | ||
| 76 | ); | ||
| 77 | |||
| 78 | sub deploy { | ||
| 79 | my ($this) = @_; | ||
| 80 | $this->_check_container_is_not_existing(); | ||
| 81 | my $utsname = $this->get_utsname(); | ||
| 82 | my $template = $this->get_template(); | ||
| 83 | $this->_qx("lxc-create -n $utsname -t $template", undef, wantarray); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub destroy { | ||
| 87 | my ($this) = @_; | ||
| 88 | $this->is_running() and $this->stop(); | ||
| 89 | $this->_qx('lxc-destroy -n '.$this->get_utsname(), undef, wantarray); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub exec { | ||
| 93 | my ($this, $cmd) = @_; | ||
| 94 | $this->_check_container_is_running(); | ||
| 95 | $this->_qx('lxc-attach --clear-env -n '.$this->get_utsname(), $cmd, wantarray); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub get_lxc_path { | ||
| 99 | my ($this) = @_; | ||
| 100 | '/var/lib/lxc/' . $this->get_utsname(); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub get_config { | ||
| 104 | my ($this, $attr, $filter, $flags) = @_; | ||
| 105 | unless ($attr) { | ||
| 106 | croak 'Parameter to get is missing'; | ||
| 107 | } | ||
| 108 | if (defined $filter and ref($filter) ne 'Regexp') { | ||
| 109 | croak '$filter should be a regular expresion'; | ||
| 110 | } | ||
| 111 | $filter //= qr/(.*)/; | ||
| 112 | my $allow_undef = defined ($flags) && $flags & ALLOW_UNDEF; | ||
| 113 | $this->_check_container_is_existing(); | ||
| 114 | open my $CONF, '<', $this->get_lxc_path() . '/config'; | ||
| 115 | my @results; | ||
| 116 | for (<$CONF>) { | ||
| 117 | if (/^$attr\W*=\W*(?P<value>.*)$/) { | ||
| 118 | push @results, $+{value} =~ $filter; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | if (!@results && !$allow_undef) { | ||
| 122 | croak "'$attr' attribute was not found in lxc configuration file with filter $filter"; | ||
| 123 | } | ||
| 124 | return @results; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub get_template { | ||
| 128 | my ($this) = @_; | ||
| 129 | if (!$this->_get_template()) { | ||
| 130 | my $utsname = $this->get_utsname(); | ||
| 131 | croak "Template is not provided for '$utsname' container."; | ||
| 132 | } | ||
| 133 | $this->_get_template(); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub is_existing { | ||
| 137 | my ($this) = @_; | ||
| 138 | my $name = $this->get_utsname(); | ||
| 139 | grep {/^$name$/} get_existing_containers(); | ||
| 140 | } | ||
| 141 | |||
| 142 | sub is_running { | ||
| 143 | my ($this) = @_; | ||
| 144 | my $name = $this->get_utsname(); | ||
| 145 | grep {/^$name$/} get_running_containers(); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub is_stopped { | ||
| 149 | my ($this) = @_; | ||
| 150 | my $name = $this->get_utsname(); | ||
| 151 | grep {/^$name$/} get_stopped_containers(); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub put { | ||
| 155 | my ($this, $input, $dest) = @_; | ||
| 156 | # id map configuration attribute name changes between LXC version 2 and 3… Purpose of $idmap_label variable is to | ||
| 157 | # take care of it. | ||
| 158 | my $idmap_label = $this->get_version() =~ /^2/ | ||
| 159 | ? 'lxc.id_map' | ||
| 160 | : 'lxc.idmap'; | ||
| 161 | my ($uid) = $this->get_config($idmap_label, qr/^u 0 (\d+)/, ALLOW_UNDEF); | ||
| 162 | $this->_check_container_is_existing(); | ||
| 163 | if (!-r $input) { | ||
| 164 | croak "Input $input is not readable"; | ||
| 165 | } | ||
| 166 | if ($dest !~ /^\//) { | ||
| 167 | croak 'Destination should be an absolute path'; | ||
| 168 | } | ||
| 169 | $dest = $this->get_lxc_path().'/rootfs'.$dest; | ||
| 170 | my ($dir_dest) = $dest =~ /^(.*\/)/; | ||
| 171 | if (defined $uid) { | ||
| 172 | my @folders = split(/\//, $dir_dest); | ||
| 173 | shift @folders; | ||
| 174 | my $abs_folder = ''; | ||
| 175 | while (my $cur_folder = shift @folders) { | ||
| 176 | $abs_folder .= '/' . $cur_folder; | ||
| 177 | if (!-d $abs_folder) { | ||
| 178 | `mkdir $abs_folder`; | ||
| 179 | `chown $uid:$uid $abs_folder`; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | `cp -R $input $dest`; | ||
| 183 | `chown -R $uid:$uid $dest` if defined $uid; | ||
| 184 | } else { | ||
| 185 | -d $dir_dest or `mkdir -p $dir_dest`; | ||
| 186 | `cp -R $input $dest`; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | sub del_config { | ||
| 191 | my ($this, $attr, $filter) = @_; | ||
| 192 | if (defined $filter and ref($filter) ne 'Regexp') { | ||
| 193 | croak '$filter should be a regular expression'; | ||
| 194 | } | ||
| 195 | $filter //= qr/(.*)/; | ||
| 196 | open my $CONF_R, '<', $this->get_lxc_path() . '/config'; | ||
| 197 | my $CONF_W = new File::Temp(); | ||
| 198 | my $entries_deleted = 0; | ||
| 199 | for (<$CONF_R>) { | ||
| 200 | if (/^$attr\W*=\W*(?P<value>.*)$/) { | ||
| 201 | if ($+{value} =~ $filter) { | ||
| 202 | $entries_deleted++; | ||
| 203 | next; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | print $CONF_W $_; | ||
| 207 | } | ||
| 208 | close $CONF_W; | ||
| 209 | move ($CONF_W->filename, $this->get_lxc_path() . '/config') or die ($!); | ||
| 210 | return $entries_deleted; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub set_config { | ||
| 214 | my ($this, $attr, $value, $flags) = @_; | ||
| 215 | $flags = ERASING_MODE unless defined $flags; | ||
| 216 | croak 'set_config can not be in erasing and addition mode' | ||
| 217 | if ($flags == (ERASING_MODE | ADDITION_MODE)); | ||
| 218 | $this->_check_container_is_existing(); | ||
| 219 | if ($flags & ADDITION_MODE) { | ||
| 220 | open my $CONF, '>>', $this->get_lxc_path() . '/config'; | ||
| 221 | print $CONF "$attr = $value\n"; | ||
| 222 | } else { | ||
| 223 | my $written = 0; | ||
| 224 | open my $CONF_R, '<', $this->get_lxc_path().'/config'; | ||
| 225 | my $CONF_W = File::Temp->new(); | ||
| 226 | for (<$CONF_R>) { | ||
| 227 | if (/^$attr = .*$/) { | ||
| 228 | print $CONF_W "$attr = $value\n"; | ||
| 229 | $written = 1; | ||
| 230 | } else { | ||
| 231 | print $CONF_W $_; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | !$written and print $CONF_W "$attr = $value\n"; | ||
| 235 | close $CONF_R; | ||
| 236 | close $CONF_W; | ||
| 237 | move ($CONF_W->filename, $this->get_lxc_path().'/config') or die ($!); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | sub start { | ||
| 242 | my ($this) = @_; | ||
| 243 | my $utsname = $this->get_utsname(); | ||
| 244 | $this->_check_container_is_existing(); | ||
| 245 | if ($this->is_running()) { | ||
| 246 | return; | ||
| 247 | } | ||
| 248 | $this->_qx("lxc-start -d -n $utsname", undef, wantarray); | ||
| 249 | } | ||
| 250 | |||
| 251 | sub stop { | ||
| 252 | my ($this) = @_; | ||
| 253 | if (!$this->is_running()) { | ||
| 254 | return; | ||
| 255 | } | ||
| 256 | $this->_qx('lxc-stop -n '.$this->get_utsname(), undef, wantarray); | ||
| 257 | } | ||
| 258 | |||
| 259 | ######################## | ||
| 260 | # Internal subroutines # | ||
| 261 | ######################## | ||
| 262 | sub _check_container_is_existing { | ||
| 263 | my ($this) = @_; | ||
| 264 | if (!$this->is_existing()) { | ||
| 265 | my (undef, undef, undef, $caller) = caller(1); | ||
| 266 | $caller =~ /::(\w*)$/; | ||
| 267 | croak 'Container ' . $this->get_utsname() . ' doesn\'t exist'; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | sub _check_container_is_not_existing { | ||
| 272 | my ($this) = @_; | ||
| 273 | if ($this->is_existing()) { | ||
| 274 | croak 'Container ' . $this->get_utsname() . ' already exists'; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | sub _check_container_is_running { | ||
| 279 | my ($this) = @_; | ||
| 280 | if ($this->is_stopped()) { | ||
| 281 | croak 'Container ' . $this->get_utsname() . ' is not running'; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | sub _qx { | ||
| 286 | my ($this, $cmd, $params, $wantarray) = @_; | ||
| 287 | my @cmd = split(' ', $cmd); | ||
| 288 | my ($stdout, $stderr); | ||
| 289 | my $result = run \@cmd, \$params, \$stdout, \$stderr; | ||
| 290 | $wantarray and return ($result, $stdout, $stderr); | ||
| 291 | return $result; | ||
| 292 | } | ||
| 293 | |||
| 294 | 1 |