Ejemplo n.º 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.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")
	}
}
Ejemplo n.º 2
0
func (m *JSONDir) Keys() chan bakapy.TaskId {
	dir, err := ioutil.ReadDir(m.RootDir)
	if err != nil {
		panic(fmt.Errorf("cannot list metadata directory: %s", err))
	}
	ch := make(chan bakapy.TaskId, 100)
	go func() {
		for _, f := range dir {
			ch <- bakapy.TaskId(f.Name())
		}
		close(ch)
	}()
	return ch
}
Ejemplo n.º 3
0
func (sc *StorageConn) ReadTaskId() (bakapy.TaskId, error) {
	if sc.State != bakapy.STATE_WAIT_TASK_ID {
		msg := fmt.Sprintf("protocol error - cannot read task id in state %d", sc.State)
		return bakapy.TaskId(""), errors.New(msg)
	}

	sc.logger.Debug("reading task id")
	taskIdBuf := make([]byte, bakapy.STORAGE_TASK_ID_LEN)
	readed, err := io.ReadFull(sc, taskIdBuf)
	sc.logger.Debug("readed %d bytes", readed)
	if err != nil {
		msg := fmt.Sprintf("received error on reading task id: %s", err)
		return bakapy.TaskId(""), errors.New(msg)
	}

	taskId := bakapy.TaskId(taskIdBuf)
	sc.logger.Debug("task id '%s' successfully readed.", taskId)
	sc.State = bakapy.STATE_WAIT_FILENAME
	loggerName := fmt.Sprintf("bakapy.storage.conn[%s][%s]", sc.RemoteAddr().String(), taskId)
	sc.logger = logging.MustGetLogger(loggerName)

	return taskId, nil
}
Ejemplo n.º 4
0
func TestStorageConn_ReadTaskId_Ok(t *testing.T) {
	expectedTaskId := bakapy.TaskId(uuid.NewUUID().String())
	reader := &DummyReader{
		data: []byte(expectedTaskId),
	}
	conn := NewStorageConn(reader, logging.MustGetLogger("connection.test"))
	taskId, err := conn.ReadTaskId()
	if err != nil {
		t.Fatal("error", err)
	}
	if taskId != expectedTaskId {
		t.Fatal("bad taskid:", taskId)
	}
	if conn.State != bakapy.STATE_WAIT_FILENAME {
		t.Fatal("conn.State must be ", bakapy.STATE_WAIT_FILENAME, "not", conn.State)
	}
}
Ejemplo n.º 5
0
func main() {
	flag.Parse()
	if len(flag.Args()) == 0 {
		fmt.Println(USAGE)
		os.Exit(1)
	}
	if err := bakapy.SetupLogging("warning"); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	config, err := bakapy.ParseConfig(*CONFIG_PATH)
	if err != nil {
		fmt.Println("Cannot read config", err)
		os.Exit(1)
	}

	metaman := bakapy.NewMetaManClient(config.MetadataAddr, config.Secret)
	taskId := bakapy.TaskId(flag.Arg(0))

	meta, err := metaman.View(taskId)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Cannot load metadata: %s\n", err)
		os.Exit(1)
	}
	if *ONLY_KEY != "" {
		field := reflect.ValueOf(meta).FieldByName(*ONLY_KEY)
		if !field.IsValid() {
			fmt.Fprintf(os.Stderr, "Key %s not found\n", *ONLY_KEY)
			os.Exit(1)
		}
		fmt.Println(field.Interface())
	} else {
		printMetadata(meta)
	}

}
Ejemplo n.º 6
0
func (p *NullStorageProtocolErrorReadTaskId) ReadTaskId() (bakapy.TaskId, error) {
	return bakapy.TaskId(""), errors.New("test error")
}
Ejemplo n.º 7
0
func (p *NullStorageProtocol) ReadTaskId() (bakapy.TaskId, error) {
	return bakapy.TaskId(p.taskId), nil
}