Class: Monitor::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_monitor/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Item

Returns a new instance of Item.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chef_monitor/item.rb', line 24

def initialize(data)
  @path = nil
  @name = data['name']
  @time = data['time'].freeze
  @user = data['user'].freeze
  @object = data['object'].freeze
  @server = data['server'].freeze
  @action = data['action'].freeze
  @version = data['version'] unless data['version'].nil?
  @organization = data['org'].freeze
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



41
42
43
# File 'lib/chef_monitor/item.rb', line 41

def action
  @action
end

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/chef_monitor/item.rb', line 37

def name
  @name
end

#objectObject (readonly)

Returns the value of attribute object.



40
41
42
# File 'lib/chef_monitor/item.rb', line 40

def object
  @object
end

#organizationObject (readonly)

Returns the value of attribute organization.



43
44
45
# File 'lib/chef_monitor/item.rb', line 43

def organization
  @organization
end

#pathObject

Returns the value of attribute path.



36
37
38
# File 'lib/chef_monitor/item.rb', line 36

def path
  @path
end

#timeObject (readonly)

Returns the value of attribute time.



38
39
40
# File 'lib/chef_monitor/item.rb', line 38

def time
  @time
end

#userObject (readonly)

Returns the value of attribute user.



39
40
41
# File 'lib/chef_monitor/item.rb', line 39

def user
  @user
end

#versionObject (readonly)

Returns the value of attribute version.



42
43
44
# File 'lib/chef_monitor/item.rb', line 42

def version
  @version
end

Instance Method Details

#commit(path) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chef_monitor/item.rb', line 121

def commit(path)
  current_dir = Dir.pwd
  commit_dir = File.join(path, @organization)
  Dir.chdir(commit_dir)
  @version ? object = [@organization, @object, @name, @version].join('/') : object = [@organization, @object, @name].join('/')
  text = "User     : " + @user + "\nObject   : " + object + "\nAction   : " + @action + "\nLog Time : " + @time
  domain = %x(git config hooks.emaildomain) || "acme.com"
  domain = "@" + domain unless domain[0,1] == "@"        
  username = @user
  useremail = @user + domain
  %x(git config hooks.username #{username} )
  %x(git config hooks.useremail #{useremail} )
  %x(git add .)
  %x(git commit -am \"#{text}\")
  Dir.chdir(current_dir)
end

#commit_nohook(path) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/chef_monitor/item.rb', line 138

def commit_nohook(path)
  current_dir = Dir.pwd
  commit_dir = File.join(path, @organization)
  Dir.chdir(commit_dir)
  %x(git config hooks.exclude true)
  %x(git add .)
  %x(git commit -am \"no hook executed on this commit\")
  Dir.chdir(current_dir)
end

#delete(path) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef_monitor/item.rb', line 67

def delete(path)
  file = nil
  items = [ path ]
  items << @organization
  items << @object
  if @name
    filename=@name + ".json" 
 
    if @object == "cookbooks"
      (@version.nil? || @version.empty?) ? filename=@name : filename=@name + "-" + @version
    end

    if @object == "data"
      (@version.nil? || @version.empty?) ? filename=@name : filename=@name + "/" + @version + ".json"
    end

    items << filename
  end
  file = items.join("/")
  delete_file(file)
end

#delete_file(file) ⇒ Object



89
90
91
92
93
# File 'lib/chef_monitor/item.rb', line 89

def delete_file(file)
  FileUtils.rm_rf(file)
  Monitor::Log.new("Deleted   : " + file, 'INFO')
  true
end

#download(path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chef_monitor/item.rb', line 45

def download(path)

  if @object == "cookbooks" 
    r = download_cookbook(path)
  else
    r = download_object(path)
  end

  (@version.nil? || @version.empty?) ? object = [@organization, @object, @name].join('/') : object = [@organization, @object, @name, @version].join('/') 

  if r == true
    Monitor::Log.new("Downloaded: " + object, 'INFO')
    return true 
  end
  if r.respond_to?("message")
    Monitor::Log.new(r.message + ' with object: ' + object , 'ERROR')
  else
    Monitor::Log.new('Error while downloading object: ' + object , 'ERROR')
  end
  return false
end

#get_item_json(object) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef_monitor/item.rb', line 95

def get_item_json(object)
  begin
    item = rest.get_rest(object)
    # item.delete('automatic') if item['automatric']
    item = view_node_data(item) if item.class == Chef::Node
    result = JSON.pretty_generate(item)
  rescue Exception => e
    Monitor::Log.new(e.message + ' with object: ' + @organization + "/" + object , 'ERROR')
    return nil
  end
  return result
end

#view_node_data(node) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef_monitor/item.rb', line 108

def view_node_data(node)
  result = {}
  result["name"] = node.name
  result["chef_environment"] = node.chef_environment
  result["normal"] = node.normal_attrs
  result["run_list"] = node.run_list
  #result["default"]   = node.default_attrs
  #result["override"]  = node.override_attrs
  # result["automatic"] = node.automatic_attrs
  result
end