]> git.openstreetmap.org Git - chef.git/blob - cookbooks/web/templates/default/cleanup-assets.erb
Run statistics and cleanup scripts with right ruby version
[chef.git] / cookbooks / web / templates / default / cleanup-assets.erb
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use IO::Dir;
7 use IO::File;
8 use JSON::XS;
9
10 my $root = shift @ARGV;
11
12 my $manifest;
13
14 if (my $file = IO::File->new("${root}/tmp/manifest.json", "r"))
15 {
16     $manifest = decode_json(join("\n", $file->getlines));
17
18     $file->close;
19 }
20 else
21 {
22     die "Can't open ${root}/tmp/manifest.json: $!";
23 }
24
25 expire_assets($manifest, "${root}/public/assets", "");
26
27 exit 0;
28
29 sub expire_assets
30 {
31     my $manifest = shift;
32     my $assetroot = shift;
33     my $prefix = shift;
34
35     if (my $dir = IO::Dir->new("${assetroot}/${prefix}"))
36     {
37         $prefix = "${prefix}/" unless $prefix eq "";
38
39         while (my $name = $dir->read)
40         {
41             next if $name eq "." || $name eq "..";
42
43             my $path = $name = "${prefix}${name}";
44
45             $name =~ s/\.gz$//;
46
47             if (-d "${assetroot}/${path}")
48             {
49                 expire_assets($manifest, $assetroot, $path);
50             }
51             elsif (exists($manifest->{files}->{$name}))
52             {
53                 # Still a live asset (with checksum)
54             }
55             elsif (exists($manifest->{assets}->{$name}))
56             {
57                 # Still a live asset (without checksum)
58             }
59             elsif (-A "${assetroot}/${path}" > 28)
60             {
61                 unlink("${assetroot}/${path}") || die "Can't remove ${assetroot}/${path}: $!";
62             }
63         }
64
65         $dir->close;
66     }
67     else
68     {
69         die "Can't read ${assetroot}/${prefix}: $!";
70     }
71
72     return;
73 }