Esempio n. 1
0
func (c *shareCmd) RunCommand(args []string) error {
	unsigned := schema.NewShareRef(schema.ShareHaveRef, c.transitive)

	if c.search != "" {
		if len(args) != 0 {
			return cmdmain.UsageError("when using the -search flag, share takes zero arguments")
		}
		var q search.SearchQuery
		if err := json.Unmarshal([]byte(c.search), &q); err != nil {
			return cmdmain.UsageError(fmt.Sprintf("invalid search: %s", err))
		}
		unsigned.SetShareSearch(&q)
	} else {
		if len(args) != 1 {
			return cmdmain.UsageError("share takes at most one argument")
		}
		target, ok := blob.Parse(args[0])
		if !ok {
			return cmdmain.UsageError("invalid blobref")
		}
		unsigned.SetShareTarget(target)
	}

	if c.duration != 0 {
		unsigned.SetShareExpiration(time.Now().Add(c.duration))
	}

	pr, err := getUploader().UploadAndSignBlob(unsigned)
	handleResult("share", pr, err)
	return nil
}
Esempio n. 2
0
func (c *serverCmd) checkFlags(args []string) error {
	if len(args) != 0 {
		c.Usage()
	}
	if c.mini {
		if c.things {
			return cmdmain.UsageError("--mini and --makethings are mutually exclusive.")
		}
		c.publish = false
		c.hello = false
	}
	if c.things && !c.wipe {
		return cmdmain.UsageError("--makethings requires --wipe.")
	}
	nindex := 0
	for _, v := range []bool{c.mongo, c.mysql, c.postgres, c.sqlite, c.memory} {
		if v {
			nindex++
		}
	}
	if nindex > 1 {
		return fmt.Errorf("Only one index option allowed")
	}

	if _, err := strconv.ParseInt(c.port, 0, 0); err != nil {
		return fmt.Errorf("Invalid -port value: %q", c.port)
	}
	return nil
}
Esempio n. 3
0
func (c *desCmd) RunCommand(args []string) error {
	if len(args) == 0 {
		return cmdmain.UsageError("requires blobref")
	}
	var blobs []blob.Ref
	for _, arg := range args {
		br, ok := blob.Parse(arg)
		if !ok {
			return cmdmain.UsageError(fmt.Sprintf("invalid blobref %q", arg))
		}
		blobs = append(blobs, br)
	}
	var at time.Time // TODO: implement. from "2 days ago" "-2d", "-2h", "2013-02-05", etc

	cl := newClient(c.server)
	res, err := cl.Describe(&search.DescribeRequest{
		BlobRefs: blobs,
		Depth:    c.depth,
		At:       types.Time3339(at),
	})
	if err != nil {
		return err
	}
	resj, err := json.MarshalIndent(res, "", "  ")
	if err != nil {
		return err
	}
	resj = append(resj, '\n')
	_, err = os.Stdout.Write(resj)
	return err
}
Esempio n. 4
0
func (c *syncCmd) RunCommand(args []string) error {
	if c.loop && !c.removeSrc {
		return cmdmain.UsageError("Can't use --loop without --removesrc")
	}
	if c.verbose {
		c.logger = log.New(os.Stderr, "", 0) // else nil
	}
	if c.all {
		err := c.syncAll()
		if err != nil {
			return fmt.Errorf("sync all failed: %v", err)
		}
		return nil
	}
	if c.dest == "" {
		return cmdmain.UsageError("No --dest specified.")
	}

	discl := c.discoClient()
	discl.SetLogger(c.logger)
	src, err := discl.BlobRoot()
	if err != nil {
		return fmt.Errorf("Failed to get blob source: %v", err)
	}

	sc := client.New(src)
	sc.SetupAuth()
	sc.SetLogger(c.logger)

	dc := client.New(c.dest)
	dc.SetupAuth()
	dc.SetLogger(c.logger)

	var tc *client.Client
	if c.third != "" {
		tc = client.New(c.third)
		tc.SetupAuth()
		tc.SetLogger(c.logger)
	}

	passNum := 0
	for {
		passNum++
		stats, err := c.doPass(sc, dc, tc)
		if c.verbose {
			log.Printf("sync stats - pass: %d, blobs: %d, bytes %d\n", passNum, stats.BlobsCopied, stats.BytesCopied)
		}
		if err != nil {
			return fmt.Errorf("sync failed: %v", err)
		}
		if !c.loop {
			break
		}
	}
	return nil
}
Esempio n. 5
0
func (c *syncCmd) syncAll() error {
	if c.loop {
		return cmdmain.UsageError("--all can't be used with --loop")
	}
	if c.third != "" {
		return cmdmain.UsageError("--all can't be used with --thirdleg")
	}
	if c.dest != "" {
		return cmdmain.UsageError("--all can't be used with --dest")
	}

	dc := c.discoClient()
	dc.SetLogger(c.logger)
	syncHandlers, err := dc.SyncHandlers()
	if err != nil {
		return fmt.Errorf("sync handlers discovery failed: %v", err)
	}
	if c.verbose {
		log.Printf("To be synced:\n")
		for _, sh := range syncHandlers {
			log.Printf("%v -> %v", sh.From, sh.To)
		}
	}
	for _, sh := range syncHandlers {
		from := client.New(sh.From)
		from.SetLogger(c.logger)
		from.InsecureTLS = c.insecureTLS
		from.SetHTTPClient(&http.Client{
			Transport: from.TransportForConfig(nil),
		})
		if err := from.SetupAuth(); err != nil {
			return fmt.Errorf("could not setup auth for connecting to %v: %v", sh.From, err)
		}
		to := client.New(sh.To)
		to.SetLogger(c.logger)
		to.InsecureTLS = c.insecureTLS
		to.SetHTTPClient(&http.Client{
			Transport: to.TransportForConfig(nil),
		})
		if err := to.SetupAuth(); err != nil {
			return fmt.Errorf("could not setup auth for connecting to %v: %v", sh.To, err)
		}
		if c.verbose {
			log.Printf("Now syncing: %v -> %v", sh.From, sh.To)
		}
		stats, err := c.doPass(from, to, nil)
		if c.verbose {
			log.Printf("sync stats, blobs: %d, bytes %d\n", stats.BlobsCopied, stats.BytesCopied)
		}
		if err != nil {
			return err
		}
	}
	return nil
}
Esempio n. 6
0
func (c *debugCmd) RunCommand(args []string) error {
	if args == nil || len(args) != 2 {
		return cmdmain.UsageError("Incorrect number of arguments.")
	}
	subMode, ok := debugSubModes[args[0]]
	if !ok {
		return cmdmain.UsageError(fmt.Sprintf("Invalid submode: %v", args[0]))
	}
	subMode.fun(args[1])
	return nil
}
Esempio n. 7
0
func (c *shareCmd) RunCommand(args []string) error {
	if len(args) != 1 {
		return cmdmain.UsageError("share takes exactly one argument, a blobref")
	}
	br := blobref.Parse(args[0])
	if br == nil {
		return cmdmain.UsageError("invalid blobref")
	}
	pr, err := getUploader().UploadShare(br, c.transitive)
	handleResult("share", pr, err)
	return nil
}
Esempio n. 8
0
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
}
Esempio n. 9
0
func (c *attrCmd) checkArgs(args []string) error {
	if c.del {
		if c.add {
			return cmdmain.UsageError("Add and del options are exclusive")
		}
		if len(args) < 2 {
			return cmdmain.UsageError("Attr -del takes at least 2 args: <permanode> <attr> [<value>]")
		}
		return nil
	}
	if len(args) != 3 {
		return cmdmain.UsageError("Attr takes 3 args: <permanode> <attr> <value>")
	}
	return nil
}
Esempio n. 10
0
func (c *mountCmd) RunCommand(args []string) error {
	err := c.checkFlags(args)
	if err != nil {
		return cmdmain.UsageError(fmt.Sprint(err))
	}
	if !*noBuild {
		if err := build(filepath.Join("cmd", "cammount")); err != nil {
			return fmt.Errorf("Could not build cammount: %v", err)
		}
	}
	c.env.SetCamdevVars(c.altkey)

	tryUnmount(mountpoint)
	if err := os.Mkdir(mountpoint, 0700); err != nil && !os.IsExist(err) {
		return fmt.Errorf("Could not make mount point: %v", err)
	}

	blobserver := "http://localhost:" + c.port + c.path
	if c.tls {
		blobserver = strings.Replace(blobserver, "http://", "https://", 1)
	}

	cmdBin := filepath.Join("bin", "cammount")
	cmdArgs := []string{
		"-xterm=" + strconv.FormatBool(c.xterm),
		"-debug=" + strconv.FormatBool(c.debug),
		"-server=" + blobserver,
		mountpoint,
	}
	cmdArgs = append(cmdArgs, args...)
	fmt.Printf("Cammount running with mountpoint %v. Press 'q' <enter> or ctrl-c to shut down.\n", mountpoint)
	return runExec(cmdBin, cmdArgs, c.env)
}
Esempio n. 11
0
// which is one of "src", "dest", or "thirdleg"
func (c *syncCmd) storageFromParam(which storageType, val string) (blobserver.Storage, error) {
	if val == "" {
		switch which {
		case storageThird:
			return nil, nil
		case storageSource:
			discl := c.discoClient()
			discl.SetLogger(c.logger)
			src, err := discl.BlobRoot()
			if err != nil {
				return nil, fmt.Errorf("Failed to discover source server's blob path: %v", err)
			}
			val = src
		}
		if val == "" {
			return nil, cmdmain.UsageError("No --" + string(which) + " flag value specified")
		}
	}
	if which == storageDest && val == "stdout" {
		return nil, nil
	}
	if looksLikePath(val) {
		disk, err := localdisk.New(val)
		if err != nil {
			return nil, fmt.Errorf("Interpreted --%v=%q as a local disk path, but got error: %v", val, err)
		}
		return disk, nil
	}
	cl := client.New(val)
	// TODO(mpl): probably needs the transport setup for trusted certs here.
	cl.SetupAuth()
	cl.SetLogger(c.logger)
	return noHub{cl}, nil
}
Esempio n. 12
0
func (c *syncCmd) RunCommand(args []string) error {
	if c.loop && !c.removeSrc {
		return cmdmain.UsageError("Can't use --loop without --removesrc")
	}
	if c.verbose {
		c.logger = log.New(cmdmain.Stderr, "", 0) // else nil
	}
	if c.dumpConfigFlag {
		err := c.dumpConfig()
		if err != nil {
			return fmt.Errorf("dumb-config failed: %v", err)
		}
		return nil
	}
	if c.all {
		err := c.syncAll()
		if err != nil {
			return fmt.Errorf("sync all failed: %v", err)
		}
		return nil
	}

	ss, err := c.storageFromParam("src", c.src)
	if err != nil {
		return err
	}
	ds, err := c.storageFromParam("dest", c.dest)
	if err != nil {
		return err
	}
	ts, err := c.storageFromParam("thirdleg", c.third)
	if err != nil {
		return err
	}

	differentKeyIDs := fmt.Sprintf("WARNING: the source server GPG key ID (%v) and the destination's (%v) differ. All blobs will be synced, but because the indexer at the other side is indexing claims by a different user, you may not see what you expect in that server's web UI, etc.", c.srcKeyID, c.destKeyID)

	if c.dest != "stdout" && !c.oneIsDisk && c.srcKeyID != c.destKeyID { // both blank is ok.
		// Warn at the top (and hope the user sees it and can abort if it was a mistake):
		fmt.Fprintln(cmdmain.Stderr, differentKeyIDs)
		// Warn also at the end (in case the user missed the first one)
		defer fmt.Fprintln(cmdmain.Stderr, differentKeyIDs)
	}

	passNum := 0
	for {
		passNum++
		stats, err := c.doPass(ss, ds, ts)
		if c.verbose {
			log.Printf("sync stats - pass: %d, blobs: %d, bytes %d\n", passNum, stats.BlobsCopied, stats.BytesCopied)
		}
		if err != nil {
			return fmt.Errorf("sync failed: %v", err)
		}
		if !c.loop {
			break
		}
	}
	return nil
}
Esempio n. 13
0
func (c *getCmd) RunCommand(args []string) error {
	err := c.checkFlags(args)
	if err != nil {
		return cmdmain.UsageError(fmt.Sprint(err))
	}
	if !*noBuild {
		if err := build(filepath.Join("cmd", "camget")); err != nil {
			return fmt.Errorf("Could not build camget: %v", err)
		}
	}
	c.env.SetCamdevVars(c.altkey)

	cmdBin := filepath.Join("bin", "camget")
	cmdArgs := []string{
		"-verbose=" + strconv.FormatBool(*cmdmain.FlagVerbose || !quiet),
	}
	if !isSharedMode(args) {
		blobserver := "http://localhost:" + c.port + c.path
		if c.tls {
			blobserver = strings.Replace(blobserver, "http://", "https://", 1)
		}
		cmdArgs = append(cmdArgs, "-server="+blobserver)
	}
	cmdArgs = append(cmdArgs, args...)
	return runExec(cmdBin, cmdArgs, c.env)
}
Esempio n. 14
0
func (c *putCmd) RunCommand(args []string) error {
	err := c.checkFlags(args)
	if err != nil {
		return cmdmain.UsageError(fmt.Sprint(err))
	}
	if err := c.build(); err != nil {
		return fmt.Errorf("Could not build camput: %v", err)
	}
	if err := c.setEnvVars(); err != nil {
		return fmt.Errorf("Could not setup the env vars: %v", err)
	}

	blobserver := "http://localhost:" + c.port + c.path
	if c.tls {
		blobserver = strings.Replace(blobserver, "http://", "https://", 1)
	}

	cmdBin := filepath.Join("bin", "camput")
	cmdArgs := []string{
		"-verbose=" + strconv.FormatBool(c.verbose),
		"-server=" + blobserver,
	}
	cmdArgs = append(cmdArgs, args...)
	return runExec(cmdBin, cmdArgs, c.env)
}
Esempio n. 15
0
func (c *gaeCmd) RunCommand(args []string) error {
	err := c.checkFlags(args)
	if err != nil {
		return cmdmain.UsageError(fmt.Sprint(err))
	}
	applicationDir := filepath.Join("server", "appengine")
	if _, err := os.Stat(applicationDir); err != nil {
		return fmt.Errorf("Appengine application dir not found at %s", applicationDir)
	}
	if err = c.checkSDK(); err != nil {
		return err
	}
	if err = c.mirrorSourceRoot(applicationDir); err != nil {
		return err
	}

	devAppServerBin := filepath.Join(c.sdk, "dev_appserver.py")
	cmdArgs := []string{
		"--skip_sdk_update_check",
		fmt.Sprintf("--port=%s", c.port),
	}
	if c.all {
		cmdArgs = append(cmdArgs, "--host", "0.0.0.0")
	}
	if c.wipe {
		cmdArgs = append(cmdArgs, "--clear_datastore")
	}
	cmdArgs = append(cmdArgs, args...)
	cmdArgs = append(cmdArgs, applicationDir)
	return runExec(devAppServerBin, cmdArgs, NewCopyEnv())
}
Esempio n. 16
0
func (c *putCmd) RunCommand(args []string) error {
	err := c.checkFlags(args)
	if err != nil {
		return cmdmain.UsageError(fmt.Sprint(err))
	}
	if !*noBuild {
		if err := build(filepath.Join("cmd", "camput")); err != nil {
			return fmt.Errorf("Could not build camput: %v", err)
		}
	}
	c.env.SetCamdevVars(c.altkey)
	// wipeCacheDir needs to be called after SetCamdevVars, because that is
	// where CAMLI_CACHE_DIR is defined.
	if *wipeCache {
		c.env.wipeCacheDir()
	}

	blobserver := "http://localhost:" + c.port + c.path
	if c.tls {
		blobserver = strings.Replace(blobserver, "http://", "https://", 1)
	}

	cmdBin := filepath.Join("bin", "camput")
	cmdArgs := []string{
		"-verbose=" + strconv.FormatBool(*cmdmain.FlagVerbose || !quiet),
		"-server=" + blobserver,
	}
	cmdArgs = append(cmdArgs, args...)
	return runExec(cmdBin, cmdArgs, c.env)
}
Esempio n. 17
0
func (c *shareCmd) RunCommand(args []string) error {
	if len(args) != 1 {
		return cmdmain.UsageError("share takes exactly one argument, a blobref")
	}
	target, ok := blob.Parse(args[0])
	if !ok {
		return cmdmain.UsageError("invalid blobref")
	}
	unsigned := schema.NewShareRef(schema.ShareHaveRef, target, c.transitive)
	if c.duration != 0 {
		unsigned.SetShareExpiration(time.Now().Add(c.duration))
	}

	pr, err := getUploader().UploadAndSignBlob(unsigned)
	handleResult("share", pr, err)
	return nil
}
Esempio n. 18
0
func (c *searchDocCmd) RunCommand(args []string) error {
	if len(args) > 0 {
		return cmdmain.UsageError("No arguments allowed")
	}

	formattedSearchHelp()
	return nil
}
Esempio n. 19
0
func (c *searchCmd) RunCommand(args []string) error {
	if len(args) != 1 {
		return cmdmain.UsageError("requires search expression or Constraint JSON")
	}
	q := args[0]
	if q == "-" {
		slurp, err := ioutil.ReadAll(cmdmain.Stdin)
		if err != nil {
			return err
		}
		q = string(slurp)
	}
	q = strings.TrimSpace(q)

	req := &search.SearchQuery{
		Limit:    c.limit,
		Continue: c.cont,
	}
	if c.rawQuery {
		req.Limit = 0     // clear it if they provided it
		req.Continue = "" // clear this as well

		if err := json.NewDecoder(strings.NewReader(q)).Decode(&req); err != nil {
			return err
		}
		if c.limit != 0 {
			req.Limit = c.limit
		}
		if c.cont != "" {
			req.Continue = c.cont
		}
	} else if strutil.IsPlausibleJSON(q) {
		cs := new(search.Constraint)
		if err := json.NewDecoder(strings.NewReader(q)).Decode(&cs); err != nil {
			return err
		}
		req.Constraint = cs
	} else {
		req.Expression = q
	}
	if c.describe {
		req.Describe = &search.DescribeRequest{}
	}

	cl := newClient(c.server)
	res, err := cl.Query(req)
	if err != nil {
		return err
	}
	resj, err := json.MarshalIndent(res, "", "  ")
	if err != nil {
		return err
	}
	resj = append(resj, '\n')
	_, err = os.Stdout.Write(resj)
	return err
}
Esempio n. 20
0
func (c *deleteCmd) RunCommand(args []string) error {
	if len(args) < 1 {
		return cmdmain.UsageError("Need at least one blob to delete.")
	}
	if err := delete(args); err != nil {
		return err
	}
	return nil
}
Esempio n. 21
0
func (c *googinitCmd) RunCommand(args []string) error {
	var (
		err          error
		clientId     string
		clientSecret string
		oauthConfig  *oauth2.Config
	)

	if c.storageType != "drive" && c.storageType != "cloud" {
		return cmdmain.UsageError("Invalid storage type: must be drive for Google Drive or cloud for Google Cloud Storage.")
	}

	clientId, clientSecret = getClientInfo()

	switch c.storageType {
	case "drive":
		oauthConfig = &oauth2.Config{
			Scopes:       []string{drive.Scope},
			Endpoint:     google.Endpoint,
			ClientID:     clientId,
			ClientSecret: clientSecret,
			RedirectURL:  oauthutil.TitleBarRedirectURL,
		}
	case "cloud":
		oauthConfig = &oauth2.Config{
			Scopes:       []string{storage.ScopeReadWrite},
			Endpoint:     google.Endpoint,
			ClientID:     clientId,
			ClientSecret: clientSecret,
			RedirectURL:  oauthutil.TitleBarRedirectURL,
		}
	}

	token, err := oauth2.ReuseTokenSource(nil, &oauthutil.TokenSource{
		Config: oauthConfig,
		AuthCode: func() string {
			fmt.Fprintf(cmdmain.Stdout, "Get auth code from:\n\n")
			fmt.Fprintf(cmdmain.Stdout, "%v\n\n", oauthConfig.AuthCodeURL("", oauth2.AccessTypeOffline, oauth2.ApprovalForce))
			return prompt("Enter auth code:")
		},
	}).Token()
	if err != nil {
		return fmt.Errorf("could not acquire token: %v", err)
	}

	fmt.Fprintf(cmdmain.Stdout, "\nYour Google auth object:\n\n")
	enc := json.NewEncoder(cmdmain.Stdout)
	authObj := map[string]string{
		"client_id":     clientId,
		"client_secret": clientSecret,
		"refresh_token": token.RefreshToken,
	}
	enc.Encode(authObj)
	fmt.Fprint(cmdmain.Stdout, "\n\nFor server-config.json, your 'googlecloudstorage' value (update with your bucket name and path):\n\n")
	fmt.Fprintf(cmdmain.Stdout, "%s:%s:%s:bucketName[/optional/dir]\n", clientId, clientSecret, token.RefreshToken)
	return nil
}
Esempio n. 22
0
func (c *reviewCmd) RunCommand(args []string) error {
	if len(args) > 0 {
		return cmdmain.UsageError("too many arguments.")
	}
	goToCamliRoot()
	c.checkHook()
	gitPush()
	return nil
}
Esempio n. 23
0
// which is one of "src", "dest", or "thirdleg"
func (c *syncCmd) storageFromParam(which storageType, val string) (blobserver.Storage, error) {
	var httpClient *http.Client

	if val == "" {
		switch which {
		case storageThird:
			return nil, nil
		case storageSource:
			discl := c.discoClient()
			discl.SetLogger(c.logger)
			src, err := discl.BlobRoot()
			if err != nil {
				return nil, fmt.Errorf("Failed to discover source server's blob path: %v", err)
			}
			val = src
			httpClient = discl.HTTPClient()
		}
		if val == "" {
			return nil, cmdmain.UsageError("No --" + string(which) + " flag value specified")
		}
	}
	if which == storageDest && val == "stdout" {
		return nil, nil
	}
	if looksLikePath(val) {
		disk, err := localdisk.New(val)
		if err != nil {
			return nil, fmt.Errorf("Interpreted --%v=%q as a local disk path, but got error: %v", which, val, err)
		}
		c.oneIsDisk = true
		return disk, nil
	}
	cl := client.New(val)
	cl.InsecureTLS = c.insecureTLS
	if httpClient == nil {
		httpClient = &http.Client{
			Transport: cl.TransportForConfig(nil),
		}
	}
	cl.SetHTTPClient(httpClient)
	if err := cl.SetupAuth(); err != nil {
		return nil, fmt.Errorf("could not setup auth for connecting to %v: %v", val, err)
	}
	cl.SetLogger(c.logger)
	serverKeyID, err := cl.ServerKeyID()
	if err != nil && err != client.ErrNoSigning {
		fmt.Fprintf(cmdmain.Stderr, "Failed to discover keyId for server %v: %v", val, err)
	} else {
		if which == storageSource {
			c.srcKeyID = serverKeyID
		} else if which == storageDest {
			c.destKeyID = serverKeyID
		}
	}
	return cl, nil
}
Esempio n. 24
0
func (c *syncCmd) syncAll() error {
	if c.loop {
		return cmdmain.UsageError("--all can't be used with --loop")
	}
	if c.third != "" {
		return cmdmain.UsageError("--all can't be used with --thirdleg")
	}
	if c.dest != "" {
		return cmdmain.UsageError("--all can't be used with --dest")
	}

	dc := c.discoClient()
	dc.SetLogger(c.logger)
	syncHandlers, err := dc.SyncHandlers()
	if err != nil {
		return fmt.Errorf("sync handlers discovery failed: %v", err)
	}
	if c.verbose {
		log.Printf("To be synced:\n")
		for _, sh := range syncHandlers {
			log.Printf("%v -> %v", sh.From, sh.To)
		}
	}
	for _, sh := range syncHandlers {
		from := client.New(sh.From)
		from.SetLogger(c.logger)
		from.SetupAuth()
		to := client.New(sh.To)
		to.SetLogger(c.logger)
		to.SetupAuth()
		if c.verbose {
			log.Printf("Now syncing: %v -> %v", sh.From, sh.To)
		}
		stats, err := c.doPass(noHub{from}, noHub{to}, nil)
		if c.verbose {
			log.Printf("sync stats, blobs: %d, bytes %d\n", stats.BlobsCopied, stats.BytesCopied)
		}
		if err != nil {
			return err
		}
	}
	return nil
}
Esempio n. 25
0
func (c *serverCmd) RunCommand(args []string) error {
	err := c.checkFlags(args)
	if err != nil {
		return cmdmain.UsageError(fmt.Sprint(err))
	}
	c.camliSrcRoot, err = osutil.GoPackagePath("camlistore.org")
	if err != nil {
		return errors.New("Package camlistore.org not found in $GOPATH (or $GOPATH not defined).")
	}
	err = os.Chdir(c.camliSrcRoot)
	if err != nil {
		return fmt.Errorf("Could not chdir to %v: %v", c.camliSrcRoot, err)
	}
	if !c.noBuild {
		for _, name := range []string{"camlistored", "camtool"} {
			err := c.build(name)
			if err != nil {
				return fmt.Errorf("Could not build %v: %v", name, err)
			}
		}
	}
	if err := c.setCamliRoot(); err != nil {
		return fmt.Errorf("Could not setup the camli root: %v", err)
	}
	if err := c.setEnvVars(); err != nil {
		return fmt.Errorf("Could not setup the env vars: %v", err)
	}
	if err := c.setupIndexer(); err != nil {
		return fmt.Errorf("Could not setup the indexer: %v", err)
	}
	if err := c.syncTemplateBlobs(); err != nil {
		return fmt.Errorf("Could not copy the template blobs: %v", err)
	}
	if err := c.setFullClosure(); err != nil {
		return fmt.Errorf("Could not setup the closure lib: %v", err)
	}

	log.Printf("Starting dev server on %v/ui/ with password \"pass%v\"\n",
		os.Getenv("CAMLI_BASEURL"), c.port)

	camliBin := filepath.Join(c.camliSrcRoot, "bin", "camlistored")
	cmdArgs := []string{
		"-configfile=" + filepath.Join(c.camliSrcRoot, "config", "dev-server-config.json"),
		"-listen=" + c.listen}
	cmd := exec.Command(camliBin, cmdArgs...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Start(); err != nil {
		return fmt.Errorf("Could not start camlistored: %v", err)
	}
	go handleKillCamliSignal(cmd.Process)
	cmd.Wait()
	return nil
}
Esempio n. 26
0
func (c *discoCmd) RunCommand(args []string) error {
	if len(args) > 0 {
		return cmdmain.UsageError("doesn't take args")
	}
	cl := newClient(c.server)
	disco, err := cl.DiscoveryDoc()
	if err != nil {
		return err
	}
	_, err = io.Copy(os.Stdout, disco)
	return err
}
Esempio n. 27
0
func (c *syncCmd) dumpConfig() error {
	if c.loop {
		return cmdmain.UsageError("--dump-config can't be used with --loop")
	}
	if c.third != "" {
		return cmdmain.UsageError("--dump-config can't be used with --thirdleg")
	}
	if c.dest != "" {
		return cmdmain.UsageError("--dump-config can't be used with --dest")
	}
	dc := c.discoClient()
	dc.SetLogger(c.logger)
	syncHandlers, err := dc.SyncHandlers()
	if err != nil {
		return fmt.Errorf("sync handlers discovery failed: %v", err)
	}
	for _, sh := range syncHandlers {
		fmt.Printf("%v -> %v\n", sh.From, sh.To)
	}
	return nil
}
Esempio n. 28
0
func (c *packBlobsCmd) RunCommand(args []string) error {
	if len(args) != 0 {
		return cmdmain.UsageError("doesn't take arguments")
	}
	req := &search.SearchQuery{
		Limit: -1,
		Sort:  search.BlobRefAsc,
		Constraint: &search.Constraint{
			File: &search.FileConstraint{
				FileSize: &search.IntConstraint{
					Min: 512 << 10,
				},
			},
		},
	}
	cl := newClient(c.server)
	looseClient := cl.NewPathClient("/bs-loose/")

	res, err := cl.Query(req)
	if err != nil {
		return err
	}
	total := len(res.Blobs)
	n := 0
	var buf bytes.Buffer
	for _, sr := range res.Blobs {
		n++
		fileRef := sr.Blob
		rc, _, err := looseClient.Fetch(fileRef)
		if err == os.ErrNotExist {
			fmt.Printf("%d/%d: %v already done\n", n, total, fileRef)
			continue
		}
		if err != nil {
			log.Printf("error fetching %v: %v\n", fileRef, err)
			continue
		}
		buf.Reset()
		_, err = io.Copy(&buf, rc)
		rc.Close()
		if err != nil {
			log.Printf("error reading %v: %v\n", fileRef, err)
			continue
		}
		_, err = cl.ReceiveBlob(fileRef, &buf)
		if err != nil {
			log.Printf("error write %v: %v\n", fileRef, err)
			continue
		}
		fmt.Printf("%d/%d: %v\n", n, total, fileRef)
	}
	return nil
}
Esempio n. 29
0
func (c *desCmd) RunCommand(args []string) error {
	if len(args) == 0 {
		return cmdmain.UsageError("requires blobref")
	}
	var blobs []blob.Ref
	for _, arg := range args {
		br, ok := blob.Parse(arg)
		if !ok {
			return cmdmain.UsageError(fmt.Sprintf("invalid blobref %q", arg))
		}
		blobs = append(blobs, br)
	}
	var at time.Time // TODO: add "2 days ago" "-2d", "-2h", "2013-02-05", etc
	if c.at != "" {
		var err error
		at, err = time.Parse(time.RFC3339, c.at)
		if err != nil {
			return fmt.Errorf("error parsing --at value %q: %v", c.at, err)
		}
	}

	cl := newClient(c.server)
	res, err := cl.Describe(context.Background(), &search.DescribeRequest{
		BlobRefs: blobs,
		Depth:    c.depth,
		At:       types.Time3339(at),
	})
	if err != nil {
		return err
	}
	resj, err := json.MarshalIndent(res, "", "  ")
	if err != nil {
		return err
	}
	resj = append(resj, '\n')
	_, err = os.Stdout.Write(resj)
	return err
}
Esempio n. 30
0
func (c *claimsCmd) RunCommand(args []string) error {
	if len(args) != 1 {
		return cmdmain.UsageError("requires 1 blobref")
	}
	br, ok := blob.Parse(args[0])
	if !ok {
		return cmdmain.UsageError("invalid blobref")
	}
	cl := c.client()
	res, err := cl.GetClaims(&search.ClaimsRequest{
		Permanode: br,
	})
	if err != nil {
		return err
	}
	resj, err := json.MarshalIndent(res, "", "  ")
	if err != nil {
		return err
	}
	resj = append(resj, '\n')
	_, err = os.Stdout.Write(resj)
	return err
}