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