예제 #1
0
파일: main.go 프로젝트: lopaka/rsc
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)
}
예제 #2
0
// Client() returns a new client for accessing RightScale.
func (c *Config) Client() (*cm15.API, error) {
	auth := rsapi.NewOAuthAuthenticator(c.RefreshToken, c.AccountID)
	client := cm15.New("us-3.rightscale.com", auth)

	log.Printf("[INFO] RightScale Client configured for account: %s", c.AccountID)

	return client, nil
}
예제 #3
0
func (account *Account) Client15() (*cm15.API, error) {
	if account.client15 == nil {
		if err := account.validate(); err != nil {
			return nil, err
		}
		auth := rsapi.NewOAuthAuthenticator(account.RefreshToken, account.Id)
		account.client15 = cm15.New(account.Host, auth)
	}
	return account.client15, nil
}
예제 #4
0
파일: main.go 프로젝트: dylanmei/rsc
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
		}
	}
}
예제 #5
0
파일: main.go 프로젝트: lopaka/rsc
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"])
		}
	}
}
예제 #6
0
파일: commands_test.go 프로젝트: lopaka/rsc
			Ω(*parsed).Should(Equal(expected))
		})
	})
})

var _ = Describe("ParseCommand with cm15", func() {
	var cmd, hrefPrefix string
	var values rsapi.ActionCommands
	var api *rsapi.API

	var parsed *rsapi.ParsedCommand
	var parseErr error

	BeforeEach(func() {
		values = nil
		cm := cm15.New("", nil)
		api = cm.API
	})

	JustBeforeEach(func() {
		parsed, parseErr = api.ParseCommand(cmd, hrefPrefix, values)
	})

	Describe("with a deep map of inputs", func() {
		BeforeEach(func() {
			cmd = "wrap_instance"
			wrapCmd := rsapi.ActionCommand{
				Href: "/api/servers",
				Params: []string{
					"server[name]=server name",
					"server[deployment_href]=/api/deployments/1",
예제 #7
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)))
	}
}