irgat 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. data/History.txt +4 -0
  2. data/Manifest.txt +45 -0
  3. data/PostInstall.txt +4 -0
  4. data/README.rdoc +29 -0
  5. data/Rakefile +4 -0
  6. data/TODO +26 -0
  7. data/bin/irgat +87 -0
  8. data/bin/irgat_setup +135 -0
  9. data/config/hoe.rb +80 -0
  10. data/config/requirements.rb +15 -0
  11. data/lib/irgat.rb +94 -0
  12. data/lib/irgat/console.rb +85 -0
  13. data/lib/irgat/dependencies.rb +75 -0
  14. data/lib/irgat/execs.rb +73 -0
  15. data/lib/irgat/log.rb +116 -0
  16. data/lib/irgat/mail.rb +88 -0
  17. data/lib/irgat/utils.rb +17 -0
  18. data/lib/irgat/version.rb +10 -0
  19. data/lib/modules/backup.rb +348 -0
  20. data/lib/modules/console.rb +48 -0
  21. data/lib/modules/kwallet.rb +133 -0
  22. data/script/console +10 -0
  23. data/script/destroy +14 -0
  24. data/script/generate +14 -0
  25. data/script/txt2html +82 -0
  26. data/setup.rb +1585 -0
  27. data/setup/etc/backup.yml.example +103 -0
  28. data/setup/etc/chroot.yml.example +42 -0
  29. data/setup/etc/console.yml.default +11 -0
  30. data/setup/etc/cron.yml.example +56 -0
  31. data/setup/etc/dar.yml.example +16 -0
  32. data/setup/etc/irgat.yml.example +37 -0
  33. data/setup/etc/kwallet.yml.default +18 -0
  34. data/setup/etc/kwallet.yml.example +18 -0
  35. data/setup/etc/ssh.yml.example +17 -0
  36. data/tasks/deployment.rake +34 -0
  37. data/tasks/environment.rake +7 -0
  38. data/tasks/website.rake +17 -0
  39. data/test/test_helper.rb +2 -0
  40. data/test/test_irgat.rb +11 -0
  41. data/website/index.html +83 -0
  42. data/website/index.txt +83 -0
  43. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  44. data/website/stylesheets/screen.css +138 -0
  45. data/website/template.html.erb +48 -0
  46. metadata +157 -0
