hookit 0.7.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +22 -0
  5. data/README.md +3 -0
  6. data/Rakefile +10 -0
  7. data/bin/hookit +41 -0
  8. data/hookit.gemspec +30 -0
  9. data/lib/hookit/converginator.rb +141 -0
  10. data/lib/hookit/db.rb +37 -0
  11. data/lib/hookit/error.rb +8 -0
  12. data/lib/hookit/exit.rb +23 -0
  13. data/lib/hookit/helper/cron.rb +42 -0
  14. data/lib/hookit/helper/nfs.rb +113 -0
  15. data/lib/hookit/helper/shell.rb +35 -0
  16. data/lib/hookit/helper/xml.rb +25 -0
  17. data/lib/hookit/helper.rb +9 -0
  18. data/lib/hookit/hook.rb +102 -0
  19. data/lib/hookit/logger.rb +58 -0
  20. data/lib/hookit/logvac.rb +34 -0
  21. data/lib/hookit/platform/base.rb +11 -0
  22. data/lib/hookit/platform/smartos.rb +19 -0
  23. data/lib/hookit/platform/ubuntu.rb +19 -0
  24. data/lib/hookit/platform.rb +9 -0
  25. data/lib/hookit/platforms.rb +2 -0
  26. data/lib/hookit/registry.rb +53 -0
  27. data/lib/hookit/resource/base.rb +105 -0
  28. data/lib/hookit/resource/cron.rb +26 -0
  29. data/lib/hookit/resource/directory.rb +67 -0
  30. data/lib/hookit/resource/execute.rb +148 -0
  31. data/lib/hookit/resource/file.rb +71 -0
  32. data/lib/hookit/resource/hook_file.rb +80 -0
  33. data/lib/hookit/resource/link.rb +61 -0
  34. data/lib/hookit/resource/logrotate.rb +38 -0
  35. data/lib/hookit/resource/mount.rb +77 -0
  36. data/lib/hookit/resource/package.rb +74 -0
  37. data/lib/hookit/resource/rsync.rb +67 -0
  38. data/lib/hookit/resource/scp.rb +87 -0
  39. data/lib/hookit/resource/service.rb +89 -0
  40. data/lib/hookit/resource/socket.rb +67 -0
  41. data/lib/hookit/resource/template.rb +87 -0
  42. data/lib/hookit/resource/warning.rb +87 -0
  43. data/lib/hookit/resource/zfs.rb +99 -0
  44. data/lib/hookit/resource.rb +21 -0
  45. data/lib/hookit/resources.rb +15 -0
  46. data/lib/hookit/version.rb +3 -0
  47. data/lib/hookit.rb +27 -0
  48. metadata +205 -0
