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