fall back from YAML::Any to just YAML
[ikiwiki] / 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->ornaments("md,me");
19         $r->readline(encode_utf8($question)." ", $default);
20 }
21
22 sub prettydir ($) {
23         my $dir=shift;
24         $dir=~s/^\Q$ENV{HOME}\E\//~\//;
25         return $dir;
26 }
27
28 sub sanitize_wikiname ($) {
29         my $wikiname=shift;
30
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 }
38
39 sub import (@) {
40         my $this=shift;
41         $config{setuptype}='Standard';
42         IkiWiki::Setup::merge({@_});
43
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                 }
76                 elsif ($config{rcs} eq 'mercurial') {
77                         # TODO
78                 }
79                 elsif ($config{rcs} eq 'cvs') {
80                         $config{cvs_wrapper}=$config{repository}."/CVSROOT/post-commit";
81                 }
82                 else {
83                         error sprintf(gettext("unsupported revision control system %s"),
84                                 $config{rcs});
85                 }
86         }
87
88         IkiWiki::checkconfig();
89
90         print "\n\nSetting up $config{wikiname} ...\n";
91
92         # Set up the srcdir.
93         mkpath($config{srcdir}) || die "mkdir $config{srcdir}: $!";
94         # Copy in example wiki.
95         if (exists $config{example}) {
96                 # cp -R is POSIX
97                 # Another reason not to use -a is so that pages such as blog
98                 # posts will not have old creation dates on this new wiki.
99                 system("cp -R $IkiWiki::installdir/share/ikiwiki/examples/$config{example}/* $config{srcdir}");
100                 delete $config{example};
101         }
102
103         # Set up the repository.
104         delete $config{repository} if ! $config{rcs} || $config{rcs}=~/bzr|mercurial/;
105         if ($config{rcs}) {
106                 my @params=($config{rcs}, $config{srcdir});
107                 push @params, $config{repository} if exists $config{repository};
108                 if (system("ikiwiki-makerepo", @params) != 0) {
109                         error gettext("failed to set up the repository with ikiwiki-makerepo");
110                 }
111         }
112
113         # Make sure that all the listed plugins can load
114         # and checkconfig is ok. If a plugin fails to work,
115         # remove it from the configuration and keep on truckin'.
116         my %bakconfig=%config; # checkconfig can modify %config so back up
117         if (! eval { IkiWiki::loadplugins(); IkiWiki::checkconfig() }) {
118                 foreach my $plugin (@{$config{default_plugins}}, @{$bakconfig{add_plugins}}) {
119                         eval {
120                                 # delete all hooks so that only this plugins's
121                                 # checkconfig will be run
122                                 %IkiWiki::hooks=();
123                                 IkiWiki::loadplugin($plugin);
124                                 IkiWiki::run_hooks(checkconfig => sub { shift->() });
125                         };
126                         if ($@) {
127                                 print STDERR sprintf(gettext("** Disabling plugin %s, since it is failing with this message:"),
128                                         $plugin)."\n";
129                                 print STDERR "$@\n";
130                                 push @{$bakconfig{disable_plugins}}, $plugin;
131                         }
132                 }
133         }
134         %config=%bakconfig;
135
136         # Generate setup file.
137         require IkiWiki::Setup;
138         IkiWiki::Setup::dump($config{dumpsetup});
139
140         # Build the wiki, but w/o wrappers, so it's not live yet.
141         mkpath($config{destdir}) || die "mkdir $config{destdir}: $!";
142         if (system("ikiwiki", "--refresh", "--setup", $config{dumpsetup}) != 0) {
143                 die "ikiwiki --refresh --setup $config{dumpsetup} failed";
144         }
145
146         # Create admin user(s).
147         foreach my $admin (@{$config{adminuser}}) {
148                 next if defined IkiWiki::openiduser($admin);
149                 
150                 # Prompt for password w/o echo.
151                 my ($password, $password2);
152                 system('stty -echo 2>/dev/null');
153                 local $|=1;
154                 print "\n\nCreating wiki admin $admin ...\n";
155                 for (;;) {
156                         print "Choose a password: ";
157                         chomp($password=<STDIN>);
158                         print "\n";
159                         print "Confirm password: ";
160                         chomp($password2=<STDIN>);
161
162                         last if $password2 eq $password;
163
164                         print "Password mismatch.\n\n";
165                 }
166                 print "\n\n\n";
167                 system('stty sane 2>/dev/null');
168
169                 if (IkiWiki::userinfo_setall($admin, { regdate => time }) &&
170                     IkiWiki::Plugin::passwordauth::setpassword($admin, $password)) {
171                         IkiWiki::userinfo_set($admin, "email", $config{adminemail}) if defined $config{adminemail};
172                 }
173                 else {
174                         error("problem setting up $admin user");
175                 }
176         }
177         
178         # Add wrappers, make live.
179         if (system("ikiwiki", "--wrappers", "--setup", $config{dumpsetup}) != 0) {
180                 die "ikiwiki --wrappers --setup $config{dumpsetup} failed";
181         }
182
183         # Add it to the wikilist.
184         mkpath("$ENV{HOME}/.ikiwiki");
185         open (WIKILIST, ">>$ENV{HOME}/.ikiwiki/wikilist") || die "$ENV{HOME}/.ikiwiki/wikilist: $!";
186         print WIKILIST "$ENV{USER} $config{dumpsetup}\n";
187         close WIKILIST;
188         if (system("ikiwiki-update-wikilist") != 0) {
189                 print STDERR "** Failed to add you to the system wikilist file.\n";
190                 print STDERR "** (Probably ikiwiki-update-wikilist is not SUID root.)\n";
191                 print STDERR "** Your wiki will not be automatically updated when ikiwiki is upgraded.\n";
192         }
193         
194         # Done!
195         print "\n\nSuccessfully set up $config{wikiname}:\n";
196         foreach my $key (qw{url srcdir destdir repository}) {
197                 next unless exists $config{$key};
198                 print "\t$key: ".(" " x (10 - length($key)))." ".
199                         prettydir($config{$key})."\n";
200         }
201         print "To modify settings, edit ".prettydir($config{dumpsetup})." and then run:\n";
202         print " ikiwiki -setup ".prettydir($config{dumpsetup})."\n";
203         exit 0;
204 }
205
206 1