hookit 0.12.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,87 +0,0 @@
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
@@ -1,117 +0,0 @@
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
- when 'docker'
23
- init(:runit)
24
- end
25
- end
26
- end
27
-
28
- def run(action)
29
- case action
30
- when :enable
31
- enable!
32
- when :disable
33
- disable!
34
- when :start
35
- enable!
36
- when :stop
37
- disable!
38
- when :restart
39
- restart!
40
- when :reload
41
- reload!
42
- end
43
- end
44
-
45
- protected
46
-
47
- def enable!
48
- case init
49
- when :smf
50
- run_command! "svcadm enable -s #{"-r" if recursive} #{service_name}"
51
- when :runit
52
- # register and potentially start the service
53
- run_command! "sv start #{service_name}", false
54
-
55
- # runit can take up to 5 seconds to register the service before the
56
- # service starts to run. We'll keep checking the status for up to
57
- # 6 seconds, after which time we'll raise an exception.
58
- registered = false
59
-
60
- 6.times do
61
- # check the status
62
- `sv status #{service_name}`
63
- if $?.exitstatus == 0
64
- registered = true
65
- break
66
- end
67
-
68
- sleep 1
69
- end
70
-
71
- if registered
72
- # just in case the service is registered but not started, try
73
- # to start the service one more time
74
- run_command! "sv start #{service_name}"
75
- else
76
- raise Hookit::Error::UnexpectedExit "Service #{service_name} did not register within 6 seconds."
77
- end
78
-
79
- else
80
- raise Hookit::Error::UnsupportedOption "Unsupported init schema '#{init}'"
81
- end
82
- end
83
-
84
- def disable!
85
- case init
86
- when :smf
87
- run_command! "svcadm disable -s #{service_name}"
88
- when :runit
89
- run_command! "sv stop #{service_name}"
90
- else
91
- raise Hookit::Error::UnsupportedOption "Unsupported init schema '#{init}'"
92
- end
93
- end
94
-
95
- def restart!
96
- case init
97
- when :smf
98
- run_command! "svcadm restart #{service_name}"
99
- when :runit
100
- disable!; enable!
101
- else
102
- raise Hookit::Error::UnsupportedOption "Unsupported init schema '#{init}'"
103
- end
104
- end
105
-
106
- def reload!
107
- case init
108
- when :smf
109
- run_command! "svcadm refresh #{service_name}"
110
- else
111
- raise Hookit::Error::UnsupportedOption "Unsupported init schema '#{init}'"
112
- end
113
- end
114
-
115
- end
116
- end
117
- end
@@ -1,78 +0,0 @@
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
-
18
- case platform.os
19
- when 'sun'
20
- @active_com = "netstat -an | egrep '\*\.#{port}' | grep LISTEN"
21
- @inactive_com = "netstat -an | grep 'ESTABLISHED' | awk '{ print $1 }' | grep \"$(ifconfig #{interface} | grep inet | awk '{ print $2 }')\.#{port}\""
22
- else
23
- @active_com = "netstat -an | egrep ':#{port}' | grep LISTEN"
24
- @inactive_com = "netstat -an | grep 'ESTABLISHED' | awk '{ print $4 }' | grep \"$(ifconfig #{interface} | awk '/inet addr/ { print $2}' | cut -f2 -d':' | tr -d '\n'):#{port}\""
25
- end
26
- end
27
-
28
- def run(action)
29
- case action
30
- when :listening
31
- check_listening!
32
- when :no_connections
33
- check_no_connections!
34
- when :reset
35
- reset!
36
- end
37
- end
38
-
39
- protected
40
-
41
- def check_listening!
42
- # increment check
43
- registry("#{service}.listening", registry("#{service}.listening").to_i + 1)
44
-
45
- if `#{@active_com}`.empty?
46
- count = registry("#{service}.listening").to_i
47
- if count <= max_checks
48
- sleep 1
49
- exit(count + 10)
50
- else
51
- $stderr.puts "ERROR: timed out waiting for #{service} to listen"
52
- exit(Hookit::Exit::ERROR)
53
- end
54
- end
55
-
56
- end
57
-
58
- def check_no_connections!
59
- # increment check
60
- registry("#{service}.no_connections", registry("#{service}.no_connections").to_i + 1)
61
-
62
- unless `#{@inactive_com}`.empty?
63
- count = registry("#{service}.no_connections").to_i
64
- sleep 1
65
- if count <= max_checks
66
- exit(count + 10)
67
- end
68
- end
69
- end
70
-
71
- def reset!
72
- registry("#{service}.listening", 0)
73
- registry("#{service}.no_connections", 0)
74
- end
75
-
76
- end
77
- end
78
- end
@@ -1,87 +0,0 @@
1
- module Hookit
2
- module Resource
3
- class Warning < Base
4
-
5
- field :source
6
- field :content
7
- field :stream
8
-
9
- actions :warn
10
- default_action :warn
11
-
12
- def initialize(name)
13
- source(name) unless source or content
14
- stream :stdout unless stream
15
-
16
- @default_header = "\u25BC\u25BC\u25BC\u25BC :: WARNING :: \u25BC\u25BC\u25BC\u25BC"
17
- @block_width = @default_header.length
18
- end
19
-
20
- def run(action)
21
- case action
22
- when :warn
23
- warn!
24
- end
25
- end
26
-
27
- protected
28
-
29
- def gem
30
- dict[:template_gem]
31
- end
32
-
33
- def gem_spec
34
- Gem::Specification.find_by_name(gem)
35
- end
36
-
37
- def gem_root
38
- gem_spec.gem_dir
39
- end
40
-
41
- def content!
42
- output_string ||= content or ::File.open("#{gem_root}/messages/#{source}").read
43
- return output_string
44
- end
45
-
46
- def header!
47
-
48
- header = @default_header
49
- padding = "\u25BC"
50
-
51
- longest_line = content!.split.sort_by {|x| x.length}.last
52
-
53
- if longest_line.length > header.length
54
-
55
- difference = longest_line.length - header.length
56
- padding *= (difference.to_f / 2).ceil
57
-
58
- header = padding + header + padding
59
- end
60
-
61
- @block_width = header.length
62
-
63
- puts header
64
- end
65
-
66
- def footer!
67
- footer = "\u25B2" * @block_width
68
- puts footer
69
- end
70
-
71
- def warn!
72
-
73
- header!
74
-
75
- case stream
76
- when :stdout
77
- puts content!
78
- when :stderr
79
- $stderr.puts content!
80
- end
81
-
82
- footer!
83
- end
84
-
85
- end
86
- end
87
- end
@@ -1,99 +0,0 @@
1
- module Hookit
2
- module Resource
3
- class Zfs < Base
4
-
5
- field :snapshot
6
- field :dataset
7
- field :destination
8
- field :source
9
- field :ssh_host
10
- field :validator
11
- field :options
12
-
13
- actions :send, :receive, :transmit, :snapshot, :destroy, :rollback, :clone
14
- default_action :send
15
-
16
- def initialize(name)
17
- snapshot(name) unless snapshot
18
- super
19
- end
20
-
21
- def run(action)
22
- case action
23
- when :send
24
- send!
25
- when :receive
26
- receive!
27
- when :transmit
28
- transmit!
29
- when :snapshot
30
- snapshot!
31
- when :destroy
32
- destroy!
33
- when :rollback
34
- rollback!
35
- when :clone
36
- clone!
37
- end
38
- end
39
-
40
- def send!
41
- run_command! "zfs send #{snapshot} | #{destination}"
42
- end
43
-
44
- def receive!
45
- run_command! "#{source.to_s.strip} | zfs receive -F #{dataset}"
46
- end
47
-
48
- def transmit!
49
- if ssh_host
50
- run_command! "zfs send #{options} #{snapshot} | ssh -o stricthostkeychecking=no #{ssh_host} zfs receive -F #{dataset}"
51
- else
52
- run_command! "zfs send #{options} #{snapshot} | zfs receive -F #{dataset}"
53
- end
54
- end
55
-
56
- def snapshot!
57
- destroy!
58
- run_command! "zfs snapshot #{snapshot}"
59
- end
60
-
61
- def destroy!
62
- `zfs list -t snapshot | grep #{snapshot}`
63
- if $?.exitstatus == 0
64
- run_command! "zfs destroy #{snapshot}"
65
- end
66
- end
67
-
68
- def rollback!
69
- run_command! "zfs rollback -r #{snapshot}"
70
- end
71
-
72
- def clone!
73
- run_command! "zfs clone #{snapshot} #{dataset}"
74
- end
75
-
76
- def validate!(res)
77
- if validator.is_a? Proc
78
- if validator.call(res)
79
- res
80
- else
81
- raise "ERROR: execute resource \"#{name}\" failed validation!"
82
- end
83
- else
84
- res
85
- end
86
- end
87
-
88
- def run_command!(cmd, expect_code=0)
89
- res = `#{cmd}`
90
- code = $?.exitstatus
91
- if code != expect_code
92
- raise Hookit::Error::UnexpectedExit, "#{cmd} failed with exit code '#{code}'"
93
- end
94
- return validate!(res)
95
- end
96
-
97
- end
98
- end
99
- end
data/lib/hookit/setup.rb DELETED
@@ -1,59 +0,0 @@
1
- # This file is meant ONLY as a way to make a hookit hook directly executable.
2
- #
3
- # Normally hookit is bootstrapped via `hookit hook-name "payload"`.
4
- #
5
- # This script allows a ruby executable script to bootstrap hookit and run as
6
- # a hook directly.
7
- #
8
- # Usage:
9
- #
10
- # #!/usr/bin/env ruby
11
- #
12
- # # optionally, set some configuration
13
- # # (if not set, MODULE_ROOT will default to the directory if this script)
14
- #
15
- # $LOG_LEVEL = :error
16
- # $LOGFILE = '/var/log/hookit/hookit.log'
17
- # $MODULE_DIR = "/opt/local/hookit/mod"
18
- #
19
- # # load hookit/setup to bootstrap hookit
20
- # require 'hookit/setup'
21
- #
22
- # execute 'list all the files!' do
23
- # command 'ls -lah /'
24
- # end
25
- #
26
-
27
- # This won't handle every scenario, but essentially it tries to find the
28
- # location of the script that was executed
29
- hook = begin
30
- if $0[0] == '/'
31
- $0
32
- else
33
- "#{Dir.getwd}/#{$0}"
34
- end
35
- end
36
-
37
- module_dir = File.dirname(hook)
38
-
39
- MODULE_DIR = $MODULE_DIR || ENV['MODULE_DIR'] || module_dir
40
- LOG_LEVEL = $LOG_LEVEL || ENV['LOG_LEVEL'] || :error
41
- LOGFILE = $LOGFILE || ENV['LOGFILE'] || '/var/log/hookit/hookit.log'
42
-
43
- require 'hookit'
44
- require 'json'
45
-
46
- include Hookit::Hook # payload helpers / resource dsl
47
-
48
- set :log_level, LOG_LEVEL
49
- set :logfile, LOGFILE
50
- set :module_root, MODULE_DIR
51
-
52
- # require hook libs
53
- Dir.glob("#{MODULE_DIR}/lib/*.rb").each do |file|
54
- require file
55
- end
56
-
57
- logger.info ""
58
- logger.info "hook: #{hook}"
59
- logger.info "payload: #{payload.to_json}"