Ejemplo n.º 1
0
func New(addr string, config *Config) (*Winrmcp, error) {
	endpoint, err := parseEndpoint(addr, config.Https, config.Insecure, config.CACertBytes, config.ConnectTimeout)
	if err != nil {
		return nil, err
	}
	if config == nil {
		config = &Config{}
	}

	params := winrm.NewParameters(
		winrm.DefaultParameters.Timeout,
		winrm.DefaultParameters.Locale,
		winrm.DefaultParameters.EnvelopeSize,
	)

	if config.TransportDecorator != nil {
		params.TransportDecorator = config.TransportDecorator
	}

	if config.OperationTimeout.Seconds() > 0 {
		params.Timeout = iso8601.FormatDuration(config.OperationTimeout)
	}
	client, err := winrm.NewClientWithParameters(
		endpoint, config.Auth.User, config.Auth.Password, params)
	return &Winrmcp{client, config}, err
}
// Creates a new packer.Communicator implementation over WinRM.
// Called when Packer tries to connect to WinRM
func New(endpoint *winrm.Endpoint, user string, password string, timeout time.Duration) (*Communicator, error) {
	// Create the WinRM client we use internally
	params := winrm.DefaultParameters()
	params.Timeout = iso8601.FormatDuration(timeout)
	client, err := winrm.NewClientWithParameters(endpoint, user, password, params)
	if err != nil {
		return nil, err
	}

	// Attempt to connect to the WinRM service
	shell, err := client.CreateShell()
	if err != nil {
		return nil, err
	}

	err = shell.Close()
	if err != nil {
		return nil, err
	}

	return &Communicator{
		endpoint: endpoint,
		user:     user,
		password: password,
	}, nil
}
func (c *Communicator) Start(rc *packer.RemoteCmd) error {
	log.Printf("starting remote command: %s", rc.Command)

	// Create a new shell process on the guest
	params := winrm.DefaultParameters()
	params.Timeout = iso8601.FormatDuration(time.Hour * 24)
	client, err := winrm.NewClientWithParameters(c.endpoint, c.user, c.password, params)
	if err != nil {
		return err
	}

	shell, err := client.CreateShell()
	if err != nil {
		return err
	}

	cmd, err := shell.Execute(rc.Command)
	if err != nil {
		return err
	}

	go runCommand(shell, cmd, rc)
	return nil
}