func (c *gceCmd) RunCommand(args []string) error { if c.verbose { gce.Verbose = true } if c.project == "" { return cmdmain.UsageError("Missing --project flag.") } if (c.certFile == "") != (c.keyFile == "") { return cmdmain.UsageError("--cert and --key must both be given together.") } if c.certFile == "" && c.hostname == "" { return cmdmain.UsageError("Either --hostname, or --cert & --key must provided.") } config := gce.NewOAuthConfig(readFile(clientIdDat), readFile(clientSecretDat)) config.RedirectURL = "urn:ietf:wg:oauth:2.0:oob" instConf := &gce.InstanceConf{ Name: c.instName, Project: c.project, Machine: c.machine, Zone: c.zone, CertFile: c.certFile, KeyFile: c.keyFile, Hostname: c.hostname, } if c.sshPub != "" { instConf.SSHPub = strings.TrimSpace(readFile(c.sshPub)) } depl := &gce.Deployer{ Client: oauth2.NewClient(oauth2.NoContext, oauth2.ReuseTokenSource(nil, &oauthutil.TokenSource{ Config: config, CacheFile: c.project + "-token.json", AuthCode: func() string { fmt.Println("Get auth code from:") fmt.Printf("%v\n", config.AuthCodeURL("my-state", oauth2.AccessTypeOffline, oauth2.ApprovalForce)) fmt.Println("Enter auth code:") sc := bufio.NewScanner(os.Stdin) sc.Scan() return strings.TrimSpace(sc.Text()) }, })), Conf: instConf, } inst, err := depl.Create(context.TODO()) if err != nil { return err } log.Printf("Instance is up at %s", inst.NetworkInterfaces[0].AccessConfigs[0].NatIP) return nil }
func (c *gceCmd) RunCommand(args []string) error { if c.verbose { gce.Verbose = true } if c.project == "" { return cmdmain.UsageError("Missing --project flag.") } if (c.certFile == "") != (c.keyFile == "") { return cmdmain.UsageError("--cert and --key must both be given together.") } if c.certFile == "" && c.hostname == "" { return cmdmain.UsageError("Either --hostname, or --cert & --key must provided.") } // We embed the client ID and client secret, per // https://developers.google.com/identity/protocols/OAuth2InstalledApp // Notably: "The client ID and client secret obtained from the // Developers Console are embedded in the source code of your // application. In this context, the client secret is // obviously not treated as a secret." // // These were created at: // https://console.developers.google.com/apis/credentials?project=camlistore-website // (Notes for Brad and Mathieu) const ( clientID = "574004351801-9qqoggh6b5v3jqt722v43ikmgmtv60h3.apps.googleusercontent.com" clientSecret = "Gf1zwaOcbJnRTE5zD4feKaTI" // NOT a secret, despite name ) config := gce.NewOAuthConfig(clientID, clientSecret) config.RedirectURL = "urn:ietf:wg:oauth:2.0:oob" hc := oauth2.NewClient(oauth2.NoContext, oauth2.ReuseTokenSource(nil, &oauthutil.TokenSource{ Config: config, CacheFile: c.project + "-token.json", AuthCode: func() string { fmt.Println("Get auth code from:") fmt.Printf("%v\n\n", config.AuthCodeURL("my-state", oauth2.AccessTypeOffline, oauth2.ApprovalForce)) fmt.Print("Enter auth code: ") sc := bufio.NewScanner(os.Stdin) sc.Scan() return strings.TrimSpace(sc.Text()) }, })) zone := c.zone if gce.LooksLikeRegion(zone) { region := zone zones, err := gce.ZonesOfRegion(hc, c.project, region) if err != nil { return err } if len(zones) == 0 { return fmt.Errorf("no zones found in region %q; invalid region?", region) } zone = zones[rand.Intn(len(zones))] } instConf := &gce.InstanceConf{ Name: c.instName, Project: c.project, Machine: c.machine, Zone: zone, CertFile: c.certFile, KeyFile: c.keyFile, Hostname: c.hostname, } if c.sshPub != "" { instConf.SSHPub = strings.TrimSpace(readFile(c.sshPub)) } log.Printf("Creating instance %s (in project %s) in zone %s ...", c.instName, c.project, zone) depl := &gce.Deployer{ Client: hc, Conf: instConf, } inst, err := depl.Create(context.Background()) if err != nil { return err } log.Printf("Instance created; starting up at %s", inst.NetworkInterfaces[0].AccessConfigs[0].NatIP) return nil }