@@ -0,0 +1,61 @@
1
+ module Hookit
2
+ module Resource
3
+ class Link < Base
4
+
5
+ field :owner
6
+ field :group
7
+ field :link_type
8
+ field :target_file
9
+ field :to
10
+
11
+ actions :create, :delete
12
+ default_action :create
13
+
14
+ def initialize(name)
15
+ target_file name unless target_file
16
+ link_type :symbolic
17
+ super
18
+ end
19
+
20
+ def run(action)
21
+ case action
22
+ when :create
23
+ create!
24
+ chown!
25
+ when :delete
26
+ delete!
27
+ end
28
+ end
29
+
30
+ protected
31
+
32
+ def create!
33
+ args = ['f']
34
+ args << 'sn' if link_type == :symbolic
35
+ cmd = "ln -#{args.join} #{to} #{target_file}"
36
+ `#{cmd}`
37
+ code = $?.exitstatus
38
+ if code != 0
39
+ raise Hookit::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
40
+ end
41
+ end
42
+
43
+ def delete!
44
+ cmd = "rm -f #{target_file}"
45
+ `#{cmd}`
46
+ code = $?.exitstatus
47
+ if code != 0
48
+ raise Hookit::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
49
+ end
50
+ end
51
+
52
+ def chown!
53
+ return unless owner or group
54
+ if ::File.exists? target_file
55
+ `chown #{(group.nil?) ? owner : "#{owner}:#{group}"} #{target_file}`
56
+ end
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,38 @@
1
+ module Hookit
2
+ module Resource
3
+ class Logrotate < Base
4
+
5
+ field :path
6
+ field :filesize
7
+ field :max_size
8
+ field :count
9
+
10
+ actions :create
11
+ default_action :create
12
+
13
+ def initialize(name)
14
+ path name unless path
15
+ super
16
+ end
17
+
18
+ def run(action)
19
+ case action
20
+ when :create
21
+ create!
22
+ end
23
+ end
24
+
25
+ protected
26
+
27
+ def create!
28
+ case platform.os
29
+ when 'sun'
30
+ `logadm -c -w #{path} -s #{filesize ||= '10m'} -S #{max_size ||= '500m'} -C #{count ||= '10'} -N`
31
+ else
32
+ raise Hookit::Error::UnsupportedPlatform, "unsupported platform '#{platform.name}'"
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,77 @@
1
+ module Hookit
2
+ module Resource
3
+ class Mount < Base
4
+
5
+ field :device
6
+ # field :device_type
7
+ # field :dump
8
+ field :fstype
9
+ field :mount_point
10
+ field :options
11
+ field :pass
12
+ field :supports
13
+
14
+ actions :mount, :umount, :remount, :enable, :disable
15
+ default_action :mount
16
+
17
+ def initialize(name)
18
+ mount_point(name) unless mount_point
19
+ pass('-') unless pass
20
+ super
21
+ end
22
+
23
+ def run(action)
24
+ case action
25
+ when :mount
26
+ mount!
27
+ when :umount
28
+ umount!
29
+ when :remount
30
+ umount!
31
+ mount!
32
+ when :enable
33
+ disable!
34
+ enable!
35
+ when :disable
36
+ disable!
37
+ end
38
+ end
39
+
40
+ protected
41
+
42
+ def mount!
43
+ ::FileUtils.mkdir_p(mount_point)
44
+ run_command! "mount -O -F #{fstype} -o retry=5,timeo=300 #{options!(as_arg=true)} #{device} #{mount_point}"
45
+ end
46
+
47
+ def umount!
48
+ run_command! "umount #{mount_point}"
49
+ end
50
+
51
+ def enable!
52
+ entry = "#{device}\t#{device =~ /^\/dev/ ? device : "-"}\t#{mount_point}\t#{fstype}\t#{pass}\tyes\t#{options!}"
53
+ `echo "#{entry}" >> /etc/vfstab`
54
+ end
55
+
56
+ def disable!
57
+ `egrep -v "#{device}.*#{mount_point}" /etc/vfstab > /tmp/vfstab.tmp; mv -f /tmp/vfstab.tmp /etc/vfstab`
58
+ end
59
+
60
+ def options!(as_arg=false)
61
+ options = self.options.kind_of?(Array) ? self.options.join(',') : self.options
62
+ if as_arg
63
+ options ? (return "-o #{options}") : (return "")
64
+ end
65
+ options != "" ? (return "#{options}") : (return "-")
66
+ end
67
+
68
+ def run_command!(cmd, expect_code=0)
69
+ `#{cmd}`
70
+ code = $?.exitstatus
71
+ if code != expect_code
72
+ raise Hookit::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,74 @@
1
+ module Hookit
2
+ module Resource
3
+ class Package < Base
4
+
5
+ field :package_name
6
+ field :source
7
+ field :version
8
+ field :scope
9
+
10
+ actions :install
11
+ default_action :install
12
+
13
+ def initialize(name)
14
+ package_name(name) unless source
15
+ scope :default unless scope
16
+ super
17
+ end
18
+
19
+ def run(action)
20
+ case action
21
+ when :install
22
+ install!
23
+ end
24
+ end
25
+
26
+ def install!
27
+ begin
28
+ install_package
29
+ rescue Hookit::Error::UnexpectedExit
30
+ if not registry("pkgsrc.#{scope}.updated")
31
+ update_pkg_db
32
+ registry("pkgsrc.#{scope}.updated", true)
33
+ retry
34
+ else
35
+ raise
36
+ end
37
+ end
38
+ end
39
+
40
+ protected
41
+
42
+ def install_package
43
+ `#{pkgin} -y in #{package}`
44
+
45
+ code = $?.exitstatus
46
+ if not code == 0
47
+ raise Hookit::Error::UnexpectedExit, "pkgin in #{package} failed with exit code '#{code}'"
48
+ end
49
+ end
50
+
51
+ def update_pkg_db
52
+ `#{pkgin} -y up`
53
+ end
54
+
55
+ def package
56
+ if version
57
+ "#{package_name}-#{version}"
58
+ else
59
+ package_name
60
+ end
61
+ end
62
+
63
+ def pkgin
64
+ case scope
65
+ when :default
66
+ "/opt/local/bin/pkgin"
67
+ when :gopagoda
68
+ "/opt/gopagoda/bin/pkgin"
69
+ end
70
+ end
71
+
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,67 @@
1
+ module Hookit
2
+ module Resource
3
+ class Rsync < Base
4
+
5
+ field :source
6
+ field :destination
7
+ field :wrapper
8
+ field :archive
9
+ field :recursive
10
+ field :checksum
11
+ field :compress
12
+
13
+ actions :sync
14
+ default_action :sync
15
+
16
+ def initialize(name)
17
+ source name unless source
18
+ super
19
+ end
20
+
21
+ def run(action)
22
+ case action
23
+ when :sync
24
+ sync!
25
+ end
26
+ end
27
+
28
+ def sync!
29
+ run_command! "rsync -q#{archive!}#{recursive!}#{checksum!}#{compress!} #{wrapper!} #{source} #{destination}"
30
+ end
31
+
32
+ def archive!
33
+ (return "a") if archive
34
+ ""
35
+ end
36
+
37
+ def recursive!
38
+ (return "r") if archive
39
+ ""
40
+ end
41
+
42
+ def checksum!
43
+ (return "c") if archive
44
+ ""
45
+ end
46
+
47
+ def compress!
48
+ (return "z") if archive
49
+ ""
50
+ end
51
+
52
+ def wrapper!
53
+ (return "-e '#{wrapper}'") if wrapper
54
+ ""
55
+ end
56
+
57
+ def run_command!(cmd, expect_code=0)
58
+ `#{cmd}`
59
+ code = $?.exitstatus
60
+ if code != expect_code
61
+ raise Hookit::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
62
+ end
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,87 @@
1
+ module Hookit
2
+ module Resource
3
+ class Scp < Base
4
+
5
+ field :source
6
+ field :destination
7
+ field :port
8
+ field :recursive
9
+ field :config
10
+ field :cipher
11
+ field :identity
12
+ field :ssh_options
13
+ field :compression
14
+ field :preserve
15
+
16
+ actions :copy
17
+ default_action :copy
18
+
19
+ def initialize(name)
20
+ source(name) unless source
21
+ preserve(true) unless preserve
22
+ recursive(true) unless recursive
23
+ super
24
+ end
25
+
26
+ def run(action)
27
+ case action
28
+ when :copy
29
+ copy!
30
+ end
31
+ end
32
+
33
+ def copy!
34
+ run_command!("scp -q#{preserve!}#{recursive!}B#{compression!} #{config!} #{port!} #{cipher!} #{identity!} #{ssh_options!} #{source} #{destination}")
35
+ end
36
+
37
+ def cipher!
38
+ (return "-c #{cipher}") if cipher
39
+ ""
40
+ end
41
+
42
+ def compression!
43
+ (return "C") if compression
44
+ ""
45
+ end
46
+
47
+ def config!
48
+ (return "-F #{config}") if config
49
+ ""
50
+ end
51
+
52
+ def identity!
53
+ (return "-i #{identity}") if identity
54
+ ""
55
+ end
56
+
57
+ def port!
58
+ (return "-P #{port}") if port
59
+ ""
60
+ end
61
+
62
+ def preserve!
63
+ (return "p") if preserve
64
+ ""
65
+ end
66
+
67
+ def recursive!
68
+ (return "r") if recursive
69
+ ""
70
+ end
71
+
72
+ def ssh_options!
73
+ (return "-o #{ssh_options}") if ssh_options
74
+ ""
75
+ end
76
+
77
+ def run_command!(cmd, expect_code=0)
78
+ `#{cmd}`
79
+ code = $?.exitstatus
80
+ if code != expect_code
81
+ raise Hookit::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
82
+ end
83
+ end
84
+
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,89 @@
1
+ module Hookit
2
+ module Resource
3
+ class Service < Base
4
+
5
+ field :recursive
6
+ field :service_name
7
+ field :init
8
+
9
+ actions :enable, :disable, :start, :stop, :restart, :reload
10
+ default_action :enable
11
+
12
+ def initialize(name)
13
+ service_name(name) unless service_name
14
+
15
+ # if init scheme is not provided, try to set reasonable defaults
16
+ if not init
17
+ case platform.name
18
+ when 'smartos'
19
+ init(:smf)
20
+ when 'ubuntu'
21
+ init(:upstart)
22
+ end
23
+ end
24
+ end
25
+
26
+ def run(action)
27
+ case action
28
+ when :enable
29
+ enable!
30
+ when :disable
31
+ disable!
32
+ when :start
33
+ enable!
34
+ when :stop
35
+ disable!
36
+ when :restart
37
+ restart!
38
+ when :reload
39
+ reload!
40
+ end
41
+ end
42
+
43
+ protected
44
+
45
+ def enable!
46
+ case init
47
+ when :smf
48
+ run_command! "svcadm enable -s #{"-r" if recursive} #{service_name}"
49
+ when :runit
50
+ run_command! "sv start #{service_name}"
51
+ else
52
+ Hookit::Error::UnsupportedOption, "Unsupported init schema '#{init}'"
53
+ end
54
+ end
55
+
56
+ def disable!
57
+ case init
58
+ when :smf
59
+ run_command! "svcadm disable -s #{service_name}"
60
+ when :runit
61
+ run_command! "sv stop #{service_name}"
62
+ else
63
+ Hookit::Error::UnsupportedOption, "Unsupported init schema '#{init}'"
64
+ end
65
+ end
66
+
67
+ def restart!
68
+ case init
69
+ when :smf
70
+ run_command! "svcadm restart #{service_name}"
71
+ when :runit
72
+ disable!; enable!
73
+ else
74
+ Hookit::Error::UnsupportedOption, "Unsupported init schema '#{init}'"
75
+ end
76
+ end
77
+
78
+ def reload!
79
+ case init
80
+ when :smf
81
+ run_command! "svcadm refresh #{service_name}"
82
+ else
83
+ Hookit::Error::UnsupportedOption, "Unsupported init schema '#{init}'"
84
+ end
85
+ end
86
+
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,67 @@
1
+ module Hookit
2
+ module Resource
3
+ class Socket < Base
4
+
5
+ field :port
6
+ field :interface
7
+ field :service
8
+ field :max_checks
9
+
10
+ actions :listening, :no_connections, :reset
11
+ default_action :listening
12
+
13
+ def initialize(name)
14
+ service name unless service
15
+ max_checks 3 unless max_checks
16
+ super
17
+ end
18
+
19
+ def run(action)
20
+ case action
21
+ when :listening
22
+ check_listening!
23
+ when :no_connections
24
+ check_no_connections!
25
+ when :reset
26
+ reset!
27
+ end
28
+ end
29
+
30
+ protected
31
+
32
+ def check_listening!
33
+ # increment check
34
+ registry("#{service}.listening", registry("#{service}.listening").to_i + 1)
35
+
36
+ if `netstat -an | grep '\*\.#{port}' | grep LISTEN`.empty?
37
+ count = registry("#{service}.listening").to_i
38
+ if count <= max_checks
39
+ exit(count + 10)
40
+ else
41
+ $stderr.puts "ERROR: timed out waiting for #{service} to listen"
42
+ exit(Hookit::Exit::ERROR)
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ def check_no_connections!
49
+ # increment check
50
+ registry("#{service}.no_connections", registry("#{service}.no_connections").to_i + 1)
51
+
52
+ unless `netstat -an | grep 'ESTABLISHED' | awk '{ print $1 }' | grep "$(ifconfig #{interface} | grep inet | awk '{ print $2 }')\.#{port}"`.empty?
53
+ count = registry("#{service}.no_connections").to_i
54
+ if count <= max_checks
55
+ exit(count + 10)
56
+ end
57
+ end
58
+ end
59
+
60
+ def reset!
61
+ registry("#{service}.listening", 0)
62
+ registry("#{service}.no_connections", 0)
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,87 @@
1
+ require 'tilt'
2
+ require 'erubis'
3
+ require 'erb'
4
+ require 'oj'
5
+ require 'multi_json'
6
+
7
+ module Hookit
8
+ module Resource
9
+ class Template < Base
10
+
11
+ field :path
12
+ field :source
13
+ field :variables
14
+ field :mode
15
+ field :owner
16
+ field :group
17
+
18
+ actions :create, :create_if_missing, :delete, :touch
19
+ default_action :create
20
+
21
+ def initialize(name)
22
+ path name unless path
23
+ source "#{::File.basename(name)}.erb"
24
+ super
25
+ end
26
+
27
+ def run(action)
28
+ case action
29
+ when :create
30
+ create!
31
+ chown!
32
+ chmod!
33
+ when :create_if_missing
34
+ create_if_missing!
35
+ chown!
36
+ chmod!
37
+ when :delete
38
+ delete!
39
+ when :touch
40
+ touch!
41
+ end
42
+ end
43
+
44
+ protected
45
+
46
+ def create!
47
+ ::File.write path, render
48
+ end
49
+
50
+ def create_if_missing!
51
+ if not ::File.exists? path
52
+ create!
53
+ end
54
+ end
55
+
56
+ def delete!
57
+ ::File.delete path
58
+ end
59
+
60
+ def chown!
61
+ return unless owner or group
62
+ `chown #{(group.nil?) ? owner : "#{owner}:#{group}"} #{path}`
63
+ end
64
+
65
+ def chmod!
66
+ ::File.chmod(mode, path) if mode
67
+ end
68
+
69
+ def touch!
70
+ `touch -c #{path}`
71
+ end
72
+
73
+ def render
74
+ Tilt.new("#{template_dir}/#{source}").render(self, variables)
75
+ end
76
+
77
+ def template_dir
78
+ "#{module_root}/templates"
79
+ end
80
+
81
+ def module_root
82
+ dict[:module_root]
83
+ end
84
+
85
+ end
86
+ end
87
+ end