mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
run install.sh script when initialising a cluster on the init machine
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -54,6 +55,37 @@ func (r *Remote) Run(ctx context.Context, cmd string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Stream runs the command on the remote host and streams its output to the provided writers.
|
||||
func (r *Remote) Stream(ctx context.Context, cmd string, stdout, stderr io.Writer) error {
|
||||
session, err := r.client.NewSession()
|
||||
if err != nil {
|
||||
return fmt.Errorf("create session: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
_ = session.Close()
|
||||
}()
|
||||
|
||||
session.Stdout, session.Stderr = stdout, stderr
|
||||
// Run the command in a goroutine to be able to cancel it.
|
||||
done := make(chan error)
|
||||
go func() {
|
||||
done <- session.Run(cmd)
|
||||
}()
|
||||
|
||||
select {
|
||||
case err = <-done:
|
||||
if err != nil {
|
||||
return fmt.Errorf("run command on remote host: %w", err)
|
||||
}
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
if err = session.Signal(ssh.SIGINT); err != nil {
|
||||
return fmt.Errorf("send interrupt signal to remote process: %w", err)
|
||||
}
|
||||
return fmt.Errorf("canceled: %w", ctx.Err())
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes the connection to the remote host.
|
||||
func (r *Remote) Close() error {
|
||||
return r.client.Close()
|
||||
|
||||
Reference in New Issue
Block a user