| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | package reload
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"crypto/md5"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 07:41:56 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/log"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // reload periodically checks if the Corefile has changed, and reloads if so
 | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | const (
 | 
					
						
							|  |  |  | 	unused    = 0
 | 
					
						
							|  |  |  | 	maybeUsed = 1
 | 
					
						
							|  |  |  | 	used      = 2
 | 
					
						
							|  |  |  | )
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | type reload struct {
 | 
					
						
							|  |  |  | 	interval time.Duration
 | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 	usage    int
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 	quit     chan bool
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func hook(event caddy.EventName, info interface{}) error {
 | 
					
						
							|  |  |  | 	if event != caddy.InstanceStartupEvent {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// if reload is removed from the Corefile, then the hook
 | 
					
						
							|  |  |  | 	// is still registered but setup is never called again
 | 
					
						
							|  |  |  | 	// so we need a flag to tell us not to reload
 | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 	if r.usage == unused {
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// this should be an instance. ok to panic if not
 | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 	instance := info.(*caddy.Instance)
 | 
					
						
							|  |  |  | 	md5sum := md5.Sum(instance.Caddyfile().Body())
 | 
					
						
							| 
									
										
										
										
											2018-04-19 07:41:56 +01:00
										 |  |  | 	log.Infof("Running configuration MD5 = %x\n", md5sum)
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	go func() {
 | 
					
						
							|  |  |  | 		tick := time.NewTicker(r.interval)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for {
 | 
					
						
							|  |  |  | 			select {
 | 
					
						
							|  |  |  | 			case <-tick.C:
 | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 				corefile, err := caddy.LoadCaddyfile(instance.Caddyfile().ServerType())
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 				if err != nil {
 | 
					
						
							|  |  |  | 					continue
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				s := md5.Sum(corefile.Body())
 | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 				if s != md5sum {
 | 
					
						
							|  |  |  | 					// Let not try to restart with the same file, even though it is wrong.
 | 
					
						
							|  |  |  | 					md5sum = s
 | 
					
						
							|  |  |  | 					// now lets consider that plugin will not be reload, unless appear in next config file
 | 
					
						
							|  |  |  | 					// change status iof usage will be reset in setup if the plugin appears in config file
 | 
					
						
							|  |  |  | 					r.usage = maybeUsed
 | 
					
						
							|  |  |  | 					_, err := instance.Restart(corefile)
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 					if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-04-19 07:41:56 +01:00
										 |  |  | 						log.Errorf("Corefile changed but reload failed: %s\n", err)
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 						continue
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 					// we are done, if the plugin was not set used, then it is not.
 | 
					
						
							|  |  |  | 					if r.usage == maybeUsed {
 | 
					
						
							|  |  |  | 						r.usage = unused
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 					return
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			case <-r.quit:
 | 
					
						
							|  |  |  | 				return
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |