Skip to content

Commit

Permalink
Merge pull request #358 from xxwjj/drcontroller
Browse files Browse the repository at this point in the history
Add dr controller and fixed some bugs
  • Loading branch information
xing-yang committed Apr 18, 2018
2 parents b39ddbb + 1a17f87 commit beda15f
Show file tree
Hide file tree
Showing 28 changed files with 1,302 additions and 514 deletions.
65 changes: 40 additions & 25 deletions contrib/connector/iscsi/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ func waitForPathToExistInternal(devicePath *string, maxRetries int, deviceTransp
// GetInitiator returns all the ISCSI Initiator Name
func GetInitiator() ([]string, error) {
res, err := exec.Command("cat", "/etc/iscsi/initiatorname.iscsi").CombinedOutput()
log.Printf("result from cat: %s", res)
iqns := []string{}
if err != nil {
log.Printf("Error encountered gathering initiator names: %v", err)
Expand All @@ -110,7 +109,6 @@ func GetInitiator() ([]string, error) {

lines := strings.Split(string(res), "\n")
for _, l := range lines {
log.Printf("Inspect line: %s", l)
if strings.Contains(l, "InitiatorName=") {
iqns = append(iqns, strings.Split(l, "=")[1])
}
Expand All @@ -123,9 +121,9 @@ func GetInitiator() ([]string, error) {
// Discovery ISCSI Target
func Discovery(portal string) error {
log.Printf("Discovery portal: %s", portal)
_, err := exec.Command("iscsiadm", "-m", "discovery", "-t", "sendtargets", "-p", portal).CombinedOutput()
info, err := exec.Command("iscsiadm", "-m", "discovery", "-t", "sendtargets", "-p", portal).CombinedOutput()
if err != nil {
log.Fatalf("Error encountered in sendtargets: %v", err)
log.Println("Error encountered in sendtargets:", string(info), err)
return err
}
return nil
Expand All @@ -134,9 +132,9 @@ func Discovery(portal string) error {
// Login ISCSI Target
func Login(portal string, targetiqn string) error {
log.Printf("Login portal: %s targetiqn: %s", portal, targetiqn)
_, err := exec.Command("iscsiadm", "-m", "node", "-p", portal, "-T", targetiqn, "--login").CombinedOutput()
info, err := exec.Command("iscsiadm", "-m", "node", "-p", portal, "-T", targetiqn, "--login").CombinedOutput()
if err != nil {
log.Fatalf("Received error on login attempt: %v", err)
log.Println("Received error on login attempt:", string(info), err)
return err
}
return nil
Expand All @@ -145,20 +143,20 @@ func Login(portal string, targetiqn string) error {
// Logout ISCSI Target
func Logout(portal string, targetiqn string) error {
log.Printf("Logout portal: %s targetiqn: %s", portal, targetiqn)
_, err := exec.Command("iscsiadm", "-m", "node", "-p", portal, "-T", targetiqn, "--logout").CombinedOutput()
info, err := exec.Command("iscsiadm", "-m", "node", "-p", portal, "-T", targetiqn, "--logout").CombinedOutput()
if err != nil {
log.Fatalf("Received error on logout attempt: %v", err)
log.Println("Received error on logout attempt:", string(info), err)
return err
}
return nil
}

// Delete ISCSI Node
func Delete(targetiqn string) (err error) {
func Delete(targetiqn string) error {
log.Printf("Delete targetiqn: %s", targetiqn)
_, err = exec.Command("iscsiadm", "-m", "node", "-o", "delete", "-T", targetiqn).CombinedOutput()
info, err := exec.Command("iscsiadm", "-m", "node", "-o", "delete", "-T", targetiqn).CombinedOutput()
if err != nil {
log.Fatalf("Received error on Delete attempt: %v", err)
log.Println("Received error on Delete attempt:", string(info), err)
return err
}
return nil
Expand Down Expand Up @@ -200,22 +198,39 @@ func Connect(portal string, targetiqn string, targetlun string) (string, error)
return devicePath, nil
}

func sessionExists(portal string, tgtIqn string) bool {
info, err := exec.Command("iscsiadm", "-m", "session", "-s").CombinedOutput()
if err != nil {
log.Println("Warning: get session failed,", string(info))
return false
}
portal = strings.Replace(portal, ":", ",", -1)
for _, line := range strings.Split(string(info), "\n") {
if strings.Contains(line, tgtIqn) && strings.Contains(line, portal) {
return true
}
}
return false
}

func recordExists(portal string, tgtIqn string) bool {
_, err := exec.Command("iscsiadm", "-m", "node", "-o", "show",
"-T", tgtIqn, "-p", portal).CombinedOutput()
return err == nil
}

// Disconnect ISCSI Target
func Disconnect(portal string, targetiqn string) error {
log.Printf("Disconnect portal: %s targetiqn: %s", portal, targetiqn)

// Logout
err := Logout(portal, targetiqn)
if err != nil {
return err
if sessionExists(portal, targetiqn) {
if err := Logout(portal, targetiqn); err != nil {
return err
}
}

//Delete
err = Delete(targetiqn)
if err != nil {
return err
if recordExists(portal, targetiqn) {
return Delete(targetiqn)
}

return nil
}

Expand Down Expand Up @@ -253,7 +268,7 @@ func Format(device string, fstype string) error {
}
_, err := exec.Command("mkfs", "-t", fstype, "-F", device).CombinedOutput()
if err != nil {
log.Fatalf("failed to Format: %v", err)
log.Printf("failed to Format: %v", err)
return err
}
} else {
Expand All @@ -268,11 +283,11 @@ func Mount(device string, mountpoint string) error {

_, err := exec.Command("mkdir", "-p", mountpoint).CombinedOutput()
if err != nil {
log.Fatalf("failed to mkdir: %v", err)
log.Printf("failed to mkdir: %v", err)
}
_, err = exec.Command("mount", device, mountpoint).CombinedOutput()
if err != nil {
log.Fatalf("failed to mount: %v", err)
log.Printf("failed to mount: %v", err)
return err
}
return nil
Expand Down Expand Up @@ -303,7 +318,7 @@ func Umount(mountpoint string) error {

_, err := exec.Command("umount", mountpoint).CombinedOutput()
if err != nil {
log.Fatalf("failed to Umount: %v", err)
log.Printf("failed to Umount: %v", err)
return err
}
return nil
Expand Down
12 changes: 11 additions & 1 deletion contrib/drivers/drbd/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package drbd

import (
log "github.com/golang/glog"
pb "github.com/opensds/opensds/pkg/dock/proto"
"github.com/opensds/opensds/pkg/model"
)
Expand All @@ -28,21 +29,30 @@ func (r *ReplicationDriver) Unset() error { return nil }

// CreateReplication
func (r *ReplicationDriver) CreateReplication(opt *pb.CreateReplicationOpts) (*model.ReplicationSpec, error) {
return nil, nil
log.Infof("DRBD create replication ....")
return &model.ReplicationSpec{
PrimaryReplicationDriverData: map[string]string{"primary-key1": "test1"},
SecondaryReplicationDriverData: map[string]string{"secondary-key1": "test2"},
Metadata: map[string]string{"meta-key1": "test2"},
}, nil
}

func (r *ReplicationDriver) DeleteReplication(opt *pb.DeleteReplicationOpts) error {
log.Infof("DRBD delete replication ....")
return nil
}

func (r *ReplicationDriver) EnableReplication(opt *pb.EnableReplicationOpts) error {
log.Infof("DRBD enable replication ....")
return nil
}

func (r *ReplicationDriver) DisableReplication(opt *pb.DisableReplicationOpts) error {
log.Infof("DRBD disable replication ....")
return nil
}

func (r *ReplicationDriver) FailoverReplication(opt *pb.FailoverReplicationOpts) error {
log.Infof("DRBD failover replication ....")
return nil
}
6 changes: 0 additions & 6 deletions contrib/drivers/huawei/dorado/replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@
package dorado

import (
"fmt"
"github.com/opensds/opensds/pkg/utils/config"
"testing"
)

func TestLoadConf(t *testing.T) {
r := ReplicationDriver{}
config.CONF.OsdsDock.Backends.HuaweiDorado.ConfigPath = "./testdata/dorado.yaml"
r.Setup()
fmt.Println(r.conf)
}
37 changes: 13 additions & 24 deletions contrib/drivers/lvm/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ import (
)

const (
defaultConfPath = "/etc/opensds/driver/lvm.yaml"
volumePrefix = "volume-"
snapshotPrefix = "_snapshot-"
defaultTgtConfDir = "/etc/tgt/conf.d"
defaultTgtBindIp = "127.0.0.1"
defaultConfPath = "/etc/opensds/driver/lvm.yaml"
volumePrefix = "volume-"
snapshotPrefix = "_snapshot-"
)

type LVMConfig struct {
TgtBindIp string `yaml:"tgtBindIp"`
Pool map[string]PoolProperties `yaml:"pool,flow"`
TgtBindIp string `yaml:"tgtBindIp"`
TgtConfDir string `yaml:"tgtConfDir"`
Pool map[string]PoolProperties `yaml:"pool,flow"`
}

type Driver struct {
Expand All @@ -50,7 +53,7 @@ type Driver struct {

func (d *Driver) Setup() error {
// Read lvm config file
d.conf = &LVMConfig{TgtBindIp: "127.0.0.1"}
d.conf = &LVMConfig{TgtBindIp: defaultTgtBindIp, TgtConfDir: defaultTgtConfDir}
p := config.CONF.OsdsDock.Backends.LVM.ConfigPath
if "" == p {
p = defaultConfPath
Expand Down Expand Up @@ -184,16 +187,15 @@ func (d *Driver) InitializeConnection(opt *pb.CreateAttachmentOpts) (*model.Conn
if initiator = opt.HostInfo.GetInitiator(); initiator == "" {
initiator = "ALL"
}
// TODO Add lvm path in Metadata field.
lvPath, ok := opt.GetMetadata()["lvPath"]
if !ok {
err := errors.New("Failed to find logic volume path in volume attachment metadata!")
log.Error(err)
return nil, err
}

t := targets.NewTarget(d.conf.TgtBindIp)
expt, err := t.CreateExport(lvPath, initiator)
t := targets.NewTarget(d.conf.TgtBindIp, d.conf.TgtConfDir)
expt, err := t.CreateExport(opt.GetVolumeId(), lvPath, initiator)
if err != nil {
log.Error("Failed to initialize connection of logic volume:", err)
return nil, err
Expand All @@ -206,24 +208,11 @@ func (d *Driver) InitializeConnection(opt *pb.CreateAttachmentOpts) (*model.Conn
}

func (d *Driver) TerminateConnection(opt *pb.DeleteAttachmentOpts) error {
var initiator string
if initiator = opt.HostInfo.GetInitiator(); initiator == "" {
initiator = "ALL"
}
// TODO Add lvm path in Metadata field.
lvPath, ok := opt.GetMetadata()["lvPath"]
if !ok {
err := errors.New("Failed to find logic volume path in volume attachment metadata!")
log.Error(err)
return err
}

t := targets.NewTarget(d.conf.TgtBindIp)
if err := t.RemoveExport(lvPath, initiator); err != nil {
t := targets.NewTarget(d.conf.TgtBindIp, d.conf.TgtConfDir)
if err := t.RemoveExport(opt.GetVolumeId()); err != nil {
log.Error("Failed to initialize connection of logic volume:", err)
return err
}

return nil
}

Expand Down
5 changes: 3 additions & 2 deletions contrib/drivers/lvm/lvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ func TestSetup(t *testing.T) {
config.CONF.OsdsDock.Backends.LVM.ConfigPath = "testdata/lvm.yaml"
var expectedDriver = &Driver{
conf: &LVMConfig{
Pool: fp,
TgtBindIp: "192.168.56.105",
Pool: fp,
TgtBindIp: "192.168.56.105",
TgtConfDir: "/etc/tgt/conf.d",
},
handler: execCmd,
}
Expand Down

0 comments on commit beda15f

Please sign in to comment.