| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | package setup
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/file"
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/secondary"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Secondary sets up the secondary middleware.
 | 
					
						
							|  |  |  | func Secondary(c *Controller) (middleware.Middleware, error) {
 | 
					
						
							|  |  |  | 	zones, err := secondaryParse(c)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 	// Add startup functions to retrieve the zone and keep it up to date.
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	for _, n := range zones.Names {
 | 
					
						
							|  |  |  | 		if len(zones.Z[n].TransferFrom) > 0 {
 | 
					
						
							|  |  |  | 			c.Startup = append(c.Startup, func() error {
 | 
					
						
							| 
									
										
										
										
											2016-04-13 20:14:03 +01:00
										 |  |  | 				zones.Z[n].StartupOnce.Do(func() {
 | 
					
						
							|  |  |  | 					zones.Z[n].TransferIn()
 | 
					
						
							|  |  |  | 					go func() {
 | 
					
						
							|  |  |  | 						zones.Z[n].Update()
 | 
					
						
							|  |  |  | 					}()
 | 
					
						
							|  |  |  | 				})
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 				return nil
 | 
					
						
							|  |  |  | 			})
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return func(next middleware.Handler) middleware.Handler {
 | 
					
						
							|  |  |  | 		return secondary.Secondary{file.File{Next: next, Zones: zones}}
 | 
					
						
							|  |  |  | 	}, nil
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func secondaryParse(c *Controller) (file.Zones, error) {
 | 
					
						
							|  |  |  | 	z := make(map[string]*file.Zone)
 | 
					
						
							|  |  |  | 	names := []string{}
 | 
					
						
							|  |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		if c.Val() == "secondary" {
 | 
					
						
							|  |  |  | 			// secondary [origin]
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 			origins := []string{c.ServerBlockHosts[c.ServerBlockHostIndex]}
 | 
					
						
							|  |  |  | 			args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 			if len(args) > 0 {
 | 
					
						
							|  |  |  | 				origins = args
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			for i, _ := range origins {
 | 
					
						
							|  |  |  | 				origins[i] = middleware.Host(origins[i]).Normalize()
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 				z[origins[i]] = file.NewZone(origins[i], "stdin")
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				names = append(names, origins[i])
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for c.NextBlock() {
 | 
					
						
							|  |  |  | 				t, f, e := parseTransfer(c)
 | 
					
						
							|  |  |  | 				if e != nil {
 | 
					
						
							|  |  |  | 					return file.Zones{}, e
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				for _, origin := range origins {
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 					if t != nil {
 | 
					
						
							|  |  |  | 						z[origin].TransferTo = append(z[origin].TransferTo, t...)
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 					if f != nil {
 | 
					
						
							|  |  |  | 						z[origin].TransferFrom = append(z[origin].TransferFrom, f...)
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return file.Zones{Z: z, Names: names}, nil
 | 
					
						
							|  |  |  | }
 |