Beispiel #1
0
func main() {
	// 1. Retrieve login and endpoint information
	email := flag.String("e", "", "Login email")
	pwd := flag.String("p", "", "Login password")
	host := flag.String("h", "us-3.rightscale.com", "RightScale API host")
	conf := flag.String("c", "", "Configuration file")
	insecure := flag.Bool("insecure", false, "Use HTTP instead of HTTPS - used for testing")
	flag.Parse()
	if *email == "" {
		fail("Login email required")
	}
	if *pwd == "" {
		fail("Login password required")
	}
	if *host == "" {
		fail("Host required")
	}
	if *conf == "" {
		usr, _ := user.Current()
		*conf = usr.HomeDir + "/.rsssh"
	}

	// Read the configuration
	content, err := ioutil.ReadFile(*conf)
	if err != nil {
		fail("Failed to read configuration file: %v\n", err.Error())
	}
	var config Config
	err = json.Unmarshal(content, &config)
	if err != nil {
		fail("Failed to Unmarshal configuration file: %v\n", err.Error())
	}
	if config.OutputFile == "" {
		config.OutputFile = "rightscale_aliases"
	}
	if config.SSHUser == "" {
		config.SSHUser = "******"
	}
	var sshConfig = make([]SSHConfig, 0)

	for envName, envDetail := range config.Environments {
		if envDetail.Account == 0 {
			fail("No account specified for environment: %v\n", envName)
		}
		// Create a new client for every environment because they can be in different accounts.
		auth := rsapi.NewBasicAuthenticator(*email, *pwd, envDetail.Account)
		client := cm15.New(*host, auth)
		if *insecure {
			httpclient.Insecure = true
		}
		if err := client.CanAuthenticate(); err != nil {
			fail("invalid credentials: %s", err)
		}
		fetchDetails(client, envName, envDetail, &sshConfig)
	}

	aliases := buildAliases(sshConfig, config.SSHOptions, config.SSHUser)
	writeFile(config.OutputFile, []byte(aliases), 0644)
}
Beispiel #2
0
func main() {
	// 1. Retrieve login and endpoint information
	email := flag.String("e", "", "Login email")
	pwd := flag.String("p", "", "Login password")
	account := flag.Int("a", 0, "Account id")
	host := flag.String("h", "us-3.rightscale.com", "RightScale API host")
	insecure := flag.Bool("insecure", false, "Bypass certificate check, used for testing")
	cat := flag.String("cat", "", "Path to CAT file")
	flag.Parse()
	if *email == "" {
		fail("Login email required")
	}
	if *pwd == "" {
		fail("Login password required")
	}
	if *account == 0 {
		fail("Account id required")
	}
	if *host == "" {
		fail("Host required")
	}
	if *insecure {
		httpclient.NoCertCheck = true
	}
	if *cat == "" {
		fail("Path to CAT required")
	}
	if _, err := os.Stat(*cat); err != nil {
		fail("Can't find file %s", *cat)
	}

	// 2. Setup client using basic auth
	auth := rsapi.NewBasicAuthenticator(*email, *pwd, *account)
	ssAuth := rsapi.NewSSAuthenticator(auth, *account)
	client := ssd.New(*host, ssAuth)
	if err := client.CanAuthenticate(); err != nil {
		fail("invalid credentials: %s", err)
	}

	// 3. Make execution index call using expanded view
	file, err := os.Open(*cat)
	if err != nil {
		fail("Could not open %s", *cat)
	}
	name := filepath.Base(*cat)
	l := client.TemplateLocator(fmt.Sprintf("/designer/collections/%d/templates", *account))
	upload := rsapi.FileUpload{Name: name, Filename: name, Reader: file}
	t, err := l.Create(&upload)
	if err != nil {
		fail("failed to create template: %s", err)
	}
	created, err := t.Show(rsapi.ApiParams{})
	if err != nil {
		fail("failed to retrieve created template: %s", err)
	}
	fmt.Fprintf(osStdout, "%s created at %s by %s\n",
		created.Name, created.Timestamps.CreatedAt, created.CreatedBy.Name)
}
Beispiel #3
0
func main() {
	// 1. Retrieve login and endpoint information
	email := flag.String("e", "", "Login email")
	pwd := flag.String("p", "", "Login password")
	account := flag.Int("a", 0, "Account id")
	host := flag.String("h", "us-3.rightscale.com", "RightScale API host")
	filterEmail := flag.String("f", "", "Audit user email for filtering audit entries")
	flag.Parse()
	if *email == "" {
		fail("Login email required")
	}
	if *pwd == "" {
		fail("Login password required")
	}
	if *account == 0 {
		fail("Account id required")
	}
	if *host == "" {
		fail("Host required")
	}
	if *filterEmail == "" {
		*filterEmail = *email
	}

	// 2. Setup client using basic auth
	auth := rsapi.NewBasicAuthenticator(*email, *pwd, *account)
	client := cm15.New(*host, auth)
	if err := client.CanAuthenticate(); err != nil {
		fail("invalid credentials: %s", err)
	}
	ticker := time.NewTicker(time.Second)

	// 3. Make an initial API call and retrieve the audit entries
	entries, err := fetchAuditEntries(client, *filterEmail)
	oldEntries := entries
	if err != nil {
		fail("Failed to retrieve audit entries: %v\n", err.Error())
	}
	printAudits(entries)

	// 4. Periodically retrieve audit entries and print the new ones
	for {
		select {
		case <-ticker.C:
			entries, err := fetchAuditEntries(client, *filterEmail)
			if err != nil {
				fail("Failed to retrieve audit entries: %v\n", err.Error())
			}
			printAudits(extractUnique(oldEntries, entries))
			oldEntries = entries
		}
	}
}
Beispiel #4
0
func main() {
	// 1. Retrieve login and endpoint information
	email := flag.String("e", "", "Login email")
	pwd := flag.String("p", "", "Login password")
	account := flag.Int("a", 0, "Account id")
	host := flag.String("h", "us-3.rightscale.com", "RightScale API host")
	insecure := flag.Bool("insecure", false, "Use HTTP instead of HTTPS - used for testing")
	flag.Parse()
	if *email == "" {
		fail("Login email required")
	}
	if *pwd == "" {
		fail("Login password required")
	}
	if *account == 0 {
		fail("Account id required")
	}
	if *host == "" {
		fail("Host required")
	}

	// 2. Setup client using basic auth
	auth := rsapi.NewBasicAuthenticator(*email, *pwd, *account)
	ssAuth := rsapi.NewSSAuthenticator(auth, *account)
	client := ssm.New(*host, ssAuth)
	if *insecure {
		httpclient.Insecure = true
	}
	if err := client.CanAuthenticate(); err != nil {
		fail("invalid credentials: %s", err)
	}

	// 3. Make execution index call using expanded view
	l := client.ExecutionLocator(fmt.Sprintf("/projects/%d/executions", *account))
	execs, err := l.Index(rsapi.APIParams{})
	if err != nil {
		fail("failed to list executions: %s", err)
	}
	sort.Sort(ByName(execs))

	// 4. Print executions launch from
	w := new(tabwriter.Writer)
	w.Init(osStdout, 5, 0, 1, ' ', 0)
	fmt.Fprintln(w, "Name\tState\tBy\tLink")
	fmt.Fprintln(w, "-----\t-----\t-----\t-----")
	for _, e := range execs {
		link := fmt.Sprintf("https://%s/manager/#/exe/%s", ss.HostFromLogin(client.Host),
			e.Id)
		fmt.Fprintln(w, fmt.Sprintf("%s\t%s\t%s\t%s", e.Name, e.Status, e.CreatedBy.Email, link))
	}
	w.Flush()
}
Beispiel #5
0
func main() {
	// 1. Retrieve login and endpoint information
	email := flag.String("e", "", "Login email")
	pwd := flag.String("p", "", "Login password")
	account := flag.Int("a", 0, "Account id")
	host := flag.String("h", "us-3.rightscale.com", "RightScale API host")
	insecure := flag.Bool("insecure", false, "Use HTTP instead of HTTPS - used for testing")
	flag.Parse()
	if *email == "" {
		fail("Login email required")
	}
	if *pwd == "" {
		fail("Login password required")
	}
	if *account == 0 {
		fail("Account id required")
	}
	if *host == "" {
		fail("Host required")
	}

	// 2. Setup client using basic auth
	auth := rsapi.NewBasicAuthenticator(*email, *pwd, *account)
	client := cm15.New(*host, auth)
	if *insecure {
		httpclient.Insecure = true
	}
	if err := client.CanAuthenticate(); err != nil {
		fail("invalid credentials: %s", err)
	}

	// 3. Make cloud index call using extended view
	l := client.CloudLocator("/api/clouds")
	clouds, err := l.Index(rsapi.APIParams{"view": "extended"})
	if err != nil {
		fail("failed to list clouds: %s", err)
	}

	// 4. Print cloud capabilities
	for i, c := range clouds {
		fmt.Fprintln(osStdout, "")
		fmt.Fprintf(osStdout, "%d. %s\n", i+1, c.Name)
		for _, ca := range c.Capabilities {
			fmt.Fprintf(osStdout, "%s: %v\n", ca["name"], ca["value"])
		}
	}
}
Beispiel #6
0
func main() {
	// Retrieve login and endpoint information
	email := flag.String("e", "", "Login email")
	pwd := flag.String("p", "", "Login password")
	account := flag.Int("a", 0, "Account id")
	host := flag.String("h", "us-3.rightscale.com", "RightScale API host")
	deploymentId := flag.String("d", "", "Deployment id")
	flag.Parse()
	if *email == "" {
		fmt.Println("Login email required")
	}
	if *pwd == "" {
		fmt.Println("Login password required")
	}
	if *account == 0 {
		fmt.Println("Account id required")
	}
	if *host == "" {
		fmt.Println("Host required")
	}
	if *deploymentId == "" {
		fmt.Println("Deployment required")
	}

	// Setup client using basic auth
	auth := rsapi.NewBasicAuthenticator(*email, *pwd, *account)
	client := cm15.New(*host, auth)

	if err := client.CanAuthenticate(); err != nil {
		fmt.Println("invalid credentials: %s", err)
	}

	// Deployment show
	deploymentLocator := client.DeploymentLocator("/api/deployments/" + *deploymentId)
	deployment, err := deploymentLocator.Show(rsapi.ApiParams{})
	if err != nil {
		fmt.Println("failed to find deployment: %s", err)
	}
	// Initialize the template maps to store only unique server templates
	templates = make(map[string]string)
	serversLocator := extractHref(deployment.Links, "servers")
	servers := serversRetrieve(client, serversLocator)
	serverArraysLocator := extractHref(deployment.Links, "server_arrays")
	serverArrays := serverArraysRetrieve(client, serverArraysLocator)
	var serverTemplates = make([]ServerTemplate, len(templates))
	i := 0
	for key, _ := range templates {
		template := templateRetrieve(client, key)
		st := ServerTemplate{
			Name:     template.Name,
			Revision: template.Revision,
		}
		runnableBindings := runnableBindingsRetrieve(client, extractHref(template.Links, "runnable_bindings"))
		cookbookAttachments := cookbookAttachmentsRetrieve(client, extractHref(template.Links, "cookbook_attachments"))
		cookbooks := extractCookbooks(client, cookbookAttachments)
		st.RightScripts, st.Recipes = extractAttachmentsInfo(client, runnableBindings, cookbooks)
		alertSpecs := alertsRetrieve(client, extractHref(template.Links, "alert_specs"))
		alerts = append(alerts, alertSpecs...)
		serverTemplates[i] = st
		i++
	}
	var alertSpecs = make([]Alert, len(alerts))
	for i = 0; i < len(alerts); i++ {
		alert := Alert{
			Name:           alerts[i].Name,
			Escaltion:      alerts[i].EscalationName,
			AlertCondition: fmt.Sprintf("%s.%s %s %s for %d", alerts[i].File, alerts[i].Variable, alerts[i].Condition, alerts[i].Threshold, alerts[i].Duration),
			Subject:        extractHref(alerts[i].Links, "subject"),
			CreatedAt:      alerts[i].CreatedAt,
			UpdatedAt:      alerts[i].UpdatedAt,
		}
		alertSpecs[i] = alert
	}

	deploymentStruct := Deployment{
		Name:               deployment.Name,
		Inputs:             inputsRetrieve(client, extractHref(deployment.Links, "inputs")),
		Servers:            servers,
		ServersNumber:      len(servers),
		ServerArrays:       serverArrays,
		ServerArraysNumber: len(serverArrays),
		ServerTemplates:    serverTemplates,
		Alerts:             alertSpecs,
	}
	jsonBody, err := json.MarshalIndent(deploymentStruct, "", "    ")
	if err == nil {
		fmt.Println(htmlReplace(string(jsonBody)))
	}
}