chefdore 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a84f685835980c8244fb75ea91e0aed988162e3
4
- data.tar.gz: ca1089f95b4403bdcc480e7e4edc577cf9155bfe
3
+ metadata.gz: 36e9cb7d6225f41e9bd7213a3ad4bc89de92f7ff
4
+ data.tar.gz: e5f615b3fca910f964826135c43627dee595619d
5
5
  SHA512:
6
- metadata.gz: 625ba115805fe88a94c0829d6316947a5bed4a5ee15d98158e529c2e0513516916ef715c26d99598dc5b25e1de720d39e5773f7c3dd0dc72599aa6ec470a7af5
7
- data.tar.gz: 1d3b1ce26793566b6fcd1f0fcfc4cecd3dfa71d2f45b13620f6b4e93a75b45a8f247efb8c90b2859448d120bbd9ff1cf66350d103c5927cae4db48d1940ea72d
6
+ metadata.gz: cd1df4b4dd91a2d1afc902009f382edf0d351740c0528120fe47f2800d0825ff1071c76336d39a4ba348c4b2628a9a4cec19b039365a410f6a1924c7f4819551
7
+ data.tar.gz: ebbd400323524a9cc9eb1955cb368c7bfa4204eba7d78f25ff49eb3d3eb7afe19286a8731fa78c07229e19ad56a6a79e082cf8b78cce4b2c9a55ece9932adbee
data/README.md CHANGED
@@ -2,13 +2,141 @@
2
2
 
3
3
  Chef tool that was named after Dumbledore.
4
4
 
5
+ I created this gem in order to help those who may want to convert Chef Roles to Chef Cookbooks.
6
+
5
7
  ## Installation
6
8
 
7
9
  $ gem install chefdore
8
10
 
9
11
  ## Usage
10
12
 
