App-FuguVM

 view release on metacpan or  search on metacpan

lib/App/FuguVM/Proxy.pm  view on Meta::CPAN

sub guest_url ($self)
{
	return if !$self->is_running;

	my $port = $self->port;
	return if !defined $port;

	return 'http://' . HOST_GATEWAY . ":$port";
}

package App::FuguVM::Proxy::Cache;
our $VERSION = '0.3.1';

use Fugu::Log;
use Fugu::Proxy;

our @ISA = ('Fugu::Proxy::Cache');

# App::FuguVM::Proxy::Cache - which parts of an OpenBSD mirror to keep.
#
# The patterns are the release tree, the packages, the syspatch sets,
# the source tarballs and the files that an installer reads. Every one
# of them is version-scoped, so nothing here outlives the release it
# belongs to. That is what makes prune safe.
#
# The distfile tree is the one exception: a distfile belongs to a
# port and not to a release, so no version scopes it and prune leaves
# it. The distfile cap of trim_distfiles is its bound instead, and
# the tree is cacheable only while the cap is above zero.

my @CACHEABLE = (
	qr{/pub/OpenBSD/\d+\.\d+/\w+/.*\.(tgz|img|gz)$},    # File sets
	qr{/pub/OpenBSD/syspatch/.*\.tgz$},                 # Patches
	qr{/pub/OpenBSD/\d+\.\d+/packages/\w+/.*\.tgz$},    # Packages
	qr{/pub/OpenBSD/\d+\.\d+/\w+/SHA256(\.sig)?$},      # Checksums
	qr{/pub/OpenBSD/\d+\.\d+/\w+/miniroot\d+\.img$},    # Miniroot images
	qr{/pub/OpenBSD/\d+\.\d+/\w+/bsd(\.mp|\.rd)?$},     # Kernel files
	qr{/pub/OpenBSD/\d+\.\d+/\w+/BUILDINFO$},           # Build info
	qr{/pub/OpenBSD/\d+\.\d+/\w+/.*\.txt$},    # Text files (index, etc)

	# The source tarballs of a release, by name. A wildcard at the
	# version level would admit any file that a mirror puts there.
	qr{/pub/OpenBSD/\d+\.\d+/(ports|src|sys|xenocara)\.tar\.gz$},

	# The manifest of the version directory, which signs the four
	# source tarballs.
	qr{/pub/OpenBSD/\d+\.\d+/SHA256(\.sig)?$},
);

# The distfile pattern is conditional on the cap. It requires a file
# name at the end, so a directory listing with a trailing solidus
# stays outside the cache. It filters no extension, because a
# distfile carries every extension and sometimes none.
my $DISTFILE = qr{/pub/OpenBSD/distfiles/(?:[^/]+/)*[^/]+$};

# $class->new($cache_dir, $limit):
#	Build the cache. The optional limit is the distfile cap in
#	bytes, and 0 turns the distfile cache off.
sub new ( $class, $cache_dir, $limit = 0 )
{
	my $self = $class->SUPER::new(
		dir       => $cache_dir,
		cacheable => sub ($url) { _is_openbsd_content( $url, $limit ) },
		types     => {

			# A kernel has no extension, so the generic
			# table cannot name it
			qr{/bsd(\.mp|\.rd)?$} => 'application/octet-stream',
		},
	);
	$self->{distfile_limit} = $limit;

	return $self;
}

# _is_openbsd_content($url, $limit):
#	Report if the URL is part of an OpenBSD release tree, or a
#	distfile while the cap is above zero. A cache that grows with
#	no bound in a home directory is the failure that the default
#	of 0 avoids.
sub _is_openbsd_content ( $url, $limit = 0 )
{
	for my $pattern (@CACHEABLE) {
		return 1 if $url =~ $pattern;
	}

	return 1 if $limit > 0 && $url =~ $DISTFILE;

	return 0;
}

# $self->store($url, $content):
#	Store one response body, and hold the distfile tree under the
#	cap after a distfile store. A set store must not walk the
#	distfile tree.
sub store ( $self, $url, $content )
{
	my $path = $self->SUPER::store( $url, $content );
	return if !defined $path;

	$self->trim_distfiles if $url =~ $DISTFILE;

	return $path;
}

# $self->distfile_limit:
#	Return the distfile cap in bytes.
sub distfile_limit ($self)
{
	return $self->{distfile_limit};
}

# $self->distfile_size:
#	Return the bytes of the distfile tree, over every cached host.
sub distfile_size ($self)
{
	my $total = 0;
	$total += $self->dir_size($_) for $self->_distfile_roots;

	return $total;
}

# $self->trim_distfiles:
#	Hold the distfile tree under the cap. The eviction removes the
#	oldest file first, by modification time. The modification time
#	is the store time of the file, because store writes each file
#	one time; the access time is not usable, because a host can
#	mount its home directory with noatime. A cap of 0 removes the
#	whole tree, because the operator turned the cache off. The
#	method removes files and leaves the directories, because a
#	later fetch refills them. It returns the removed files as
#	[ { path, size } ].
sub trim_distfiles ($self)
{
	my @files;
	for my $root ( $self->_distfile_roots ) {
		$self->walk(
			$root,
			sub ($path) {
				my @stat = stat $path;
				return if !@stat;
				push @files,
				    {
					path  => $path,
					size  => $stat[7],
					mtime => $stat[9],
				    };
			} );
	}

	my $total = 0;
	$total += $_->{size} for @files;

	my @removed;
	for my $file ( sort { $a->{mtime} <=> $b->{mtime} } @files ) {
		last if $total <= $self->{distfile_limit};

		unlink $file->{path} or do {



( run in 1.658 second using v1.01-cache-2.11-cpan-4e491c91c6e )