Class: Chef::WhiskGenerate

Inherits:
Knife
  • Object
show all
Includes:
Knife::WhiskBase
Defined in:
lib/chef/knife/knife-whisk.rb

Instance Method Summary collapse

Methods included from Knife::WhiskBase

#add_quotes, #exit_with_message, #get_config, #get_security_groups, included, #mixin_exists?, #security_group_exists?, #server_exists?

Instance Method Details

#runObject



139
140
141
142
143
144
145
146
147
148
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
# File 'lib/chef/knife/knife-whisk.rb', line 139

def run
  exit_with_message("Required whisk.yml does not exist") unless get_config
  
  full_hash = get_config
        
  if name_args.size == 0
    server_config = full_hash["mixins"]["defaults"]
    server_mixins = ["defaults"]
  elsif server_exists?(name_args.first)
    server_mixins = full_hash["servers"][name_args.first]["mixins"]
    server_config = full_hash["servers"][name_args.first]["config"]
  else
    exit_with_message("#{name_args.first} server template does not exist")
  end
  
  unless @config[:mixins].nil?
    server_mixins = server_mixins + @config[:mixins]
  end

  #checks to make sure all mixins exist
  server_mixins.each { |mixin| exit_with_message("#{mixin} mixin does not exist") unless mixin_exists?(mixin) }

  #merges together all mixin config values with server config values
  output_hash = server_mixins.inject(Hash.new) {|output, mixin| output.merge(full_hash["mixins"][mixin])}.merge(server_config)
  
  #convert config values that are arrays to comma seperates strings
  output_hash.each do |mixin, value|
    if value.kind_of?(Array)
      output_hash[mixin] = value.join(",")
    end
  end
  
  unless @config[:overrides].nil?
    exit_with_message("Please use long form flags only in overrides") unless @config[:overrides].kind_of?(Hash)
    output_hash = output_hash.merge(@config[:overrides])
  end
  
  #check things for aws
  if output_hash["provider"]["aws"]
    #convert security-group names to ids if needed and make sure they exist in the lookup hash
    unless output_hash["security-groups"].nil?
      exit_with_message("security-groups not defined for this config in whisk.yml") unless get_config["provider_config"]["aws"]["security-groups"]
      output_hash["security-groups"].split(',').each { |group| exit_with_message("#{group} security group does not exist in whisk.yml") unless security_group_exists?(group)}
      output_hash["security-group-ids"] = get_security_groups(output_hash["security-groups"])
      output_hash.delete("security-groups")
    end
  end
  
  #some values need quotes for knife ec2 to accept the arg
  output_hash["run-list"] = add_quotes(output_hash["run-list"]) unless output_hash["run-list"].nil?
  # json doesn't work currently output_hash["json-attributes"] = add_quotes(output_hash["json-attributes"]) unless output_hash["json-attributes"].nil?      

  #get config string and check to make sure it exists
  exit_with_message("provider attribute must be provided") unless output_hash["provider"]
  exit_with_message("#{output_hash["provider"]} cli_command doesn't exist in whisk.yml") unless full_hash["provider_config"][output_hash["provider"]]["cli_command"]
  cli_command = full_hash["provider_config"][output_hash["provider"]]["cli_command"]

  output_hash.delete("provider")

  printf "knife %s %s\n", cli_command, output_hash.map { |key, value| ["--"+key, value] }.join(" ")
end