@@ -0,0 +1,85 @@
1
+ # extend the IRB commands
2
+ module IRB
3
+ module ExtendCommandBundle
4
+ # irgat_help. Load a file with help about command
5
+ def irgat_help(what=nil)
6
+ # TODO check paths
7
+ help_file=IRGAT_LANG+"/help_cmd_#{@config[:lang]}.rb"
8
+ # control if exist file
9
+ if ! test(?d,help_file)
10
+ puts "Help file #{help_file} not found. Check the install or choose another language in your irgat config file"
11
+ end
12
+ #get comments in file
13
+ comment = File.open(help_file) do |file|
14
+ RDoc.find_comment(file)
15
+ end
16
+
17
+ # parse it, and create needed objects
18
+ comment = comment.gsub(/^\s*#/, '')
19
+ markup = SM::SimpleMarkup.new
20
+ flow_convertor = SM::ToFlow.new
21
+ flow = markup.convert(comment, flow_convertor)
22
+
23
+ #define output format
24
+ format = "plain"
25
+
26
+ #index help if nothing was wrotten
27
+ what='index' if what==nil
28
+
29
+ #i don't want output in these...
30
+ $stdout=File.new('/dev/null',"w+")
31
+ searched_flow = RDoc.extract_sections(flow, what.to_a)
32
+ #retake output
33
+ $stdout=STDOUT
34
+
35
+ # i don't like the rdoc way to return the things... it is assumed, that if you don't found your desired section, you want to see all!! O_o
36
+ if flow==searched_flow
37
+ puts '[ Error ] No section founded for your petition '+what
38
+ puts 'Try a diffent pattern search or try irgat_help without search pattern to view a list of avaibles sections'
39
+ else
40
+ #we have found, render it
41
+ options = RI::Options.instance
42
+ if args = ENV["RI"]
43
+ options.parse(args.split)
44
+ end
45
+ formatter = options.formatter.new(options, "")
46
+ formatter.display_flow(searched_flow)
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+
53
+
54
+
55
+ # define irgat promt
56
+ IRB.conf[:PROMPT][:IRGAT] = { # name of prompt mode
57
+ :PROMPT_I => "irgat:$> ", # normal prompt
58
+ :PROMPT_S => "irgat:$> ", # prompt for continuing strings
59
+ :PROMPT_C => "irgat:$> ", # prompt for continuing statement
60
+ :RETURN => " irgat_return => %s \n\n" # format to return value
61
+
62
+ }
63
+
64
+ # set promt
65
+ IRB.conf[:PROMPT_MODE] = :IRGAT
66
+
67
+
68
+ # create console irgat
69
+ require 'modules/console'
70
+ @console = Irgat::Console.new
71
+
72
+ enable_modules = []
73
+
74
+ if @console.config_module[:load_modules] == "all"
75
+ @console.config[:enable_subsystem].each_pair {| mod, enable |
76
+ if mod != "console" && enable != false
77
+ require 'modules/' + mod
78
+ mod_enable = Object.module_eval("Irgat::#{ mod.capitalize }").new
79
+ instance_variable_set "@#{ mod }", mod_enable
80
+ enable_modules << mod
81
+ end
82
+ }
83
+ end
84
+
85
+ puts "** Console enable modules => #{ enable_modules.join(', ') }"
@@ -0,0 +1,75 @@
1
+ module Irgat
2
+ module Dependencies
3
+
4
+ def check_dependencies(dependencies = {})
5
+ # check programs
6
+ @programs = check_programs(dependencies[:check_programs]) if dependencies[:check_programs]
7
+ # check folders
8
+ @folders = check_folders(dependencies[:check_folders]) if dependencies[:check_folders]
9
+ # check mounted folder
10
+ @mounted_folder = check_mounted_folder(dependencies[:check_mounted_folder]) if dependencies[:check_mounted_folder]
11
+ # check egid
12
+ check_egid(dependencies[:check_egid]) if dependencies[:check_egid]
13
+ end
14
+
15
+ private
16
+ def check_programs(programs)
17
+ @programs = Hash.new
18
+ programs.each { |p|
19
+ # locate program
20
+ program_path = %x[ which #{p} ]
21
+ if program_path
22
+ @programs[p.to_sym] = program_path.strip
23
+ else
24
+ exit_with_error("Irgat couldn't find the program #{ p } needed by #{ self.class } module")
25
+ end
26
+ }
27
+ @programs
28
+
29
+ end
30
+
31
+ def check_folders(folders_array = [])
32
+ # first is root folder
33
+ root_folder = folders_array.shift
34
+ @folders = Hash.new
35
+ # check the root_folder
36
+ exist_or_create_folder(root_folder)
37
+ @folders[:root] = root_folder
38
+ # check the others
39
+ folders_array.shift.each { |f|
40
+ folder = root_folder + "/" + f
41
+ exist_or_create_folder(folder)
42
+ @folders[f.gsub('/', '_').to_sym] = folder
43
+ }
44
+ @folders
45
+ end
46
+
47
+ def check_mounted_folder(folder)
48
+ mount_points = execute_command("mount","Getting mount points")
49
+ if mount_points.include?(folder)
50
+ log("text","OK. #{folder} already mounted")
51
+ else
52
+ # we try to mount
53
+ log("text","Warning. #{folder} not mounted. Triying to mount")
54
+ execute_command("mount #{folder}",
55
+ "Mounting working directory #{folder}",
56
+ {:fatal_error => true})
57
+ end
58
+ end
59
+
60
+ def check_egid(user)
61
+ if user != execute_command("whoami", "Getting irgat launcher user", 3).strip
62
+ exit_with_error("#{ @config_module[:subsystem_name] } Irgat module must run as #{ user } User")
63
+ end
64
+ end
65
+
66
+ # create if not exist the folder
67
+ def exist_or_create_folder(folder_absolute_path)
68
+ if ! test(?d,folder_absolute_path)
69
+ Dir.mkdir(folder_absolute_path)
70
+ log("text", "Creating folder #{ folder_absolute_path }", 2)
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,73 @@
1
+ # add utils to irgat::irgat class
2
+
3
+ module Irgat
4
+ module Execs
5
+
6
+ def execute_command(command, command_description, options = {})
7
+ @commands_launched ||= Hash.new
8
+
9
+ command_exec = command.to_s
10
+ pre_command_time = Time.now
11
+ command_output = %x[ #{command_exec} 2> /tmp/error_out ]
12
+ finish_status = $?
13
+ pos_command_time = Time.now
14
+ error_output = File.new("/tmp/error_out").read
15
+
16
+ # exit?
17
+ if finish_status != 0 && options[:fatal_error]
18
+ debug_mode = "\n"
19
+ debug_mode << "\n [ Command Syntax ] => #{ command_exec }"
20
+ debug_mode << "\n [ Command Output ] => #{ command_output }"
21
+ debug_mode << "\n [ Command Errors ] => #{ error_output }"
22
+
23
+ exit_with_error("Command '#{ command_description }' exit with a #{ finish_status } status. Error is reported as FATAL, then, exit Irgat process. #{ debug_mode if @config[:debug_level] == 3 }")
24
+ end
25
+
26
+ # Log the outputs || debug 1
27
+ log("command", "* [#{ (finish_status == 0 ? " OK " : " WARNING") }: #{Time.now.strftime("%H:%M:%S")}] -> #{ command_description }", 1)
28
+
29
+ # Log warning level
30
+ if finish_status != 0
31
+ log("warning", "Command #{ command_description } exited with a #{ finish_status } status.", 2)
32
+ end
33
+
34
+ # Log 3 level... commands launched and reports
35
+ text_to_log = "\n ** [COMMAND LOG]"
36
+ text_to_log << "\n [ Command Syntax ] => #{ command_exec }"
37
+ text_to_log << "\n [ Command Status ] => #{ finish_status }"
38
+ text_to_log << "\n [ Command Output ] => #{ command_output }"
39
+ text_to_log << "\n [ Command Errors ] => #{ error_output }"
40
+ text_to_log << "\n [ Command P.Time ] => #{ pos_command_time - pre_command_time }\n\n"
41
+
42
+ log("text", text_to_log, 3)
43
+
44
+ # add the command to the commands_launched hash
45
+ @commands_launched[command_description] = finish_status
46
+
47
+ return command_output
48
+ end
49
+
50
+ # method to end irgat process and report
51
+ def finish_process(options = {})
52
+ output_options = Hash.new
53
+ output_options[:fatal] = options[:fatal] || false
54
+ output_options[:main_msj] = options[:msj] || "Finish Process"
55
+ # output the logs
56
+ output_log({ :fatal => output_options[:fatal],
57
+ :main_msj => output_options[:main_msj] })
58
+ end
59
+
60
+ # Exit irgat process with error and optional exit status
61
+ def exit_with_error(msj, options = {})
62
+
63
+ log("text", msj, 1)
64
+
65
+ finish_process( { :fatal => true, :msj => "Exit with error #{ msj }" } ) unless options[:no_finish_process]
66
+
67
+ exit (options[:exit_level] ? options[:exit_level] : 1)
68
+ end
69
+
70
+
71
+ end
72
+ end
73
+
@@ -0,0 +1,116 @@
1
+ # add utils to irgat::irgat class
2
+
3
+ module Irgat
4
+ module Log
5
+
6
+ def log(output_type, message, debug = nil)
7
+ # create @log_process if not exist or use it
8
+ @log_process ||= Array.new
9
+ # add new hash to point variable
10
+ log_point = Hash.new
11
+ log_point[:type] = output_type
12
+ log_point[:msj] = message
13
+ log_point[:debug] = debug || 3
14
+ @log_process << log_point
15
+ # need to puts
16
+
17
+ if @log_to_stdout && @config[:debug_level] >= log_point[:debug]
18
+ puts "[#{ log_point[:type] }] \n #{ log_point[:msj] }"
19
+ end
20
+ end
21
+
22
+ def output_log(options = {})
23
+ fatal = options[:fatal] || false
24
+
25
+ # build report
26
+ report = build_report
27
+ resume = build_resume(fatal)
28
+
29
+ if @config_module[:output_log]
30
+ # add a extend header to the log report
31
+ report_to_log(report)
32
+ end
33
+
34
+ if @config_module[:output_email]
35
+ report_to_email(resume, report, options[:main_msj] )
36
+ end
37
+ end
38
+
39
+ def report_to_log(report)
40
+ exist_or_create_folder(@config[:log_path])
41
+
42
+ log_file_folder = "#{ @config[:log_path] }/#{ @config_module[:subsystem_name] }"
43
+
44
+ exist_or_create_folder(log_file_folder)
45
+
46
+ log_file = "#{ log_file_folder }/#{ Time.now.strftime("%Y-%m-%d") }_#{ @config[:server_name] }.log"
47
+
48
+ if log_out = File.new(log_file,"w+")
49
+ log_out.puts report
50
+ log_out.close
51
+ else
52
+ puts "Unable to open log #{ log_file }. Output won't be wrotten"
53
+ end
54
+ end
55
+
56
+ def report_to_email(resume, report, subject )
57
+ # body
58
+ if @config[:debug_level] == 3
59
+ resume = resume + "\n\n LOG \n\n" + report
60
+ end
61
+
62
+ send_email({ :subject => subject, :body => resume })
63
+ end
64
+
65
+ def build_report
66
+ report = ''
67
+
68
+ @log_process.each { |log_entry|
69
+ case log_entry[:type]
70
+ when "point"
71
+ @point ||= 0
72
+ underscroll_caracter = "="
73
+ report << "\n #{ @point } #{ log_entry[:msj] } \n"
74
+ report << ' ' + (underscroll_caracter * @point.to_s.length) + underscroll_caracter + (underscroll_caracter * log_entry[:msj].length) + "\n"
75
+ @point += 1
76
+ when "text"
77
+ if @config[:debug_level] >= log_entry[:debug]
78
+ report << "#{ log_entry[:msj] }"
79
+ end
80
+ when "command"
81
+ if @config[:debug_level] >= log_entry[:debug]
82
+ report << "\n\n #{ log_entry[:msj] }"
83
+ end
84
+ end
85
+ }
86
+ report
87
+ end
88
+
89
+ # method to report a info of the irgat process
90
+ def build_resume(fatal)
91
+
92
+
93
+ total_time = Time.now - @init_time
94
+ total_time_log = "#{ total_time.to_s.slice(0,5) } sec., #{ (total_time / 60).to_s.slice(0,5) } min., #{ ((total_time / 60) / 60).to_s.slice(0,5) } hours."
95
+ fatal_log = (fatal ? "ERROR" : "OK")
96
+
97
+ report_resume = "\n"
98
+ report_resume << " * Status \n"
99
+ report_resume << " [ #{ fatal_log } ] \n"
100
+ report_resume << "\n"
101
+ report_resume << " * Resume info \n"
102
+ report_resume << " [ Serv Host ] -> #{ @config[:server_name] } \n"
103
+ report_resume << " [ Module ] -> #{ self.class.to_s } \n"
104
+ report_resume << " [ Init Time ] -> #{ @init_time } \n"
105
+ report_resume << " [ End Time ] -> #{ Time.now } \n"
106
+ report_resume << " [ TotalTime ] -> #{ total_time_log } \n"
107
+ if @commands_launched
108
+ report_resume << " [ CmdsExecs ] -> #{ @commands_launched.values.select { |v| v == 0 }.size } \n"
109
+ report_resume << " [ Warnings ] -> #{ @commands_launched.values.select { |v| v != 0 }.size} \n"
110
+ end
111
+
112
+ return report_resume
113
+ end
114
+
115
+ end
116
+ end
@@ -0,0 +1,88 @@
1
+ # add utils to irgat::irgat class
2
+
3
+ module Irgat
4
+ module Mail
5
+
6
+ def send_email(options = {})
7
+ mail = build_email(options)
8
+
9
+ if self.config[:smtp_mode]=='smtp'
10
+ Net::SMTP.start(self.config[:smtp_values]["server"],
11
+ self.config[:smtp_values]["port"],
12
+ self.config[:smtp_values]["server"],
13
+ self.config[:smtp_values]["user"],
14
+ self.config[:smtp_values]["password"],
15
+ :login) do |smtp|
16
+ smtp.send_mail mail.encoded,
17
+ "irgat@#{ @config[:server_domain] }",
18
+ mail.destinations
19
+ end
20
+ else
21
+ #send email to localhost without authentication
22
+ Net::SMTP.start('localhost', 25) do |smtp|
23
+ smtp.send_mail mail.encoded,
24
+ "irgat@#{ @config[:server_domain] }",
25
+ mail.destinations
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+ def build_email(options = {})
32
+ # crete emails and fill it
33
+ mail = TMail::Mail.new
34
+ mail.date = Time.now
35
+ mail.subject = options[:subject] || "[Irgat][#{ self.class.to_s }] Email from #{ @config[:server_name] }"
36
+ mail.to = options[:to] || @config[:mail_to]
37
+
38
+ # add cc and bcc if exist
39
+ mail.cc = options[:cc] if options[:cc]
40
+ mail.bcc = options[:bcc] if options[:bcc]
41
+
42
+ # body
43
+ mail.body = options[:body] || "body empty"
44
+
45
+ mail
46
+ end
47
+
48
+ #method to send emails using mutt... maybe, will be better to use action_mailer
49
+ # FIXME, added an option to run the mutt command in chroot, but probably is not good idea, only parse to litura
50
+ # DEPRECATED
51
+ def send_email_with_mutt(options={})
52
+ #check mutt program
53
+ command=(%x[which mutt]).strip
54
+ if command==""
55
+ puts "Mutt is not installed, send_mail method can't be used"
56
+ else
57
+ if options[:in_chroot]
58
+ command='chroot /var/local/chroot '+command
59
+ end
60
+ # parse options
61
+ # cc copy
62
+ command=command+(options[:to_cc].split(',').collect {|c| ' -c '+c }).to_s if options[:to_cc]
63
+ # bcc copy
64
+ command=command+(options[:to_bcc].split(',').collect {|b| ' -b '+b }).to_s if options[:to_bcc]
65
+ # attach files
66
+ command=command+" -a '"+options[:attach_files].split(',').join(' ')+"'" if options[:attach_files]
67
+ # body, we include it from file
68
+ if options[:body]
69
+ email_body_file=File.new('/tmp/email_body_file',"w+")
70
+ email_body_file.puts options[:body]
71
+ email_body_file.close
72
+ command=command+" -i "+email_body_file.path
73
+ end
74
+
75
+ # subject
76
+ command=command+" -s '"+(options[:subject] || "[Irgat] Email from #{self.class.to_s} Subsytem")+"'"
77
+ # to
78
+ command=command+" -- "+(options[:to] || "#{self.config[:mail_to]}")+""
79
+ # end command
80
+ command=command+' <.'
81
+ # exec it
82
+ self.execute_command(command,("Sending email"|| options[:log_text]))
83
+ # clean file
84
+ File.delete('/tmp/email_body_file')
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,17 @@
1
+ #= License
2
+ #
3
+ # IRGAT (Intuitive Ruby for Gnoxys Administration Tools)
4
+ # 2007 - 2008 Gnoxys. info@gnoxys.net
5
+ # This program is released under the terms of the GPL v3 license as you cand find in http://www.gnu.org/licenses/gpl.html
6
+
7
+ begin
8
+ # require all the irgat utils
9
+ # FIXME atd wants includes in irgat.
10
+ require 'irgat/log'
11
+ require 'irgat/mail'
12
+ require 'irgat/execs'
13
+ require 'irgat/dependencies'
14
+ rescue LoadError => msj
15
+ puts "Irgat needed the follow utils:"+(msj.to_s.split('--'))[1]+", but not found."
16
+ puts "[ ERROR ] in file "+msj.backtrace.to_s
17
+ end