Class: Puppetclass

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Authorization
Defined in:
app/models/puppetclass.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Authorization

#enforce_create_permissions, #enforce_destroy_permissions, #enforce_edit_permissions, #enforce_permissions, included, #permission_failed?

Class Method Details

.classes2hash(classes) ⇒ Object

returns a hash containing modules and associated classes


38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/puppetclass.rb', line 38

def self.classes2hash classes
  hash = {}
  for klass in classes
    if (mod = klass.module_name)
      hash[mod] ||= []
      hash[mod] << klass
    else
      next
    end
  end
  return hash
end

.prepare_rdoc(root) ⇒ Object

Optionally creates a copy of the current puppet modules and sanitizes it. If your 'live' manifests and modules can be parsed by puppetdoc then you do not need to do this step. (Unfortunately some sites have circular symlinks which have to be removed.) If the executable Rails,root/script/rdoc_prepare_script exists then it is run and passed a list of all directory paths in all environments. It should return the directory into which it has copied the cleaned modules“


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/puppetclass.rb', line 130

def self.prepare_rdoc root
  debug, verbose = false, false

  prepare_script = Pathname.new(Rails.root) + "script/rdoc_prepare_script.rb"
  if prepare_script.executable?
    dirs = Environment.puppetEnvs.values.join(":").split(":").uniq.sort.join(" ")
    puts "Running #{prepare_script} #{dirs}" if debug
    location = %x{#{prepare_script} #{dirs}}
    if $? == 0
      root = location.chomp
      puts "Relocated modules to #{root}" if verbose
    end
  else
    puts "No executable #{prepare_script} found so using the uncopied module sources" if verbose
  end
  root
end

.rdoc(root) ⇒ Object

Populates the rdoc tree with information about all the classes in your modules.

Firstly, we prepare the modules tree
Secondly we run puppetdoc over the modulespath and manifestdir for all environments

The results are written into document_root/puppet/rdoc/<env>/<class>“


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/puppetclass.rb', line 66

def self.rdoc root
  debug, verbose = false, false
  relocated      = root != "/"             # This is true if the prepare phase copied the modules tree

  # Retrieve an optional http server's DocumentRoot from the settings.yaml file, and prepare it for writing
  doc_root = Pathname.new(Setting[:document_root])
  doc_root.mkpath
  unless doc_root.directory? and doc_root.writable?
    puts "Unable to write html to #{doc_root}"
    return false
  end
  validator = '<div id="validator-badges">'
  # For each environment we write a puppetdoc tree
  for env, path in Environment.puppetEnvs
    # We may need to rewrite the modulepaths because they have been changed by the prepare step
    modulepaths = relocated ? path.split(":").map{|p| root + p}.join(":") : path

    # Identify and prepare the output directory
    out = doc_root + env
    out.rmtree if out.directory?

    replacement = "<div id=\\\"validator-badges\\\"><small><a href=\\\"/puppet/rdoc/#{env}/\\\">[Browser]</a></small>"

    # Create the documentation

    puts "*********Proccessing environment #{env} *************"
    cmd = "puppetdoc --output #{out} --modulepath #{modulepaths} -m rdoc"
    puts cmd if defined?(Rake)
    sh cmd do |ok, res|
      if ok
        # Add a link to the class browser
        files =  %x{find #{out} -exec grep -l 'validator-badges' {} \\; 2>/dev/null}.gsub(/\n/, " ")
        if files.empty?
          warn "No files to update with the browser link in #{out}. This is probably due to a previous error."
        else
          cmd = "ruby -p -i -e '$_.gsub!(/#{validator}/,\"#{replacement}\")' #{files}"
          puts cmd if debug
         sh cmd
        end
        # Relocate the paths for files and references if the manifests were relocated and sanitized
        if relocated and (files = %x{find #{out} -exec grep -l '#{root}' {} \\;}.gsub(/\n/, " ")) != ""
          puts "Rewriting..." if verbose
          cmd = "ruby -p -i -e 'rex=%r{#{root}};$_.gsub!(rex,\"\")' #{files}"
          puts cmd if debug
          sh cmd
          # Now relocate the files/* files to match the rewritten url
          mv Dir.glob("#{out}/files/#{root}/*"), "#{out}/files", :verbose => verbose
        end
      else
        logger.warn "Failed to process puppetdocs for #{out} while executing #{cmd}"
        puts "Failed to process puppetdocs for #{out} while executing #{cmd}"
      end
    end
    puts
  end
end

.search_by_host(key, operator, value) ⇒ Object


152
153
154
155
156
157
158
159
160
161
# File 'app/models/puppetclass.rb', line 152

def self.search_by_host(key, operator, value)
  conditions = sanitize_sql_for_conditions(["hosts.name #{operator} ?", value_to_sql(operator, value)])
  direct     = Puppetclass.joins(:hosts).where(conditions).select('puppetclasses.id').map(&:id).uniq
  hostgroup  = Hostgroup.joins(:hosts).where(conditions).first
  indirect   = hostgroup.blank? ? [] : HostgroupClass.where(:hostgroup_id => hostgroup.path_ids).pluck('DISTINCT puppetclass_id')
  return { :conditions => "1=0" } if direct.blank? && indirect.blank?

  puppet_classes = (direct + indirect).uniq
  { :conditions => "puppetclasses.id IN(#{puppet_classes.join(',')})" }
end

.value_to_sql(operator, value) ⇒ Object


164
165
166
167
168
169
# File 'app/models/puppetclass.rb', line 164

def self.value_to_sql(operator, value)
  return value                 if operator !~ /LIKE/i
  return value.tr_s('%*', '%') if (value ~ /%|\*/)

  return "%#{value}%"
end

Instance Method Details

#as_json(options = {}) ⇒ Object


148
149
150
# File 'app/models/puppetclass.rb', line 148

def as_json(options={})
  super({:only => [:name, :id], :include => [:lookup_keys]})
end

#klassObject

returns class name (excluding of the module name)


58
59
60
# File 'app/models/puppetclass.rb', line 58

def klass
  name.gsub(module_name+"::","")
end

#module_nameObject

returns module name (excluding of the class name) if class separator does not exists (the “::” chars), then returns the whole class name


53
54
55
# File 'app/models/puppetclass.rb', line 53

def module_name
  (i = name.index("::")) ? name[0..i-1] : name
end

#to_paramObject


33
34
35
# File 'app/models/puppetclass.rb', line 33

def to_param
  name
end