Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

images: replacing the dependency on acceptance.AzureProvider #10519

Merged
merged 16 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions azurerm/internal/acceptance/ssh/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ssh

const (
// LinuxAgentDeprovisionCommand is the command needed to "deprovision" the (Windows)Azure VM Agent
// this clears out caches, resets the vm agent configuration and generally "generalizes" this VM
// and is vaguely akin to `sysprep` on Windows.
LinuxAgentDeprovisionCommand = "sudo waagent -verbose -deprovision+user -force"
)
61 changes: 61 additions & 0 deletions azurerm/internal/acceptance/ssh/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ssh

import (
"bytes"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"golang.org/x/crypto/ssh"
)

type Runner struct {
Hostname string
Port int
Username string
Password string
CommandsToRun []string
}

func (r Runner) Run() error {
if err := resource.Retry(5*time.Minute, r.tryRun); err != nil {
return err
}

return nil
}

func (r Runner) tryRun() *resource.RetryError {
config := &ssh.ClientConfig{
User: r.Username,
Auth: []ssh.AuthMethod{
ssh.Password(r.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}

hostAddress := fmt.Sprintf("%s:%d", r.Hostname, r.Port)
log.Printf("[INFO] SSHing to %q...", hostAddress)
client, err := ssh.Dial("tcp", hostAddress, config)
if err != nil {
return resource.RetryableError(fmt.Errorf("connecting to host: %+v", err))
}

session, err := client.NewSession()
if err != nil {
return resource.RetryableError(fmt.Errorf("creating session: %+v", err))
}
defer session.Close()

for _, cmd := range r.CommandsToRun {
log.Printf("[DEBUG] Running %q..", cmd)
var b bytes.Buffer
session.Stdout = &b
if err := session.Run(cmd); err != nil {
return resource.NonRetryableError(fmt.Errorf("failure running command %q: %+v", cmd, err))
}
}

return nil
}
Loading