-
Notifications
You must be signed in to change notification settings - Fork 8
/
hda-install
executable file
·1366 lines (1149 loc) · 36.7 KB
/
hda-install
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
#
# Amahi Home Server
# Copyright (C) 2007-2013 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."
# final install and propagation of the hda settings
require 'optparse'
require 'uri'
require 'open-uri'
require 'net/http'
require 'mysql2'
require 'tempfile'
require 'dbus'
require 'ipaddr'
$version = "11.1.0"
# default network device
@network_device = "eth0"
# FIXME - make this more flexible - or read it out
DATABASE_NAME = "hda_production"
DATABASE_USER = "amahihda"
DATABASE_PASSWORD = "AmahiHDARulez"
DATABASE_MAIN_USER = "root"
DATABASE_MAIN_PASSWORD = "hda"
GREYHOLE_DATABASE = "greyhole"
API_URL_BASE = "https://api.amahi.org/api2"
# upstart conf file for an ubuntu system
UPSTART_CONF = "/etc/init/%s.conf"
# legacy fallback (needs trailing slash)
LEGACY_INIT_PATH = "/etc/init.d/"
OLDOUT = $stdout.dup
@opt = ARGV.getopts("VfusdrDhnwmk:oyqi:p")
def set_platform
issue = nil
if File.exists?('/etc/amahi-release')
file = nil
issue = File.open("/etc/amahi-release", "r")
elsif File.exists?('/etc/system-release')
file = nil
issue = File.open("/etc/system-release", "r")
elsif File.exists?('/etc/issue')
issue = File.open("/etc/issue", "r")
else
raise "Ohhh. System not known. Sorry."
end
line = issue.gets;
$platform = "unknown";
if (line.include? "Ubuntu")
$platform = "ubuntu";
end
if (line.include? "Debian")
$platform = "debian";
end
if (line.include? "Fedora")
$platform = "fedora";
end
if (line.include? "CentOS")
$platform = "centos";
end
end
set_platform()
def fedora?
$platform == "fedora";
end
def centos?
$platform == "centos";
end
def ubuntu?
$platform == "ubuntu";
end
def debian?
$platform == "debian";
end
def rpm_based?
fedora? or centos?
end
def deb_based?
ubuntu? or debian?
end
# platform specific constants
if fedora? or centos?
DHCPD_SERVERNAME = 'dhcpd'
HTTPD_SERVERNAME = 'httpd'
HTTPD_ENVFILE = '/etc/sysconfig/httpd'
MYSQL_SERVERNAME = 'mariadb'
NAMED_SERVERNAME = 'named'
NETWORK_SERVERNAME = 'network'
NETWORKMANAGER_SERVERNAME = 'NetworkManager'
NMB_SERVERNAME = 'nmb'
SMB_SERVERNAME = 'smb'
else
DHCPD_SERVERNAME = 'isc-dhcp-server'
HTTPD_SERVERNAME = 'apache2'
HTTPD_ENVFILE = '/etc/apache2/envvars'
MYSQL_SERVERNAME = 'mysql'
NAMED_SERVERNAME = 'bind9'
NETWORK_SERVERNAME = 'networking'
NETWORKMANAGER_SERVERNAME = 'network-manager'
# FIXME: change needed for debian: their service samba starts both smb and nmb
NMB_SERVERNAME = 'nmbd'
SMB_SERVERNAME = 'smbd'
end
# need to define it before we re-assign signals
def signal_handler
caught_signal = 1
Signal.trap "HUP", "IGNORE"
Signal.trap "TERM", "IGNORE"
Signal.trap "INT", "IGNORE"
pid = $$
Process.kill "INT", -pid
Process.kill "TERM", -pid
sleep 1
Process.kill "KILL", -pid
exit
end
def autodetect_network
cmds = ["/sbin/ip", "/bin/ip", "/usr/bin/ip", "/usr/sbin/ip"]
cmd = cmds.select { |f| File.exist? f }.first
logfile = File.new "/root/ip-route.txt", "w"
IO.popen("#{cmd} route") do |f|
until f.eof?
line = f.gets
logfile.write line
if line =~ /^default via ([\d\.]+) dev (\w+)/
gatewayip = $1
device = $2
octets = gatewayip.split(/\./)
net = octets[0] + "." + octets[1] + "." + octets[2]
gw = octets[3]
logfile.close
return [net, gw, device]
end
end
end
logfile.close
[nil, nil, nil]
end
# Write the network information to the database
def autoconfigure_network
net, gw, device = autodetect_network
api_key = @settings['api-key']
if net and gw
# set the device here from the existing setup!
@network_device = device
if net != @settings['net'] || gw != @settings['gateway']
sth = @database.query("UPDATE #{DATABASE_NAME}.settings SET value = '#{net}' WHERE settings.name = 'net' LIMIT 1 ;")
sth = @database.query("UPDATE #{DATABASE_NAME}.settings SET value = '#{gw}' WHERE settings.name = 'gateway' LIMIT 1 ;")
# load db settings
get_db_settings
end
puts "Installer automatically determined that your router/gateway IP address is: #{net}.#{gw}, over #{device}"
puts "If this is incorrect, please report it, then run hda-change-gw after"
puts "installation is finished to set your gateway settings to the proper IP."
postData = Net::HTTP.post_form(URI.parse(API_URL_BASE+'/router_update'), {'api_key' => api_key , 'net'=> net, 'gw' => gw })
puts "Post DATA:- "
puts postData
else
puts "WARNING: cannot autoconfigure the network!"
postData = Net::HTTP.post_form(URI.parse(API_URL_BASE+'/router_update'), {'api_key' => api_key , 'net'=> net, 'gw' => gw })
puts "Post DATA:- "
puts postData
end
end
# global database handle for both functions below
# FIXME - install the mysql gem first, then require it??
@database = nil
@settings = {}
@network_device = @opt['k'] || @network_device
$net = nil
$gateway = nil
$gatewayip = nil
# remote db content
@system_configuration = nil;
@system_configuration_length = 0
$caught_signal = 0
Signal.trap("HUP", proc {signal_handler})
Signal.trap("TERM", proc {signal_handler})
Signal.trap("INT", proc {signal_handler})
# FIXME - why does this cause system to always return -1??
# Signal.trap("CHLD", "IGNORE")
def db_connect
# connect to the database
begin
# args: hostname, username, password, database
@database = Mysql2::Client.new(host: "localhost", username: DATABASE_USER, password: DATABASE_PASSWORD, database: DATABASE_NAME)
rescue Mysql2::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
exit(-1)
end
end
def get_db_settings
@database.query("SELECT name, value FROM settings").each do |row|
name = row['name']
value = row['value']
@settings[name] = value
puts "SETTINGS: name = #{name}, value = #{value}" if @opt["D"]
end
end
def clone_perm(i, o)
p = File.stat(i).mode & 07777
$stdout.printf "clone permissions #{i}/%03o -> #{o}", p if @opt["D"]
File.chmod p, o
end
def initialize_mysql
message "Initializing MariaDB"
do_system_silent "rm /etc/my.cnf.d/cracklib_password_check.cnf"
# configure mysql daemon - fail safe
do_system_silent "echo \"UNINSTALL PLUGIN cracklib_password_check;\" | mysql -u #{DATABASE_MAIN_USER}"
do_system_silent "echo \"GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '#{DATABASE_MAIN_PASSWORD}' WITH GRANT OPTION;\" | mysql -u #{DATABASE_MAIN_USER}"
message "Setting up Amahi Dashboard DB user"
# drop the DB first, force and silent, since most times it will not be there
do_system_silent "echo \"drop database if exists #{DATABASE_NAME}; create database #{DATABASE_NAME}; GRANT ALL PRIVILEGES ON *.* TO '#{DATABASE_USER}'@'localhost' IDENTIFIED BY '#{DATABASE_PASSWORD}' WITH GRANT OPTION;\" | mysql -u #{DATABASE_MAIN_USER} -p#{DATABASE_MAIN_PASSWORD}"
end
def substitute_dir(dir_in, dir_out)
if !File.exists?(dir_out)
begin
FileUtils.mkdir dir_out
rescue
abort "cannot create '#{dir_out}': #{$!}"
end
end
abort "both '#{dir_in}' and '#{dir_out}' must be directories" unless File.directory?(dir_in) && File.directory?(dir_out)
# make sure we can read/write the destinaiton directory.
# FIXME: security hole until the end of this function!
# FIXME: there is probably a huge danger hidden here that stuff may
# accidentally/unintentionally get destroyed.
File.chmod 0777, dir_out if File.writable?(dir_out)
begin
files = Dir.entries(dir_in)
files.each do |file|
# skip dot and dot dot, to avoid infinite recursion
next if (file =~ /^\.$/ || file =~ /^\.\.$/)
fi = dir_in + "/" + file
fo = dir_out + "/" + file
substitute_dir fi, fo if File.directory?(fi)
substitute fi, fo if File.file?(fi)
end
rescue
abort "cannot open #{dir_in}: #{$!}"
end
clone_perm dir_in, dir_out
end
# create a common user/db/password for the greyhole app
def initialize_greyhole
message "*************** TBD: Initialize storage pooling ... *******************"
return
message "Initializing Storage Pooling ..."
db = open "| mysql --user #{DATABASE_MAIN_USER} -p#{DATABASE_MAIN_PASSWORD} 2>&1", "r+"
dbname = user = user_password = GREYHOLE_DATABASE
host = "localhost"
if db
db.puts "drop database if exists `#{dbname}`;"
db.puts "create database `#{dbname}`;"
db.puts "create user '#{user}'@'#{host}' IDENTIFIED BY '#{user_password}';"
db.puts "grant all privileges on `#{dbname}`.* to '#{user}'@'#{host}';"
db.puts "use greyhole;"
open("/usr/share/greyhole/schema-mysql.sql") do |f|
until (f.eof) do
db.puts f.readline
end
end
db.puts "quit"
db.flush
db.close
else
raise "cannot create greyole db!"
end
open("/etc/cron.weekly/greyhole", "w") do |f|
f.puts "#!/bin/sh\n/usr/bin/greyhole --fsck --email-report --dont-walk-metadata-store > /dev/null;\n"
f.puts "/usr/bin/greyhole --getuid > /tmp/greyhole.stats;\n"
f.puts "/usr/bin/greyhole --stats --json >> /tmp/greyhole.stats;\n"
f.puts "curl -s --data @/tmp/greyhole.stats #{API_URL_BASE}/greyhole > /dev/null;\n"
end
end
def substitute(file_in, file_out)
puts "substitute: #{file_in} -> #{file_out}" if @opt["D"]
# if it's a directory, go in and iterate
if File.directory?(file_in)
substitute_dir file_in, file_out
return
end
# make sure we can read/write the destination file.
# FIXME: security hole until the end of this function!
# FIXME: there is probably a huge danger hidden here that stuff may
# accidentally/unintentionally get destroyed.
File.chmod 0777, file_out if File.exists?(file_out) && File.writable?(file_out)
fin = fout = nil
begin
fin = File.new(file_in, "r")
rescue
abort "cannot open '#{file_in}' for reading"
end
begin
fout = File.new(file_out, "w")
rescue
abort "cannot open '#{file_out}' for writing"
end
netmask = @settings['netmask']
self_address = @settings['self-address']
domain = @settings['domain']
net = @settings['net']
api_key = @settings['api-key']
uname = `uname -p`
arch_64 = "";
arch = "32";
if uname =~ /64/
arch = "64"
arch_64 = "64"
end
fin.readlines.each do |line|
line.gsub! "\@HDA_NETWORK\@", net
line.gsub! "\@HDA_SELF\@", self_address
line.gsub! "\@HDA_DOMAIN\@", domain
line.gsub! "\@HDA_NETMASK\@", netmask
line.gsub! "\@HDA_API_KEY\@", api_key
line.gsub! "\@HDA_ARCH\@/", arch
line.gsub! "\@HDA_ARCH_64\@", arch_64
fout.print line
end
fin.close
fout.close
clone_perm file_in, file_out
end
def do_restart_network
#puts "Restarting the network ..."
# do_system_silent "chkconfig " + NETWORKMANAGER_SERVERNAME + " off"
# do_system_silent "service " + NETWORKMANAGER_SERVERNAME + " stop"
do_system_silent "killall dhclient"
sleep 1
## generate the conf files
#do_system_silent "service hda-ctl start"
## stop the net and related services
#do_system_silent "service " + NETWORK_SERVERNAME + " stop"
#sleep 1
#do_system_silent "service " = DHCPD_SERVERNAME + " stop"
#do_system_silent "service " + NAMED_SERVERNAME + " stop"
#sleep 1
#do_system_silent "service " + NETWORK_SERVERNAME + " start"
#puts "Network restart done."
#sleep 1
#puts "Starting network services ..."
#do_system_silent "service hda-ctl stop"
#sleep 1
#do_system_silent "service hda-ctl start"
#sleep 2
do_system_silent "service " + HTTPD_SERVERNAME + " start"
#puts "Network services started."
end
def do_samba
puts "Configuring Samba ... "
substitute "/usr/share/hda-ctl/samba/", "/etc/samba/"
puts "Samba install done."
end
def in_file?(file, string)
return false unless File.exists?(file)
! File.open(file){|file| file.grep(/#{string}/) }.empty?
end
def do_httpd
puts "Installing httpd ... "
substitute "/usr/share/hda-ctl/httpd/", "/etc/httpd/conf.d/"
unless in_file?(HTTPD_ENVFILE, 'umask')
File.open(HTTPD_ENVFILE, "a+") { |file| file.puts "\numask 002\n" }
end
# add apache to users group to make httpd capable of writing user files, e.g. for downloading apps
system "usermod -g users -G apache apache 2>&1"
# remove interfering configs
do_system_silent "cd /etc/httpd/conf.d/; rm -f welcome.conf autoindex.conf userdir.conf"
puts "HTTPD install done."
end
def start_all_services
puts "Starting services ... "
# NOTE: monit must be the last one, to prevent race conditions
services_start = [ 'hda-ctl', HTTPD_SERVERNAME, 'dnsmasq', NETWORK_SERVERNAME,
MYSQL_SERVERNAME, SMB_SERVERNAME, NMB_SERVERNAME, 'monit', 'hddtemp', 'docker', 'memcached']
if fedora? or centos?
services_stop = [ 'gpm', 'hplip', 'isdn', 'kudzu', 'sendmail',
'yum-updatesd', 'dhcrelay', 'ldap', 'rolekit', 'firewalld', 'cockpit']
else
services_stop = []
end
if @opt[p]
services_stop << DHCPD_SERVERNAME
end
services_start.each do |service|
if fedora? or centos?
system "systemctl enable #{service}.service 2>&1"
else
system "insserv #{service},start=2,3,4,5 2>&1"
end
end
puts "Services started."
puts "Stopping and disabling unused services ... "
services_stop.each do |service|
if fedora? or centos?
system "systemctl disable #{service}.service 2>&1"
else
system "insserv -r #{service},start=2,3,4,5 2>&1"
end
system "systemctl stop #{service}.service 2>&1"
end
puts "Stopping unused services done."
end
def start_service(service)
if fedora? or centos?
do_system "/usr/bin/systemctl start #{service}.service"
elsif ubuntu? and File.exist?(UPSTART_CONF % service)
do_system "/sbin/initctl start #{service}"
else
# legacy fallback
do_system LEGACY_INIT_PATH + service + " start"
end
end
def get_os_version
if fedora? or centos?
if File.exists?('/etc/amahi-release')
file = nil
begin
file = File.new "/etc/amahi-release", "r"
rescue
return "cannot-open-amahi-release-file"
end
return file.read.chomp()
end
"no-amahi-release-file"
if File.exists?('/etc/system-release')
file = nil
begin
file = File.new "/etc/system-release", "r"
rescue
return "cannot-open-sys-release-file"
end
return file.read.chomp()
end
"no-sys-release-file"
else
if File.exists?('/etc/issue.net')
file = nil
begin
file = File.new "/etc/issue.net", "r"
rescue
return "cannot-open-issue.net-file"
end
return file.read.chomp()
end
"no-issue.net-file"
end
end
# WARNING-cpg: this is needed to differentiate OSs and it makes
# hda-ctl require redhat-lsb in Fedora. However I do not like that
# redhat-lsb requires 50MB of packages when this can be
# determined in a MUCH simpler way (e.g. /etc/issue)!
# So we need to fix this first!
def identify_os_broken
distro = "Unknown"
release = "Unknown"
d, r = open("|lsb_release -ir") { |f| f.readlines }
distro = $1 if d =~ /Distributor ID:\t(.*)\n$/
release = $1 if r =~ /Release:\t(.*)\n$/
[distro, release]
end
def get_arch
arch = ""
if fedora? or centos?
file = IO.popen("uname -i") { |file| arch = file.read.chomp }
arch = "cannot-run-uname-i" if $? != 0
else
# others use uname -m
file = IO.popen("uname -m") { |file| arch = file.read.chomp }
arch = "cannot-run-uname-m" if $? != 0
end
arch
end
def check_install_code(inst_code)
os = URI.escape(get_os_version)
arch = URI.escape(get_arch)
url = "#{API_URL_BASE}/install/#{inst_code}?os=#{os}&arch=#{arch}&ver=#{$version}"
@system_configuration = nil
message "Retrieving install code ..."
time = 3
# get the settings remotely and update it on client
(1..3).each do
begin
@system_configuration = open(url, "User-Agent" => "ruby/#{RUBY_VERSION}").read
rescue
sleep time
time += 1
@system_configuration = nil
end
break if @system_configuration != nil
end
if @system_configuration == nil
sleep 2
(1..3).each do
begin
@system_configuration = open(url, "User-Agent" => "ruby/#{RUBY_VERSION}").read
rescue
time += 2
@system_configuration = nil
end
break if @system_configuration != nil
end
if @system_configuration == nil
message "ERROR: the installer cannot access the network to retrieve your HDAs settings."
message "\tPlease double check that the machine has network/internet access."
message "\tTry 'dhclient eth0' (as root) to configure your network interface."
exit(-1)
end
end
@system_configuration_length = @system_configuration.size
if @system_configuration =~ /^error: unknown code$/
puts @system_configuration
message "ERROR: install code '#{inst_code}' is not a valid install code."
exit(-1)
elsif @system_configuration_length == 0
message "ERROR: the amahi.org server may not be available. Try again later or email support."
exit(-1)
end
message "Install code looks good"
end
def copy_remote_db_configuration()
if @system_configuration_length == 0
abort "hda-install: ERROR: @system_configuration is empty."
end
begin
file = Kernel.open("| mysql -u #{DATABASE_USER} -p#{DATABASE_PASSWORD} #{DATABASE_NAME}", "w+")
file.puts @system_configuration
file.close
rescue
if ($@ =~ /^open/)
abort "cannot open mysql command : #{$!} \n #{$@} \n"
end
raise "cannot see settings file"
end
end
def do_db(install_code)
puts "Syncing settings for #{install_code} ... "
copy_remote_db_configuration
puts "Syncing settings done."
end
def setup_default_homepage
files = Dir.glob "/usr/lib/firefox-*/defaults/preferences/all-redhat.js"
files.each do |file|
sed = '/browser.startup.homepage/s/\"[^\"]*\")/\"http:\/\/hda\")/' + "\n"
sed += '/startup.homepage_override_url/s/\"[^\"]*\")/\"\")/' + "\n"
sed += '/startup.homepage_welcome_url/s/\"[^\"]*\")/\"\")/' + "\n"
puts sed
begin
sed_pipe = Kernel.open("| sed -i #{file} -f -", "w+")
sed_pipe.puts sed
sed_pipe.close()
rescue
if ($@ =~ /^open/)
$stderr.puts "cannot execute sed : #{$!} \n #{$@} \n"
break
end
raise
end
end
end
def do_monit
# configure monit daemon
if deb_based?
open("/etc/monit/monitrc", "w") do |f|
f.puts "# Automatically generated on #{Time.now.utc}- WARNING - any manual edits may be lost!"
f.puts "set daemon 30"
f.puts "include /etc/monit/conf.d/*.conf"
end
else
open("/etc/monit.conf", "w") do |f|
f.puts "# Automatically generated on #{Time.now.utc}- WARNING - any manual edits may be lost!"
f.puts "set daemon 30"
f.puts "include /etc/monit.d/logging"
f.puts "include /etc/monit.d/*.conf"
end
end
end
def mark_as_installed
f = "/var/cache/hda-ctl.cache"
file = nil
begin
file = File.new f, "w"
file.puts "hda_installed=\"yes\""
rescue
abort "cannot open '#{f}': #{$!}"
ensure
file.close if file
end
end
def message(m)
OLDOUT.print m
OLDOUT.print "\n"
OLDOUT.flush
puts m
end
def selinux_permissive
do_system_silent "setenforce 0"
end
# FIXME - in some cases we may not want to do this
def do_desktop_icons
do_system_silent "rsync -a /etc/skel/Desktop /root/"
end
def do_install_rpms
installed = IO.popen('rpm -qa') { |f| f.readlines }
rpms = []
rpms << "yum-plugin-fastestmirror"
rpms << "mariadb-server perl-libwww-perl perl-LWP-Protocol-https perl-Regexp-Common dnsmasq"
rpms << "php dhclient monit perl-Authen-PAM"
rpms << "hddtemp ruby-augeas memcached"
rpms << "samba httpd cadaver patch sudo wol bc"
rpms << "fpaste perl-DBI rsync wget curl cronie pmount"
rpms << "v8"
rpms << "php-gd php-mbstring php-xml php-mcrypt"
rpms << "docker"
list = rpms.join(' ').split(' ')
to_install = list.delete_if { |pname| installed.select{|p| p =~ /^#{pname}-[0-9]/} != [] }
if to_install.size == 0
message "No packages needed installation"
return
end
set = to_install.join ' '
puts "Packages to install: #{to_install.join ', '}"
do_system_silent "killall yum-updatesd"
message "Installing #{to_install.size} packages ..."
do_system_silent "killall dnf"
ret = do_system_multiple("dnf -y install #{set}")
puts "WARNING: rpm install failed ... please run\n\t dnf -y install #{set}\n\tby hand until it installs correctly" unless ret
# FIXME - needed for F12 updates of sudo
set = "sudo"
ret = do_system_multiple("dnf -y update #{set}")
puts "WARNING: rpm install failed ... please run\n\t dnf -y install #{set}\n\tby hand until it installs correctly" unless ret
message "RPM packages installed"
end
def do_install_debs
pkgs = []
pkgs << "apache2 libapache2-mod-fcgid "
pkgs << "ruby-dev rubygems hddtemp pastebinit"
pkgs << "chkconfig dnsmasq mysql-server samba monit libauthen-pam-perl"
pkgs << "eruby ri1.8 php5-mysql php5 cadaver"
pkgs << "sysvinit-utils network-manager libboost-all-dev isc-dhcp-client"
pkgs << "libapache2-mod-passenger libapache2-mod-ruby"
pkgs << "libmagickcore-dev libmagickwand-dev libfcgi-dev"
pkgs << "libauthen-pam-perl libdbi-perl libwww-perl libregexp-common-perl sudo"
pkgs << "greyhole pmount ntpdate bc ruby-dbus"
# TODO: decide if already installed packaged are eliminated first
to_install = pkgs.join(' ').split(' ')
set = to_install.join ' '
puts "Packages to install: #{to_install.join ', '}"
# TODO: kill running versions; kill updater
ret = do_system_multiple("apt-get update")
puts "WARNING: debian install failed ... please run\n\t apt-get update\n\tby hand until it installs correctly" unless ret
ret = do_system_multiple("apt-get -y install #{set}")
puts "WARNING: debian install failed ... please run\n\t apt-get -y install #{set}\n\tby hand until it installs correctly" unless ret
# enable debian modules
do_system_multiple("a2enmod \"*\"")
end
def do_install_packages
if fedora? or centos?
do_install_rpms
else
do_install_debs
end
end
# create an apaste as an "Amahi paste"
def link_apaste
if fedora? or centos?
system('ln -sf /usr/bin/fpaste /usr/bin/apaste')
else
system('ln -sf /usr/bin/pastebinit /usr/bin/apaste')
end
end
def do_full_install()
if fedora? or centos?
selinux_permissive
end
if File.exists?("/etc/sysconfig/amahi-hda")
# perhaps not a new install, do nothing unless forced
unless @opt["f"]
message "ERROR: this appears to be initialized."
message "... use option -f to force complete reinstall."
exit 1
end
end
if fedora? or centos?
message "Stopping background software updates ..."
# stop background dnf to prevent waits - silently because they may not be there
do_system_silent "systemctl stop yum-updatesd.service"
sleep 1
do_system_silent "killall yum-updatesd"
sleep 1
do_system_silent "killall yum-updatesd"
message "Background software updates stopped"
setup_default_homepage
rpms_to_remove = []
rpms_to_remove.each do |rpm|
do_system_silent "killall yum-updatesd"
puts "RPM UNINSTALL OF: #{rpm}";
do_system_silent "killall yum"
ret = do_system_multiple("rpm -e #{rpm}")
puts "WARNING: rpm uninstall failed ... please run\n\t rpm -e #{rpm}\n\tby hand until it uninstalls correctly" unless ret
end
end
do_install_packages
link_apaste
message "Starting MySQL"
unless fedora? or centos?
do_system_silent "service " + MYSQL_SERVERNAME + " stop"
end
start_service MYSQL_SERVERNAME
sleep 2
# disable DNSSEC
disable_dnssec
# initialize mysql
initialize_mysql
# create greyhole db
initialize_greyhole
# initialize rails and database
message "Initializing Ruby on Rails and DB"
do_system_silent "cd /var/hda/platform/html; bin/rake --trace db:migrate RAILS_ENV=production"
# update DB with remote data
message "Activating your HDA's settings"
copy_remote_db_configuration
sleep 1
# use the HDA data from the local db now
db_connect
message "Initializing settings"
# load db settings
get_db_settings
message "Configuring network"
autoconfigure_network
message "Setting up watchdog monitor"
do_monit
if fedora? or centos?
# set php timezone
do_php_timezone
end
# instantiate /etc/sysconfig/amahi-hda
substitute "/usr/share/hda-ctl/amahi-hda", "/etc/sysconfig/amahi-hda"
# lockdown f12 user install of software
# do_fedora12_lockdown
# samba configuration
message "Starting SAMBA server ..."
do_samba
# http configuration
message "Starting HTTP server ..."
do_httpd
# sudoers configuration
do_sudoers
# desktop icons
if fedora? or centos?
do_desktop_icons
# restart the network and the rest of the services
message "Setting the network services ..."
end
# do_restart_network
# NOTE: important to do this before the apps are
# installed
mark_as_installed
# let the games begin!
message "Starting the rest of the services ..."
start_all_services
sleep 2
if fedora? or centos?
disable_selinux
end
do_network
puts "****************************************************\n"
puts " Amahi installed successfully! Please reboot it!\n"
puts " Amahi installed successfully! Please reboot it!\n"
puts "****************************************************\n"
if @opt["q"]
# indicate we're done to the automated installer
message ("Instalation completed! Please reboot your HDA and all your computers!" * 4)
return
end
final_message2
message "\nThe system needs to reboot for your settings to take effect."
message "\nWould you like to reboot now? (strongly recommended)? (yes/no)?"
system "reboot" if @opt["y"]
resp = $stdin.gets
if resp != nil
if resp =~ /(y|yes)/
system "sync; sync; reboot"
exit 0 # just in case
end
end
message "OK. It is strongly recommended that you reboot your HDA ASAP."
end
def final_message2
message "\nCongratulations! Your Amahi HDA is set up!\n"
message "**** IMPORTANT **** IMPORTANT ***** IMPORTANT **** IMPORTANT ****"
message " Reboot your HDA, *make sure* it can see the internet and "
message " *only then* (optionally) you may turn off your router's DHCP "
message " server. See the FAQ for the tradeoffs of using your DHCP server."
message " http://tinyurl.com/amahi-dhcp"
message ""
message "**** IMPORTANT **** IMPORTANT ***** IMPORTANT **** IMPORTANT ****"
message "\nWhen your HDA reboots, reboot the machines in your network,"
message "or \"repair\" their network interface, for them to see the HDA."
message "\nAnd You Are Ready To Go!"
message "\thttp://hda Your dashboard"
message "\thttp://setup Your setup pages, including storage, applications, etc."
end
def ip2int(ip_addr)
IPAddr.new(ip_addr).hton.unpack('L').first
end
def rand_hex(l)
"%0#{l}x" % rand(1 << l*4)
end
def rand_uuid
[8,4,4,4,12].map {|n| rand_hex(n)}.join('-')
end
def setup_network_manager(ip, gw, dns1, dns2, domain, iface = nil)
puts "starting network install with IP: #{ip} and GW: #{gw}"
s_ip4 = {
"addresses" => ["aau", [[ip2int(ip), 24, ip2int(gw)]]],
"method" => ["s", "manual"],
"dns" => ["au", [ip2int(ip), ip2int(ip)]],
"dns-search" => ["as", [domain]],
}