2019-03-26 08:29:15 -04:00
|
|
|
package yandex
|
|
|
|
|
|
|
|
import (
|
2020-11-30 05:27:45 -05:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2019-03-26 08:29:15 -04:00
|
|
|
"github.com/c2h5oh/datasize"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2020-11-30 05:27:45 -05:00
|
|
|
"github.com/yandex-cloud/go-genproto/yandex/cloud/compute/v1"
|
|
|
|
ycsdk "github.com/yandex-cloud/go-sdk"
|
2019-03-26 08:29:15 -04:00
|
|
|
)
|
|
|
|
|
2020-12-09 10:51:34 -05:00
|
|
|
func StepHaltWithError(state multistep.StateBag, err error) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-03-26 08:29:15 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
func toGigabytes(bytesCount int64) int {
|
|
|
|
return int((datasize.ByteSize(bytesCount) * datasize.B).GBytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBytes(gigabytesCount int) int64 {
|
|
|
|
return int64((datasize.ByteSize(gigabytesCount) * datasize.GB).Bytes())
|
|
|
|
}
|
2020-11-30 05:27:45 -05:00
|
|
|
|
|
|
|
func writeSerialLogFile(ctx context.Context, state multistep.StateBag, serialLogFile string) error {
|
|
|
|
sdk := state.Get("sdk").(*ycsdk.SDK)
|
2020-12-03 04:27:13 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-11-30 05:27:45 -05:00
|
|
|
|
2021-01-25 06:26:21 -05:00
|
|
|
instanceID, ok := state.GetOk("instance_id")
|
|
|
|
|
|
|
|
if !ok || instanceID.(string) == "" {
|
2020-11-30 05:27:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ui.Say("Try get instance's serial port output and write to file " + serialLogFile)
|
|
|
|
serialOutput, err := sdk.Compute().Instance().GetSerialPortOutput(ctx, &compute.GetInstanceSerialPortOutputRequest{
|
2021-01-25 06:26:21 -05:00
|
|
|
InstanceId: instanceID.(string),
|
2020-11-30 05:27:45 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to get serial port output for instance (id: %s): %s", instanceID, err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(serialLogFile, []byte(serialOutput.Contents), 0600); err != nil {
|
|
|
|
return fmt.Errorf("Failed to write serial port output to file: %s", err)
|
|
|
|
}
|
|
|
|
ui.Message("Serial port output has been successfully written")
|
|
|
|
return nil
|
|
|
|
}
|