Class: Bebox::Profile

Inherits:
Object
  • Object
show all
Includes:
FilesHelper
Defined in:
lib/bebox/profile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FilesHelper

#file_content_trimmed, #generate_file_from_template, included, #render_erb_template, templates_path, #write_content_to_file

Constructor Details

#initialize(name, project_root, path) ⇒ Profile

Returns a new instance of Profile.



9
10
11
12
13
# File 'lib/bebox/profile.rb', line 9

def initialize(name, project_root, path)
  self.project_root = project_root
  self.name = name
  self.path = path
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/bebox/profile.rb', line 7

def name
  @name
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/bebox/profile.rb', line 7

def path
  @path
end

#project_rootObject

Returns the value of attribute project_root.



7
8
9
# File 'lib/bebox/profile.rb', line 7

def project_root
  @project_root
end

Class Method Details

.cleanpath(path_name) ⇒ Object

Clean a path to make it valid



76
77
78
79
# File 'lib/bebox/profile.rb', line 76

def self.cleanpath(path_name)
  valid_path = Pathname.new(path_name).cleanpath.to_path.split('/').reject{|c| c.empty? }
  return valid_path.nil? ? '' : valid_path.join('/')
end

.list(project_root) ⇒ Object

Lists existing profiles



28
29
30
31
# File 'lib/bebox/profile.rb', line 28

def self.list(project_root)
  Dir.chdir("#{project_root}/puppet/profiles") { Dir.glob("**/manifests").map{ |f| File.dirname(f) } }
  # Dir["#{project_root}/puppet/profiles/*"].map { |f| File.basename(f) }
end

.profiles_count(project_root) ⇒ Object

Counts existing profiles



59
60
61
# File 'lib/bebox/profile.rb', line 59

def self.profiles_count(project_root)
  Bebox::Profile.list(project_root).count
end

.valid_pathname?(pathname) ⇒ Boolean

Check if the profile has a valid path name

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/bebox/profile.rb', line 64

def self.valid_pathname?(pathname)
  #Split the name and validate each path part
  pathname.split('/').each do |path_child|
    valid_name = (path_child =~ /\A[a-z][a-z0-9_]*\Z/).nil? ? false : true
    valid_name && !Bebox::RESERVED_WORDS.include?(path_child)
    return false unless valid_name
  end
  # Return true if all parts are valid
  true
end

Instance Method Details

#absolute_pathObject

Path to the profile directory in the project



54
55
56
# File 'lib/bebox/profile.rb', line 54

def absolute_path
  "#{self.project_root}/puppet/profiles/#{relative_path}"
end

#createObject

Create all files and directories related to a profile



16
17
18
19
20
# File 'lib/bebox/profile.rb', line 16

def create
  create_profile_directory
  generate_manifests_file
  generate_puppetfile
end

#create_profile_directoryObject

Create the directories for the profile



34
35
36
# File 'lib/bebox/profile.rb', line 34

def create_profile_directory
  `cd #{self.project_root} && mkdir -p puppet/profiles/#{relative_path}/manifests`
end

#generate_manifests_fileObject

Generate the manifests init.pp file



39
40
41
# File 'lib/bebox/profile.rb', line 39

def generate_manifests_file
  generate_file_from_template("#{templates_path}/puppet/profiles/manifests/init.pp.erb", "#{absolute_path}/manifests/init.pp", {profile: self})
end

#generate_puppetfileObject

Generate the Puppetfile



44
45
46
# File 'lib/bebox/profile.rb', line 44

def generate_puppetfile
  generate_file_from_template("#{templates_path}/puppet/profiles/Puppetfile.erb", "#{absolute_path}/Puppetfile", {})
end

#namespace_nameObject

Generate the namespace name from the profile relative path



87
88
89
# File 'lib/bebox/profile.rb', line 87

def namespace_name
  relative_path.gsub('/','::')
end

#relative_pathObject

Create the profile path relative to the project



82
83
84
# File 'lib/bebox/profile.rb', line 82

def relative_path
  path.empty? ? self.name : File.join("#{self.path}", "#{self.name}")
end

#removeObject

Delete all files and directories related to a profile



23
24
25
# File 'lib/bebox/profile.rb', line 23

def remove
  `cd #{self.project_root} && rm -r puppet/profiles/#{relative_path}`
end

#templates_pathObject

Path to the templates directory in the gem



49
50
51
# File 'lib/bebox/profile.rb', line 49

def templates_path
  File.join((File.expand_path '..', File.dirname(__FILE__)), 'templates')
end