Beispiel #1
0
// New returns a RADOS cluster handle that is used to create IO
// Contexts and perform other RADOS actions. If configFile is
// non-empty, RADOS will look for its configuration there, otherwise
// the default paths will be searched (e.g., /etc/ceph/ceph.conf).
//
// TODO: allow caller to specify Ceph user.
func New(configFile string) (*Rados, error) {
	r := &Rados{}
	var cerr C.int

	if cerr = C.rados_create(&r.rados, nil); cerr < 0 {
		return nil, radosReturnCodeError(cerr)
	}

	if configFile == "" {
		cerr = C.rados_conf_read_file(r.rados, nil)
	} else {
		cconfigFile := C.CString(configFile)
		defer C.free(unsafe.Pointer(cconfigFile))

		cerr = C.rados_conf_read_file(r.rados, cconfigFile)
	}

	if cerr < 0 {
		return nil, radosReturnCodeError(cerr)
	}

	if cerr = C.rados_connect(r.rados); cerr < 0 {
		return nil, radosReturnCodeError(cerr)
	}

	// Fill in cluster statistics
	if err := r.Stat(); err != nil {
		r.Release()
		return nil, err
	}

	return r, nil
}
Beispiel #2
0
func (rc *RadosCluster) Connect(conf string, pool string) (err error) {
	c_conf := C.CString(conf)
	c_pool := C.CString(pool)
	defer C.free(unsafe.Pointer(c_conf))
	defer C.free(unsafe.Pointer(c_pool))

	ret := C.rados_create(&rc.Cluster, nil)
	if ret < 0 {
		return errors.New("create cluster failed")
	}

	ret = C.rados_conf_read_file(rc.Cluster, c_conf)
	if ret < 0 {
		return errors.New("read conf file failed")
	}

	ret = C.rados_connect(rc.Cluster)
	if ret < 0 {
		return errors.New("connect to cluster failed")
	}

	ret = C.rados_ioctx_create(rc.Cluster, c_pool, &rc.Ioctx)
	if ret < 0 {
		return errors.New("create ioctx failed")
	}
	return nil
}
Beispiel #3
0
// NewConn creates a new connection object. It returns the connection and an
// error, if any.
func NewConn() (*Conn, error) {
	conn := &Conn{}
	ret := C.rados_create(&conn.cluster, nil)

	if ret == 0 {
		return conn, nil
	} else {
		return nil, RadosError(int(ret))
	}
}
Beispiel #4
0
// NewConn creates a new connection object. It returns the connection and an
// error, if any.
func NewConn(id string) (*Conn, error) {
	c_id := C.CString(id)
	defer C.free(unsafe.Pointer(c_id))
	conn := &Conn{}
	ret := C.rados_create(&conn.cluster, c_id)

	if ret == 0 {
		return conn, nil
	} else {
		return nil, RadosError(int(ret))
	}
}
Beispiel #5
0
func (r *Rados) ClusterCreate() error {
	var cluster C.rados_t
	//cerr := C.rados_create(&cluster, (*C.char)(unsafe.Pointer(uintptr(0))))
	cerr := C.rados_create(&cluster, nil)
	if cerr < 0 {
		return errors.New("create cluster handler failed")
	}
	//id := int64(C.random())
	//map_cluster[id] = &cluster
	r.cluster = &cluster
	return nil
}
Beispiel #6
0
// create the cluster handle with the ClusterName and UserName configured.
func (conn *Connection) createClusterHandle() error {
	conn.cluster = new(Cluster)
	var ret C.int

	switch {

	// use rados_create2() since cluster and user are both specified
	case conn.ClusterName != "" && conn.UserName != "":
		cluster := C.CString(conn.ClusterName)
		user := C.CString(conn.UserName)
		defer freeString(cluster)
		defer freeString(user)
		ret = C.rados_create2(&conn.cluster.handle, cluster, user, 0)

	// use rados_create() with the given UserName
	case conn.ClusterName == "" && conn.UserName != "":
		user := C.CString(conn.UserName)
		defer freeString(user)
		ret = C.rados_create(&conn.cluster.handle, user)

	// use rados_create() with a nil user
	case conn.ClusterName == "" && conn.UserName == "":
		ret = C.rados_create(&conn.cluster.handle, nil)

	default:
		return &RadosError{
			Code:    -2,
			Message: "Unable to create cluster handle. If cluster name is specified, also include qualified user name.",
		}
	}

	if err := toRadosError(ret); err != nil {
		err.Message = "Unable to create cluster handle. Make sure you have access to the ceph cluster."
		return err
	}
	return nil
}
Beispiel #7
0
func (r *Rados) ClusterCreateAsUser(id string) error {
	var cluster C.rados_t
	//cerr := C.rados_create(&cluster, (*C.char)(unsafe.Pointer(uintptr(0))))
	cid := C.CString(id)
	defer func() {
		C.free(unsafe.Pointer(cid))
	}()

	cerr := C.rados_create(&cluster, cid)
	if cerr < 0 {
		return errors.New("create cluster handler failed")
	}
	//id := int64(C.random())
	//map_cluster[id] = &cluster
	r.cluster = &cluster
	return nil
}
Beispiel #8
0
func getRados(username string) (C.rados_t, error) {
	var cluster C.rados_t

	str := C.CString(username)
	defer C.free(unsafe.Pointer(str))

	if i := C.rados_create(&cluster, str); i < 0 {
		return nil, strerror(i)
	}

	if i := C.rados_conf_read_file(cluster, nil); i < 0 {
		return nil, strerror(i)
	}

	if i := C.rados_connect(cluster); i != 0 {
		return nil, strerror(i)
	}

	return cluster, nil
}