-
Notifications
You must be signed in to change notification settings - Fork 8
/
amahi-installer
executable file
·172 lines (147 loc) · 4.26 KB
/
amahi-installer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/perl -w
#
# Amahi Home Server
# Copyright (C) 2007-2009 Amahi Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License v3
# (29 June 2007), as published in the COPYING file.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# file COPYING for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Amahi
# team at http://www.amahi.org/ under "Contact Us."
# web based installer front end
use strict;
use Getopt::Std;
use POSIX qw(strftime);
our($opt_f, $opt_h);
getopts('fh');
my $LOG = "/tmp/amahi-ruby-install.log";
my $ICF = "/var/hda/install-code.txt";
my $platform = "EE";
# FIXME support ubuntu too
sub get_platform {
open (my $file, "< /etc/issue") or return "EE";
my $p = <$file>;
chomp $p;
if ($p =~ /Fedora/) {
return "fedora"
}
if ($p =~ /Ubuntu|Debian/) {
return "debian"
}
return "EE";
}
# sleep and exit, so that it's not restarted too quickly
sub sleep_and_exit {
my $code = shift;
sleep(2);
exit($code);
}
sub daemonize {
my $pid = fork;
if ($pid < 0) {
print STDERR "ERROR: can not fork/detach ($!)\n";
&sleep_and_exit(-1);
} elsif ($pid) {
# parent simply exit
exit 0;
}
# chil continues execution
$SIG{'CHLD'} = 'DEFAULT';
open(STDOUT, ">$LOG");
open(STDERR, ">$LOG");
open(STDIN, "</dev/null");
}
sub do_system_multiple () {
my $args = shift;
# attempt the install at most this times
my $times = 10;
my $res = 0;
my $i = 0;
foreach (1..$times) {
last unless ($res = system ($args . " &> $LOG"));
}
return $res;
}
sub do_basic_checks {
$ENV{PATH} .= ":/sbin:/usr/sbin:/bin";
my $ruid = $<;
my $euid = $>;
$platform = &get_platform();
if ($opt_h) {
printf "usage: amahi-installer [-f|-h]\n";
printf "\t -h - help\n";
printf "\t -f - force\n";
&sleep_and_exit(1);
}
if ($euid != 0) {
printf "ERROR: this program must be run as \"root\". type \"su\" (to Switch User to root),\n";
printf " then input your root password, then run this program again.\n\n";
printf " \$ su\n";
printf " Password: (enter your root password)\n";
&sleep_and_exit(1);
}
if (-e "/etc/sysconfig/amahi-hda") {
if ($opt_f) {
printf "WARNING: this system is already intialized for Amahi use\n";
} else {
# exit silently
printf "the system already has Amahi installed.\n";
&sleep_and_exit(0);
}
}
}
sub package_installed () {
my $pkg = shift;
return (system("rpm -q $pkg &> /dev/null") == 0);
}
sub gem_installed () {
my $gem = shift;
my $ret = system("gem list ^$gem\$ | grep -q $gem &> /dev/null");
return ($ret ? 0 : 1);
}
sub install_rpm_packages() {
&do_system_multiple("yum -y install ruby") unless &package_installed("ruby");
&do_system_multiple("yum -y install ruby-mysql") unless &package_installed("ruby-mysql");
&do_system_multiple("yum -y install rubygem-rack") unless &package_installed("rubygem-rack");
&do_system_multiple("yum -y install rubygem-sinatra") unless &package_installed("rubygem-sinatra");
}
sub install_packages() {
if ($platform eq "fedora") {
&install_rpm_packages();
}
}
&do_basic_checks();
if (-e "$ICF") {
# delayed express install
printf "starting the express installer.\n";
open(my $aicf, "< $ICF") || die ("cannot open the install code $ICF: $!");
my $aic = <$aicf>;
chomp($aic);
my $logfile = strftime("/root/hda-install-express-%y%m%d-%H%M%S.log", localtime);
# run the installer!
my $result = system("hda-install $aic &> $logfile");
if ($result == 0) {
printf "Amahi express installer finished successfully. Rebooting.\n";
system("reboot");
} else {
printf "WARNING: express installer failed to start!\n";
}
}
printf "starting the web-based installer at:\n";
printf "\thttp://localhost:2000\n\n";
printf "For help:\n";
printf "\t- visit http://talk.amahi.org for live community help\n";
# if the above fails, still run the web installer
&install_packages();
&daemonize;
exec("/usr/share/hda-ctl/web-installer/install-server &> $LOG");
# sleep, in case it fails
warn "FATAL: web installer failed to start!\n";
&sleep_and_exit(-1);