| 1 | package Ubic::Admin::Setup; | ||
| 2 | $Ubic::Admin::Setup::VERSION = '1.59'; | ||
| 3 | # ABSTRACT: this module handles ubic setup: asks user some questions and configures your system | ||
| 4 | |||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Getopt::Long 2.33; | ||
| 10 | use Carp; | ||
| 11 | use IPC::Open3; | ||
| 12 | use File::Path; | ||
| 13 | use File::Which; | ||
| 14 | use File::Spec; | ||
| 15 | |||
| 16 | use Ubic::AtomicFile; | ||
| 17 | use Ubic::Settings; | ||
| 18 | use Ubic::Settings::ConfigFile; | ||
| 19 | |||
| 20 | my $batch_mode; | ||
| 21 | my $quiet; | ||
| 22 | |||
| 23 | sub _defaults { | ||
| 24 | if ($^O eq 'freebsd') { | ||
| 25 | return ( | ||
| 26 | config => '/usr/local/etc/ubic/ubic.cfg', | ||
| 27 | data_dir => '/var/db/ubic', | ||
| 28 | service_dir => '/usr/local/etc/ubic/service', | ||
| 29 | log_dir => '/var/log/ubic', | ||
| 30 | example => '/usr/local/etc, /var', | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | else { | ||
| 34 | # fhs | ||
| 35 | return ( | ||
| 36 | config => '/etc/ubic/ubic.cfg', | ||
| 37 | data_dir => '/var/lib/ubic', | ||
| 38 | service_dir => '/etc/ubic/service', | ||
| 39 | log_dir => '/var/log/ubic', | ||
| 40 | example => '/etc, /var', | ||
| 41 | ); | ||
| 42 | } | ||
| 43 | }; | ||
| 44 | |||
| 45 | sub print_tty { | ||
| 46 | print @_ unless $quiet or $batch_mode; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub prompt ($;$) { | ||
| 50 | my($mess, $def) = @_; | ||
| 51 | Carp::confess("prompt function called without an argument") | ||
| 52 | unless defined $mess; | ||
| 53 | |||
| 54 | return $def if $batch_mode; | ||
| 55 | |||
| 56 | my $isa_tty = -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT)); | ||
| 57 | Carp::confess("tty not found") if not $isa_tty; | ||
| 58 | |||
| 59 | $def = defined $def ? $def : ""; | ||
| 60 | |||
| 61 | local $| = 1; | ||
| 62 | local $\ = undef; | ||
| 63 | print "$mess "; | ||
| 64 | |||
| 65 | my $ans; | ||
| 66 | if (not $isa_tty and eof STDIN) { | ||
| 67 | print "$def\n"; | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | $ans = <STDIN>; | ||
| 71 | if( defined $ans ) { | ||
| 72 | chomp $ans; | ||
| 73 | } | ||
| 74 | else { # user hit ctrl-D | ||
| 75 | print "\n"; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | return (!defined $ans || $ans eq '') ? $def : $ans; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub prompt_str { | ||
| 83 | my ($description, $default) = @_; | ||
| 84 | return prompt("$description [$default]", $default); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub prompt_bool { | ||
| 88 | my ($description, $default) = @_; | ||
| 89 | my $yn = ($default ? 'y' : 'n'); | ||
| 90 | my $yn_hint = ($default ? 'Y/n' : 'y/N'); | ||
| 91 | my $result = prompt("$description [$yn_hint]", $yn); | ||
| 92 | if ($result =~ /^y/i) { | ||
| 93 | return 1; | ||
| 94 | } | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub xsystem { | ||
| 99 | local $! = local $? = 0; | ||
| 100 | return if system(@_) == 0; | ||
| 101 | |||
| 102 | my @msg; | ||
| 103 | if ($!) { | ||
| 104 | push @msg, "error ".int($!)." '$!'"; | ||
| 105 | } | ||
| 106 | if ($? > 0) { | ||
| 107 | push @msg, "kill by signal ".($? & 127) if ($? & 127); | ||
| 108 | push @msg, "core dumped" if ($? & 128); | ||
| 109 | push @msg, "exit code ".($? >> 8) if $? >> 8; | ||
| 110 | } | ||
| 111 | die join ", ", @msg; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub slurp { | ||
| 115 | my ($file) = @_; | ||
| 116 | open my $fh, '<', $file or die "Can't open $file: $!"; | ||
| 117 | my $result = join '', <$fh>; | ||
| 118 | close $fh; | ||
| 119 | return $result; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub setup { | ||
| 123 | |||
| 124 | my $opt_reconfigure; | ||
| 125 | my $opt_service_dir; | ||
| 126 | my $opt_data_dir; | ||
| 127 | my $opt_log_dir; | ||
| 128 | my $opt_default_user = 'root'; | ||
| 129 | my $opt_sticky_777 = 1; | ||
| 130 | my $opt_install_services = 1; | ||
| 131 | my $opt_crontab = 1; | ||
| 132 | my $opt_local; | ||
| 133 | |||
| 134 | # These options are documented in ubic-admin script POD. | ||
| 135 | # Don't forget to update their description if you change them. | ||
| 136 | GetOptions( | ||
| 137 | 'local!' => \$opt_local, | ||
| 138 | 'batch-mode' => \$batch_mode, | ||
| 139 | 'quiet' => \$quiet, | ||
| 140 | 'reconfigure!' => \$opt_reconfigure, | ||
| 141 | 'service-dir=s' => \$opt_service_dir, | ||
| 142 | 'data-dir=s' => \$opt_data_dir, | ||
| 143 | 'log-dir=s' => \$opt_log_dir, | ||
| 144 | 'default-user=s' => \$opt_default_user, | ||
| 145 | 'sticky-777!' => \$opt_sticky_777, | ||
| 146 | 'install-services!' => \$opt_install_services, | ||
| 147 | 'crontab!' => \$opt_crontab, | ||
| 148 | ) or die "Getopt failed"; | ||
| 149 | |||
| 150 | die "Unexpected arguments '@ARGV'" if @ARGV; | ||
| 151 | |||
| 152 | my %defaults = _defaults(); | ||
| 153 | |||
| 154 | eval { Ubic::Settings->check_settings }; | ||
| 155 | unless ($@) { | ||
| 156 | my $go = prompt_bool("Looks like ubic is already configured, do you want to reconfigure?", $opt_reconfigure); | ||
| 157 | return unless $go; | ||
| 158 | print_tty "\n"; | ||
| 159 | } | ||
| 160 | |||
| 161 | print_tty "Ubic can be installed either in your home dir or into standard system paths ($defaults{example}).\n"; | ||
| 162 | print_tty "You need to be root to install it into system.\n"; | ||
| 163 | |||
| 164 | unless ($batch_mode) { | ||
| 165 | $batch_mode = prompt_bool("Would you like to configure as much as possible automatically?", 1); | ||
| 166 | } | ||
| 167 | |||
| 168 | # ideally, we want is_root option and local option to be orthogonal | ||
| 169 | # it's not completely true by now, though | ||
| 170 | my $is_root = ( $> ? 0 : 1 ); | ||
| 171 | my $local = $opt_local; | ||
| 172 | unless (defined $local) { | ||
| 173 | if ($is_root) { | ||
| 174 | my $ok = prompt_bool("You are root, install into system?", 1); | ||
| 175 | $local = 1 unless $ok; # root can install locally | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | my $ok = prompt_bool("You are not root, install locally?", 1); | ||
| 179 | return unless $ok; # non-root user can't install into system | ||
| 180 | $local = 1; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | my $local_dir; | ||
| 185 | if ($local) { | ||
| 186 | $local_dir = $ENV{HOME}; | ||
| 187 | unless (defined $local_dir) { | ||
| 188 | die "Can't find your home!"; | ||
| 189 | } | ||
| 190 | unless (-d $local_dir) { | ||
| 191 | die "Can't find your home dir '$local_dir'!"; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | print_tty "\nService dir is a directory with descriptions of your services.\n"; | ||
| 196 | my $default_service_dir = ( | ||
| 197 | defined($local_dir) | ||
| 198 | ? "$local_dir/ubic/service" | ||
| 199 | : $defaults{service_dir} | ||
| 200 | ); | ||
| 201 | $default_service_dir = $opt_service_dir if defined $opt_service_dir; | ||
| 202 | my $service_dir = prompt_str("Service dir?", $default_service_dir); | ||
| 203 | |||
| 204 | print_tty "\nData dir is where ubic stores all of its data: locks,\n"; | ||
| 205 | print_tty "status files, tmp files.\n"; | ||
| 206 | my $default_data_dir = ( | ||
| 207 | defined($local_dir) | ||
| 208 | ? "$local_dir/ubic/data" | ||
| 209 | : $defaults{data_dir} | ||
| 210 | ); | ||
| 211 | $default_data_dir = $opt_data_dir if defined $opt_data_dir; | ||
| 212 | my $data_dir = prompt_str("Data dir?", $default_data_dir); | ||
| 213 | |||
| 214 | print_tty "\nLog dir is where ubic.watchdog will write its logs.\n"; | ||
| 215 | print_tty "(Your own services are free to write logs wherever they want.)\n"; | ||
| 216 | my $default_log_dir = ( | ||
| 217 | defined($local_dir) | ||
| 218 | ? "$local_dir/ubic/log" | ||
| 219 | : $defaults{log_dir} | ||
| 220 | ); | ||
| 221 | $default_log_dir = $opt_log_dir if defined $opt_log_dir; | ||
| 222 | my $log_dir = prompt_str("Log dir?", $default_log_dir); | ||
| 223 | |||
| 224 | # TODO - sanity checks? | ||
| 225 | |||
| 226 | my $default_user; | ||
| 227 | if ($is_root) { | ||
| 228 | print_tty "\nUbic services can be started from any user.\n"; | ||
| 229 | print_tty "Some services don't specify the user from which they must be started.\n"; | ||
| 230 | print_tty "Default user will be used in this case.\n"; | ||
| 231 | $default_user = prompt_str("Default user?", $opt_default_user); | ||
| 232 | } | ||
| 233 | else { | ||
| 234 | print_tty "\n"; | ||
| 235 | $default_user = getpwuid($>); | ||
| 236 | unless (defined $default_user) { | ||
| 237 | die "Can't get login (uid '$>')"; | ||
| 238 | } | ||
| 239 | print_tty "You're using local installation, so default service user will be set to '$default_user'.\n"; | ||
| 240 | } | ||
| 241 | |||
| 242 | my $enable_1777; | ||
| 243 | if ($is_root) { | ||
| 244 | print_tty "\nSystem-wide installations usually need to store service-related data\n"; | ||
| 245 | print_tty "into data dir for different users. For non-root services to work\n"; | ||
| 246 | print_tty "1777 grants for some data dir subdirectories is required.\n"; | ||
| 247 | print_tty "(1777 grants means that everyone is able to write to the dir,\n"; | ||
| 248 | print_tty "but only file owners are able to modify and remove their files.)\n"; | ||
| 249 | print_tty "There are no known security issues with this approach, but you have\n"; | ||
| 250 | print_tty "to decide for yourself if that's ok for you.\n"; | ||
| 251 | |||
| 252 | $enable_1777 = prompt_bool("Enable 1777 grants for data dir?", $opt_sticky_777); | ||
| 253 | } | ||
| 254 | |||
| 255 | my $install_services; | ||
| 256 | { | ||
| 257 | print_tty "There are three standard services in ubic service tree:\n"; | ||
| 258 | print_tty " - ubic.watchdog (universal watchdog)\n"; | ||
| 259 | print_tty " - ubic.ping (http service status reporter)\n"; | ||
| 260 | print_tty " - ubic.update (helper process which updates service portmap, used by ubic.ping service)\n"; | ||
| 261 | print_tty "If you'll choose to install them, ubic.watchdog will be started automatically\n"; | ||
| 262 | print_tty "and two other services will be initially disabled.\n"; | ||
| 263 | $install_services = prompt_bool("Do you want to install standard services?", $opt_install_services); | ||
| 264 | } | ||
| 265 | |||
| 266 | my $enable_crontab; | ||
| 267 | { | ||
| 268 | print_tty "\n'ubic.watchdog' is a service which checks all services and restarts them if\n"; | ||
| 269 | print_tty "there are any problems with their statuses.\n"; | ||
| 270 | print_tty "It is very simple and robust, but since it's important that watchdog never\n"; | ||
| 271 | print_tty "goes down, we recommended to install the cron job which checks watchdog itself.\n"; | ||
| 272 | print_tty "Also, this cron job will bring watchdog and all other services online on host reboots.\n"; | ||
| 273 | $enable_crontab = prompt_bool("Install watchdog's watchdog as a cron job?", $opt_crontab); | ||
| 274 | } | ||
| 275 | |||
| 276 | my $crontab_env_fix = ''; | ||
| 277 | my $crontab_wrap_bash; | ||
| 278 | my $ubic_watchdog_full_name = which('ubic-watchdog') or die "ubic-watchdog script not found in your current PATH"; | ||
| 279 | { | ||
| 280 | my @path = split /:/, $ENV{PATH}; | ||
| 281 | my @perls = grep { -x $_ } map { "$_/perl" } @path; | ||
| 282 | if ($perls[0] =~ /perlbrew/) { | ||
| 283 | print_tty "\nYou're using perlbrew.\n"; | ||
| 284 | |||
| 285 | my $HOME = $ENV{ORIGINAL_HOME} || $ENV{HOME}; # ORIGINAL_HOME is set in ubic tests | ||
| 286 | unless ($HOME) { | ||
| 287 | die "HOME env variable not defined"; | ||
| 288 | } | ||
| 289 | my $perlbrew_config = File::Spec->catfile($ENV{PERLBREW_ROOT} || "$HOME/perl5/perlbrew", "etc/bashrc"); | ||
| 290 | if (not -e $perlbrew_config) { | ||
| 291 | die "Can't find perlbrew config (assumed $perlbrew_config)"; | ||
| 292 | } | ||
| 293 | print_tty "I'll source your perlbrew config in ubic crontab entry to start the watchdog in the correct environment.\n"; | ||
| 294 | $crontab_env_fix .= "source $perlbrew_config && "; | ||
| 295 | $crontab_wrap_bash = 1; | ||
| 296 | } | ||
| 297 | elsif (@perls > 1) { | ||
| 298 | print_tty "\nYou're using custom perl and it's not from perlbrew.\n"; | ||
| 299 | print_tty "I'll add your current PATH to ubic crontab entry.\n"; | ||
| 300 | |||
| 301 | # TODO - what if PATH contains " quotes? hopefully nobody is that crazy... | ||
| 302 | $crontab_env_fix .= qq[PATH="$ENV{PATH}" ]; | ||
| 303 | } | ||
| 304 | |||
| 305 | if ($ENV{PERL5LIB}) { | ||
| 306 | print_tty "\nYou're using custom PERL5LIB.\n"; | ||
| 307 | print_tty "I'll add your current PERL5LIB to ubic crontab entry.\n"; | ||
| 308 | print_tty "Feel free to edit your crontab manually after installation if necessary.\n"; | ||
| 309 | $crontab_env_fix .= qq[PERL5LIB="$ENV{PERL5LIB}" ]; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | my $config_file = ( | ||
| 314 | defined($local_dir) | ||
| 315 | ? "$local_dir/.ubic.cfg" | ||
| 316 | : $defaults{config} | ||
| 317 | ); | ||
| 318 | |||
| 319 | { | ||
| 320 | print_tty "\nThat's all I need to know.\n"; | ||
| 321 | print_tty "If you proceed, all necessary directories will be created,\n"; | ||
| 322 | print_tty "and configuration file will be stored into $config_file.\n"; | ||
| 323 | my $run = prompt_bool("Complete setup?", 1); | ||
| 324 | return unless $run; | ||
| 325 | } | ||
| 326 | |||
| 327 | print "Installing dirs...\n"; | ||
| 328 | |||
| 329 | mkpath($_) for ($service_dir, $data_dir, $log_dir); | ||
| 330 | |||
| 331 | for my $subdir (qw[ | ||
| 332 | status simple-daemon/pid lock ubic-daemon tmp watchdog/lock watchdog/status | ||
| 333 | ]) { | ||
| 334 | mkpath("$data_dir/$subdir"); | ||
| 335 | chmod(01777, "$data_dir/$subdir") or die "chmod failed: $!"; | ||
| 336 | } | ||
| 337 | |||
| 338 | mkpath("$service_dir/ubic"); | ||
| 339 | |||
| 340 | if ($install_services) { | ||
| 341 | my $add_service = sub { | ||
| 342 | my ($name, $content) = @_; | ||
| 343 | print "Installing ubic.$name service...\n"; | ||
| 344 | |||
| 345 | my $file = "$service_dir/ubic/$name"; | ||
| 346 | Ubic::AtomicFile::store($content => $file); | ||
| 347 | }; | ||
| 348 | |||
| 349 | $add_service->( | ||
| 350 | 'ping', | ||
| 351 | "use Ubic::Ping::Service;\n" | ||
| 352 | ."Ubic::Ping::Service->new;\n" | ||
| 353 | ); | ||
| 354 | |||
| 355 | $add_service->( | ||
| 356 | 'watchdog', | ||
| 357 | "use Ubic::Service::SimpleDaemon;\n" | ||
| 358 | ."Ubic::Service::SimpleDaemon->new(\n" | ||
| 359 | ."bin => [ 'ubic-periodic', '--rotate-logs', '--period=60', '--stdout=$log_dir/watchdog.log', '--stderr=$log_dir/watchdog.err.log', 'ubic-watchdog' ],\n" | ||
| 360 | .");\n" | ||
| 361 | ); | ||
| 362 | |||
| 363 | $add_service->( | ||
| 364 | 'update', | ||
| 365 | "use Ubic::Service::SimpleDaemon;\n" | ||
| 366 | ."Ubic::Service::SimpleDaemon->new(\n" | ||
| 367 | ."bin => [ 'ubic-periodic', '--rotate-logs', '--period=60', '--stdout=$log_dir/update.log', '--stderr=$log_dir/update.err.log', 'ubic-update' ],\n" | ||
| 368 | .");\n" | ||
| 369 | ); | ||
| 370 | } | ||
| 371 | |||
| 372 | if ($enable_crontab) { | ||
| 373 | print "Installing cron jobs...\n"; | ||
| 374 | |||
| 375 | system("crontab -l >$data_dir/tmp/crontab.stdout 2>$data_dir/tmp/crontab.stderr"); | ||
| 376 | my $old_crontab = slurp("$data_dir/tmp/crontab.stdout"); | ||
| 377 | my $stderr = slurp("$data_dir/tmp/crontab.stderr"); | ||
| 378 | unlink "$data_dir/tmp/crontab.stdout"; | ||
| 379 | unlink "$data_dir/tmp/crontab.stderr"; | ||
| 380 | if ($stderr and $stderr !~ /no crontab/) { | ||
| 381 | die "crontab -l failed"; | ||
| 382 | } | ||
| 383 | |||
| 384 | my @crontab_lines = split /\n/, $old_crontab; | ||
| 385 | @crontab_lines = grep { $_ !~ /\Qubic-watchdog ubic.watchdog\E/ } @crontab_lines; | ||
| 386 | |||
| 387 | open my $fh, '|-', 'crontab -' or die "Can't run 'crontab -': $!"; | ||
| 388 | my $printc = sub { | ||
| 389 | print {$fh} @_ or die "Can't write to pipe: $!"; | ||
| 390 | }; | ||
| 391 | |||
| 392 | my $crontab_command = "$crontab_env_fix$ubic_watchdog_full_name ubic.watchdog"; | ||
| 393 | if ($crontab_wrap_bash) { | ||
| 394 | $crontab_command = "bash -c '$crontab_command'"; | ||
| 395 | } | ||
| 396 | push @crontab_lines, "* * * * * $crontab_command >>$log_dir/watchdog.log 2>>$log_dir/watchdog.err.log"; | ||
| 397 | $printc->("$_\n") for @crontab_lines; | ||
| 398 | close $fh or die "Can't close pipe: $!"; | ||
| 399 | } | ||
| 400 | |||
| 401 | print "Installing $config_file...\n"; | ||
| 402 | Ubic::Settings::ConfigFile->write($config_file, { | ||
| 403 | service_dir => $service_dir, | ||
| 404 | data_dir => $data_dir, | ||
| 405 | default_user => $default_user, | ||
| 406 | }); | ||
| 407 | |||
| 408 | if ($install_services) { | ||
| 409 | print "Starting ubic.watchdog...\n"; | ||
| 410 | xsystem('ubic start ubic.watchdog'); | ||
| 411 | } | ||
| 412 | |||
| 413 | print "Installation complete.\n"; | ||
| 414 | } | ||
| 415 | |||
| 416 | |||
| 417 | 1; | ||
| 418 | |||
| 419 | __END__ |