Class: Simp::Cli::Config::ItemListFactory
- Inherits:
-
Object
- Object
- Simp::Cli::Config::ItemListFactory
- Defined in:
- lib/simp/cli/config/item_list_factory.rb
Overview
Builds an Array of Config::Items
Instance Method Summary collapse
- #assign_value_from_hash(hash, item) ⇒ Object
-
#build_item_queue(item_queue, items) ⇒ Object
recursively build an item queue.
-
#create_item(item_string) ⇒ Object
returns an instance of an Config::Item based on a String of its class name.
-
#create_safety_writer_item ⇒ Object
create a YAML writer that will “safety save” after each answer.
-
#initialize(options) ⇒ ItemListFactory
constructor
A new instance of ItemListFactory.
- #print_warning(error) ⇒ Object
- #process(yaml = nil, answers_hash = {}) ⇒ Object
Constructor Details
#initialize(options) ⇒ ItemListFactory
Returns a new instance of ItemListFactory.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/simp/cli/config/item_list_factory.rb', line 9 def initialize( ) = { :verbose => 0, :puppet_system_file => '/tmp/out.yaml', }.merge( ) # A hash to look up Config::Item values set from other sources (files, cli). # for each Hash element: # - the key will be the the Config::Item#key # - the value will be the @options#value @answers_hash = {} end |
Instance Method Details
#assign_value_from_hash(hash, item) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/simp/cli/config/item_list_factory.rb', line 146 def assign_value_from_hash( hash, item ) value = hash.fetch( item.key, nil ) if !value.nil? # workaround to allow cli/env var arrays value = value.split(',,') if item.is_a?(Simp::Cli::Config::ListItem) && !value.is_a?(Array) if ! item.validate value print_warning "'#{value}' is not an acceptable answer for '#{item.key}' (skipping)." else item.value = value end end item end |
#build_item_queue(item_queue, items) ⇒ Object
recursively build an item queue
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/simp/cli/config/item_list_factory.rb', line 196 def build_item_queue( item_queue, items ) writer = create_safety_writer_item if !items.empty? item = items.shift item_queue << writer if writer if item.is_a? String item_queue << create_item( item ) elsif item.is_a? Hash answers_tree = {} item.values.first.each{ |answer, values| answers_tree[ answer ] = build_item_queue( [], values ) } _item = create_item( item.keys.first ) _item.next_items_tree = answers_tree item_queue << _item item_queue << writer if writer end item_queue = build_item_queue( item_queue, items ) end # append a silent YAML writer to save progress after each item item_queue end |
#create_item(item_string) ⇒ Object
returns an instance of an Config::Item based on a String of its class name
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/simp/cli/config/item_list_factory.rb', line 162 def create_item item_string # create item instance parts = item_string.split( /\s+/ ) name = parts.shift item = Simp::Cli::Config::Item.const_get(name).new # set item options # ...based on YAML keywords while !parts.empty? part = parts.shift if part =~ /^#/ parts = [] next end item.silent = true if part == 'SILENT' item.skip_apply = true if part == 'NOAPPLY' item.skip_query = true if part == 'SKIPQUERY' item.skip_yaml = true if part == 'NOYAML' item.allow_user_apply = true if part == 'USERAPPLY' if part =~ /^FILE=(.+)/ item.file = $1 end end # ...based on cli options item.silent = true if .fetch( :verbose ) < 0 item.skip_apply = true if .fetch( :dry_run, false ) # (try to) assign item values from various sources item = assign_value_from_hash( @answers_hash, item ) end |
#create_safety_writer_item ⇒ Object
create a YAML writer that will “safety save” after each answer
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/simp/cli/config/item_list_factory.rb', line 226 def create_safety_writer_item if file = .fetch( :output_file, nil) FileUtils.mkdir_p File.dirname( file ), :verbose => false writer = Simp::Cli::Config::Item::AnswersYAMLFileWriter.new file = File.join( File.dirname( file ), ".#{File.basename( file )}" ) writer.file = file writer.allow_user_apply = true writer.silent = true if .fetch(:verbose, 0) < 2 writer end end |
#print_warning(error) ⇒ Object
238 239 240 |
# File 'lib/simp/cli/config/item_list_factory.rb', line 238 def print_warning error say "<%= color(%q{WARNING: }, YELLOW,BOLD) %><%= color(%q{#{error}}, YELLOW) %>\n" end |
#process(yaml = nil, answers_hash = {}) ⇒ Object
23 24 25 26 27 28 29 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 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/simp/cli/config/item_list_factory.rb', line 23 def process( yaml=nil, answers_hash={} ) @answers_hash = answers_hash # Require the config items rb_files = File.( '../config/item/*.rb', File.dirname(__FILE__)) Dir.glob( rb_files ).sort_by(&:to_s).each { |file| require file } items_yaml = yaml || " # The Config::Item list is really a conditional tree. Some Items can\n # prepend additional Items to the queue, depending on the answer.\n #\n # This YAML describes the full Item structure. The format is:\n #\n # - ItemA\n # - ItemB\n # answer1:\n # - ItemC\n # - ItemD\n # answer2:\n # - ItemE\n # - ItemF\n # - ItemG\n #\n # modifers:\n # USERAPPLY = execute apply() even when running non-privileged\n # SILENT = set the Item's .silent flag to true\n ---\n # ==== network ====\n - UseFips\n - NetworkInterface\n - SetupNIC:\n true:\n - DHCP:\n static: # gather info first, then configure network\n - Hostname\n - IPAddress\n - Netmask\n - Gateway\n - DNSServers\n - DNSSearch\n - NetworkConf\n dhcp: # configure network, then get info (silently)\n - NetworkConf\n - Hostname SILENT\n - IPAddress SILENT\n - Netmask SILENT\n - Gateway SILENT\n - DNSServers SILENT\n - DNSSearch SILENT\n false: # don't configure network (but ask for info)\n - Hostname\n - IPAddress\n - Netmask\n - Gateway\n - DNSServers\n - DNSSearch\n - HostnameConf\n - ClientNets\n\n # ==== globals ====\n - NTPServers NOAPPLY\n - LogServers\n - FailoverLogServers\n - SimpYumServers\n - UseAuditd\n - UseIPtables\n - CommonRunLevelDefault\n - UseSELinux\n - SetGrubPassword:\n true:\n - GrubPassword\n - Certificates\n - IsMasterYumServer\n - YumRepositories\n - RenameFqdnYaml\n\n # ==== puppet ====\n - PuppetServer\n - PuppetServerIP\n - PuppetCA\n - PuppetCAPort\n ### NOTE: removed since update to puppet server: - PuppetFileServer\n - PuppetAutosign\n - PuppetConf\n - PuppetHostsEntry\n - PuppetDBServer\n - PuppetDBPort\n\n # ==== ldap ====\n - UseLdap:\n true:\n - AddLdapToHiera\n - LdapBaseDn\n - LdapBindDn\n - LdapBindPw\n - LdapBindHash\n - LdapSyncDn\n - LdapSyncPw\n - LdapSyncHash\n - LdapRootDn\n - LdapRootHash\n - LdapMaster\n - LdapUri\n false:\n - RemoveLdapFromHiera\n - SssdDomains\n\n # ==== rsync ====\n - RsyncBase\n - RsyncServer\n - RsyncTimeout\n\n # ==== writers ====\n - AnswersYAMLFileWriter FILE=\#{ @options.fetch( :puppet_system_file, '/dev/null') }\n - AnswersYAMLFileWriter FILE=\#{ @options.fetch( :output_file, '/dev/null') } USERAPPLY\n EOF\n items = YAML.load items_yaml\n item_queue = build_item_queue( [], items )\n item_queue\nend\n".gsub(/^ {6}/,'') |