Class: Gitchefsync::KnifeUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/gitchefsync/knife_util.rb

Instance Method Summary collapse

Constructor Details

#initialize(knife, wd) ⇒ KnifeUtil

Returns a new instance of KnifeUtil.



13
14
15
16
# File 'lib/gitchefsync/knife_util.rb', line 13

def initialize(knife, wd)
  @knife = knife
  @wd = wd
end

Instance Method Details

#attr_val(contents, name) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/gitchefsync/knife_util.rb', line 153

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



112
113
114
115
116
117
118
# File 'lib/gitchefsync/knife_util.rb', line 112

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.message}:e=#{e.backtrace}"
  end
end

#inList(name, version, list) ⇒ Object

checks if the cookbook name and version exist in the array of cookbooks

Parameters:

  • name
    • name of cookbook

  • version
    • version of cookbook

  • list
    • the list of cookbooks - from listCookbooks



86
87
88
89
90
91
92
# File 'lib/gitchefsync/knife_util.rb', line 86

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



96
97
98
# File 'lib/gitchefsync/knife_util.rb', line 96

def isCBinList(cookbook, list)
  return inList( cookbook.name, cookbook.version, list)
end

#listCookbooksObject

needs knife on the command path parses the knife command “cookbook list -a” returns a list of cookbooks



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gitchefsync/knife_util.rb', line 21

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

#listDBObject

get a list of existing data bags items (in [bag, item] pairs) on chef server



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitchefsync/knife_util.rb', line 46

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

#listEnvObject

get a list of existing environment names on chef server



35
36
37
38
39
40
41
42
43
# File 'lib/gitchefsync/knife_util.rb', line 35

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

#listRoleObject

get a list of existing role names on chef server



71
72
73
74
75
76
77
78
79
# File 'lib/gitchefsync/knife_util.rb', line 71

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gitchefsync/knife_util.rb', line 121

def (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



60
61
62
63
64
65
66
67
68
# File 'lib/gitchefsync/knife_util.rb', line 60

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



101
102
103
104
105
106
107
108
109
# File 'lib/gitchefsync/knife_util.rb', line 101

def subtract(list1,list2)
  list = Array.new
  list1.each do |cookbook|
    if !isCBinList(cookbook,list2)
      list << cookbook
    end
  end
  list
end