@@ -139,63 +139,57 @@ def getconfig
139139 facts = self . class . facts
140140 end
141141
142- if self . configuration or FileTest . exists? ( self . cachefile )
143- if self . fresh? ( facts )
144- Puppet . info "Config is up to date"
145- if self . configuration
146- return
147- end
148- if oldtext = self . retrievecache
149- begin
150- @configuration = YAML . load ( oldtext ) . to_configuration
151- rescue => detail
152- Puppet . warning "Could not load cached configuration: %s" % detail
153- end
154- return
155- end
156- end
157- end
158- Puppet . debug ( "getting config" )
142+ raise Puppet ::Network ::ClientError . new ( "Could not retrieve any facts" ) unless facts . length > 0
159143
160144 # Retrieve the plugins.
161- if Puppet [ :pluginsync ]
162- getplugins ( )
163- end
145+ getplugins ( ) if Puppet [ :pluginsync ]
164146
165- unless facts . length > 0
166- raise Puppet ::Network ::ClientError . new (
167- "Could not retrieve any facts"
168- )
147+ if ( self . configuration or FileTest . exist? ( self . cachefile ) ) and self . fresh? ( facts )
148+ Puppet . info "Configuration is up to date"
149+ return if use_cached_config
169150 end
170151
171- unless objects = get_actual_config ( facts )
172- @configuration = nil
152+ Puppet . debug ( "Retrieving configuration" )
153+
154+ # If we can't retrieve the configuration, just return, which will either
155+ # fail, or use the in-memory configuration.
156+ unless yaml_objects = get_actual_config ( facts )
157+ use_cached_config ( true )
173158 return
174159 end
175160
176- unless objects . is_a? ( Puppet ::TransBucket )
177- raise NetworkClientError ,
178- "Invalid returned objects of type %s" % objects . class
161+ begin
162+ objects = YAML . load ( yaml_objects )
163+ rescue => detail
164+ msg = "Configuration could not be translated from yaml"
165+ msg += "; using cached configuration" if use_cached_config ( true )
166+ Puppet . warning msg
167+ return
179168 end
180169
181170 self . setclasses ( objects . classes )
182171
183172 # Clear all existing objects, so we can recreate our stack.
184- if self . configuration
185- clear ( )
186- end
173+ clear ( ) if self . configuration
187174
188175 # Now convert the objects to a puppet configuration graph.
189- @configuration = objects . to_configuration
176+ begin
177+ @configuration = objects . to_configuration
178+ rescue => detail
179+ clear ( )
180+ puts detail . backtrace if Puppet [ :trace ]
181+ msg = "Configuration could not be instantiated: %s" % detail
182+ msg += "; using cached configuration" if use_cached_config ( true )
183+ Puppet . warning msg
184+ return
185+ end
190186
191- if @configuration . nil?
192- raise Puppet :: Error , "Configuration could not be processed"
187+ if ! @configuration . from_cache
188+ self . cache ( yaml_objects )
193189 end
194190
195191 # Keep the state database up to date.
196192 @configuration . host_config = true
197-
198- return @configuration
199193 end
200194
201195 # A simple proxy method, so it's easy to test.
@@ -270,11 +264,9 @@ def run(options = {})
270264 Puppet . err "Could not retrieve configuration: %s" % detail
271265 end
272266
273- if defined? @configuration and @ configuration
267+ if self . configuration
274268 @configuration . retrieval_duration = duration
275- unless @local
276- Puppet . notice "Starting configuration run"
277- end
269+ Puppet . notice "Starting configuration run" unless @local
278270 benchmark ( :notice , "Finished configuration run" ) do
279271 @configuration . apply ( options )
280272 end
@@ -500,34 +492,16 @@ def facts_changed?(facts)
500492 # Actually retrieve the configuration, either from the server or from a
501493 # local master.
502494 def get_actual_config ( facts )
503- if @local
504- return get_local_config ( facts )
505- else
506- begin
507- Timeout ::timeout ( self . class . timeout ) do
508- return get_remote_config ( facts )
509- end
510- rescue Timeout ::Error
511- Puppet . err "Configuration retrieval timed out"
512- return nil
495+ begin
496+ Timeout ::timeout ( self . class . timeout ) do
497+ return get_remote_config ( facts )
513498 end
499+ rescue Timeout ::Error
500+ Puppet . err "Configuration retrieval timed out"
501+ return nil
514502 end
515503 end
516504
517- # Retrieve a configuration from a local master.
518- def get_local_config ( facts )
519- # If we're local, we don't have to do any of the conversion
520- # stuff.
521- objects = @driver . getconfig ( facts , "yaml" )
522- @compile_time = Time . now
523-
524- if objects == ""
525- raise Puppet ::Error , "Could not retrieve configuration"
526- end
527-
528- return objects
529- end
530-
531505 # Retrieve a config from a remote master.
532506 def get_remote_config ( facts )
533507 textobjects = ""
@@ -545,45 +519,18 @@ def get_remote_config(facts)
545519 end
546520
547521 rescue => detail
548- puts detail . backtrace
549522 Puppet . err "Could not retrieve configuration: %s" % detail
550-
551- unless Puppet [ :usecacheonfailure ]
552- @configuration = nil
553- Puppet . warning "Not using cache on failed configuration"
554- return
555- end
523+ return nil
556524 end
557525 end
558526
559- fromcache = false
560- if textobjects == ""
561- unless textobjects = self . retrievecache
562- raise Puppet ::Error . new (
563- "Cannot connect to server and there is no cached configuration"
564- )
565- end
566- Puppet . warning "Could not get config; using cached copy"
567- fromcache = true
568- else
569- @compile_time = Time . now
570- Puppet ::Util ::Storage . cache ( :configuration ) [ :facts ] = facts
571- Puppet ::Util ::Storage . cache ( :configuration ) [ :compile_time ] = @compile_time
572- end
527+ return nil if textobjects == ""
573528
574- begin
575- objects = YAML . load ( textobjects )
576- rescue => detail
577- raise Puppet ::Error ,
578- "Could not understand configuration: %s" %
579- detail . to_s
580- end
529+ @compile_time = Time . now
530+ Puppet ::Util ::Storage . cache ( :configuration ) [ :facts ] = facts
531+ Puppet ::Util ::Storage . cache ( :configuration ) [ :compile_time ] = @compile_time
581532
582- if @cache and ! fromcache
583- self . cache ( textobjects )
584- end
585-
586- return objects
533+ return textobjects
587534 end
588535
589536 def lockfile
@@ -609,4 +556,32 @@ def splay
609556 Puppet . info "Sleeping for %s seconds (splay is enabled)" % time
610557 sleep ( time )
611558 end
559+
560+ private
561+
562+ # Use our cached config, optionally specifying whether this is
563+ # necessary because of a failure.
564+ def use_cached_config ( because_of_failure = false )
565+ return true if self . configuration
566+
567+ if because_of_failure and ! Puppet [ :usecacheonfailure ]
568+ @configuration = nil
569+ Puppet . warning "Not using cache on failed configuration"
570+ return false
571+ end
572+
573+ return false unless oldtext = self . retrievecache
574+
575+ begin
576+ @configuration = YAML . load ( oldtext ) . to_configuration
577+ @configuration . from_cache = true
578+ @configuration . host_config = true
579+ rescue => detail
580+ puts detail . backtrace if Puppet [ :trace ]
581+ Puppet . warning "Could not load cached configuration: %s" % detail
582+ clear
583+ return false
584+ end
585+ return true
586+ end
612587end
0 commit comments