chef2wiki 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 438be1b605ce6823c9b6e9bd118ae1fd73511a08
4
+ data.tar.gz: b895ce7dda352b90a10e63a235d3234f4d89a396
5
+ SHA512:
6
+ metadata.gz: d698d29e42806f541f1afae7b7d55977b889091dd1ed385b9954703081c6489b129d38a1c86aa10ac0d43d62486c34c492db2865d3993f0a2a1337f2ddc1ebd3
7
+ data.tar.gz: 0fc34fb07b9c7bc1856b8a545f37b0ea242fad741d24ef116674fb6dd7261a23915c527ded9fa4e486ec945280ac03446da40fc2864342832125ca25a94429a7
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Ivan Larionov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ # chef2wiki
2
+
3
+ Generates wiki documentation from Chef node attributes using ERB templates.
4
+
5
+ ## Quick start:
6
+
7
+ On the first launch chef2wiki creates default config and templates files at `~/.chef2wiki`.
8
+
9
+ ### Configure wiki parameters:
10
+ `~/.chef2wiki/config.yml` example:
11
+
12
+ ```
13
+ wiki:
14
+ api_url: "http://my.wiki.org/api.php"
15
+ login: Admin
16
+ password: Password
17
+ # domain: my_ldap
18
+
19
+ chef:
20
+ config: ~/.chef/knife.rb
21
+
22
+ cookbooks:
23
+ repo_paths:
24
+ - "~/chef/cookbooks/repo/cookbooks"
25
+ - "~/chef/cookbooks/repo/site-cookbooks"
26
+
27
+ nodes:
28
+ exclude: ["old_node.example.com", "node2.example.com"]
29
+ ```
30
+
31
+ - change wiki api url to yours
32
+ - change login and password if you need it (or just remove if not)
33
+ - provide a path to knife/chef config
34
+ - provide a path(s) to cookbooks if you want README.md files being posted to wiki as well
35
+ - you can exclude nodes from being processed
36
+ - execute chef2wiki
37
+
38
+ LDAP domain support requires http://www.mediawiki.org/wiki/Extension:LDAP_Authentication
39
+
40
+ ## Templates system
41
+
42
+ Templates are located at `~/.chef2wiki/templates/`. Required templates are:
43
+
44
+ | Template | Wiki page | Variables |
45
+ | -------- | --------- | --------- |
46
+ | `node.erb` – main node template | As node name | `@data` – node attributes (Hash) |
47
+ | `nodes.erb` – nodes list | `Servers_list` | `@node_list` – list of nodes (Array of Strings) |
48
+ | `documentation.erb` – template for README.md | As cookbook name | `@content` – content of README.md |
49
+ | `documentation_list.erb` – cookbooks list | `Cookbooks_documentation` | `@cookbooks` – list of cookbooks (Array) |
50
+
51
+ You can use partials. For example default `node.erb` calls `node/common.erb`: `<%= render 'node/common.erb' %>`
52
+
53
+ ## Contributing to chef2wiki
54
+
55
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
56
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
57
+ * Fork the project.
58
+ * Start a feature/bugfix branch.
59
+ * Commit and push until you are happy with your contribution.
60
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
61
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
62
+
63
+ ## Copyright
64
+
65
+ Copyright (c) 2014 Ivan Larionov. See LICENSE for further details.
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rubygems/package_task'
3
+
4
+ gemspec = eval(File.read("chef2wiki.gemspec"))
5
+ Gem::PackageTask.new(gemspec).define
6
+
7
+ require 'rdoc/task'
8
+ Rake::RDocTask.new do |rdoc|
9
+ version = File.exist?("VERSION") ? File.read("VERSION") : ""
10
+
11
+ rdoc.rdoc_dir = "rdoc"
12
+ rdoc.title = "chef2wiki #{version}"
13
+ rdoc.rdoc_files.include("README*")
14
+ rdoc.rdoc_files.include("lib/**/*.rb")
15
+ end
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "chef2wiki"
4
+
5
+ c2w = Chef2Wiki.new
6
+
7
+ @node_list = c2w.node_list
8
+ @node_list.each do |node|
9
+ unless c2w.excluded_node_list.include?(node)
10
+ c2w.add_page(node, c2w.render_node(node))
11
+ else
12
+ puts "[Chef2Wiki]\tNode #{node} exluded by configuration."
13
+ end
14
+ end
15
+
16
+ # Create other pages
17
+ c2w.add_page("Servers_list", c2w.templates["nodes"].result)
18
+
19
+ # Create documentation pages
20
+ c2w.generate_docs
@@ -0,0 +1,11 @@
1
+ require 'chef'
2
+ require 'yaml'
3
+ require 'json'
4
+ require 'erb'
5
+ require 'set'
6
+ require 'media_wiki'
7
+
8
+ require 'chef2wiki/base'
9
+ require 'chef2wiki/chef'
10
+ require 'chef2wiki/cookbook'
11
+ require 'chef2wiki/mediawiki'
@@ -0,0 +1,124 @@
1
+ class Chef2Wiki
2
+
3
+ attr_reader :wiki, :config, :templates, :options
4
+
5
+ GEM_DATADIR = Gem.datadir("chef2wiki") || "data"
6
+ GEM_HOMEDIR = File.join(ENV["HOME"], ".chef2wiki")
7
+ DEFAULT_CONFIG_PATH = File.join(GEM_HOMEDIR, "config.yml")
8
+ DEFAULT_TEMPLATES_PATH = File.join(GEM_HOMEDIR, "templates")
9
+
10
+ def initialize(options = {})
11
+ options[:config] ||= DEFAULT_CONFIG_PATH
12
+ options[:templates] ||= DEFAULT_TEMPLATES_PATH
13
+ options[:wiki] ||= :mediawiki
14
+
15
+ @options = options
16
+ @config = read_config(File.expand_path(options[:config]))
17
+ @templates = register_templates(File.expand_path(options[:templates]))
18
+ @wiki = case options[:wiki]
19
+ when :mediawiki
20
+ setup_media_wiki
21
+ end
22
+
23
+ Chef::Config.from_file(File.expand_path(config["chef"]["config"]))
24
+ end
25
+
26
+ # Render node.
27
+ #
28
+ # ==== Attributes
29
+ # * +node+ - Node name (String)
30
+ def render_node(node)
31
+ puts "[Chef2Wiki]\tWorking for node #{node}..."
32
+ @data = node_data(node)
33
+ templates["node"].result(binding)
34
+ end
35
+
36
+ private
37
+
38
+ # Read chef2wiki config file. Create default config if missing.
39
+ #
40
+ # ==== Attributes
41
+ # * +path+ - path for chef2wiki config file
42
+ # ==== Returns
43
+ # * Config data (Hash)
44
+ def read_config(path)
45
+ unless File.exist?(path)
46
+ puts "[Chef2Wiki]\tCreating config file at #{path}..."
47
+ FileUtils.mkdir_p(File.dirname(path))
48
+ FileUtils.cp(File.join(GEM_DATADIR, "config.yml"), path)
49
+ end
50
+
51
+ YAML::load_file(path)
52
+ end
53
+
54
+ # Register templates from templates folder. Create default templates if missing.
55
+ #
56
+ # ==== Attributes
57
+ # * +path+ - path for erb templates
58
+ # ==== Returns
59
+ # * Templates data (Hash)
60
+ def register_templates(path)
61
+ unless File.directory?(path)
62
+ puts "[Chef2Wiki]\tCreating default templates at #{path}..."
63
+ FileUtils.mkdir_p(path)
64
+ FileUtils.cp_r(File.join(GEM_DATADIR, "templates", "."), path)
65
+ end
66
+
67
+ templates_data = {}
68
+ Dir.glob(File.join(path, "*.erb")) do |file|
69
+ name = File.basename(file, ".erb")
70
+ begin
71
+ templates_data[name] = ERB.new(File.read(file), nil, "%<>")
72
+ rescue => ex
73
+ puts "[Chef2Wiki]\tError loading template #{tem}: " + ex.to_s
74
+ end
75
+ end
76
+ puts "[Chef2Wiki]\tRegistered templates: #{templates_data.keys.join(", ")}"
77
+ return templates_data
78
+ end
79
+
80
+ # Render partial template.
81
+ #
82
+ # ==== Attributes
83
+ # * +name+ - Name of template (String)
84
+ def render(name)
85
+ puts "[Chef2Wiki]\tRendering partial #{name}..."
86
+ template = ERB.new(File.read(File.join(options[:templates], name)), nil, "%<>")
87
+ template.result(binding)
88
+ end
89
+
90
+ # Recursively print object attributes with arg and value.
91
+ # If value class is a Hash - call this method again for value.
92
+ # If value class is an Array - print value with join.
93
+ #
94
+ # ==== Attributes
95
+ # * +obj+ - Object for print (Hash)
96
+ # * +point+ - String for start of line (String)
97
+ # * +splitter+ - String for split attribute and value (String)
98
+ # * +endline+ - String for end of line (String)
99
+ def print_attrs(obj, point="* ", splitter=":", endline="\n")
100
+ result = ""
101
+ if obj
102
+ obj.each do |arg, value|
103
+ result += "#{point}#{arg}"
104
+ if !value.to_s.empty?
105
+ result += "#{splitter} "
106
+ if value.is_a?(Hash)
107
+ # TODO: Hate this recursion. Also it's too wiki-related with this pointer usage.
108
+ result += "#{endline}#{print_attrs(value, point[0,1]+point, splitter, endline)}"
109
+ elsif value.is_a?(Array)
110
+ result += value.join(", ")
111
+ result += "#{endline}"
112
+ else
113
+ result += "#{value}"
114
+ result += "#{endline}"
115
+ end
116
+ else
117
+ result += "#{endline}"
118
+ end
119
+ end
120
+ end
121
+ return result
122
+ end
123
+
124
+ end
@@ -0,0 +1,32 @@
1
+ class Chef2Wiki
2
+
3
+ # Get data for node.
4
+ #
5
+ # ==== Attributes
6
+ # * +node_name+ - Name of the node from chef (String)
7
+ # ==== Returns
8
+ # * Node data (Hash)
9
+ def node_data(node_name)
10
+ node = Chef::Node.load(node_name)
11
+ data = node.to_hash
12
+ data["run_list"] = node.run_list.run_list_items
13
+ return data
14
+ end
15
+
16
+ # Get node list.
17
+ #
18
+ # ==== Returns
19
+ # * Node list (Array)
20
+ def node_list
21
+ Chef::Node.list.keys
22
+ end
23
+
24
+ # Get excluded node list.
25
+ #
26
+ # ==== Returns
27
+ # * Node list (Array)
28
+ def excluded_node_list
29
+ config["nodes"] ? config["nodes"]["exclude"] : []
30
+ end
31
+
32
+ end
@@ -0,0 +1,27 @@
1
+ class Chef2Wiki
2
+
3
+ # This requires markdown extension for mediawiki.
4
+
5
+ def generate_docs
6
+ if config["cookbooks"]["repo_paths"].any?
7
+ puts "[Chef2Wiki]\tCreating documentation..."
8
+ @cookbooks = []
9
+
10
+ config["cookbooks"]["repo_paths"].each do |repo_path|
11
+ doc_files = File.join(repo_path, "**", "README.md")
12
+
13
+ Dir.glob(File.expand_path(doc_files)).sort.each do |file|
14
+ cookbook = file.gsub("#{File.expand_path(repo_path)}/", "").gsub("/README.md", "")
15
+ if cookbook != "README.md"
16
+ @cookbooks += [cookbook]
17
+ @content = File.read(file)
18
+ add_page(cookbook, templates["documentation"].result(binding))
19
+ end
20
+ end
21
+ end
22
+
23
+ add_page("Cookbooks_documentation", templates["documentation_list"].result(binding))
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,41 @@
1
+ class Chef2Wiki
2
+
3
+ # Add page to MediaWiki
4
+ #
5
+ # ==== Attributes
6
+ # * +title+ - Title of the page (String)
7
+ # * +content+ - Content of the page (String)
8
+ # * +overwrite+ - Overwrite flag (Boolean)
9
+ def add_page(title, content, overwrite = true)
10
+ begin
11
+ print "[MediaWiki]\tCreating page #{title}... "
12
+ wiki.create(title, content, :overwrite => overwrite)
13
+ puts "done."
14
+ rescue Exception => ex
15
+ puts "[MediaWiki]\tError adding page #{title}: " + ex.message.to_s
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ # Do wiki connect and login.
22
+ def setup_media_wiki
23
+ if config["wiki"]["api_url"] && !config["wiki"]["api_url"].empty?
24
+ mediawiki = MediaWiki::Gateway.new(config["wiki"]["api_url"])
25
+ else
26
+ abort "[MediaWiki]\tPlease, configure mediawiki api url."
27
+ end
28
+
29
+ if config["wiki"]["login"] && !config["wiki"]["login"].empty?
30
+ begin
31
+ mediawiki.login(config["wiki"]["login"], config["wiki"]["password"], config["wiki"]["domain"])
32
+ puts "[MediaWiki]\tLogged in successfully."
33
+ rescue Exception => ex
34
+ puts "[MediaWiki]\tError while login into wiki: " + ex.message.to_s
35
+ end
36
+ end
37
+
38
+ return mediawiki
39
+ end
40
+
41
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef2wiki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Larionov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chef
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '11.16'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '11.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mediawiki-gateway
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 0.6.2
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '0.6'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.6.2
47
+ description: Generates wiki documentation from Chef node attributes using ERB templates.
48
+ email: xeron.oskom@gmail.com
49
+ executables:
50
+ - chef2wiki
51
+ extensions: []
52
+ extra_rdoc_files:
53
+ - README.md
54
+ - LICENSE
55
+ files:
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - bin/chef2wiki
60
+ - lib/chef2wiki.rb
61
+ - lib/chef2wiki/base.rb
62
+ - lib/chef2wiki/chef.rb
63
+ - lib/chef2wiki/cookbook.rb
64
+ - lib/chef2wiki/mediawiki.rb
65
+ homepage: http://github.com/xeron/chef2wiki
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 1.9.3
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Generates wiki documentation from Chef node attributes using ERB templates.
89
+ test_files: []