Class: Chef2Wiki

Inherits:
Object
  • Object
show all
Defined in:
lib/chef2wiki/base.rb,
lib/chef2wiki/chef.rb,
lib/chef2wiki/cookbook.rb,
lib/chef2wiki/mediawiki.rb

Constant Summary collapse

GEM_DATADIR =
Gem.datadir("chef2wiki") || "data"
GEM_HOMEDIR =
File.join(ENV["HOME"], ".chef2wiki")
DEFAULT_CONFIG_PATH =
File.join(GEM_HOMEDIR, "config.yml")
DEFAULT_TEMPLATES_PATH =
File.join(GEM_HOMEDIR, "templates")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Chef2Wiki

Returns a new instance of Chef2Wiki.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/chef2wiki/base.rb', line 10

def initialize(options = {})
  options[:config] ||= DEFAULT_CONFIG_PATH
  options[:templates] ||= DEFAULT_TEMPLATES_PATH
  options[:wiki] ||= :mediawiki

  @options = options
  @config = read_config(File.expand_path(options[:config]))
  @templates = register_templates(File.expand_path(options[:templates]))
  @wiki = case options[:wiki]
  when :mediawiki
    setup_media_wiki
  end

  Chef::Config.from_file(File.expand_path(config["chef"]["config"]))
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/chef2wiki/base.rb', line 3

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/chef2wiki/base.rb', line 3

def options
  @options
end

#templatesObject (readonly)

Returns the value of attribute templates.



3
4
5
# File 'lib/chef2wiki/base.rb', line 3

def templates
  @templates
end

#wikiObject (readonly)

Returns the value of attribute wiki.



3
4
5
# File 'lib/chef2wiki/base.rb', line 3

def wiki
  @wiki
end

Instance Method Details

#add_page(title, content, overwrite = true) ⇒ Object

Add page to MediaWiki

Attributes

  • title - Title of the page (String)

  • content - Content of the page (String)

  • overwrite - Overwrite flag (Boolean)



9
10
11
12
13
14
15
16
17
# File 'lib/chef2wiki/mediawiki.rb', line 9

def add_page(title, content, overwrite = true)
  begin
    print "[MediaWiki]\tCreating page #{title}... "
    wiki.create(title, content, :overwrite => overwrite)
    puts "done."
  rescue Exception => ex
    puts "[MediaWiki]\tError adding page #{title}: " + ex.message.to_s
  end
end

#excluded_node_listObject

Get excluded node list.

Returns

  • Node list (Array)



28
29
30
# File 'lib/chef2wiki/chef.rb', line 28

def excluded_node_list
  config["nodes"] ? config["nodes"]["exclude"] : []
end

#generate_docsObject

This requires markdown extension for mediawiki.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chef2wiki/cookbook.rb', line 5

def generate_docs
  if config["cookbooks"]["repo_paths"].any?
    puts "[Chef2Wiki]\tCreating documentation..."
    @cookbooks = []

    config["cookbooks"]["repo_paths"].each do |repo_path|
      doc_files = File.join(repo_path, "**", "README.md")

      Dir.glob(File.expand_path(doc_files)).sort.each do |file|
        cookbook = file.gsub("#{File.expand_path(repo_path)}/", "").gsub("/README.md", "")
        if cookbook != "README.md"
          @cookbooks += [cookbook]
          @content = File.read(file)
          add_page(cookbook, templates["documentation"].result(binding))
        end
      end
    end

    add_page("Cookbooks_documentation", templates["documentation_list"].result(binding))
  end
end

#node_data(node_name) ⇒ Object

Get data for node.

Attributes

  • node_name - Name of the node from chef (String)

Returns

  • Node data (Hash)



9
10
11
12
13
14
# File 'lib/chef2wiki/chef.rb', line 9

def node_data(node_name)
  node = Chef::Node.load(node_name)
  data = node.to_hash
  data["run_list"] = node.run_list.run_list_items
  return data
end

#node_listObject

Get node list.

Returns

  • Node list (Array)



20
21
22
# File 'lib/chef2wiki/chef.rb', line 20

def node_list
  Chef::Node.list.keys
end

#render_node(node) ⇒ Object

Render node.

Attributes

  • node - Node name (String)



30
31
32
33
34
# File 'lib/chef2wiki/base.rb', line 30

def render_node(node)
  puts "[Chef2Wiki]\tWorking for node #{node}..."
  @data = node_data(node)
  templates["node"].result(binding)
end