func NewDirector(ctx context.Context, target string, user string, password string) (*Director, error) { var re *regexp.Regexp re = regexp.MustCompile("^http(s)?://") if re.FindString(target) == "" { target = "https://" + target } re = regexp.MustCompile(":\\d+$") if re.FindString(target) == "" { target = target + ":25555" } d := gogobosh.NewDirector(target, user, password) c := &Director{ director: api.NewBoshDirectorRepository(&d, net.NewDirectorGateway()), } err := c.Connect() if err != nil { if !strings.Contains(err.Error(), "connection refused") { return nil, err } log.Printf("[DEBUG] Connection to director not successful. Could be because " + "the Director does no exist yet. So connection has been deferred.") } else { log.Printf("[DEBUG] Connection to director successful.") } return c, nil }
func createDirectorRepo(reqs ...testhelpers.TestRequest) (ts *httptest.Server, handler *testhelpers.TestHandler, repo api.DirectorRepository) { ts, handler = testhelpers.NewTLSServer(reqs) config := &models.Director{ TargetURL: ts.URL, Username: "******", Password: "******", } gateway := net.NewDirectorGateway() repo = api.NewBoshDirectorRepository(config, gateway) return }
// FetchAndUpload fetches deployments from BOSH and uploads to collector API func (agent Agent) FetchAndUpload() { director := gogobosh.NewDirector(agent.Config.BOSHTarget, agent.Config.BOSHUsername, agent.Config.BOSHPassword) repo := api.NewBoshDirectorRepository(&director, net.NewDirectorGateway()) info, apiResponse := repo.GetInfo() if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch BOSH info") return } boshDeployments, apiResponse := repo.GetDeployments() if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch BOSH deployments") return } reallyUUID := ReallyUUID(agent.Config.BOSHTarget, info.UUID) uploadData := ToBOSH{ Name: info.Name, Target: agent.Config.BOSHTarget, UUID: info.UUID, ReallyUUID: reallyUUID, Version: info.Version, CPI: info.CPI, Deployments: models.Deployments{}, } fmt.Println("Data to upload", uploadData) b, err := json.Marshal(uploadData) if err != nil { log.Fatalln("MARSHAL ERROR", err) } uploadEndpoint := fmt.Sprintf("%s/upload", agent.Config.WebserverTarget) uploadDeploymentData(agent.Config, uploadEndpoint, bytes.NewReader(b)) for _, boshDeployment := range boshDeployments { deploymentName := boshDeployment.Name b, err = json.Marshal(boshDeployment) if err != nil { log.Fatalln("MARSHAL ERROR", err) } uploadEndpoint = fmt.Sprintf("%s/upload/%s/deployments/%s", agent.Config.WebserverTarget, reallyUUID, deploymentName) uploadDeploymentData(agent.Config, uploadEndpoint, bytes.NewReader(b)) } }
func main() { utils.Logger = utils.NewLogger() configPath, err := local.DefaultBoshConfigPath() if err != nil { fmt.Println(err) return } config, err := local.LoadBoshConfig(configPath) if err != nil { fmt.Println(err) return } target, username, password, err := config.CurrentBoshTarget() if err != nil { fmt.Println(err) return } fmt.Printf("Targeting %s with user %s...\n", target, username) director := gogobosh.NewDirector(target, username, password) repo := api.NewBoshDirectorRepository(&director, net.NewDirectorGateway()) info, apiResponse := repo.GetInfo() if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch BOSH info") return } fmt.Println("Director") fmt.Printf(" Name %s\n", info.Name) fmt.Printf(" URL %s\n", info.URL) fmt.Printf(" Version %s\n", info.Version) fmt.Printf(" User %s\n", info.User) fmt.Printf(" UUID %s\n", info.UUID) fmt.Printf(" CPI %s\n", info.CPI) }
func main() { utils.Logger = utils.NewLogger() target := flag.String("target", "https://192.168.50.4:25555", "BOSH director host") username := flag.String("username", "admin", "Login with username") password := flag.String("password", "admin", "Login with password") flag.Parse() director := gogobosh.NewDirector(*target, *username, *password) repo := api.NewBoshDirectorRepository(&director, net.NewDirectorGateway()) info, apiResponse := repo.GetInfo() if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch BOSH info") return } fmt.Println("Director") fmt.Printf(" Name %s\n", info.Name) fmt.Printf(" URL %s\n", info.URL) fmt.Printf(" Version %s\n", info.Version) fmt.Printf(" User %s\n", info.User) fmt.Printf(" UUID %s\n", info.UUID) fmt.Printf(" CPI %s\n", info.CPI) if info.DNSEnabled { fmt.Printf(" dns %#v (%s)\n", info.DNSEnabled, info.DNSDomainName) } else { fmt.Printf(" dns %#v\n", info.DNSEnabled) } if info.CompiledPackageCacheEnabled { fmt.Printf(" compiled_package_cache %#v (provider: %s)\n", info.CompiledPackageCacheEnabled, info.CompiledPackageCacheProvider) } else { fmt.Printf(" compiled_package_cache %#v\n", info.CompiledPackageCacheEnabled) } fmt.Printf(" snapshots %#v\n", info.SnapshotsEnabled) fmt.Println("") fmt.Printf("%#v\n", info) fmt.Println("") stemcells, apiResponse := repo.GetStemcells() if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch BOSH stemcells") return } else { for _, stemcell := range stemcells { fmt.Printf("%#v\n", stemcell) } fmt.Println("") } releases, apiResponse := repo.GetReleases() if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch BOSH releases") return } else { for _, release := range releases { fmt.Printf("%#v\n", release) } fmt.Println("") } deployments, apiResponse := repo.GetDeployments() if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch BOSH deployments") return } else { for _, deployment := range deployments { fmt.Printf("%#v\n", deployment) } fmt.Println("") } tasks, apiResponse := repo.GetTaskStatusesWithLimit(3) if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch tasks") return } else { fmt.Printf("%#v\n", tasks) } task, apiResponse := repo.GetTaskStatus(tasks[0].ID) if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch BOSH task 1") return } else { fmt.Printf("%#v\n", task) } fmt.Println("") fmt.Println("VMs in cf-warden deployment:") vms, apiResponse := repo.ListDeploymentVMs("cf-warden") if apiResponse.IsNotSuccessful() { fmt.Println("Could not get list of VM for cf-warden") return } else { for _, vm := range vms { fmt.Printf("%#v\n", vm) } } fmt.Println("") fmt.Println("VMs status in cf-warden deployment:") vmsStatuses, apiResponse := repo.FetchVMsStatus("cf-warden") if apiResponse.IsNotSuccessful() { fmt.Println("Could not fetch VMs status for cf-warden") return } else { for _, vmStatus := range vmsStatuses { fmt.Printf("%s/%d is %s, IPs %#v\n", vmStatus.JobName, vmStatus.Index, vmStatus.JobState, vmStatus.IPs) } } }