generate-puppetfile 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZmU2ODhjY2FlMTczMTAxMzIzY2ZmOWVlZmRkYzBjMTBjNGFiN2JmMA==
5
+ data.tar.gz: !binary |-
6
+ ZjhmMzUwOTM4Yzc1YTFmZjUxMjA2Njg2NGVkZWIwZDRiMjFjNGNlNA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZDE0Njg0ZmNhY2Q1MzU4MTI1MjUzZTgwM2NiYmQ1MDJiMjQ3YzUwZmJkNWIy
10
+ NGMxODA0M2Y3ZDJiNjllNDYxY2VkMzE2ZjQxZDFjZDVlYThjMDc0NmRlZTFi
11
+ YTY4ZWM3Y2IyNjkwYmVlZGIyYjM2NmU4YjcwMWJkZjU1NGIwMmQ=
12
+ data.tar.gz: !binary |-
13
+ YzM5MTUyZDNiOWQ4MDE3YzcyMjNhNWE0MTA3MmVkYmI4YmQ2NzExYjM2NzIx
14
+ NzdlMTU3M2MwOThlZmNiYzFkZmM0ZDAyY2IwNDE2ZDZkOWQzZDliNzM5NDE3
15
+ N2ZjOGE3MmE5NGJiYWFjZjBiZjk1NDgwNjdmZmRlOTUwZTUxMzk=
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ Tired of searching for dependencies on the forge and hoping you got everything? Use generate-puppetfile to generate a list of modules and their dependencies in a Puppetfile format for use with r10k or librarian-puppet.
2
+
3
+ To use this, simply clone the repository and run the generate-puppetfile script with a list of valid module names:
4
+
5
+ ```
6
+ $ ./generate-puppetfile echocat/nfs rnelson0/certs
7
+ Download echocat/nfs and its dependencies...
8
+ Download rnelson0/certs and its dependencies...
9
+ Module download complete. Generating Puppetfile.
10
+
11
+ Here is your suggested Puppetfile:
12
+ ----------------------------------
13
+
14
+ forge 'http://forge.puppetlabs.com'
15
+
16
+ mod 'echocat/nfs', '1.7.1'
17
+ mod 'herculesteam/augeasproviders_core', '2.1.2'
18
+ mod 'herculesteam/augeasproviders_shellvar', '2.2.0'
19
+ mod 'puppetlabs/concat', '1.2.4'
20
+ mod 'puppetlabs/stdlib', '4.9.0'
21
+ mod 'rnelson0/certs', '0.6.2'
22
+ ```
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'generate_puppetfile'
4
+
5
+ begin
6
+ GeneratePuppetfile::Bin.new(ARGV).run
7
+ end
@@ -0,0 +1,5 @@
1
+ module GeneratePuppetfile
2
+ require 'generate_puppetfile/bin'
3
+ require 'generate_puppetfile/optparser'
4
+ require 'generate_puppetfile/version'
5
+ end
@@ -0,0 +1,153 @@
1
+ # encoding: utf-8
2
+ require 'generate_puppetfile'
3
+ require 'generate_puppetfile/optparser'
4
+ require 'fileutils'
5
+
6
+ module GeneratePuppetfile
7
+ class Bin
8
+ Module_regex = Regexp.new("mod ['\"]([a-z0-9_]+\/[a-z0-9_]+)['\"](, ['\"](\\d\.\\d\.\\d)['\"])?", Regexp::IGNORECASE)
9
+ @options = {}
10
+
11
+ def initialize(args)
12
+ @args = args
13
+ end
14
+
15
+ def run
16
+ @options = GeneratePuppetfile::OptParser.parse(@args)
17
+
18
+ helpmsg = "generate-puppetfile: try 'generate-puppetfile --help' for more information."
19
+
20
+ if @args[0].nil? && (! @options[:puppetfile])
21
+ puts "generate-puppetfile: No modules or existing Puppetfile specified."
22
+ puts helpmsg
23
+ return 1
24
+ end
25
+
26
+ forge_module_list = Array.new
27
+
28
+ if @args
29
+ puts "\nProcessing modules from the command line...\n\n" if @options[:debug]
30
+ cli_modules = Array.new
31
+ @args.each do |modulename|
32
+ validate(modulename) && (cli_modules.push(modulename))
33
+ end
34
+ end
35
+
36
+ puppetfile_contents = Hash.new
37
+ extras = Array.new
38
+ if @options[:puppetfile]
39
+ puts "\nProcessing the puppetfile '#{@options[:puppetfile]}'...\n\n" if @options[:debug]
40
+ puppetfile_contents = read_puppetfile(@options[:puppetfile])
41
+ extras = puppetfile_contents[:extras]
42
+ end
43
+
44
+ forge_module_list.push(*cli_modules) if @args
45
+ forge_module_list.push(*puppetfile_contents[:modules]) if puppetfile_contents[:modules]
46
+
47
+ list_modules(forge_module_list) if puppetfile_contents && @options[:debug]
48
+ list_extras(puppetfile_contents[:extras]) if puppetfile_contents[:extras] && @options[:debug]
49
+
50
+ generate_puppetfile_contents(forge_module_list, puppetfile_contents[:extras])
51
+ end
52
+
53
+ def validate (modulename)
54
+ success = (modulename =~ /[a-z0-9_]\/[a-z0-9_]/i)
55
+ puts " '#{modulename}' is not a valid module name. Skipping." unless success
56
+ success
57
+ end
58
+
59
+ def list_modules (module_list)
60
+ puts "\nListing discovered modules from CLI and/or Puppetfile:\n\n"
61
+ module_list.each do |name|
62
+ puts " #{name}"
63
+ end
64
+ puts ""
65
+ end
66
+
67
+ def list_extras (extras)
68
+ puts "\nExtras found in the existing Puppetfile:\n\n"
69
+ extras.each do |line|
70
+ puts " #{line}"
71
+ end
72
+ puts ""
73
+ end
74
+
75
+ def read_puppetfile (puppetfile)
76
+ puppetfile_contents = {
77
+ :modules => Array.new,
78
+ :extras => Array.new,
79
+ }
80
+
81
+ File.foreach(puppetfile) do |line|
82
+ if Module_regex.match(line)
83
+ name = $1
84
+ print " #{name} looks like a forge module.\n" if @options[:debug]
85
+ puppetfile_contents[:modules].push(name)
86
+ else
87
+ next if line =~ /^forge/
88
+ next if line =~ /^\s+$/
89
+ next if line =~ /# Discovered elements from existing Puppetfile/
90
+
91
+ puppetfile_contents[:extras].push(line)
92
+ end
93
+ end
94
+
95
+ puppetfile_contents
96
+ end
97
+
98
+ def generate_puppetfile_contents (module_list, extras = [])
99
+ # cli_modules is a hash of module name => version
100
+ # puppetfile_contents is a hash with two keys:
101
+ # module_list is an array of forge module names to be downloaded
102
+ # extras is an array of strings
103
+ workspace = `mktemp -d`
104
+
105
+ modulepath = "--modulepath #{workspace.chomp} "
106
+ silence = '>/dev/null 2>&1 '
107
+ strip_colors = 'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
108
+
109
+ puts "\nInstalling modules. This may take a few minutes.\n\n"
110
+ module_list.each do |name|
111
+ command = "puppet module install #{name} "
112
+ command += modulepath + silence
113
+
114
+ system(command)
115
+ end
116
+
117
+ puppetfile_header = <<-EOF
118
+ forge 'http://forge.puppetlabs.com'
119
+
120
+ # Modules discovered by generate-puppetfile
121
+ EOF
122
+
123
+ puppetfile_body = `puppet module list #{modulepath} 2>/dev/null | #{strip_colors}`
124
+
125
+ puppetfile_body.gsub!(/^\/.*$/, '')
126
+ puppetfile_body.gsub!(/-/, '/')
127
+ puppetfile_body.gsub!(/├── /, "mod '")
128
+ puppetfile_body.gsub!(/└── /, "mod '")
129
+ puppetfile_body.gsub!(/ \(v/, "', '")
130
+ puppetfile_body.gsub!(/\)$/, "'")
131
+ puppetfile_body.gsub!(/^$\n/, '')
132
+
133
+ puppetfile_footer = "# Discovered elements from existing Puppetfile\n"
134
+ extras.each do |line|
135
+ puppetfile_footer += "#{line}"
136
+ end
137
+
138
+ puts <<-EOF
139
+
140
+
141
+ Your Puppetfile has been generated. Copy and paste between the markers:
142
+
143
+ =======================================================================
144
+ #{puppetfile_header}
145
+ #{puppetfile_body}
146
+ #{puppetfile_footer}
147
+ =======================================================================
148
+ EOF
149
+
150
+ FileUtils.rm_rf(workspace)
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,33 @@
1
+ require 'generate_puppetfile'
2
+ require 'optparse'
3
+
4
+ module GeneratePuppetfile
5
+ class OptParser
6
+ def self.parse(args)
7
+ options = {}
8
+ opts = OptionParser.new do |opts|
9
+ opts.banner = "generate-puppetfile [OPTIONS] [<MODULE> ... <MODULE>]"
10
+
11
+ opts.on('-p', '--puppetfile FILE', 'Name of existing Puppetfile to verify and update') do |file|
12
+ unless File.readable?(file)
13
+ puts "\nPuppetfile '#{file}' cannot be read. Are you sure you passed in the correct filename?\n\n"
14
+ exit 1
15
+ end
16
+
17
+ options[:puppetfile] = file
18
+ end
19
+
20
+ opts.on('-d', '--debug', 'Enable debug logging') do
21
+ options[:debug] = true
22
+ end
23
+
24
+ opts.on_tail('-v', '--version', 'Show version') do
25
+ puts "generate-puppetfile v#{GeneratePuppetfile::VERSION}"
26
+ end
27
+ end
28
+
29
+ opts.parse!(args)
30
+ options
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module GeneratePuppetfile
2
+ VERSION = '0.9.0'
3
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: generate-puppetfile
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Nelson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Generate a Puppetfile for use with r10k based on an existing file or
14
+ a list of modules.
15
+ email: rnelson0@gmail.com
16
+ executables:
17
+ - generate-puppetfile
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - bin/generate-puppetfile
23
+ - lib/generate_puppetfile.rb
24
+ - lib/generate_puppetfile/bin.rb
25
+ - lib/generate_puppetfile/optparser.rb
26
+ - lib/generate_puppetfile/version.rb
27
+ homepage: https://github.com/rnelson0/puppet-generate-puppetfile
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.4.3
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Generate a Puppetfile
51
+ test_files: []