generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 56
/
iscsi_ps_scripts.go
189 lines (148 loc) · 5.02 KB
/
iscsi_ps_scripts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package integrationtests
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
)
func installIscsiTarget() error {
_, err := runPowershellScript(IscsiTargetInstallScript)
if err != nil {
return fmt.Errorf("failed installing iSCSI target. err=%v", err)
}
return nil
}
const IscsiTargetInstallScript = `
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
# Install iSCSI Target
Install-WindowsFeature FS-iSCSITarget-Server
# Setup for loopback usage
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\iSCSI Target" -Name AllowLoopBack -Value 1
Restart-Service WinTarget
`
type IscsiSetupConfig struct {
Iqn string `json:"iqn"`
Ip string `json:"ip"`
}
const IscsiEnvironmentSetupScript = `
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$targetName = "%s"
# Get local IPv4 (e.g. 10.30.1.15, not 127.0.0.1)
$address = $(Get-NetIPAddress | Where-Object { $_.InterfaceAlias -eq "Ethernet" -and $_.AddressFamily -eq "IPv4" }).IPAddress
# Create virtual disk in RAM
New-IscsiVirtualDisk -Path "ramdisk:scratch-${targetName}.vhdx" -Size 100MB | Out-Null
# Create a target that allows all initiator IQNs and map a disk to the new target
$target = New-IscsiServerTarget -TargetName $targetName -InitiatorIds @("Iqn:*")
Add-IscsiVirtualDiskTargetMapping -TargetName $targetName -DevicePath "ramdisk:scratch-${targetName}.vhdx" | Out-Null
$output = @{
"iqn" = "$($target.TargetIqn)"
"ip" = $address
}
$output | ConvertTo-Json | Write-Output
`
const IscsiSetChapScript = `
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$targetName = "%s"
$username = "%s"
$password = "%s"
$securestring = ConvertTo-SecureString -String $password -AsPlainText -Force
$chap = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($username, $securestring)
Set-IscsiServerTarget -TargetName $targetName -EnableChap $true -Chap $chap
`
func setChap(targetName string, username string, password string) error {
script := fmt.Sprintf(IscsiSetChapScript, targetName, username, password)
_, err := runPowershellScript(script)
if err != nil {
return fmt.Errorf("failed setting CHAP on iSCSI target=%v. err=%v", targetName, err)
}
return nil
}
const IscsiSetReverseChapScript = `
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$targetName = "%s"
$password = "%s"
$username = "doesnt-matter"
$securestring = ConvertTo-SecureString -String $password -AsPlainText -Force
# Windows initiator does not uses the username for mutual authentication
$chap = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($username, $securestring)
Set-IscsiServerTarget -TargetName $targetName -EnableReverseChap $true -ReverseChap $chap
`
func setReverseChap(targetName string, password string) error {
script := fmt.Sprintf(IscsiSetReverseChapScript, targetName, password)
_, err := runPowershellScript(script)
if err != nil {
return fmt.Errorf("failed setting reverse CHAP on iSCSI target=%v. err=%v", targetName, err)
}
return nil
}
func cleanup() error {
_, err := runPowershellScript(IscsiCleanupScript)
if err != nil {
return fmt.Errorf("failed cleaning up environment. err=%v", err)
}
return nil
}
func requireCleanup(t *testing.T) {
err := cleanup()
if err != nil {
t.Fatal(err)
}
}
const IscsiCleanupScript = `
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
# Clean initiator
Get-Disk | Where-Object {$_.Bustype -eq "iSCSI"} | Set-Disk -IsOffline:$true
Get-IscsiTarget | Disconnect-IscsiTarget -Confirm:$false
Get-IscsiTargetPortal | Remove-IscsiTargetPortal -confirm:$false
# Clean target
Get-IscsiServerTarget | Remove-IscsiServerTarget
Get-IscsiVirtualDisk | Remove-IscsiVirtualDisk
# Stop iSCSI initiator
Get-Service "MsiSCSI" | Stop-Service
`
func writeTempFile(text string, extension string) (string, error) {
pattern := fmt.Sprintf("*.%s", extension)
tempfile, err := ioutil.TempFile(os.TempDir(), pattern)
if err != nil {
return "", fmt.Errorf("failed creating temp file pattern=%v: %w", pattern, err)
}
defer tempfile.Close()
_, err = tempfile.WriteString(text)
if err != nil {
return "", fmt.Errorf("failed writing to temp file name=%v: %w", tempfile.Name(), err)
}
return tempfile.Name(), nil
}
func runPowershellScript(script string) (string, error) {
path, err := writeTempFile(script, "ps1")
if err != nil {
return "", err
}
defer os.Remove(path)
cmd := exec.Command("powershell", "-File", path)
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("error running powershell script. path %s, output: %s, err: %w", path, string(out), err)
}
return string(out), nil
}
func setupEnv(targetName string) (*IscsiSetupConfig, error) {
script := fmt.Sprintf(IscsiEnvironmentSetupScript, targetName)
out, err := runPowershellScript(script)
if err != nil {
return nil, fmt.Errorf("failed setting up environment. err=%v", err)
}
config := IscsiSetupConfig{}
err = json.Unmarshal([]byte(out), &config)
if err != nil {
return nil, err
}
return &config, nil
}