summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/amazon_s3.pm
blob: 9cb74fb1e805e14dfc5fae71cfd1dd694655aea5 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::amazon_s3;
  3. use warnings;
  4. no warnings 'redefine';
  5. use strict;
  6. use IkiWiki 2.00;
  7. use IkiWiki::Render;
  8. use Net::Amazon::S3;
  9. # Store references to real subs before overriding them.
  10. our %subs;
  11. BEGIN {
  12. foreach my $sub (qw{IkiWiki::writefile IkiWiki::prune}) {
  13. $subs{$sub}=\&$sub;
  14. }
  15. };
  16. sub import { #{{{
  17. hook(type => "getopt", id => "amazon_s3", call => \&getopt);
  18. hook(type => "getsetup", id => "amazon_s3", call => \&getsetup);
  19. hook(type => "checkconfig", id => "amazon_s3", call => \&checkconfig);
  20. } # }}}
  21. sub getopt () { #{{{
  22. eval q{use Getopt::Long};
  23. error($@) if $@;
  24. Getopt::Long::Configure('pass_through');
  25. GetOptions("delete-bucket" => sub {
  26. my $bucket=getbucket();
  27. debug(gettext("deleting bucket.."));
  28. my $resp = $bucket->list_all or die $bucket->err . ": " . $bucket->errstr;
  29. foreach my $key (@{$resp->{keys}}) {
  30. debug("\t".$key->{key});
  31. $bucket->delete_key($key->{key}) or die $bucket->err . ": " . $bucket->errstr;
  32. }
  33. $bucket->delete_bucket or die $bucket->err . ": " . $bucket->errstr;
  34. debug(gettext("done"));
  35. exit(0);
  36. });
  37. } #}}}
  38. sub getsetup () { #{{{
  39. return
  40. amazon_s3_key_id => {
  41. type => "string",
  42. default => "",
  43. description => "public access key id",
  44. safe => 1,
  45. rebuild => 0,
  46. },
  47. amazon_s3_key_id => {
  48. type => "string",
  49. default => "",
  50. description => "file holding secret key",
  51. safe => 0, # ikiwiki reads this file
  52. rebuild => 0,
  53. },
  54. amazon_s3_bucket => {
  55. type => "string",
  56. default => "",
  57. example => "mywiki",
  58. description => "globally unique name of bucket to store wiki in",
  59. safe => 1,
  60. rebuild => 1,
  61. },
  62. amazon_s3_prefix => {
  63. type => "string",
  64. default => "wiki/",
  65. description => "a prefix to prepend to each page name",
  66. safe => 1,
  67. rebuild => 1,
  68. },
  69. amazon_s3_location => {
  70. type => "string",
  71. default => "",
  72. example => "EU",
  73. description => "which S3 datacenter to use (leave blank for default)",
  74. safe => 1,
  75. rebuild => 1,
  76. },
  77. amazon_s3_dupindex => {
  78. type => "boolean",
  79. default => 0,
  80. description => "store each index file twice? (to allow urls ending in \"/index.html\" and \"/\")",
  81. safe => 1,
  82. rebuild => 1,
  83. },
  84. } #}}}
  85. sub checkconfig { #{{{
  86. foreach my $field (qw{amazon_s3_key_id amazon_s3_key_file
  87. amazon_s3_bucket}) {
  88. if (! exists $config{$field} || ! defined $config{$field}) {
  89. error(sprintf(gettext("Must specify %s"), $field));
  90. }
  91. }
  92. if (! exists $config{amazon_s3_prefix} ||
  93. ! defined $config{amazon_s3_prefix}) {
  94. $config{amazon_s3_prefix}="wiki/";
  95. }
  96. } #}}}
  97. {
  98. my $bucket;
  99. sub getbucket { #{{{
  100. return $bucket if defined $bucket;
  101. open(IN, "<", $config{amazon_s3_key_file}) || error($config{amazon_s3_key_file}.": ".$!);
  102. my $key=<IN>;
  103. chomp $key;
  104. close IN;
  105. my $s3=Net::Amazon::S3->new({
  106. aws_access_key_id => $config{amazon_s3_key_id},
  107. aws_secret_access_key => $key,
  108. retry => 1,
  109. });
  110. # make sure the bucket exists
  111. if (exists $config{amazon_s3_location}) {
  112. $bucket=$s3->add_bucket({
  113. bucket => $config{amazon_s3_bucket},
  114. location_constraint => $config{amazon_s3_location},
  115. });
  116. }
  117. else {
  118. $bucket=$s3->add_bucket({
  119. bucket => $config{amazon_s3_bucket},
  120. });
  121. }
  122. if (! $bucket) {
  123. error(gettext("Failed to create bucket in S3: ").
  124. $s3->err.": ".$s3->errstr."\n");
  125. }
  126. return $bucket;
  127. } #}}}
  128. }
  129. # Given a file, return any S3 keys associated with it.
  130. sub file2keys ($) { #{{{
  131. my $file=shift;
  132. my @keys;
  133. if ($file =~ /^\Q$config{destdir}\/\E(.*)/) {
  134. push @keys, $config{amazon_s3_prefix}.$1;
  135. # Munge foo/index.html to foo/
  136. if ($keys[0]=~/(^|.*\/)index.$config{htmlext}$/) {
  137. # A duplicate might need to be stored under the
  138. # unmunged name too.
  139. if (!$config{usedirs} || $config{amazon_s3_dupindex}) {
  140. push @keys, $1;
  141. }
  142. else {
  143. @keys=($1);
  144. }
  145. }
  146. }
  147. return @keys;
  148. } #}}}
  149. package IkiWiki;
  150. use File::MimeInfo;
  151. use Encode;
  152. # This is a wrapper around the real writefile.
  153. sub writefile ($$$;$$) { #{{{
  154. my $file=shift;
  155. my $destdir=shift;
  156. my $content=shift;
  157. my $binary=shift;
  158. my $writer=shift;
  159. # First, write the file to disk.
  160. my $ret=$IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::writefile'}->($file, $destdir, $content, $binary, $writer);
  161. my @keys=IkiWiki::Plugin::amazon_s3::file2keys("$destdir/$file");
  162. # Store the data in S3.
  163. if (@keys) {
  164. my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
  165. # The http layer tries to downgrade utf-8
  166. # content, but that can fail (see
  167. # http://rt.cpan.org/Ticket/Display.html?id=35710),
  168. # so force convert it to bytes.
  169. $content=encode_utf8($content) if defined $content;
  170. my %opts=(
  171. acl_short => 'public-read',
  172. content_type => mimetype("$destdir/$file"),
  173. );
  174. # If there are multiple keys to write, data is sent
  175. # multiple times.
  176. # TODO: investigate using the new copy operation.
  177. # (It may not be robust enough.)
  178. foreach my $key (@keys) {
  179. my $res;
  180. if (! $writer) {
  181. $res=$bucket->add_key($key, $content, \%opts);
  182. }
  183. else {
  184. # This test for empty files is a workaround
  185. # for this bug:
  186. # http://rt.cpan.org//Ticket/Display.html?id=35731
  187. if (-z "$destdir/$file") {
  188. $res=$bucket->add_key($key, "", \%opts);
  189. }
  190. else {
  191. # read back in the file that the writer emitted
  192. $res=$bucket->add_key_filename($key, "$destdir/$file", \%opts);
  193. }
  194. }
  195. if (! $res) {
  196. error(gettext("Failed to save file to S3: ").
  197. $bucket->err.": ".$bucket->errstr."\n");
  198. }
  199. }
  200. }
  201. return $ret;
  202. } #}}}
  203. # This is a wrapper around the real prune.
  204. sub prune ($) { #{{{
  205. my $file=shift;
  206. my @keys=IkiWiki::Plugin::amazon_s3::file2keys($file);
  207. # Prune files out of S3 too.
  208. if (@keys) {
  209. my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
  210. foreach my $key (@keys) {
  211. my $res=$bucket->delete_key($key);
  212. if (! $res) {
  213. error(gettext("Failed to delete file from S3: ").
  214. $bucket->err.": ".$bucket->errstr."\n");
  215. }
  216. }
  217. }
  218. return $IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::prune'}->($file);
  219. } #}}}
  220. 1