Exemple #1
0
func main() {
	flag.Parse()
	err := bakapy.SetupLogging(*LOG_LEVEL)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	config, err := bakapy.ParseConfig(*CONFIG_PATH)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Configuration error: %s\n", err)
		os.Exit(1)
	}

	logger.Debug(string(config.PrettyFmt()))

	scriptPool := bakapy.NewDirectoryScriptPool(config)

	metaman := bakapy.NewMetaManClient(config.MetadataAddr, config.Secret)

	notificators := bakapy.NewNotificatorPool()
	for _, ncConfig := range config.Notificators {
		nc := bakapy.NewScriptedNotificator(scriptPool, ncConfig.Name, ncConfig.Params)
		notificators.Add(nc)
	}

	scheduler := cron.New()
	for jobName, jobConfig := range config.Jobs {
		runSpec := jobConfig.RunAt.SchedulerString()

		if jobConfig.Disabled {
			logger.Warning("job %s disabled, skipping", jobName)
			continue
		}
		storageAddr, exist := config.Storages[jobConfig.Storage]
		if !exist {
			logger.Critical("cannot find storage %s definition in config", jobConfig.Storage)
			os.Exit(1)
		}
		executor := bakapy.NewBashExecutor(jobConfig.Args, jobConfig.Host, jobConfig.Port, jobConfig.Sudo)
		job := bakapy.NewJob(
			jobName, jobConfig, storageAddr,
			scriptPool, executor, metaman,
			notificators,
		)
		logger.Info("adding job %s{%s} to scheduler", jobName, runSpec)
		err := scheduler.AddJob(runSpec, job)
		if err != nil {
			logger.Critical("cannot schedule job %s: %s", jobName, err)
			os.Exit(1)
		}
	}

	if *TEST_CONFIG_ONLY {
		return
	}

	scheduler.Start()
	<-(make(chan int))
}
Exemple #2
0
func main() {
	flag.Parse()
	err := bakapy.SetupLogging(*LOG_LEVEL)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	config, err := bakapy.ParseConfig(*CONFIG_PATH)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
	metaman := bakapy.NewMetaManClient(config.MetadataAddr, config.Secret)
	spool := bakapy.NewDirectoryScriptPool(config)

	jobName := *JOB_NAME
	jobConfig, jobExist := config.Jobs[jobName]
	if !jobExist {
		fmt.Printf("Job %s not found\n", jobName)
		os.Exit(1)
	}

	notificators := bakapy.NewNotificatorPool()
	for _, ncConfig := range config.Notificators {
		nc := bakapy.NewScriptedNotificator(spool, ncConfig.Name, ncConfig.Params)
		notificators.Add(nc)
	}

	storageAddr, exist := config.Storages[jobConfig.Storage]
	if !exist {
		fmt.Printf("Error: cannot find storage %s definition in config\n", jobConfig.Storage)
		os.Exit(1)
	}
	executor := bakapy.NewBashExecutor(jobConfig.Args, jobConfig.Host, jobConfig.Port, jobConfig.Sudo)
	job := bakapy.NewJob(
		jobName, jobConfig, storageAddr,
		spool, executor, metaman,
		notificators,
	)
	if *FORCE_TASK_ID != "" {
		if len(*FORCE_TASK_ID) != 36 {
			fmt.Println("TaskId length must be 36 bytes")
			os.Exit(1)
		}
		job.TaskId = bakapy.TaskId(*FORCE_TASK_ID)
	}
	job.Run()

	md, err := metaman.View(job.TaskId)
	if !md.Success {
		fmt.Fprintf(os.Stderr, "job failed: %s, %s\n", md.Message, md)
		os.Exit(1)
	} else {
		fmt.Println("Job finished")
	}
}