Setup automator: Check that each plugin added to the generated setup file can be...
[ikiwiki.git] / IkiWiki / Setup / Automator.pm
1 #!/usr/bin/perl
2 # Ikiwiki setup automator.
3
4 package IkiWiki::Setup::Automator;
5
6 use warnings;
7 use strict;
8 use IkiWiki;
9 use IkiWiki::UserInfo;
10 use Term::ReadLine;
11 use File::Path;
12 use Encode;
13
14 sub ask ($$) {
15         my ($question, $default)=@_;
16
17         my $r=Term::ReadLine->new("ikiwiki");
18         $r->readline(encode_utf8($question)." ", $default);
19 }
20
21 sub prettydir ($) {
22         my $dir=shift;
23         $dir=~s/^\Q$ENV{HOME}\E\//~\//;
24         return $dir;
25 }
26
27 sub import (@) {
28         my $this=shift;
29         IkiWiki::Setup::merge({@_});
30
31         # Sanitize this to avoid problimatic directory names.
32         $config{wikiname}=~s/[^-A-Za-z0-9_]//g;
33         if (! length $config{wikiname}) {
34                 error gettext("you must enter a wikiname (that contains alphanumerics)");
35         }
36
37         # Avoid overwriting any existing files.
38         foreach my $key (qw{srcdir destdir repository dumpsetup}) {
39                 next unless exists $config{$key};
40                 my $add="";
41                 my $dir=IkiWiki::dirname($config{$key})."/";
42                 my $base=IkiWiki::basename($config{$key});
43                 while (-e $dir.$add.$base) {
44                         $add=1 if ! $add;
45                         $add++;
46                 }
47                 $config{$key}=$dir.$add.$base;
48         }
49         
50         # Set up wrapper
51         if ($config{rcs}) {
52                 if ($config{rcs} eq 'git') {
53                         $config{git_wrapper}=$config{repository}."/hooks/post-update";
54                 }
55                 elsif ($config{rcs} eq 'svn') {
56                         $config{svn_wrapper}=$config{repository}."/hooks/post-commit";
57                 }
58                 elsif ($config{rcs} eq 'monotone') {
59                         $config{mtn_wrapper}=$config{srcdir}."_MTN/ikiwiki-netsync-hook";
60                 }
61                 elsif ($config{rcs} eq 'darcs') {
62                         $config{darcs_wrapper}=$config{repository}."/_darcs/ikiwiki-wrapper";
63                 }
64                 elsif ($config{rcs} eq 'bzr') {
65                         # TODO
66                 }
67                 elsif ($config{rcs} eq 'mercurial') {
68                         # TODO
69                 }
70                 else {
71                         error sprintf(gettext("unsupported revision control system %s"),
72                                 $config{rcs});
73                 }
74         }
75
76         IkiWiki::checkconfig();
77
78         print "\n\nSetting up $config{wikiname} ...\n";
79
80         # Set up the srcdir.
81         mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
82         # Copy in example wiki.
83         if (exists $config{example}) {
84                 # cp -R is POSIX
85                 # Another reason not to use -a is so that pages such as blog
86                 # posts will not have old creation dates on this new wiki.
87                 system("cp -R $IkiWiki::installdir/share/ikiwiki/examples/$config{example}/* $config{srcdir}");
88                 delete $config{example};
89         }
90
91         # Set up the repository.
92         delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
93         if ($config{rcs}) {
94                 my @params=($config{rcs}, $config{srcdir});
95                 push @params, $config{repository} if exists $config{repository};
96                 if (system("ikiwiki-makerepo", @params) != 0) {
97                         error gettext("failed to set up the repository with ikiwiki-makerepo");
98                 }
99         }
100
101         # Make sure that all the listed plugins can load
102         # and checkconfig is ok. If a plugin fails to work,
103         # remove it from the configuration and keep on truckin'.
104         my %bakconfig=%config; # checkconfig can modify %config so back up
105         if (! eval { IkiWiki::loadplugins(); IkiWiki::checkconfig() }) {
106                 foreach my $plugin (@{$config{default_plugins}}, @{$bakconfig{add_plugins}}) {
107                         eval {
108                                 # delete all hooks so that only this plugins's
109                                 # checkconfig will be run
110                                 %IkiWiki::hooks=();
111                                 IkiWiki::loadplugin($plugin);
112                                 IkiWiki::run_hooks(checkconfig => sub { shift->() });
113                         };
114                         if ($@) {
115                                 print STDERR sprintf(gettext("** Disabling plugin %s, since it is failing with this message:"),
116                                         $plugin)."\n";
117                                 print STDERR "$@\n";
118                                 push @{$bakconfig{disable_plugins}}, $plugin;
119                         }
120                 }
121         }
122         %config=%bakconfig;
123
124         # Generate setup file.
125         require IkiWiki::Setup;
126         IkiWiki::Setup::dump($config{dumpsetup});
127
128         # Build the wiki, but w/o wrappers, so it's not live yet.
129         mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
130         if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
131                 die "ikiwiki --refresh --setup $config{dumpsetup} failed";
132         }
133
134         # Create admin user(s).
135         foreach my $admin (@{$config{adminuser}}) {
136                 next if $admin=~/^http\?:\/\//; # openid
137                 
138                 # Prompt for password w/o echo.
139                 my ($password, $password2);
140                 system('stty -echo 2>/dev/null');
141                 local $|=1;
142                 print "\n\nCreating wiki admin $admin ...\n";
143                 for (;;) {
144                         print "Choose a password: ";
145                         chomp($password=<STDIN>);
146                         print "\n";
147                         print "Confirm password: ";
148                         chomp($password2=<STDIN>);
149
150                         last if $password2 eq $password;
151
152                         print "Password mismatch.\n\n";
153                 }
154                 print "\n\n\n";
155                 system('stty sane 2>/dev/null');
156
157                 if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
158                     IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
159                         IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
160                 }
161                 else {
162                         error("problem setting up $admin user");
163                 }
164         }
165         
166         # Add wrappers, make live.
167         if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
168                 die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
169         }
170
171         # Add it to the wikilist.
172         mkpath("$ENV{HOME}/.ikiwiki");
173         open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
174         print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
175         close WIKILIST;
176         if (system("ikiwiki-update-wikilist") != 0) {
177                 print STDERR "** Failed to add you to the system wikilist file.\n";
178                 print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
179                 print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
180         }
181         
182         # Done!
183         print "\n\nSuccessfully set up $config{wikiname}:\n";
184         foreach my $key (qw{url srcdir destdir repository}) {
185                 next unless exists $config{$key};
186                 print "\t$key: ".(" " x (10 - length($key)))." ".
187                         prettydir($config{$key})."\n";
188         }
189         print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
190         print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
191         exit 0;
192 }
193
194 1