Class: Deadpool::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/deadpool/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Generator

include FileUtils



11
12
13
# File 'lib/deadpool/generator.rb', line 11

def initialize(argv)
  @argv     = argv
end

Instance Method Details

#execute_command(options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/deadpool/generator.rb', line 80

def execute_command(options)
  case options[:command]
  when :upstart_init
    generate_upstart_init options
  when :configuration
    generate_configuration options
  else
    help
  end
end

#generate_configuration(options) ⇒ Object

mkdir path/config/pools

path/config/environment.yml
path/config/pools/example.yml

mkdir path/lib/deadpool/monitor

path/lib/deadpool/monitor

mkdir path/lib/deadpool/failover_protocol

path/lib/deadpool/failover_protocol


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
# File 'lib/deadpool/generator.rb', line 149

def generate_configuration(options)
  config_path = options[:config_path]
  path        = config_path
  FileUtils.mkdir_p(File.join(path, 'config/pools'))
  FileUtils.mkdir_p(File.join(path, 'lib/deadpool/monitor'))
  FileUtils.mkdir_p(File.join(path, 'lib/deadpool/failover_protocol'))
  File.open File.join(path, 'config/pools/example.yml'), 'w' do |file|
    file.write <<-EOF
pool_name:         'example_database'
primary_host:      '10.1.2.3'
secondary_host:    '10.2.3.4'
check_interval:    1
max_failed_checks: 10

# There can be only one monitor per pool at this time.  The deadpool system
# defines no rules for the monitor configuration except that it is called
# monitor_config: and has monitor_class: defined at the base level.  
# All other configuration variables are plugin specific.
monitor_config:
  monitor_class: Mysql
  nagios_plugin_path: '/usr/lib/nagios/plugins'

# There can be as many Failover Protocols as you want and you can use 
# the same plugin multiple times.  The deadpool defines no riles for the 
# failover protocol config except that it be an array element of 
# failover_protocol_configs and defines protocol_class at it's base.  The rest
# of the configuration is specific to the failover protocol.
failover_protocol_configs:
  - protocol_class: EtcHosts
script_path: '/usr/local/bin/deadpool_line_modifier'
service_host_name: 'master.mysql.example.project.client'
username: 'deadpool'
password: 'p4ssw0rd'
use_sudo: 1
client_hosts:
  - '10.3.4.5'   # app server 1 (web server)
  - '10.4.5.6'   # app server 2 (web server)

  - protocol_class: ExecRemoteCommand
test_command: '/etc/init.d/nginx status'
exec_command: '/etc/init.d/nginx restart'
username: 'deadpool'
password: 'p4ssw0rd'
use_sudo: 1
client_hosts:
  - '10.3.4.5'   # app server 1 (web server)
  - '10.4.5.6'   # app server 2 (web server)
    EOF
  end
  
  environment_config_path = File.join(path, 'config/environment.yml')
  environment_conf = <<-EOF
# log_path: '/var/log/deadpool.log'
# log_level: INFO
# system_check_interval: 30
# admin_hostname: 'localhost'
# admin_port: 5507

  EOF
  # if File.exists? environment_config_path
  #   puts "#{environment_config_path} already exists.  Here's what we would have copied there."
  #   puts environment_conf
  # else
  unless File.exists? environment_config_path
    File.open environment_config_path, 'w' do |file|
      file.write environment_conf
    end
  end
  
end

#generate_upstart_init(options) ⇒ Object



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
# File 'lib/deadpool/generator.rb', line 99

def generate_upstart_init(options)
  config_path         = options[:config_path]
  upstart_config_path = options[:upstart_config_path]
  upstart_init_path   = options[:upstart_init_path]
  upstart_script_path = options[:upstart_script_path]
  config_params       = config_path.nil? ? '' : "--config_path=#{config_path}"
  ruby_path           = `which ruby`.strip
  deadpool_admin_path = `which deadpool_admin`.strip
  
  upstart_conf =<<-EOF
description     "Deadpool Service"
author          "Kirt Fitzpatrick <[email protected]>"

umask 007
start on (net-device-up and local-filesystems)
stop on runlevel [016]
respawn
exec #{ruby_path} #{deadpool_admin_path} --foreground #{config_params}

  EOF

  if upstart_config_path
    File.open File.join(upstart_config_path), 'w' do |file|
      file.write upstart_conf
    end
    
    if upstart_init_path 
      if File.exists? upstart_init_path
        puts "#{upstart_config_path} has been (over)written."
        puts "#{upstart_init_path} already exists.  It should be a symbolic link that points to #{upstart_script_path}"
        ls_command = "ls -l #{upstart_init_path}"
        # puts ls_command
        # puts `#{ls_command}`
        # puts "ln -s #{upstart_script_path} #{upstart_init_path}"
      else
        `ln -s #{upstart_script_path} #{upstart_init_path}`
      end
    end
  else
    puts upstart_conf
  end
end

#help(message = nil) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/deadpool/generator.rb', line 91

def help(message=nil)
  unless message.nil?
    puts message
  end
  puts @option_parser.help
  exit 4
end

#parse_command_lineObject



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
# File 'lib/deadpool/generator.rb', line 22

def parse_command_line
options                       = Hash.new
options[:command_count]       = 0
options[:config_path]         = '/etc/deadpool'
options[:upstart_config_path] = '/etc/init/deadpool.conf'
options[:upstart_init_path]   = '/etc/init.d/deadpool'
options[:upstart_script_path] = '/lib/init/upstart-job'

@option_parser = OptionParser.new do |opts|
  opts.banner = "Usage: deadpool_generator command [options]"

  opts.separator "Commands:"
  opts.on("-h", "--help", "Print this help message.") do |help|
    options[:command_count] += 1
    options[:command]        = :help
  end
  opts.on("--upstart_init", "Generate and upstart config.") do |upstart|
    options[:command_count] += 1
    options[:command]        = :upstart_init
  end
  opts.on("--configuration", "Generate a config directory structure and example files.") do |configuration|
    options[:command_count] += 1
    options[:command]        = :configuration
  end

  opts.separator "Configuration Options:"
  opts.on("--config_path=PATH", String, "path to create the config dir at (#{options[:config_path]})") do |config_path|
    options[:config_path] = config_path
  end

  opts.separator "Upstart Options:"
  opts.on("--upstart_config_path=PATH", String, "path to create the config dir at (#{options[:upstart_config_path]})") do |upstart_config_path|
    options[:upstart_config_path] = upstart_config_path
  end
  opts.on("--upstart_init_path=PATH", String, "path to create the config dir at (#{options[:upstart_init_path]})") do |upstart_init_path|
    options[:upstart_init_path] = upstart_init_path
  end
  opts.on("--upstart_script_path=PATH", String, "path to create the config dir at (#{options[:upstart_script_path]})") do |upstart_script_path|
    options[:upstart_script_path] = upstart_script_path
    unless File.exists? upstart_script_path
      help "The upstart script is not at #{upstart_script_path}."
    end
  end
end

  remaining_arguments = @option_parser.parse! @argv

  unless remaining_arguments.empty?
    help "[#{remaining_arguments.join(' ')}] is not understood."
  end

  if options[:command_count] == 0
    help "You must specify a command."
  end

  return options
end

#runObject



15
16
17
18
19
20
# File 'lib/deadpool/generator.rb', line 15

def run
  @options = self.parse_command_line
  @config  = Deadpool::Helper.configure @options

  self.execute_command(@options)
end