lib/stack.rb in stack-kicker-0.0.19 vs lib/stack.rb in stack-kicker-0.0.20

- old
+ new

@@ -62,12 +62,12 @@ Logger.info { " #{name}" } end end def Stack.show_stack(config) - # syntax_check is a light weight check that doesn't talk to OpenStalk - Stack.syntax_check(config) + # check_config is a light weight check that doesn't talk to OpenStalk + Stack.check_config(config) # generate an array of hostnames that this stack would create hostnames = Stack.generate_server_names(config) hostnames.each { |hostname| Logger.info " #{hostname}" } end @@ -128,11 +128,11 @@ end address_description end # check that all the required config items are set - def Stack.syntax_check(config) + def Stack.check_config(config) if config['REGION'].nil? || config['USERNAME'].nil? || config['PASSWORD'].nil? || config['AUTH_URL'].nil? || config['TENANT_NAME'].nil? && config['REGION'].empty? || config['USERNAME'].empty? || config['PASSWORD'].empty? || config['AUTH_URL'].empty? || config['TENANT_NAME'].empty? Logger.error { "REGION, USERNAME, PASSWORD, AUTH_URL & TENANT_NAME must all be set" } exit end @@ -160,12 +160,15 @@ end # validate that all our OpenStack creds, image_id, flavors, keys etc are valid def Stack.validate(config) - Stack.syntax_check(config) + Stack.check_config(config) + # populate the config & then walk through the AZs verifying the config + Stack.populate_config(config) + # check that the ssh-key is loaded, otherwise most post-install scripts will fail # this lazily assumes that the :key_pair name matches the file the keys were loaded # from if (0 == 1) ssh_keys_loaded = Stack.shellout(config, "ssh-add -L") @@ -178,13 +181,10 @@ Logger.erroLogger.error "ssh_keys_loaded: #{ssh_keys_loaded}" exit 2 end end - # populate the config & then walk through the AZs verifying the config - Stack.populate_config(config) - # Check that we have valid details for each AZ config[:azs].each do |az| # check that credentials, flavor & image are valid os = connect(config, az) @@ -227,12 +227,13 @@ end end def Stack.generate_knife_rb(config) # generate a project/.chef/knife.rb from our config - # (assumes the chef server is running for public IP access etc) + Stack.check_config(config) + # find the chef server, if we need to if config[:chef_server_hostname].nil? || config[:chef_server_private].nil? || config[:chef_server_public] Logger.debug { "Attempting to discover the chef server details" } ours = Stack.get_our_instances(config) ours.each do |node, node_details| @@ -297,11 +298,11 @@ hostname end def Stack.generate_server_names(config) - Stack.populate_config(config) + Stack.check_config(config) config[:hostnames] = config[:node_details].keys config[:hostnames] end def Stack.populate_config(config) @@ -330,11 +331,13 @@ if config[:chef_validation_pem].nil? Logger.warn { "Defaulting to .chef/validation.pem for config[:chef_validation_pem]" } config[:chef_validation_pem] = '.chef/validation.pem' end - config[:chef_validation_pem] = Stack.find_file(config, config[:chef_validation_pem]) + chef_validation_pem_abs = Stack.find_file(config, config[:chef_validation_pem]) + # only store the abs if we found it... + config[:chef_validation_pem] = chef_validation_pem_abs unless chef_validation_pem_abs.nil? if config[:name_template].nil? Logger.warn { "Defaulting to '%s-%s-%s%04d' for config[:name_template]" } config[:name_template] = '%s-%s-%s%04d' end @@ -439,11 +442,11 @@ # get all instances running in the current config # return a hash where key is the instance name, value is another hash containing :region, :id, :addresses def Stack.get_all_instances(config, refresh = false) if config[:all_instances].nil? || refresh # we need to get the server list for each AZ mentioned in the config[:roles][:role][:azs], this is populated by Stack.populate_config - Stack.populate_config(config) + Stack.check_config(config) # get the current list of servers from OpenStack & generate a hash, keyed on name servers = Hash.new config[:azs].each do |az| os = Stack.connect(config, az) @@ -521,13 +524,20 @@ ours.each do |node, node_details| printf("%-30s %20s %8d %16s %s\n", node, node_details[:region], node_details[:id], node_details[:role], node_details[:addresses].map { |address| address.address }) end end + # ssh to a host, or all hosts def Stack.ssh(config, hostname = nil, user = ENV['USER'], command = nil) - # ssh to a host, or all hosts + # check that we have something to run + if command.nil? + Logger.error "No command passed to Stack.ssh!" + end + # sanity check our config, load defaults etc + Stack.check_config(config) + # get all running instances servers = Stack.get_our_instances(config) if hostname.nil? Logger.debug { "request to SSH to all hosts" } @@ -544,11 +554,11 @@ def Stack.get_our_instances(config) Logger.debug { ">>> Stack.get_our_instances" } # build an hash of running instances that match our generated hostnames - node_details = Stack.populate_config(config) + Stack.check_config(config) # get all of our hostnames hostnames = Stack.generate_server_names(config) # get all running instances @@ -557,22 +567,22 @@ running = Hash.new # do any of the list of servers in OpenStack match one of our hostnames? hostnames.each do |hostname| if (servers.include?(hostname)) # return the instance details merged with the node_details (info like role) - running[hostname] = servers[hostname].merge(node_details[hostname]) + running[hostname] = servers[hostname].merge(config[:node_details][hostname]) end end Logger.debug { "#{running}" } Logger.debug { "<<< Stack.get_our_instances" } running end def Stack.delete_node(config, node) # this also populates out unspecified defaults, like az - Stack.populate_config(config) + Stack.check_config(config) # get info about all instances running in our account & AZs Stack.get_all_instances(config) if (config[:all_instances][node].nil?) Logger.info "Sorry, #{node} doesn't exist or isn't running" @@ -583,14 +593,12 @@ instance.delete! end end def Stack.delete_all(config) - # check that we have OS_* vars loaded etc - Stack.syntax_check(config) # this also populates out unspecified defaults, like az - Stack.populate_config(config) + Stack.check_config(config) # get the list of nodes we consider 'ours', i.e. with hostnames that match # those generated by this stack ours = Stack.get_our_instances(config) @@ -905,9 +913,10 @@ end end if filename_fqp.empty? Logger.warn "couldn't find #{filename} in #{dirs}" + filename_fqp = nil end filename_fqp end def Stack.shellout(config, command, cwd=nil)