Class: ZZDeploy

Inherits:
Object
  • Object
show all
Includes:
Subcommands
Defined in:
lib/zz_deploy.rb

Constant Summary collapse

CMD =
"zz"
RECIPES_DIR =
"/var/chef/cookbooks/zz-chef-repo"
RECIPES_BUNDLE_DIR =
"/var/chef/cookbooks/zz-chef-repo_bundle"

Instance Method Summary collapse

Instance Method Details

#define_sub_commandsObject

define sub commands here



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zz_deploy.rb', line 40

def define_sub_commands
  sub_commands[:deploy_group_create] = Commands::DeployGroupCreate.new
  sub_commands[:deploy_group_delete] = Commands::DeployGroupDelete.new
  sub_commands[:deploy_group_list] = Commands::DeployGroupList.new
  sub_commands[:deploy_group_modify] = Commands::DeployGroupModify.new
  sub_commands[:add] = Commands::AddInstance.new
  sub_commands[:delete] = Commands::DeleteInstances.new
  sub_commands[:list] = Commands::ListInstances.new
  sub_commands[:deploy] = Commands::DeployInstances.new
  sub_commands[:maint] = Commands::MaintInstances.new
  sub_commands[:ssh] = Commands::SSHInstance.new
  sub_commands[:multi_ssh] = Commands::MultiSSHInstance.new
  sub_commands[:chef_upload] = Commands::ChefUpload.new
  sub_commands[:chef_bake] = Commands::ChefBake.new
  sub_commands[:config_amazon] = Commands::ConfigAmazon.new
end

#force_fail(cmd = nil) ⇒ Object



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

def force_fail(cmd = nil)
  # force failure and show options
  ARGV.clear
  ARGV << "help"
  ARGV << cmd unless cmd.nil?
  opt_parse
end

#optionsObject

the options that were set



27
28
29
# File 'lib/zz_deploy.rb', line 27

def options
  @options ||= {}
end

#parseObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/zz_deploy.rb', line 125

def parse
  if ARGV.empty?
    ARGV << "help"
  end

  cmd = opt_parse()
  if cmd.nil?
    force_fail
  end

  # ok, we have a valid command so dispatch it
#    puts "cmd: #{cmd}"
#    puts "options ......"
#    p options
#    puts "ARGV:"
#    p ARGV

  sub_cmd = sub_commands[cmd.to_sym]
  validate(cmd, sub_cmd)

  # track both types of options
  ZZSharedLib::Options.global_options = options
  ZZSharedLib::Options.cmd_options = sub_cmd.options

  # dispatch the command
  amazon = ZZSharedLib::Amazon.new

  sub_cmd.send("run", options, amazon)
end


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/zz_deploy.rb', line 177

def print_actions
  cmdtext = "Commands are:"
  @commands.sort.map do |c, opt|
    #puts "inside opt.call loop"
    desc = opt.call.description
    cmdtext << "\n   #{c} : #{desc}"
  end

  # print aliases
  unless @aliases.empty?
    cmdtext << "\n\nAliases: \n"
    @aliases.each_pair { |name, val| cmdtext << "   #{name} - #{val}\n"  }
  end

  cmdtext << "\n\nSee '#{CMD} help COMMAND' for more information on a specific command."
end

#printerObject



35
36
37
# File 'lib/zz_deploy.rb', line 35

def printer
  @printer ||= Printer.new
end

#required_optionsObject

required options



21
22
23
24
# File 'lib/zz_deploy.rb', line 21

def required_options
  @required_options ||= Set.new [
  ]
end

#run(argv = ARGV) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/zz_deploy.rb', line 157

def run(argv = ARGV)
  exit_code = true
  begin
    setup
    parse
  rescue SystemExit => ex
    # ignore direct calls to exit
  rescue Exception => ex
    printer.error printer.color(ex.message, :red)
#      puts ex.backtrace
    exit_code = false
  end

  # make sure buffer is flushed
  # debugger doesn't seem to do this always
  STDOUT.flush

  return exit_code
end

#set_amazon_optionsObject



116
117
118
119
120
121
122
123
# File 'lib/zz_deploy.rb', line 116

def set_amazon_options
  json = File.open("/var/chef/amazon.json", 'r') {|f| f.read }
  ak = JSON.parse(json)
  options[:access_key] = ak["aws_access_key_id"]
  options[:secret_key] = ak["aws_secret_access_key"]
rescue
  # do nothing
end

#setupObject



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
# File 'lib/zz_deploy.rb', line 57

def setup
  options.clear
  set_amazon_options
  # global options
  global_options do |opts|
    opts.banner = "Version: #{Info.version} - Usage: #{CMD} [options] [subcommand [options]]"
    opts.description = "ZangZing configuration and deploy tool.  You must specify a valid sub command."
    opts.separator ""
    opts.separator "Global options are:"
    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options[:verbose] = v
    end

    opts.on("--akey AmazonAccessKey", "Amazon access key, or environment AWS_ACCESS_KEY_ID.") do |v|
      options[:access_key] = v
    end

    opts.on("--skey AmazonSecretKey", "Amazon secret key or environment AWS_SECRET_ACCESS_KEY") do |v|
      options[:secret_key] = v
    end

  end
  add_help_option

  define_sub_commands

  sub_commands.each_pair do |command_name, sub_cmd|
    command command_name do |opts|
      sub_cmd.send("register", opts, options)
    end
  end

end

#sub_commandsObject



31
32
33
# File 'lib/zz_deploy.rb', line 31

def sub_commands
  @sub_commands ||= {}
end

#validate(cmd, sub_cmd) ⇒ Object

validate the options passed



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/zz_deploy.rb', line 100

def validate(cmd, sub_cmd)
  required_options.each do |r|
    if options.has_key?(r) == false
      puts "Missing options"
      force_fail
    end
  end

  sub_cmd.required_options.each do |r|
    if sub_cmd.options.has_key?(r) == false
      puts "Missing options"
      force_fail(cmd)
    end
  end
end