Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "flag"
  5. "fmt"
  6. "net/url"
  7. "os"
  8. "strings"
  9. "time"
  10. )
  11.  
  12. const (
  13. // LogLocationStdout indicates the stdout IO stream
  14. LogLocationStdout = "stdout"
  15. // LogLocationStderr indicates the stderr IO stream
  16. LogLocationStderr = "stderr"
  17. // LogLocationNull indicates the null IO stream (/dev/null)
  18. LogLocationNull = "null"
  19. )
  20.  
  21. type config struct {
  22. // RFCCompliant determines whether `Cache-Control: no-cache` requests are honored. The ability to ignore `no-cache` is necessary to protect origin servers from DDOS attacks. In general, CDNs and caching proxies with the goal of origin protection should set RFCComplaint false. Cache with other goals (performance, load balancing, etc) should set RFCCompliant true.
  23. RFCCompliant bool `json:"rfc_compliant"`
  24. dispersion int
  25. loginDispersion int
  26. retries int
  27. waitForParents bool
  28. logLevel int
  29. toURL url
  30. toCacheURL url
  31. mode string
  32. username string
  33. password string
  34. LogLocationError string `json:"log_location_error"`
  35. LogLocationWarning string `json:"log_location_warning"`
  36. LogLocationInfo string `json:"log_location_info"`
  37. LogLocationDebug string `json:"log_location_debug"`
  38. LogLocationEvent string `json:"log_location_event"`
  39. }
  40.  
  41. func main() {
  42.  
  43. parseCommand()
  44.  
  45. hostname, error := os.Hostname()
  46. if error != nil {
  47. panic(error)
  48. }
  49. fmt.Println("Starting config check at", time.Now())
  50.  
  51. shortHostname := strings.Split(hostname, ".")[0]
  52.  
  53. fmt.Println("full hostname: ", hostname)
  54. fmt.Println("short hostname: ", shortHostname)
  55. fmt.Println(username, password, mode, urlString)
  56. fmt.Println(dispersion)
  57.  
  58. }
  59.  
  60. func parseCommand() {
  61.  
  62. var dispersionPtr = flag.Int("dispersion", 300, "Dispersion range in seconds.")
  63. var loginDispersionPtr = flag.Int("login_dispersion", 0, "Login dispersion range in seconds.")
  64. var retriesPtr = flag.Int("retries", 3, "Retry connection attempts to TO URL <number> times.")
  65. var waitForParentsPtr = flag.Bool("wait_for_parents", true, "Do not update if a parent cache is still pending.")
  66. var logLevelPtr = flag.String("log_level", "WARN", "Log level: <ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, NONE> Default:WARN")
  67. flag.Parse()
  68.  
  69. var logLevel int
  70. switch *logLevelPtr {
  71. case "ALL":
  72. logLevel = 7
  73. case "TRACE":
  74. logLevel = 6
  75. case "DEBUG":
  76. logLevel = 5
  77. case "INFO":
  78. logLevel = 4
  79. case "WARN":
  80. logLevel = 3
  81. case "ERROR":
  82. logLevel = 2
  83. case "FATAL":
  84. logLevel = 1
  85. case "NONE":
  86. logLevel = 0
  87. }
  88.  
  89. args := flag.Args()
  90.  
  91. if len(args) < 3 {
  92. usage()
  93. }
  94.  
  95. mode = args[0]
  96. if mode != "syncds" && mode != "report" && mode != "badass" && mode != "revalidate" {
  97. usage()
  98. }
  99.  
  100. toURL, e := url.Parse(args[1])
  101. if e != nil {
  102. usage()
  103. }
  104.  
  105. if strings.Count(args[2], ":") == 1 {
  106. username := strings.Split(args[2], ":")[0]
  107. password := strings.Split(args[2], ":")[1]
  108. } else {
  109. usage()
  110. }
  111.  
  112. var config = Config{
  113. dispersion: *dispersionPtr,
  114. loginDispersion: *loginDispersionPtr,
  115. retries: *retriesPtr,
  116. waitForParents: *waitForParentsPtr,
  117. logLevel: logLevel,
  118. username: username,
  119. password: password,
  120. toURL: toURL,
  121. mode: mode,
  122. LogLocationError: LogLocationStderr,
  123. LogLocationWarning: LogLocationStdout,
  124. LogLocationInfo: LogLocationNull,
  125. LogLocationDebug: LogLocationNull,
  126. LogLocationEvent: LogLocationStdout,
  127. }
  128. return
  129. }
  130.  
  131. func usage() {
  132. fmt.Println(`====-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-====
  133.  
  134. Usage: traffic_ops_ort [optional flags] <mode> <log_level> <Traffic_Ops_URL> <Traffic_Ops_Login>
  135.  
  136.  
  137. <mode> = interactive - asks questions during config process.
  138. <mode> = report - prints config differences and exits.
  139. <mode> = badass - attempts to fix all config differences that it can.
  140. <mode> = syncds - syncs delivery services with what is configured in Traffic Ops.
  141. <mode> = revalidate - checks for updated revalidations in Traffic Ops and applies them. Requires Traffic Ops 2.1.
  142.  
  143. <Traffic_Ops_URL> = URL to Traffic Ops host. Example: https://trafficops.company.net
  144.  
  145. <Traffic_Ops_Login> => Example: 'username:password'
  146.  
  147. [optional flags]:
  148. --dispersion=<time> => wait a random number between 0 and <time> before starting. Default = 300.
  149. --login_dispersion=<time> => wait a random number between 0 and <time> before login. Default = 0.
  150. --retries=<number> => retry connection to Traffic Ops URL <number> times. Default = 3.
  151. --wait_for_parents=<0|1> => do not update if parent_pending = 1 in the update json. Default = 1, wait for parents.
  152. --log_level=<ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, NONE> => Logging level. Default is WARN
  153.  
  154. ====-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-====`)
  155. os.Exit(0)
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement