Sha256: 078621feb8a20201d3f9578f9ed50492e3e77306248a32453e4eb09d58f327f4

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

module KnifeSolo
  module NodeConfigCommand

    def self.load_deps
      require 'pathname'
    end

    def self.included(other)
      other.class_eval do
        # Lazy load our dependencies if the including class did not call
        # Knife#deps yet. See KnifeSolo::SshCommand for more information.
        deps { KnifeSolo::NodeConfigCommand.load_deps } unless @dependency_loader

        option :chef_node_name,
          :short       => '-N NAME',
          :long        => '--node-name NAME',
          :description => 'The Chef node name for your new node'

        option :run_list,
          :short       => '-r RUN_LIST',
          :long        => '--run-list RUN_LIST',
          :description => 'Comma separated list of roles/recipes to put to node config (if it does not exist)',
          :proc        => lambda { |o| o.split(/[\s,]+/) },
          :default     => []

        option :json_attributes,
          :short       => '-j JSON_ATTRIBS',
          :long        => '--json-attributes',
          :description => 'A JSON string to be added to node config (if it does not exist)',
          :proc        => lambda { |o| JSON.parse(o) },
          :default     => nil
      end
    end

    def node_config
      # host method must be defined by the including class
      Pathname.new(@name_args[1] || "nodes/#{config[:chef_node_name] || host}.json")
    end

    def generate_node_config
      if node_config.exist?
        Chef::Log.debug "Node config '#{node_config}' already exists"
      else
        ui.msg "Generating node config '#{node_config}'..."
        File.open(node_config, 'w') do |f|
          attributes = config[:json_attributes] || config[:first_boot_attributes] || {}
          run_list = { :run_list => config[:run_list] || [] }
          f.print attributes.merge(run_list).to_json
        end
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
knife-solo-0.3.0.pre3 lib/knife-solo/node_config_command.rb
knife-solo-0.3.0.pre2 lib/knife-solo/node_config_command.rb
knife-solo-0.3.0.pre1 lib/knife-solo/node_config_command.rb
knife-solo-0.2.0 lib/knife-solo/node_config_command.rb
knife-solo-0.2.0.pre1 lib/knife-solo/node_config_command.rb