Class: Gitchefsync::KnifeUtil
- Inherits:
-
Object
- Object
- Gitchefsync::KnifeUtil
- Defined in:
- lib/gitchefsync/knife_util.rb
Instance Method Summary collapse
- #attr_val(contents, name) ⇒ Object
-
#delCookbook(cb) ⇒ Object
delete a cookbook from the server.
-
#initialize(knife, wd) ⇒ KnifeUtil
constructor
A new instance of KnifeUtil.
-
#inList(name, version, list) ⇒ Object
checks if the cookbook name and version exist in the array of cookbooks.
-
#isCBinList(cookbook, list) ⇒ Object
Checks to see if cookbook given is in list uses inList method to determine it.
-
#listCookbooks ⇒ Object
needs knife on the command path parses the knife command “cookbook list -a” returns a list of cookbooks.
-
#listDB ⇒ Object
get a list of existing data bags items (in [bag, item] pairs) on chef server.
-
#listEnv ⇒ Object
get a list of existing environment names on chef server.
-
#listRole ⇒ Object
get a list of existing role names on chef server.
-
#parseMetaData(path) ⇒ Object
Parse metadata.rb from a given directory path.
-
#showDBItem(bag) ⇒ Object
get a list of existing data bag items (from specified bag) on chef server.
-
#subtract(list1, list2) ⇒ Object
returns a list of are in list1 that are not in list2.
Constructor Details
#initialize(knife, wd) ⇒ KnifeUtil
Returns a new instance of KnifeUtil.
29 30 31 32 |
# File 'lib/gitchefsync/knife_util.rb', line 29 def initialize(knife, wd) @knife = knife @wd = wd end |
Instance Method Details
#attr_val(contents, name) ⇒ Object
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/gitchefsync/knife_util.rb', line 169 def attr_val(contents, name) m1 = contents.match(/#{name}\s+['|"](.*)['|"]/) val = nil if m1 != nil && m1.length == 2 val = m1[1] else Gitchefsync.logger.warn "event_id=parse_warn:name=#{name}" end val end |
#delCookbook(cb) ⇒ Object
delete a cookbook from the server
128 129 130 131 132 133 134 |
# File 'lib/gitchefsync/knife_util.rb', line 128 def delCookbook(cb) begin FS.cmd("cd #{@wd} && knife cookbook delete #{cb.name} #{cb.version} -y" ) rescue CmdError => e Gitchefsync.logger.error "event_id=cb_del:#{e.}:e=#{e.backtrace}" end end |
#inList(name, version, list) ⇒ Object
checks if the cookbook name and version exist in the array of cookbooks
102 103 104 105 106 107 108 |
# File 'lib/gitchefsync/knife_util.rb', line 102 def inList( name, version, list) found = false list.each do |item| found = true if ( (name == item.name) and (version == item.version)) end found end |
#isCBinList(cookbook, list) ⇒ Object
Checks to see if cookbook given is in list uses inList method to determine it
112 113 114 |
# File 'lib/gitchefsync/knife_util.rb', line 112 def isCBinList(cookbook, list) return inList( cookbook.name, cookbook.version, list) end |
#listCookbooks ⇒ Object
needs knife on the command path parses the knife command “cookbook list -a” returns a list of cookbooks
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/gitchefsync/knife_util.rb', line 37 def listCookbooks list = Array.new str = FS.cmd "cd #{@wd} && #{@knife} cookbook list -a" arr_str = str.split(/\n/) arr_str.each do |line| cb_name, *cb_versions = line.split(' ') cb_versions.each do |cb_version| list << Cookbook.new(cb_name, cb_version) end end list end |
#listDB ⇒ Object
get a list of existing data bags items (in [bag, item] pairs) on chef server
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gitchefsync/knife_util.rb', line 62 def listDB list = Array.new str = FS.cmd "cd #{@wd} && #{@knife} data bag list" data_bags = str.split(/\n/) data_bags.each do |bag| data_bag_items = showDBItem bag.strip data_bag_items.each do |item| list << [bag.strip, item.strip] end end list end |
#listEnv ⇒ Object
get a list of existing environment names on chef server
51 52 53 54 55 56 57 58 59 |
# File 'lib/gitchefsync/knife_util.rb', line 51 def listEnv list = Array.new str = FS.cmd "cd #{@wd} && #{@knife} environment list" environments = str.split(/\n/) environments.each do |env| list << env.strip end list end |
#listRole ⇒ Object
get a list of existing role names on chef server
87 88 89 90 91 92 93 94 95 |
# File 'lib/gitchefsync/knife_util.rb', line 87 def listRole list = Array.new str = FS.cmd "cd #{@wd} && #{@knife} role list" roles = str.split(/\n/) roles.each do |role| list << role.strip end list end |
#parseMetaData(path) ⇒ Object
Parse metadata.rb from a given directory path
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/gitchefsync/knife_util.rb', line 137 def parseMetaData(path) #Gitchefsync.logger.debug "Parsing metadata: #{path}" if !File.exists?(File.join(path, "metadata.rb")) raise NoMetaDataError end contents = "" begin file = File.new(File.join(path, "metadata.rb"), "r") contents = file.read version = attr_val(contents,"version") name = attr_val(contents,"name") if name.nil? Gitchefsync.logger.warn "event_id=parse_meta_err_name:msg=cannot be resolved, deferring to directory name #{path}" name = File.basename path end #parse maintainer information maintainer = attr_val(contents,"maintainer") email = attr_val(contents,"maintainer_email") #puts "matched:#{name}:#{version}" return Cookbook.new(name, version,maintainer,email) rescue Exception => e puts e.backtrace Gitchefsync.logger.error "#{e.backtrace}" raise KnifeError, "Unable to parse data: file=#{path}/metadata.rb #{contents}" ensure file.close unless file.nil? end end |
#showDBItem(bag) ⇒ Object
get a list of existing data bag items (from specified bag) on chef server
76 77 78 79 80 81 82 83 84 |
# File 'lib/gitchefsync/knife_util.rb', line 76 def showDBItem bag list = Array.new str = FS.cmd "cd #{@wd} && #{@knife} data bag show #{bag}" data_bag_items = str.split(/\n/) data_bag_items.each do |item| list << item.strip end list end |
#subtract(list1, list2) ⇒ Object
returns a list of are in list1 that are not in list2
117 118 119 120 121 122 123 124 125 |
# File 'lib/gitchefsync/knife_util.rb', line 117 def subtract(list1,list2) list = Array.new list1.each do |cookbook| if !isCBinList(cookbook,list2) list << cookbook end end list end |