Class: GeneratePuppetfile::Bin
- Inherits:
-
Object
- Object
- GeneratePuppetfile::Bin
- Defined in:
- lib/generate_puppetfile/bin.rb
Overview
Internal: The Bin class contains the logic for calling generate_puppetfile at the command line
Constant Summary collapse
- Module_regex =
Regexp.new("mod ['\"]([a-z0-9_]+\/[a-z0-9_]+)['\"](, ['\"](\\d\.\\d\.\\d)['\"])?", Regexp::IGNORECASE)
- Silence =
'>/dev/null 2>&1 '
Instance Method Summary collapse
-
#cleanup_workspace ⇒ Object
Public: Remove the workspace (with prejudice).
-
#create_puppetfile(puppetfile_contents) ⇒ Object
Public: Create a Puppetfile on disk The Puppetfile will be called ‘Puppetfile’ in the current working directory.
-
#create_workspace ⇒ Object
Public: Create a temporary workspace for module manipulation.
-
#display_puppetfile(puppetfile_contents) ⇒ Object
Public: Display the generated Puppetfile to STDOUT with delimiters.
-
#download_modules(module_list) ⇒ Object
Public: Download the list of modules and their dependencies to @workspace.
-
#generate_fixtures_data ⇒ Object
Public: Generate a simple fixtures file.
-
#generate_module_output ⇒ Object
Public: generate the list of modules in Puppetfile format from the @workspace.
-
#generate_puppetfile_contents(extras) ⇒ Object
Public: Generate a new Puppetfile’s contents based on a list of modules and any extras found in an existing Puppetfile.
-
#initialize(args) ⇒ Bin
constructor
Public: Initialize a new GeneratePuppetfile::Bin.
-
#list_extras(extras) ⇒ Object
Public: Display the list of extra statements found in the Puppetfile.
-
#list_forge_modules(module_list) ⇒ Object
Public: Display the list of Forge modules collected.
-
#read_puppetfile(puppetfile) ⇒ Object
Public: Read and parse the contents of an existing Puppetfile.
-
#run ⇒ Object
Public: Run generate-puppetfile at the command line.
-
#validate(modulename) ⇒ Object
Public: Validates that a provided module name is valid.
- #write_fixtures_data(data) ⇒ Object
Constructor Details
#initialize(args) ⇒ Bin
Public: Initialize a new GeneratePuppetfile::Bin
args - Array of command line arguments as Strings to be passed to GeneratePuppetfile::OptParser.parse
Example:
GeneratePuppetfile::Bin.new(ARGV).run
23 24 25 |
# File 'lib/generate_puppetfile/bin.rb', line 23 def initialize(args) @args = args end |
Instance Method Details
#cleanup_workspace ⇒ Object
Public: Remove the workspace (with prejudice)
224 225 226 |
# File 'lib/generate_puppetfile/bin.rb', line 224 def cleanup_workspace () FileUtils.rm_rf(@workspace) end |
#create_puppetfile(puppetfile_contents) ⇒ Object
Public: Create a Puppetfile on disk The Puppetfile will be called ‘Puppetfile’ in the current working directory
108 109 110 111 112 |
# File 'lib/generate_puppetfile/bin.rb', line 108 def create_puppetfile (puppetfile_contents) File.open('Puppetfile', 'w') do |file| file.write puppetfile_contents end end |
#create_workspace ⇒ Object
Public: Create a temporary workspace for module manipulation
219 220 221 |
# File 'lib/generate_puppetfile/bin.rb', line 219 def create_workspace() @workspace = (Dir.mktmpdir).chomp end |
#display_puppetfile(puppetfile_contents) ⇒ Object
Public: Display the generated Puppetfile to STDOUT with delimiters
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/generate_puppetfile/bin.rb', line 95 def display_puppetfile(puppetfile_contents) puts "\nYour Puppetfile has been generated. Copy and paste between the markers:\n\n=======================================================================\n\#{puppetfile_contents.chomp}\n=======================================================================\n EOF\nend\n" |
#download_modules(module_list) ⇒ Object
Public: Download the list of modules and their dependencies to @workspace
module_list is an array of forge module names to be downloaded
170 171 172 173 174 175 176 177 178 |
# File 'lib/generate_puppetfile/bin.rb', line 170 def download_modules(module_list) puts "\nInstalling modules. This may take a few minutes.\n" unless [:silent] module_list.each do |name| command = "puppet module install #{name} " command += @modulepath + Silence system(command) end end |
#generate_fixtures_data ⇒ Object
Public: Generate a simple fixtures file.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/generate_puppetfile/bin.rb', line 229 def generate_fixtures_data () puts "\nGenerating .fixtures.yml using module name #{@options[:modulename]}" unless [:silent] # Header for fixtures file creates a symlink for the current module" fixtures_data = "fixtures:\n symlinks:\n\#{@options[:modulename]}: \"\\\#{source_dir}\"\n EOF\n\n\n module_directories = Dir.glob(\"\#{@workspace}/*\")\n if (module_directories != [])\n fixtures_data += \" repositories:\\n\"\n end\n module_directories.each do |module_directory|\n name = File.basename(module_directory)\n file = File.read(\"\#{module_directory}/metadata.json\")\n source = (JSON.parse(file))[\"source\"]\n fixtures_data += \" \#{name}: \#{source}\\n\"\n puts \"Found a module '\#{name}' with a project page of \#{source}.\" if @options[:debug]\n end unless module_directories == []\n\n fixtures_data\nend\n" |
#generate_module_output ⇒ Object
Public: generate the list of modules in Puppetfile format from the @workspace
181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/generate_puppetfile/bin.rb', line 181 def generate_module_output () module_output = `puppet module list #{@modulepath} 2>/dev/null` module_output.gsub!(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/, '') # Strips ANSI color codes module_output.gsub!(/^\/.*$/, '') module_output.gsub!(/-/, '/') module_output.gsub!(/├── /, "mod '") module_output.gsub!(/└── /, "mod '") module_output.gsub!(/ \(v/, "', '") module_output.gsub!(/\)$/, "'") module_output.gsub!(/^$\n/, '') module_output end |
#generate_puppetfile_contents(extras) ⇒ Object
Public: Generate a new Puppetfile’s contents based on a list of modules and any extras found in an existing Puppetfile.
extras is an array of strings
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/generate_puppetfile/bin.rb', line 200 def generate_puppetfile_contents (extras) puppetfile_contents = "forge 'http://forge.puppetlabs.com'\n\n# Modules discovered by generate-puppetfile\n EOF\n\n puppetfile_contents += generate_module_output()\n\n puppetfile_contents += \"# Discovered elements from existing Puppetfile\\n\" unless extras == []\n extras.each do |line|\n puppetfile_contents += \"\#{line}\"\n end unless extras == []\n\n puppetfile_contents\nend\n" |
#list_extras(extras) ⇒ Object
Public: Display the list of extra statements found in the Puppetfile.
133 134 135 136 137 138 139 140 141 |
# File 'lib/generate_puppetfile/bin.rb', line 133 def list_extras (extras) unless [:silent] || (extras == []) puts "\nExtras found in the existing Puppetfile:\n\n" extras.each do |line| puts " #{line}" end puts "" end end |
#list_forge_modules(module_list) ⇒ Object
Public: Display the list of Forge modules collected.
122 123 124 125 126 127 128 129 130 |
# File 'lib/generate_puppetfile/bin.rb', line 122 def list_forge_modules (module_list) unless [:silent] puts "\nListing discovered modules from CLI and/or Puppetfile:\n\n" module_list.each do |name| puts " #{name}" end puts "" end end |
#read_puppetfile(puppetfile) ⇒ Object
Public: Read and parse the contents of an existing Puppetfile
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/generate_puppetfile/bin.rb', line 144 def read_puppetfile (puppetfile) puppetfile_contents = { :modules => Array.new, :extras => Array.new, } File.foreach(puppetfile) do |line| if Module_regex.match(line) name = $1 print " #{name} looks like a forge module.\n" if [:debug] puppetfile_contents[:modules].push(name) else next if line =~ /^forge/ next if line =~ /^\s+$/ next if line =~ /# Discovered elements from existing Puppetfile/ puppetfile_contents[:extras].push(line) end end puppetfile_contents end |
#run ⇒ Object
Public: Run generate-puppetfile at the command line.
Returns an Integer exit code for the shell ($?)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 |
# File 'lib/generate_puppetfile/bin.rb', line 30 def run = GeneratePuppetfile::OptParser.parse(@args) helpmsg = "generate-puppetfile: try 'generate-puppetfile --help' for more information." if @args[0].nil? && (! [:puppetfile]) $stderr.puts "generate-puppetfile: No modules or existing Puppetfile specified." puts helpmsg return 1 end forge_module_list = Array.new if @args puts "\nProcessing modules from the command line...\n\n" if [:debug] cli_modules = Array.new @args.each do |modulename| validate(modulename) && (cli_modules.push(modulename)) end end puppetfile_contents = Hash.new extras = [] if [:puppetfile] puts "\nProcessing the puppetfile '#{@options[:puppetfile]}'...\n\n" if [:debug] puppetfile_contents = read_puppetfile([:puppetfile]) extras = puppetfile_contents[:extras] end forge_module_list.push(*cli_modules) if @args forge_module_list.push(*puppetfile_contents[:modules]) if puppetfile_contents[:modules] list_forge_modules(forge_module_list) if puppetfile_contents && [:debug] list_extras(puppetfile_contents[:extras]) if puppetfile_contents[:extras] && [:debug] unless forge_module_list != [] || puppetfile_contents[:extras] != [] $stderr.puts "\nNo valid modules or existing Puppetfile content was found. Exiting.\n\n" return 1 end create_workspace() @modulepath = "--modulepath #{@workspace} " download_modules(forge_module_list) puppetfile_contents = generate_puppetfile_contents(extras) if [:create_puppetfile] create_puppetfile(puppetfile_contents) end unless [:silent] display_puppetfile(puppetfile_contents) end if [:create_fixtures] fixtures_data = generate_fixtures_data() write_fixtures_data(fixtures_data) end cleanup_workspace() return 0 end |
#validate(modulename) ⇒ Object
Public: Validates that a provided module name is valid.
115 116 117 118 119 |
# File 'lib/generate_puppetfile/bin.rb', line 115 def validate (modulename) success = (modulename =~ /[a-z0-9_]\/[a-z0-9_]/i) $stderr.puts "'#{modulename}' is not a valid module name. Skipping." unless success success end |
#write_fixtures_data(data) ⇒ Object
255 256 257 258 259 |
# File 'lib/generate_puppetfile/bin.rb', line 255 def write_fixtures_data (data) File.open('.fixtures.yml', 'w') do |file| file.write data end end |