-
Notifications
You must be signed in to change notification settings - Fork 1
/
bugadm
executable file
·133 lines (109 loc) · 2.71 KB
/
bugadm
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
#! /usr/bin/env ruby
require_relative('config/bugdata')
require_relative('lib/rapport')
require_relative('lib/bugger')
require_relative('db/dbmodule')
def install_launchd()
bugadm_path = BugData.config.base_path + '/bugadm'
plist_data = <<-EOS
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>no.brujordet.bugger</string>
<key>ProgramArguments</key>
<array>
<string>#{BugData.config.ruby_bin}</string>
<string>#{bugadm_path}</string>
<string>notify</string>
</array>
<key>StandardOutPath</key>
<string>/var/log/bugger.log</string>
<key>StandardErrorPath</key>
<string>/var/log/bugger.log</string>
<key>StartInterval</key>
<integer>#{BugData.config.bugfreq}</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOS
File.open(BugData.config.plist_file, 'w') { |file| file.write(plist_data) }
end
def create_db()
include BuggerDB
puts "Creating empty db: #{BugData.config.db_path}"
db = DatabaseAccess.new(BugData.config.db_path)
db.prepare_schema
end
def bugger_status()
status = %x[ #{"launchctl list | grep no.brujordet.bugger | awk '{print $2}' | tr -d '\n'"} ]
if (status == '0')
puts 'Bugger is running'
elsif (status.to_i > 0)
puts 'Bugger is having problems'
else
puts 'Bugger is not running'
end
end
def bugger_control(action)
if(action == 'start')
command = 'load'
else
command = 'unload'
end
cmd = "launchctl #{command} #{BugData.config.plist_file}"
system cmd
end
def reload()
bugger_control('stop')
system "rm #{BugData.config.plist_file}"
install_launchd
bugger_control('start')
sleep 1
bugger_status
end
def uninstall()
system 'rm db/bug.db'
bugger_control('stop')
system "rm #{BugData.config.plist_file}"
puts 'bye bye :('
end
def die_with_usage()
puts "usage: bugadm (install|uninstall|start|stop|reload|restart|status|prompt|notify|rapport)"
exit
end
die_with_usage unless ARGV.length > 0
parameter = ARGV[0]
BugData.configure do |config|
# nothing?
end
case parameter
when 'install'
create_db
install_launchd
bugger_control 'start'
when 'uninstall'
uninstall
when
'start', 'stop'
bugger_control(parameter)
sleep 1
bugger_status
when 'reload'
reload
when 'restart'
bugger_control 'stop'
bugger_control 'start'
sleep 1
bugger_status
when 'status'
bugger_status
when 'prompt','notify'
Bugger.new.bug(parameter)
when 'rapport'
BugRapport.new.generateRapportFor(Time.now)
else
die_with_usage
end