/main.go

https://bitbucket.org/marthjod/gocart · Go · 133 lines · 115 code · 18 blank · 0 comment · 24 complexity · 7f39fb913dfe98e0e8cfd4a2d4c8e3f7 MD5 · raw file

  1. package main
  2. import (
  3. "crypto/tls"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "os"
  9. "runtime/pprof"
  10. "sort"
  11. "github.com/marthjod/gocart/api"
  12. "github.com/marthjod/gocart/hostpool"
  13. "github.com/marthjod/gocart/vmpool"
  14. )
  15. func main() {
  16. var (
  17. verbose bool
  18. cluster string
  19. cpuprofile string
  20. user string
  21. password string
  22. url string
  23. patternFilter string
  24. patternFilterPrefix string
  25. patternFilterInfix string
  26. patternFilterSuffix string
  27. )
  28. flag.StringVar(&cluster, "cluster", "", "Cluster name for host pool lookups")
  29. flag.BoolVar(&verbose, "v", false, "Verbose mode")
  30. flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
  31. flag.StringVar(&user, "user", "", `OpenNebula User`)
  32. flag.StringVar(&password, "password", "", `OpenNebula Password`)
  33. flag.StringVar(&url, "url", "https://localhost:61443/RPC2", "OpenNebula XML-RPC API URL")
  34. flag.StringVar(&patternFilter, "pattern-filter", "^([a-z]{2}).+([a-z]{2})$", "Regexp filter for distinct VM name pattern auto-discovery")
  35. flag.StringVar(&patternFilterPrefix, "pattern-filter-prefix", "^", "Prefix for distinct VM name patterns")
  36. flag.StringVar(&patternFilterInfix, "pattern-filter-infix", ".+", "Infix for distinct VM name patterns")
  37. flag.StringVar(&patternFilterSuffix, "pattern-filter-suffix", "$", "Suffix for distinct VM name patterns")
  38. flag.Parse()
  39. if cpuprofile != "" {
  40. f, err := os.Create(cpuprofile)
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. pprof.StartCPUProfile(f)
  45. defer pprof.StopCPUProfile()
  46. }
  47. tr := &http.Transport{
  48. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  49. }
  50. apiClient, err := api.NewClient(url, user, password, tr)
  51. if err != nil {
  52. fmt.Println(err.Error())
  53. os.Exit(1)
  54. }
  55. vmPool := vmpool.NewVmPool()
  56. if err := apiClient.Call(vmPool); err != nil {
  57. fmt.Println(err.Error())
  58. os.Exit(1)
  59. }
  60. hostPool := hostpool.NewHostPool()
  61. if err := apiClient.Call(hostPool); err != nil {
  62. fmt.Println(err.Error())
  63. os.Exit(1)
  64. }
  65. if verbose {
  66. for i := 0; i < len(vmPool.Vms); i++ {
  67. vm := vmPool.Vms[i]
  68. fmt.Printf("%v %v (CPU: %v, template/mem: %v)\n",
  69. vm.Id, vm.Name, vm.Cpu, vm.Template.Memory)
  70. }
  71. }
  72. hostPool.MapVms(vmPool)
  73. if verbose {
  74. for i := 0; i < len(hostPool.Hosts); i++ {
  75. host := hostPool.Hosts[i]
  76. fmt.Printf("%v %v\n", host.Id, host.Template.Datacenter)
  77. fmt.Printf("%v %d \n", host.Name, host.State)
  78. }
  79. }
  80. clusterHosts := hostPool.GetHostsInCluster(cluster)
  81. var distinctPatternsInCluster = make(map[string]bool, 0)
  82. for _, h := range clusterHosts.Hosts {
  83. fmt.Printf("Host %q runs %d VM(s)\n", h.Name, len(h.VmPool.Vms))
  84. for _, vm := range h.VmPool.Vms {
  85. fmt.Printf("%s\n", vm.Name)
  86. }
  87. distinctPattterns := h.VmPool.GetDistinctVmNamePatterns(
  88. patternFilter, patternFilterPrefix, patternFilterInfix, patternFilterSuffix)
  89. fmt.Printf("Distinct VM name patterns on host %q: %v\n", h.Name, distinctPattterns)
  90. for pattern, _ := range distinctPattterns {
  91. distinctPatternsInCluster[pattern] = true
  92. }
  93. }
  94. var patterns = make([]string, 0)
  95. for pattern, _ := range distinctPatternsInCluster {
  96. patterns = append(patterns, pattern)
  97. }
  98. sort.Strings(patterns)
  99. fmt.Printf("Distinct VM name patterns in cluster %q: %s\n", cluster, patterns)
  100. billingVms, err := vmPool.GetVmsByName("^bil_.+")
  101. if err != nil {
  102. fmt.Println(err)
  103. return
  104. }
  105. fmt.Println("showing all billing vms")
  106. for _, bvm := range billingVms.Vms {
  107. fmt.Println(bvm.Name)
  108. fmt.Println("User Template:")
  109. acsFQDN, err := bvm.UserTemplate.Items.GetCustom("ACS_FQDN")
  110. if err != nil {
  111. fmt.Print(err.Error())
  112. }
  113. fmt.Printf("%s\n", acsFQDN)
  114. }
  115. }