Exemple #1
0
func (v *VolumeEntry) Destroy(db *bolt.DB, executor executors.Executor) error {
	logger.Info("Destroying volume %v", v.Info.Id)

	// Get the entries from the database
	brick_entries := make([]*BrickEntry, len(v.Bricks))
	var sshhost string
	db.View(func(tx *bolt.Tx) error {
		for index, id := range v.BricksIds() {
			brick, err := NewBrickEntryFromId(tx, id)
			if err != nil {
				logger.LogError("Brick %v not found in db: %v", id, err)
				continue
			}
			brick_entries[index] = brick

			// Set ssh host to send volume commands
			if sshhost == "" {
				node, err := NewNodeEntryFromId(tx, brick.Info.NodeId)
				if err != nil {
					logger.LogError("Unable to determine brick node: %v", err)
					return err
				}
				sshhost = node.ManageHostName()
			}
		}
		return nil
	})

	// :TODO: What if the host is no longer available, we may need to try others
	// Stop volume
	err := executor.VolumeDestroy(sshhost, v.Info.Name)
	if err != nil {
		logger.LogError("Unable to delete volume: %v", err)
		return err
	}

	// Destroy bricks
	err = DestroyBricks(db, executor, brick_entries)
	if err != nil {
		logger.LogError("Unable to delete bricks: %v", err)
		return err
	}

	// Remove from entries from the db
	err = db.Update(func(tx *bolt.Tx) error {
		for _, brick := range brick_entries {
			err = v.removeBrickFromDb(tx, brick)
			if err != nil {
				logger.Err(err)
				// Everything is destroyed anyways, just keep deleting the others
				// Do not return here
			}
		}

		// Remove volume from cluster
		cluster, err := NewClusterEntryFromId(tx, v.Info.Cluster)
		if err != nil {
			logger.Err(err)
			// Do not return here.. keep going
		}
		cluster.VolumeDelete(v.Info.Id)

		err = cluster.Save(tx)
		if err != nil {
			logger.Err(err)
			// Do not return here.. keep going
		}

		// Delete volume
		v.Delete(tx)

		return nil
	})

	return err
}