-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall-sm.pl
executable file
·203 lines (193 loc) · 7.68 KB
/
install-sm.pl
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/perl
# IFMI SeedManager installer.
# This file is part of IFMI SeedManager.
#
# SeedManager 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
# GNU General Public License for more details.
use strict;
use warnings;
use File::Path qw(make_path);
use File::Copy;
my $login = (getpwuid $>);
die "Please run as root (do not use sudo)" if ($login ne 'root');
die "please execute from the install directory.\n" if (!-f "./install-sm.pl") ;
if ((defined $ARGV[0]) && ($ARGV[0] eq "-q")) {
my $flag = $ARGV[0];
&doInstall($flag);
} else {
print "This will install the IFMI SeedManager for cgminer and clones on Linux.\n";
print "Are you sure? (y/n) ";
my $ireply = <>; chomp $ireply;
if ($ireply =~ m/y(es)?/i) {
if (-d "/var/www/IFMI/") {
print "It looks like this has been installed before. Do over? (y/n) ";
my $oreply = <>; chomp $oreply;
if ($oreply =~ m/y(es)?/i) {
&doInstall;
} else {
die "Installation exited!\n";
}
} else {
&doInstall;
}
} else {
die "Installation exited!\n";
}
}
sub doInstall {
my $flag = "x";;
$flag = $_[0] if (defined $_[0]);
use POSIX qw(strftime);
my $now = POSIX::strftime("%Y-%m-%d.%H.%M", localtime());
my $instlog = "SeedManager Install Log.\n$now\n";
print "Perl module check \n" if ($flag ne "-q");
require RRDs;
require YAML;
require JSON;
require Email::Simple;
require Email::Sender;
require Try::Tiny;
print " ..all set!\n" if ($flag ne "-q");
$instlog .= "Perl test passed.";
# The following three values may need adjusting on systems that are not Debian or RedHat based.
my $webdir = "/var/www";
my $cgidir = "/usr/lib/cgi-bin";
my $apacheuser = "unknown";
my $appdir = "/opt/ifmi";
$apacheuser = "apache" if (-f "/etc/redhat-release");
$apacheuser = "www-data" if (-f "/etc/debian_version");
if ($apacheuser ne "unknown") {
if (-d $webdir && -d $cgidir) {
print "Copying files...\n" if ($flag ne "-q");
#perl chown requires UID and make_path is broken, so
make_path $appdir . '/rrdtool';
`chown $apacheuser $appdir`;
make_path $webdir . '/IFMI/graphs' ;
mkdir $webdir . '/IFMI/themes';
if (!-f $webdir . "/index.html.pre-ifmi") {
copy $webdir . "/index.html", $webdir . "/index.html.pre-ifmi" if (-f $webdir . "/index.html");
}
copy "index.html", $webdir;
copy "seedstatus.pl", $cgidir;
copy "sconfig.pl", $cgidir;
copy "confedit.pl", $cgidir;
copy "favicon.ico", $webdir;
copy "smcontrol", $appdir;
copy "sm-common.pl", $appdir;
copy "smgraph.pl", $appdir;
copy "smnotify.pl", $appdir;
copy "ssendstatus.pl", $appdir;
copy "run-seedmanager.pl", $appdir;
`cp themes/* $webdir/IFMI/themes`;
`cp images/*.png $webdir/IFMI`;
`chmod 0755 $appdir/*.pl`; #because windows f's up the permissions. wtf.
`chmod 0755 $appdir/smcontrol`; #because windows
`chmod 0755 $cgidir/*.pl`; #because windows
`chown $apacheuser $appdir/seedmanager.conf` if (-f "$appdir/seedmanager.conf");
$instlog .= "files copied.\n";
} else {
die "Your web directories are in unexpected places. Quitting.\n";
}
copy "/etc/crontab", "/etc/crontab.pre-ifmi" if (!-f "/etc/crontab.pre-ifmi");
if (! `grep -E ^"\* \* \* \* \* root /opt/ifmi/run-seedmanager.pl" /etc/crontab`) {
print "Setting up crontab...\n" if ($flag ne "-q");
open my $cin, '>>', "/etc/crontab";
print $cin "* * * * * root /opt/ifmi/run-seedmanager.pl\n\n";
close $cin;
$instlog .= "crontab modified.\n";
}
#copy "/etc/sudoers.pre-ifmi", "/etc/sudoers" if (-f "/etc/sudoers.pre-ifmi");
copy "/etc/sudoers", "/etc/sudoers.pre-ifmi" if (!-f "/etc/sudoers.pre-ifmi");
if (! `grep -E /opt/ifmi/smcontrol /etc/sudoers`) {
print "Modifying sudoers....\n" if ($flag ne "-q");
my $fin = "/etc/sudoers";
if (! `grep -E /opt/ifmi/mcontrol /etc/sudoers`) {
open my $sin, '>>', $fin;
print $sin "$apacheuser ALL=(root)NOPASSWD: /opt/ifmi/smcontrol,/usr/bin/htpasswd\nDefaults:$apacheuser rootpw\n$apacheuser ALL=(root) /bin/cp\n";
close $sin;
} else {
open my $sin, '<', $fin;
open my $sout, '>', "$fin.out";
while (<$sin>) {
s/\/opt\/ifmi\/mcontrol/\/opt\/ifmi\/mcontrol,\/opt\/ifmi\/smcontrol/;
print $sout $_;
}
close $sin; close $sout;
rename "$fin.out", $fin;
}
`chmod 0440 /etc/sudoers`;
$instlog .= "sudoers modified.\n";
}
print "SeedManager attempts to set up some basic security for your web service.\n" if ($flag ne "-q");
print "It will enable SSL and redirect all web traffic over https.\n" if ($flag ne "-q");
if (!-f "/etc/ssl/certs/apache.crt") {
print "First, we need to create a self-signed cert to enable SSL.\n" if ($flag ne "-q");
print "The next set of questions is information for this cert.\n" if ($flag ne "-q");
print "Please set the country code, and the rest of the cert quetions can be left blank.\n";
print "Press any key to continue: ";
my $creply = <STDIN>;
if ($creply =~ m/.*/) {
`/usr/bin/openssl req -x509 -nodes -days 1825 -newkey rsa:2048 -keyout /etc/ssl/private/apache.key -out /etc/ssl/certs/apache.crt`;
print "...finished creating cert.\n";
$instlog .= "cert created.\n";
}
} else {
print "...cert appears to be installed, skipping...\n" if ($flag ne "-q");
}
my $restart = 0;
copy "/etc/apache2/sites-available/default-ssl", "/etc/apache2/sites-available/default-ssl.pre-ifmi"
if (!-f "/etc/apache2/sites-available/default-ssl.pre-ifmi");
if (`grep ssl-cert-snakeoil.pem /etc/apache2/sites-available/default-ssl`) {
`sed -i "s/ssl-cert-snakeoil.pem/apache.crt/g" /etc/apache2/sites-available/default-ssl`;
`sed -i "s/ssl-cert-snakeoil.key/apache.key/g" /etc/apache2/sites-available/default-ssl`;
$instlog .= "cert installed.\n";
`/usr/sbin/a2ensite default-ssl`;
`/usr/sbin/a2enmod ssl`;
$restart++;
}
if (! `grep ServerName /etc/apache2/sites-available/default-ssl`) {
open my $din, '<', "/etc/apache2/sites-available/default-ssl";
open my $dout, '>', "/etc/apache2/sites-available/default-ssl.out";
while (<$din>) {
print $dout $_;
last if /ServerAdmin /;
}
print $dout "\n ServerName IFMI:443\n";
while (<$din>) {
print $dout $_;
}
close $dout;
move "/etc/apache2/sites-available/default-ssl.out", "/etc/apache2/sites-available/default-ssl";
}
if (! `grep RewriteEngine /etc/apache2/sites-available/default`) {
copy "/etc/apache2/sites-available/default", "/etc/apache2/sites-available/default.pre-ifmi"
if (!-f "/etc/apache2/sites-available/default.pre-ifmi");
open my $din, '<', "/etc/apache2/sites-available/default";
open my $dout, '>', "/etc/apache2/sites-available/default.out";
while (<$din>) {
print $dout $_;
last if /ServerAdmin /;
}
print $dout "\n RewriteEngine On\n RewriteCond %{HTTPS} !=on\n";
print $dout " RewriteRule ^/?(.*) https://%{SERVER_NAME}/\$1 [R,L]\n";
while (<$din>) {
print $dout $_;
}
close $dout;
move "/etc/apache2/sites-available/default.out", "/etc/apache2/sites-available/default";
$instlog .= "rewrite enabled.\n";
`/usr/sbin/a2enmod rewrite`;
$restart++;
}
`service apache2 restart` if ($restart > 0);
print "Please read the README and edit your miner conf file as required.\nDone! Thank you for flying IFMI!\n" if ($flag ne "-q");
} else {
print "Cant determine apache user, Bailing out!\n";
$instlog .= "unknown apache user, bailed out.\n";
}
open my $lin, '>', "SM-install-log.$now";
print $lin $instlog;
close $lin;
}