-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwinRM.go
60 lines (49 loc) · 1.83 KB
/
winRM.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
package vpc
import (
"fmt"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)
func winRMConfig(state multistep.StateBag) (*communicator.WinRMConfig, error) {
client := state.Get("client").(*IBMCloudClient)
config := state.Get("config").(Config)
ui := state.Get("ui").(packer.Ui)
instanceData := state.Get("instance_data").(*vpcv1.Instance)
instanceID := *instanceData.ID
var username, password string
var err error
if config.Comm.WinRMPassword != "" {
username = config.Comm.WinRMUser
password = config.Comm.WinRMPassword
ui.Say(fmt.Sprintf("Setting provided winrm username and password (ID: %s, IP: %s)", instanceID, config.Comm.WinRMHost))
} else {
// Grabbing credentials for the instance
username, password, err = client.GrabCredentials(instanceID, state)
if err != nil {
err := fmt.Errorf("[ERROR] Error grabbing credentials: %s", err)
state.Put("error", err)
ui.Error(err.Error())
// log.Fatalf(err.Error())
return nil, nil
}
ui.Say(fmt.Sprintf("Successfully grabbed credentials for instance (ID: %s, IP: %s)", instanceID, config.Comm.WinRMHost))
}
// Configuring WinRM
comm := communicator.WinRMConfig{
Username: username,
Password: password,
}
// Make sure to update WinRMUser and WinRMPassword in config and shared state
config.Comm.WinRMUser = username
config.Comm.WinRMPassword = password
state.Put("config", config)
state.Put("winrm_password", password)
ui.Say(fmt.Sprintf("Attempting WinRM communication... Instance credentials (Username: %s, Password: %s)", comm.Username, comm.Password))
return &comm, nil
}
func winRMCommHost(state multistep.StateBag) (string, error) {
config := state.Get("config").(Config)
return config.Comm.WinRMHost, nil
}