@@ -88,13 +88,17 @@ func ShowInterface(iface string) (string, error) {
88
88
}
89
89
90
90
// CreateDBEntry sets up a node entry for the specified tgt in the nodes iscsi nodes db
91
- func CreateDBEntry (tgtIQN , portal , iFace string , discoverySecrets , sessionSecrets Secrets ) error {
91
+ func CreateDBEntry (tgtIQN , portal , iFace string , discoverySecrets , sessionSecrets Secrets , chapDiscovery bool ) error {
92
92
debug .Println ("Begin CreateDBEntry..." )
93
+ if ! chapDiscovery {
94
+ return nil
95
+ }
93
96
baseArgs := []string {"-m" , "node" , "-T" , tgtIQN , "-p" , portal }
94
97
_ , err := iscsiCmd (append (baseArgs , []string {"-I" , iFace , "-o" , "new" }... )... )
95
98
if err != nil {
96
99
return err
97
100
}
101
+
98
102
if discoverySecrets .SecretsType == "chap" {
99
103
debug .Printf ("Setting CHAP Discovery..." )
100
104
createCHAPEntries (baseArgs , discoverySecrets , true )
@@ -103,25 +107,59 @@ func CreateDBEntry(tgtIQN, portal, iFace string, discoverySecrets, sessionSecret
103
107
if sessionSecrets .SecretsType == "chap" {
104
108
debug .Printf ("Setting CHAP Session..." )
105
109
createCHAPEntries (baseArgs , sessionSecrets , false )
106
-
107
110
}
111
+
108
112
return err
109
113
110
114
}
111
115
116
+ func updateISCSIDiscoverydb (tp , iface string , discoverySecrets Secrets ) error {
117
+ baseArgs := []string {"-m" , "discoverydb" , "-t" , "sendtargets" , "-p" , tp , "-I" , iface }
118
+ _ , err := iscsiCmd (append (baseArgs , []string {"-o" , "update" , "-n" , "discovery.sendtargets.auth.authmethod" , "-v" , "CHAP" }... )... )
119
+ if err != nil {
120
+ return fmt .Errorf ("failed to update discoverydb with CHAP, err: %v" , err )
121
+ }
122
+
123
+ return createCHAPEntries (baseArgs , discoverySecrets , true )
124
+ }
125
+
126
+ // Discovery discover the iscsi target
127
+ func Discovery (tp , iface string , discoverySecrets Secrets , chapDiscovery bool ) error {
128
+ debug .Println ("Begin Discovery..." )
129
+ baseArgs := []string {"-m" , "discoverydb" , "-t" , "sendtargets" , "-p" , tp , "-I" , iface }
130
+ out , err := iscsiCmd (append (baseArgs , []string {"-o" , "new" }... )... )
131
+ if err != nil {
132
+ return fmt .Errorf ("failed to create new entry of target in discoverydb, output: %v, err: %v" , string (out ), err )
133
+ }
134
+
135
+ if chapDiscovery {
136
+ if err := updateISCSIDiscoverydb (tp , iface , discoverySecrets ); err != nil {
137
+ return err
138
+ }
139
+ }
140
+
141
+ _ , err = iscsiCmd (append (baseArgs , []string {"--discover" }... )... )
142
+ if err != nil {
143
+ //delete the discoverydb record
144
+ iscsiCmd (append (baseArgs , []string {"-o" , "delete" }... )... )
145
+ return fmt .Errorf ("failed to sendtargets to portal %s, err: %v" , tp , err )
146
+ }
147
+ return nil
148
+ }
149
+
112
150
func createCHAPEntries (baseArgs []string , secrets Secrets , discovery bool ) error {
113
151
args := []string {}
114
152
debug .Printf ("Begin createCHAPEntries (discovery=%t)..." , discovery )
115
153
if discovery {
116
154
args = append (baseArgs , []string {"-o" , "update" ,
117
- "-n" , "node. discovery.auth.authmethod" , "-v" , "CHAP" ,
118
- "-n" , "node. discovery.auth.username" , "-v" , secrets .UserName ,
119
- "-n" , "node. discovery.auth.password" , "-v" , secrets .Password }... )
155
+ "-n" , "discovery.sendtargets .auth.authmethod" , "-v" , "CHAP" ,
156
+ "-n" , "discovery.sendtargets .auth.username" , "-v" , secrets .UserName ,
157
+ "-n" , "discovery.sendtargets .auth.password" , "-v" , secrets .Password }... )
120
158
if secrets .UserNameIn != "" {
121
- args = append (args , []string {"-n" , "node. discovery.auth.username_in" , "-v" , secrets .UserNameIn }... )
159
+ args = append (args , []string {"-n" , "discovery.sendtargets .auth.username_in" , "-v" , secrets .UserNameIn }... )
122
160
}
123
- if secrets .UserNameIn != "" {
124
- args = append (args , []string {"-n" , "node. discovery.auth.password_in" , "-v" , secrets .PasswordIn }... )
161
+ if secrets .PasswordIn != "" {
162
+ args = append (args , []string {"-n" , "discovery.sendtargets .auth.password_in" , "-v" , secrets .PasswordIn }... )
125
163
}
126
164
127
165
} else {
@@ -133,13 +171,17 @@ func createCHAPEntries(baseArgs []string, secrets Secrets, discovery bool) error
133
171
if secrets .UserNameIn != "" {
134
172
args = append (args , []string {"-n" , "node.session.auth.username_in" , "-v" , secrets .UserNameIn }... )
135
173
}
136
- if secrets .UserNameIn != "" {
174
+ if secrets .PasswordIn != "" {
137
175
args = append (args , []string {"-n" , "node.session.auth.password_in" , "-v" , secrets .PasswordIn }... )
138
176
}
139
177
}
178
+
140
179
_ , err := iscsiCmd (args ... )
141
- return err
180
+ if err != nil {
181
+ return fmt .Errorf ("failed to update discoverydb with CHAP, err: %v" , err )
182
+ }
142
183
184
+ return nil
143
185
}
144
186
145
187
// GetSessions retrieves a list of current iscsi sessions on the node
@@ -151,7 +193,13 @@ func GetSessions() (string, error) {
151
193
152
194
// Login performs an iscsi login for the specified target
153
195
func Login (tgtIQN , portal string ) error {
154
- _ , err := iscsiCmd ([]string {"-m" , "node" , "-T" , tgtIQN , "-p" , portal , "-l" }... )
196
+ baseArgs := []string {"-m" , "node" , "-T" , tgtIQN , "-p" , portal }
197
+ _ , err := iscsiCmd (append (baseArgs , []string {"-l" }... )... )
198
+ if err != nil {
199
+ //delete the node record from database
200
+ iscsiCmd (append (baseArgs , []string {"-o" , "delete" }... )... )
201
+ return fmt .Errorf ("failed to sendtargets to portal %s, err: %v" , portal , err )
202
+ }
155
203
return err
156
204
}
157
205
@@ -173,5 +221,11 @@ func DeleteDBEntry(tgtIQN string) error {
173
221
args := []string {"-m" , "node" , "-T" , tgtIQN , "-o" , "delete" }
174
222
iscsiCmd (args ... )
175
223
return nil
224
+ }
176
225
226
+ // DeleteIFace delete the iface
227
+ func DeleteIFace (iface string ) error {
228
+ debug .Println ("Begin DeleteIFace..." )
229
+ iscsiCmd ([]string {"-m" , "iface" , "-I" , iface , "-o" , "delete" }... )
230
+ return nil
177
231
}
0 commit comments