11
- knife role show base --format json | chefdore convert
13
+ Let's say you have a Chef Role in your Chef Server and you want to convert it to a Chef Cookbook.
14
+
15
+ First let's take a look at an "example_role":
16
+
17
+ ```bash
18
+ $ knife role show example_role --format json
19
+ {
20
+ "name": "example_role",
21
+ "description": "Chef-server example role",
22
+ "json_class": "Chef::Role",
23
+ "default_attributes": {
24
+
25
+ },
26
+ "override_attributes": {
27
+ "developer_mode": false,
28
+ "monitoring": {
29
+ "metric_provider": "collectd",
30
+ "procmon_provider": "monit"
31
+ },
32
+ "glance": {
33
+ "image_upload": true,
34
+ "images": [
35
+ "cirros",
36
+ "precise"
37
+ ]
38
+ },
39
+ "nova": {
40
+ "ratelimit": {
41
+ "api": {
42
+ "enabled": true
43
+ },
44
+ "volume": {
45
+ "enabled": true
46
+ }
47
+ },
48
+ "libvirt": {
49
+ "virt_type": "qemu"
50
+ },
51
+ "networks": {
52
+ "public": {
53
+ "label": "public",
54
+ "ipv4_cidr": "10.10.100.0/24",
55
+ "bridge": "br100",
56
+ "bridge_dev": "eth0.100",
57
+ "dns1": "8.8.8.8",
58
+ "dns2": "8.8.4.4"
59
+ },
60
+ "private": {
61
+ "label": "private",
62
+ "ipv4_cidr": "172.16.101.0/24",
63
+ "bridge": "br101",
64
+ "bridge_dev": "eth0.101",
65
+ "dns1": "8.8.8.8",
66
+ "dns2": "8.8.4.4"
67
+ }
68
+ }
69
+ },
70
+ "mysql": {
71
+ "allow_remote_root": true,
72
+ "root_network_acl": "%"
73
+ },
74
+ "osops_networks": {
75
+ "nova": "192.168.1.0/24",
76
+ "public": "192.168.1.0/24",
77
+ "management": "192.168.1.0/24"
78
+ },
79
+ "package_component": "folsom"
80
+ },
81
+ "chef_type": "role",
82
+ "run_list": [
83
+ "recipe[osops-utils::packages]",
84
+ "recipe[osops-utils::nf_conntrack_max]",
85
+ "recipe[osops-utils::vhost_net]",
86
+ "recipe[openssh]",
87
+ "recipe[ntp]",
88
+ "recipe[rsyslog::default]",
89
+ "recipe[hardware]",
90
+ "recipe[osops-utils::version]"
91
+ ],
92
+ "env_run_lists": {
93
+
94
+ }
95
+ }
96
+ ```
97
+
98
+ All we have to do is pipe that output into our little command line client:
99
+
100
+ ```bash
101
+ $ knife role show example_role --format json | chefdore convert
102
+ override["developer_mode"] = false
103
+ override["monitoring"]["metric_provider"] = "collectd"
104
+ override["monitoring"]["procmon_provider"] = "monit"
105
+ override["glance"]["image_upload"] = true
106
+ override["glance"]["images"] = ["cirros", "precise"]
107
+ override["nova"]["ratelimit"]["api"]["enabled"] = true
108
+ override["nova"]["ratelimit"]["volume"]["enabled"] = true
109
+ override["nova"]["libvirt"]["virt_type"] = "qemu"
110
+ override["nova"]["networks"]["public"]["label"] = "public"
111
+ override["nova"]["networks"]["public"]["ipv4_cidr"] = "10.10.100.0/24"
112
+ override["nova"]["networks"]["public"]["bridge"] = "br100"
113
+ override["nova"]["networks"]["public"]["bridge_dev"] = "eth0.100"
114
+ override["nova"]["networks"]["public"]["dns1"] = "8.8.8.8"
115
+ override["nova"]["networks"]["public"]["dns2"] = "8.8.4.4"
116
+ override["nova"]["networks"]["private"]["label"] = "private"
117
+ override["nova"]["networks"]["private"]["ipv4_cidr"] = "172.16.101.0/24"
118
+ override["nova"]["networks"]["private"]["bridge"] = "br101"
119
+ override["nova"]["networks"]["private"]["bridge_dev"] = "eth0.101"
120
+ override["nova"]["networks"]["private"]["dns1"] = "8.8.8.8"
121
+ override["nova"]["networks"]["private"]["dns2"] = "8.8.4.4"
122
+ override["mysql"]["allow_remote_root"] = true
123
+ override["mysql"]["root_network_acl"] = "%"
124
+ override["osops_networks"]["nova"] = "192.168.1.0/24"
125
+ override["osops_networks"]["public"] = "192.168.1.0/24"
126
+ override["osops_networks"]["management"] = "192.168.1.0/24"
127
+ override["package_component"] = "folsom"
128
+ include_recipe "osops-utils::packages"
129
+ include_recipe "osops-utils::nf_conntrack_max"
130
+ include_recipe "osops-utils::vhost_net"
131
+ include_recipe "openssh"
132
+ include_recipe "ntp"
133
+ include_recipe "rsyslog::default"
134
+ include_recipe "hardware"
135
+ include_recipe "osops-utils::version"
136
+ ```
137
+
138
+ It outputs exactly what should be dropped into your new Chef Cookbook attributes file.
139
+ e.g. `attributes/default.rb`
12
140
 
13
141
  ## Contributing
14
142
 
@@ -17,3 +145,31 @@ Chef tool that was named after Dumbledore.
17
145
  3. Commit your changes (`git commit -am 'Add some feature'`)
18
146
  4. Push to the branch (`git push origin my-new-feature`)
19
147
  5. Create a new Pull Request
148
+
149
+ ## License & Authors
150
+ - Author: Jason Barnett
151
+
152
+ ```
153
+ Copyright (c) 2015 Jason Barnett
154
+
155
+ MIT License
156
+
157
+ Permission is hereby granted, free of charge, to any person obtaining
158
+ a copy of this software and associated documentation files (the
159
+ "Software"), to deal in the Software without restriction, including
160
+ without limitation the rights to use, copy, modify, merge, publish,
161
+ distribute, sublicense, and/or sell copies of the Software, and to
162
+ permit persons to whom the Software is furnished to do so, subject to
163
+ the following conditions:
164
+
165
+ The above copyright notice and this permission notice shall be
166
+ included in all copies or substantial portions of the Software.
167
+
168
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
169
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
170
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
171
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
172
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
173
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
174
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
175
+ ```
data/lib/chefdore/cli.rb CHANGED
@@ -5,27 +5,27 @@ require 'json'
5
5
  module Chefdore
