Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 102 lines (77 sloc) 2.01 KB
#!/usr/bin/env perl
use autodie;
use Modern::Perl;
use File::Basename;
use Getopt::Long;
my $site_bucket = "www.jonashietala.se";
my $file_bucket = "jonashietala-files";
my $file_endpoint = "https://s3-eu-west-1.amazonaws.com/$file_bucket";
my $help;
my $sync_files;
my $sync_site;
GetOptions(
'help|h' => \$help,
'files' => \$sync_files,
'site' => \$sync_site,
);
# Fetch path to current directory
my $root = dirname(__FILE__);
my $site = "_site";
if ($help) {
help();
}
if ($sync_site) {
say "Syncing site...\n";
# Sync media files (cache: 10 weeks)
say "Syncing images";
sync ($site_bucket, "$site/images", "max-age=6048000");
# Sync fonts (cache: 10 weeks)
say "Syncing fonts";
sync ($site_bucket, "$site/fonts", "max-age=6048000");
# Sync css (cache: 1 hour)
say "Syncing css";
sync ($site_bucket, "$site/css", "max-age=3600", "-m text/css");
# Sync rest (cache: 1 min)
say "Syncing rest";
sync ($site_bucket, "$site/", "max-age=60, must-revalidate");
say "Removing deleted";
sync ($site_bucket, "$site/", "", "--delete-removed");
}
if ($sync_files) {
# Upload files bucket (cache: 10 weeks)
say "Uploading files...\n";
my $num = 0;
my @output = capture_sync ($file_bucket, "files/", "max-age=6048000");
for (@output) {
if (m|stored as 's3://\Q$file_bucket\E/(.+)'|) {
say "New file: $file_endpoint/$1";
$num++;
}
}
say "Done. Uploaded $num files.";
}
sub sync {
my $cmd = sync_cmd (@_);
system("$cmd 2>&1");
}
sub capture_sync {
my $cmd = sync_cmd (@_);
my @output = `$cmd`;
return @output;
}
sub sync_cmd {
my ($bucket, $dir, $cache, $option) = @_;
my $header = "";
$header = "--add-header=\"Cache-Control: $cache\"" if $cache;
$option = "" unless $option;
return "s3cmd sync $option --acl-public $header $root/$dir s3://$bucket/";
}
sub help {
say "
$0 --files
upload files
$0 --site
sync site
";
exit;
}
You can’t perform that action at this time.