Пример #1
0
func TestConfig() *facade.Config {
	if testConfigCached != nil {
		return testConfigCached
	}

	cfg, err := facade.NewConfig(facade.DefaultConfigDir())
	if err != nil {
		log.Fatalf("Failed to load config: %v", err)
	}

	testConfigCached = cfg
	return testConfigCached
}
Пример #2
0
func main() {
	flag.Parse()

	cfg, err := facade.NewConfig(*flagConfigDir)
	if err != nil {
		log.Printf("%v", err)
		os.Exit(2)
	}

	_, err = auth.GetGCloudTokenSource(context.Background(), cfg.CredentialsFilePath, cfg.TokenCacheFilePath, true)
	if err != nil {
		log.Fatalf("Failed: %v", err)
	}
	log.Printf("credentials valid!")
}
Пример #3
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

	flag.Usage = Usage
	flag.Parse()

	cfg, err := facade.NewConfig(*flagConfigDir)
	if err != nil {
		log.Printf("%v", err)
		Usage()
		os.Exit(2)
	}
	minID := inodedb.LatestVersion
	if flag.NArg() < 1 {
		Usage()
		os.Exit(2)
	}
	switch flag.Arg(0) {
	case "purge":
		if flag.NArg() != 1 {
			Usage()
			os.Exit(2)
		}
	case "query":
		switch flag.NArg() {
		case 1:
			break
		case 2:
			n, err := strconv.ParseInt(flag.Arg(1), 10, 64)
			if err != nil {
				Usage()
				os.Exit(2)
			}
			minID = inodedb.TxID(n)
			break
		}
		break
	default:
		log.Printf("Unknown cmd: %v", flag.Arg(0))
		Usage()
		os.Exit(2)
	}

	tsrc, err := auth.GetGCloudTokenSource(context.Background(), cfg.CredentialsFilePath, cfg.TokenCacheFilePath, false)
	if err != nil {
		log.Fatalf("Failed to init GCloudClientSource: %v", err)
	}

	key := btncrypt.KeyFromPassword(cfg.Password)
	c, err := btncrypt.NewCipher(key)
	if err != nil {
		log.Fatalf("Failed to init btncrypt.Cipher: %v", err)
	}
	dscfg := datastore.NewConfig(cfg.ProjectName, cfg.BucketName, c, tsrc)

	txlogio := datastore.NewDBTransactionLogIO(dscfg)

	switch flag.Arg(0) {
	case "purge":
		if err := txlogio.DeleteAllTransactions(); err != nil {
			log.Printf("DeleteAllTransactions() failed: %v", err)
		}

	case "query":
		txs, err := txlogio.QueryTransactions(minID)
		if err != nil {
			log.Printf("QueryTransactions() failed: %v", err)
		}
		for _, tx := range txs {
			fmt.Printf("%s\n", tx)
		}

	default:
		log.Printf("Unknown cmd: %v", flag.Arg(0))
		os.Exit(1)
	}
}
Пример #4
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
	logger.Registry().AddOutput(logger.WriterLogger{os.Stderr})
	flag.Usage = Usage
	flag.Parse()

	cfg, err := facade.NewConfig(*flagConfigDir)
	if err != nil {
		logger.Criticalf(mylog, "%v", err)
		Usage()
		os.Exit(2)
	}
	if flag.NArg() != 1 {
		Usage()
		os.Exit(2)
	}
	mountpoint := flag.Arg(0)

	if err := facade.SetupFluentLogger(cfg); err != nil {
		logger.Criticalf(mylog, "Failed to setup fluentd logger: %v", err)
		os.Exit(1)
	}

	o, err := facade.NewOtaru(cfg, &facade.OneshotConfig{Mkfs: *flagMkfs})
	if err != nil {
		logger.Criticalf(mylog, "NewOtaru failed: %v", err)
		os.Exit(1)
	}
	var muClose sync.Mutex
	closeOtaruAndExit := func(exitCode int) {
		muClose.Lock()
		defer muClose.Unlock()

		if err := bfuse.Unmount(mountpoint); err != nil {
			logger.Warningf(mylog, "umount err: %v", err)
		}
		if o != nil {
			if err := o.Close(); err != nil {
				logger.Warningf(mylog, "Otaru.Close() returned errs: %v", err)
			}
			o = nil
		}
		os.Exit(exitCode)
	}
	defer closeOtaruAndExit(0)

	sigC := make(chan os.Signal, 1)
	signal.Notify(sigC, os.Interrupt)
	signal.Notify(sigC, syscall.SIGTERM)
	go func() {
		for s := range sigC {
			logger.Warningf(mylog, "Received signal: %v", s)
			closeOtaruAndExit(1)
		}
	}()
	logger.Registry().AddOutput(logger.HandleCritical(func() {
		logger.Warningf(mylog, "Starting shutdown due to critical event.")
		closeOtaruAndExit(1)
	}))

	bfuseLogger := logger.Registry().Category("bfuse")
	bfuse.Debug = func(msg interface{}) { logger.Debugf(bfuseLogger, "%v", msg) }
	if err := fuse.ServeFUSE(cfg.BucketName, mountpoint, o.FS, nil); err != nil {
		logger.Warningf(mylog, "ServeFUSE failed: %v", err)
		closeOtaruAndExit(1)
	}
	logger.Infof(mylog, "ServeFUSE end!")
}
Пример #5
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

	flag.Usage = Usage
	flag.Parse()
	if flag.NArg() != 1 {
		Usage()
		os.Exit(1)
	}
	switch flag.Arg(0) {
	case "list", "purge":
		break
	default:
		log.Printf("Unknown cmd: %v", flag.Arg(0))
		Usage()
		os.Exit(2)
	}

	cfg, err := facade.NewConfig(*flagConfigDir)
	if err != nil {
		log.Printf("%v", err)
		Usage()
		os.Exit(1)
	}

	tsrc, err := auth.GetGCloudTokenSource(context.Background(), cfg.CredentialsFilePath, cfg.TokenCacheFilePath, false)
	if err != nil {
		log.Fatalf("Failed to init GCloudTokenSource: %v", err)
	}
	key := btncrypt.KeyFromPassword(cfg.Password)
	c, err := btncrypt.NewCipher(key)
	if err != nil {
		log.Fatalf("Failed to init btncrypt.Cipher: %v", err)
	}

	dscfg := datastore.NewConfig(cfg.ProjectName, cfg.BucketName, c, tsrc)
	ssloc := datastore.NewINodeDBSSLocator(dscfg)
	switch flag.Arg(0) {
	case "purge":
		fmt.Printf("Do you really want to proceed with deleting all inodedbsslocator entry for %s?\n", cfg.BucketName)
		fmt.Printf("Type \"deleteall\" to proceed: ")
		sc := bufio.NewScanner(os.Stdin)
		if !sc.Scan() {
			return
		}
		if sc.Text() != "deleteall" {
			log.Printf("Cancelled.\n")
			os.Exit(1)
		}

		es, err := ssloc.DeleteAll()
		if err != nil {
			log.Printf("DeleteAll failed: %v", err)
		}
		log.Printf("DeleteAll deleted entries for blobpath: %v", es)
		// FIXME: delete the entries from blobpath too

	case "list":
		history := 0
	histloop:
		for {
			bp, err := ssloc.Locate(history)
			if err != nil {
				if err == datastore.EEMPTY {
					log.Printf("Locate(%d): no entry", history)
				} else {
					log.Printf("Locate(%d) err: %v", history, err)
				}
				break histloop
			}
			log.Printf("Locate(%d): %v", history, bp)

			history++
		}
	default:
		panic("NOT REACHED")
	}
}
Пример #6
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

	flag.Usage = Usage
	flag.Parse()
	if flag.NArg() != 0 {
		Usage()
		os.Exit(1)
	}

	cfg, err := facade.NewConfig(*flagConfigDir)
	if err != nil {
		log.Printf("%v", err)
		Usage()
		os.Exit(1)
	}

	tsrc, err := auth.GetGCloudTokenSource(context.Background(), cfg.CredentialsFilePath, cfg.TokenCacheFilePath, false)
	if err != nil {
		log.Fatalf("Failed to init GCloudClientSource: %v", err)
	}
	key := btncrypt.KeyFromPassword(cfg.Password)
	c, err := btncrypt.NewCipher(key)
	if err != nil {
		log.Fatalf("Failed to init btncrypt.Cipher: %v", err)
	}

	fmt.Printf("Do you really want to proceed with deleting all blobs in gs://%s{,-meta} and its cache in %s?\n", cfg.BucketName, cfg.CacheDir)
	fmt.Printf("Type \"deleteall\" to proceed: ")
	sc := bufio.NewScanner(os.Stdin)
	if !sc.Scan() {
		return
	}
	if sc.Text() != "deleteall" {
		log.Printf("Cancelled.\n")
		os.Exit(1)
	}

	dscfg := datastore.NewConfig(cfg.ProjectName, cfg.BucketName, c, tsrc)
	l := datastore.NewGlobalLocker(dscfg, "otaru-deleteallblobs", facade.GenHostName())
	if err := l.Lock(); err != nil {
		log.Printf("Failed to acquire global lock: %v", err)
		return
	}
	defer l.Unlock()

	if err := clearGCS(cfg.ProjectName, cfg.BucketName, tsrc); err != nil {
		log.Printf("Failed to clear bucket \"%s\": %v", cfg.BucketName, err)
		return
	}
	if cfg.UseSeparateBucketForMetadata {
		metabucketname := fmt.Sprintf("%s-meta", cfg.BucketName)
		if err := clearGCS(cfg.ProjectName, metabucketname, tsrc); err != nil {
			log.Printf("Failed to clear metadata bucket \"%s\": %v", metabucketname, err)
			return
		}
	}
	if err := clearCache(cfg.CacheDir); err != nil {
		log.Printf("Failed to clear cache \"%s\": %v", cfg.CacheDir, err)
		return
	}

	log.Printf("otaru-deleteallblobs: Successfully completed!")
	log.Printf("Hint: You might also want to run \"otaru-txlogio purge\" to delete inodedb txlogs.")
}
Пример #7
0
func main() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

	flag.Usage = Usage
	flag.Parse()

	cfg, err := facade.NewConfig(*flagConfigDir)
	if err != nil {
		log.Printf("%v", err)
		Usage()
		os.Exit(2)
	}
	if flag.NArg() != 1 {
		Usage()
		os.Exit(2)
	}
	switch flag.Arg(0) {
	case "lock", "unlock", "query":
		break
	default:
		log.Printf("Unknown cmd: %v", flag.Arg(0))
		os.Exit(1)
	}

	tsrc, err := auth.GetGCloudTokenSource(context.Background(), cfg.CredentialsFilePath, cfg.TokenCacheFilePath, false)
	if err != nil {
		log.Fatalf("Failed to init GCloudClientSource: %v", err)
	}
	c := btncrypt.Cipher{} // Null cipher is fine, as we GlobalLocker doesn't make use of it.
	dscfg := datastore.NewConfig(cfg.ProjectName, cfg.BucketName, c, tsrc)
	info := *flagInfoStr
	if info == "" {
		info = "otaru-globallock-cli cmdline debug tool"
	}
	l := datastore.NewGlobalLocker(dscfg, "otaru-globallock-cli", info)

	switch flag.Arg(0) {
	case "lock":
		if err := l.Lock(); err != nil {
			log.Printf("Lock failed: %v", err)
		}
	case "unlock":
		if *flagForce {
			if err := l.ForceUnlock(); err != nil {
				log.Printf("ForceUnlock failed: %v", err)
				os.Exit(1)
			}
		} else {
			if err := l.UnlockIgnoreCreatedAt(); err != nil {
				log.Printf("Unlock failed: %v", err)
				os.Exit(1)
			}
		}
	case "query":
		entry, err := l.Query()
		if err != nil {
			log.Printf("Query failed: %v", err)
			os.Exit(1)
		}
		fmt.Printf("%+v\n", entry)

	default:
		panic("should not be reached")
	}
}