6
6
  class Cli < Thor
7
7
  namespace "chefdore"
8
+
9
+ option :append_arrays,
10
+ type: :boolean,
11
+ desc: "If this is used we will convert arrays by first creating it and then appending each value.",
12
+ aliases: "-a",
13
+ default: false
14
+
8
15
  map %w(v V -v -V ver --version) => :version
9
16
 
10
- desc "convert", "Convert Chef role to a Chef cookbook. Expects the json formatted Chef Role to be piped to this command."
17
+ desc "convert", "Convert Chef Role/Environment to a Chef Cookbook. Expects the json formatted Chef Role/Environment to be piped to this command."
11
18
  def convert
12
19
  ARGV.replace []
13
20
  stdin = ARGF.read
14
21
 
15
22
  begin
16
- json = JSON.parse(stdin)
23
+ Chefdore::Magic.convert(json: stdin, cli: self)
17
24
  rescue JSON::ParserError => e
18
25
  puts e.message
19
26
  exit 1
20
27
  end
21
28
 
22
- toa = %w(default_attributes override_attributes)
23
-
24
- toa.each do |type|
25
- attr = json[type]
26
- prefix = type.split('_').first
27
- Chefdore::Magic.convert(attr, prefix)
28
- end
29
29
  end
30
30
 
31
31
  desc "version", "Display version information"
@@ -1,21 +1,58 @@
1
+ require 'chef/role'
2
+ require 'chef/json_compat'
3
+
1
4
  module Chefdore
2
5
  class Magic
3
- def self.convert(value={}, prefix="default", path=[])
6
+
7
+
8
+ def self.convert_run_list(opts = {})
9
+ rl = opts[:run_list] ? opts[:run_list] : []
10
+ cli = opts[:cli] ? opts[:cli] : Chefdore::Cli.new
11
+
12
+ rl.recipes.each do |x|
13
+ puts "include_recipe #{x.inspect}"
14
+ end
15
+ end
16
+
17
+
18
+ def self.convert_attr(opts = {})
19
+ value = opts[:value]
20
+ prefix = opts[:prefix] ? opts[:prefix] : "default"
21
+ path = opts[:path] ? opts[:path] : []
22
+ cli = opts[:cli] ? opts[:cli] : Chefdore::Cli.new
23
+
24
+ #puts "INFO :: #{value.inspect} (#{value.class}) -- #{prefix} -- #{path}"
25
+
4
26
  if value.is_a?(Hash)
5
27
  value.each do |subkey, subval|
6
- convert(subval, prefix, path+[subkey])
28
+ convert_attr(opts.merge(value: subval, path: path+[subkey]))
7
29
  end
