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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
package googlecompute
import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"strings"
"time"
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)
// StepCreateInstance represents a Packer build step that creates GCE instances.
type StepCreateInstance struct {
Debug bool
}
func (c *Config) createInstanceMetadata(sourceImage *Image, sshPublicKey string) (map[string]string, map[string]string, error) {
instanceMetadataNoSSHKeys := make(map[string]string)
instanceMetadataSSHKeys := make(map[string]string)
sshMetaKey := "ssh-keys"
var err error
var errs *packersdk.MultiError
// Copy metadata from config.
for k, v := range c.Metadata {
if k == sshMetaKey {
instanceMetadataSSHKeys[k] = v
} else {
instanceMetadataNoSSHKeys[k] = v
}
}
// Merge any existing ssh keys with our public key, unless there is no
// supplied public key. This is possible if a private_key_file was
// specified.
if sshPublicKey != "" {
sshMetaKey := "ssh-keys"
sshPublicKey = strings.TrimSuffix(sshPublicKey, "\n")
sshKeys := fmt.Sprintf("%s:%s %s", c.Comm.SSHUsername, sshPublicKey, c.Comm.SSHUsername)
if confSSHKeys, exists := instanceMetadataSSHKeys[sshMetaKey]; exists {
sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSSHKeys)
}
instanceMetadataSSHKeys[sshMetaKey] = sshKeys
}
startupScript := instanceMetadataNoSSHKeys[StartupScriptKey]
if c.StartupScriptFile != "" {
var content []byte
content, err = ioutil.ReadFile(c.StartupScriptFile)
if err != nil {
return nil, instanceMetadataNoSSHKeys, err
}
startupScript = string(content)
}
instanceMetadataNoSSHKeys[StartupScriptKey] = startupScript
// Wrap any found startup script with our own startup script wrapper.
if startupScript != "" && c.WrapStartupScriptFile.True() {
instanceMetadataNoSSHKeys[StartupScriptKey] = StartupScriptLinux
instanceMetadataNoSSHKeys[StartupWrappedScriptKey] = startupScript
instanceMetadataNoSSHKeys[StartupScriptStatusKey] = StartupScriptStatusNotDone
}
if sourceImage.IsWindows() {
// Windows startup script support is not yet implemented so clear any script data and set status to done
instanceMetadataNoSSHKeys[StartupScriptKey] = StartupScriptWindows
instanceMetadataNoSSHKeys[StartupScriptStatusKey] = StartupScriptStatusDone
}
// If UseOSLogin is true, force `enable-oslogin` in metadata
// In the event that `enable-oslogin` is not enabled at project level
if c.UseOSLogin {
instanceMetadataNoSSHKeys[EnableOSLoginKey] = "TRUE"
}
for key, value := range c.MetadataFiles {
var content []byte
content, err = ioutil.ReadFile(value)
if err != nil {
errs = packersdk.MultiErrorAppend(errs, err)
}
instanceMetadataNoSSHKeys[key] = string(content)
}
if errs != nil && len(errs.Errors) > 0 {
return instanceMetadataNoSSHKeys, instanceMetadataSSHKeys, errs
}
return instanceMetadataNoSSHKeys, instanceMetadataSSHKeys, nil
}
func getImage(c *Config, d Driver) (*Image, error) {
name := c.SourceImageFamily
fromFamily := true
if c.SourceImage != "" {
name = c.SourceImage
fromFamily = false
}
if len(c.SourceImageProjectId) == 0 {
return d.GetImage(name, fromFamily)
} else {
return d.GetImageFromProjects(c.SourceImageProjectId, name, fromFamily)
}
}
// Run executes the Packer build step that creates a GCE instance.
func (s *StepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
c := state.Get("config").(*Config)
d := state.Get("driver").(Driver)
ui := state.Get("ui").(packersdk.Ui)
sourceImage, err := getImage(c, d)
if err != nil {
err := fmt.Errorf("Error getting source image for instance creation: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if c.EnableSecureBoot && !sourceImage.IsSecureBootCompatible() {
err := fmt.Errorf("Image: %s is not secure boot compatible. Please set 'enable_secure_boot' to false or choose another source image.", sourceImage.Name)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Using image: %s", sourceImage.Name))
if sourceImage.IsWindows() && c.Comm.Type == "winrm" && c.Comm.WinRMPassword == "" {
state.Put("create_windows_password", true)
}
ui.Say("Creating instance...")
name := c.InstanceName
var errCh <-chan error
var metadataNoSSHKeys map[string]string
var metadataSSHKeys map[string]string
metadataForInstance := make(map[string]string)
metadataNoSSHKeys, metadataSSHKeys, errs := c.createInstanceMetadata(sourceImage, string(c.Comm.SSHPublicKey))
if errs != nil {
state.Put("error", errs.Error())
ui.Error(errs.Error())
return multistep.ActionHalt
}
if c.WaitToAddSSHKeys > 0 {
log.Printf("[DEBUG] Adding metadata during instance creation, but not SSH keys...")
metadataForInstance = metadataNoSSHKeys
} else {
log.Printf("[DEBUG] Adding metadata during instance creation...")
// Union of both non-SSH key meta data and SSH key meta data
addmap(metadataForInstance, metadataSSHKeys)
addmap(metadataForInstance, metadataNoSSHKeys)
}
errCh, err = d.RunInstance(&InstanceConfig{
AcceleratorType: c.AcceleratorType,
AcceleratorCount: c.AcceleratorCount,
Address: c.Address,
Description: "New instance created by Packer",
DisableDefaultServiceAccount: c.DisableDefaultServiceAccount,
DiskSizeGb: c.DiskSizeGb,
DiskType: c.DiskType,
EnableSecureBoot: c.EnableSecureBoot,
EnableVtpm: c.EnableVtpm,
EnableIntegrityMonitoring: c.EnableIntegrityMonitoring,
Image: sourceImage,
Labels: c.Labels,
MachineType: c.MachineType,
Metadata: metadataForInstance,
MinCpuPlatform: c.MinCpuPlatform,
Name: name,
Network: c.Network,
NetworkProjectId: c.NetworkProjectId,
OmitExternalIP: c.OmitExternalIP,
OnHostMaintenance: c.OnHostMaintenance,
Preemptible: c.Preemptible,
Region: c.Region,
ServiceAccountEmail: c.ServiceAccountEmail,
Scopes: c.Scopes,
Subnetwork: c.Subnetwork,
Tags: c.Tags,
Zone: c.Zone,
})
if err == nil {
ui.Message("Waiting for creation operation to complete...")
select {
case err = <-errCh:
case <-time.After(c.StateTimeout):
err = errors.New("time out while waiting for instance to create")
}
}
if err != nil {
err := fmt.Errorf("Error creating instance: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Message("Instance has been created!")
if s.Debug {
if name != "" {
ui.Message(fmt.Sprintf("Instance: %s started in %s", name, c.Zone))
}
}
// Things succeeded, store the name so we can remove it later
state.Put("instance_name", name)
// instance_id is the generic term used so that users can have access to the
// instance id inside of the provisioners, used in step_provision.
state.Put("instance_id", name)
if c.WaitToAddSSHKeys > 0 {
ui.Message(fmt.Sprintf("Waiting %s before adding SSH keys...",
c.WaitToAddSSHKeys.String()))
cancelled := s.waitForBoot(ctx, c.WaitToAddSSHKeys)
if cancelled {
return multistep.ActionHalt
}
log.Printf("[DEBUG] %s wait is over. Adding SSH keys to existing instance...",
c.WaitToAddSSHKeys.String())
err = d.AddToInstanceMetadata(c.Zone, name, metadataSSHKeys)
if err != nil {
err := fmt.Errorf("Error adding SSH keys to existing instance: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}
func (s *StepCreateInstance) waitForBoot(ctx context.Context, waitLen time.Duration) bool {
// Use a select to determine if we get cancelled during the wait
select {
case <-ctx.Done():
return true
case <-time.After(waitLen):
}
return false
}
// Cleanup destroys the GCE instance created during the image creation process.
func (s *StepCreateInstance) Cleanup(state multistep.StateBag) {
nameRaw, ok := state.GetOk("instance_name")
if !ok {
return
}
name := nameRaw.(string)
if name == "" {
return
}
config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packersdk.Ui)
ui.Say("Deleting instance...")
errCh, err := driver.DeleteInstance(config.Zone, name)
if err == nil {
select {
case err = <-errCh:
case <-time.After(config.StateTimeout):
err = errors.New("time out while waiting for instance to delete")
}
}
if err != nil {
ui.Error(fmt.Sprintf(
"Error deleting instance. Please delete it manually.\n\n"+
"Name: %s\n"+
"Error: %s", name, err))
}
ui.Message("Instance has been deleted!")
state.Put("instance_name", "")
// Deleting the instance does not remove the boot disk. This cleanup removes
// the disk.
ui.Say("Deleting disk...")
errCh, err = driver.DeleteDisk(config.Zone, config.DiskName)
if err == nil {
select {
case err = <-errCh:
case <-time.After(config.StateTimeout):
err = errors.New("time out while waiting for disk to delete")
}
}
if err != nil {
ui.Error(fmt.Sprintf(
"Error deleting disk. Please delete it manually.\n\n"+
"Name: %s\n"+
"Error: %s", config.InstanceName, err))
}
ui.Message("Disk has been deleted!")
return
}
func addmap(a map[string]string, b map[string]string) {
for k, v := range b {
a[k] = v
}
}
|