8
- elsif value.is_a?(Array)
9
- value.each do |i|
10
- if i.is_a?(Hash)
11
- convert(i, prefix, path)
12
- else
13
- puts "#{prefix}#{path.map{|p| "[#{p.inspect}]"}.join('')} = #{value.inspect}"
14
- end
30
+ elsif value.is_a?(Array) && cli.options[:append_arrays]
31
+ puts "#{prefix}#{path.map{|p| "[#{p.inspect}]"}.join('')} = #{Array.new.inspect}"
32
+ value.each do |x|
33
+ puts "#{prefix}#{path.map{|p| "[#{p.inspect}]"}.join('')} << #{x.inspect}"
15
34
  end
16
35
  else
17
36
  puts "#{prefix}#{path.map{|p| "[#{p.inspect}]"}.join('')} = #{value.inspect}"
18
37
  end
19
38
  end
39
+
40
+
41
+ def self.convert(opts = {})
42
+ json = opts[:json] ? opts[:json] : fail("You must pass some JSON in :json opt")
43
+ cli = opts[:cli] ? opts[:cli] : fail("You must pass the CLI in :cli opt")
44
+ klass = Chef::JSONCompat.from_json(json)
45
+
46
+ rl = klass.run_list
47
+ da = klass.default_attributes
48
+ oa = klass.override_attributes
49
+
50
+ convert_attr(cli: cli, value: da)
51
+ convert_attr(cli: cli, value: oa, prefix: "override")
52
+
53
+ convert_run_list(cli: cli, run_list: rl)
54
+ end
55
+
56
+
20
57
  end
21
58
  end
@@ -1,3 +1,3 @@
1
1
  module Chefdore
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "base_example",
3
+ "description": "Base Example role for a server",
4
+ "json_class": "Chef::Role",
5
+ "default_attributes": {
6
+ "ntp": {
7
+ "servers": [
8
+ "0.pool.ntp.org",
9
+ "1.pool.ntp.org",
10
+ "2.pool.ntp.org"
11
+ ]
12
+ }
13
+ },
14
+ "override_attributes": {
15
+
16
+ },
17
+ "chef_type": "role",
18
+ "run_list": [
19
+ "recipe[osops-utils::packages]",
20
+ "recipe[osops-utils::nf_conntrack_max]",
21
+ "recipe[osops-utils::vhost_net]",
22
+ "recipe[openssh]",
23
+ "recipe[ntp]",
24
+ "recipe[rsyslog::default]",
25
+ "recipe[hardware]",
26
+ "recipe[osops-utils::version]"
27
+ ],
28
+ "env_run_lists": {
29
+
30
+ }
31
+ }
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "example_role",
3
+ "description": "Chef-server example role",
4
+ "json_class": "Chef::Role",
5
+ "default_attributes": {
6
+
7
+ },
8
+ "override_attributes": {
9
+ "developer_mode": false,
10
+ "monitoring": {
11
+ "metric_provider": "collectd",
12
+ "procmon_provider": "monit"
13
+ },
14
+ "glance": {
15
+ "image_upload": true,
16
+ "images": [
17
+ "cirros",
18
+ "precise"
19
+ ]
20
+ },
21
+ "nova": {
22
+ "ratelimit": {
23
+ "api": {
24
+ "enabled": true
25
+ },
26
+ "volume": {
27
+ "enabled": true
28
+ }
29
+ },
30
+ "libvirt": {
31
+ "virt_type": "qemu"
32
+ },
33
+ "networks": {
34
+ "public": {
35
+ "label": "public",
36
+ "ipv4_cidr": "10.10.100.0/24",
37
+ "bridge": "br100",
38
+ "bridge_dev": "eth0.100",
39
+ "dns1": "8.8.8.8",
40
+ "dns2": "8.8.4.4"
41
+ },
42
+ "private": {
43
+ "label": "private",
44
+ "ipv4_cidr": "172.16.101.0/24",
45
+ "bridge": "br101",
46
+ "bridge_dev": "eth0.101",
47
+ "dns1": "8.8.8.8",
48
+ "dns2": "8.8.4.4"
49
+ }
50
+ }
51
+ },
52
+ "mysql": {
53
+ "allow_remote_root": true,
54
+ "root_network_acl": "%"
55
+ },
56
+ "osops_networks": {
57
+ "nova": "192.168.1.0/24",
58
+ "public": "192.168.1.0/24",
59
+ "management": "192.168.1.0/24"
60
+ },
61
+ "package_component": "folsom"
62
+ },
63
+ "chef_type": "role",
64
+ "run_list": [
65
+ "recipe[osops-utils::packages]",
66
+ "recipe[osops-utils::nf_conntrack_max]",
67
+ "recipe[osops-utils::vhost_net]",
68
+ "recipe[openssh]",
69
+ "recipe[ntp]",
70
+ "recipe[rsyslog::default]",
71
+ "recipe[hardware]",
72
+ "recipe[osops-utils::version]"
73
+ ],
74
+ "env_run_lists": {
75
+
76
+ }
77
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chefdore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Barnett
@@ -71,6 +71,8 @@ files:
71
71
  - lib/chefdore/cli.rb
72
72
  - lib/chefdore/utils.rb
73
73
  - lib/chefdore/version.rb
74
+ - spec/fixtures/base_example.json
75
+ - spec/fixtures/example_role.json
74
76
  - spec/test.rb
75
77
  homepage: https://github.com/jasonwbarnett/chefdore
76
78
  licenses:
@@ -97,5 +99,7 @@ signing_key:
97
99
  specification_version: 4
98
100
  summary: Chef tool that was named after Dumbledore.
99
101
  test_files:
102
+ - spec/fixtures/base_example.json
103
+ - spec/fixtures/example_role.json
100
104
  - spec/test.rb
101
105
  has_rdoc: