hashicorp / packer-plugin-googlecompute

Packer plugin for Google Compute Builder
https://www.packer.io/docs/builders/googlecompute
Mozilla Public License 2.0
23 stars 53 forks source link

builder googlecompute preserves intermediate machine's hostname instead of set hostname to instance name for newly created machines #103

Open EmilMaksimov opened 2 years ago

EmilMaksimov commented 2 years ago

This issue applies to the googlecompute builder.

Newly created VM based on a Packer created Windows Image in Google Compute gets hostname set to Packer intermediate machine's hostname (packer-62bd6611-ac4b-9422-203a-ed4b75e1ac50). Every next VM gets hostname set to the same value.

Expected behaviour - Newly create VM get hostname set to instance name

nywilken commented 1 year ago

Hi @EmilMaksimov apologies for the delay here. Is this still an issue for you?

To confirm you are seeing that creating a new image from a Packer built image is reusing the name of the intermediate instance (used to create the image) as the hostname for all newly created images. Is my understanding correct?

If so, are the new instances being created manually or via some automated process like Packer or Terraform?

If you could provide the template used for creating the initial image that would be helpful. From my understanding hostnames are generated by GCP unless a specific hostname is set at creation time.

EmilMaksimov commented 1 year ago

Hi @nywilken ,

Your understanding is correct.

New instances based on newly created packer image are created manually.

Template is very simple. Only install SSH and setup user account. Base image is Google'stock image - windows-server-2019-dc-v20220812.

Let me know if any other troubleshooting needs to be done.

packer {
  required_version = ">= 1.1.0"
}

variables {
  gcp_account_file            = ""
  gcp_disk_size               = ""
  gcp_image_family            = ""
  gcp_image_description       = ""
  gcp_machine_type            = ""
  gcp_project_id              = ""
  gcp_source_image            = ""
  gcp_ssh_username            = ""
  gcp_ssh_password            = ""
  gcp_zone                    = ""
  gcp_network_project_id      = ""
  gcp_subnetwork              = ""
  gcp_coverity_user           = ""
  gcp_coverity_pass           = ""
  gcp_artifactory_user        = ""
  gcp_artifactory_api_key     = ""
  gcp_artifactory_url         = ""
  gcp_user                    = ""
  gcp_pass                    = ""
  gcp_jenkins_remote_location = ""
  gcp_image_template_name     = ""
  dry_run                     = null
  time_stamp                  = ""
  job_info                    = ""
  git_commit                  = ""
  release_notes               = ""
}

source "googlecompute" "gcp_agent" {
  account_file       = "${var.gcp_account_file}"
  disk_size          = "${var.gcp_disk_size}"
  image_family       = "${var.gcp_image_family}"
  image_name         = "${var.gcp_image_template_name}-${var.time_stamp}"
  machine_type       = "${var.gcp_machine_type}"
  project_id         = "${var.gcp_project_id}"
  source_image       = "${var.gcp_source_image}"
  ssh_username       = "${var.gcp_ssh_username}"
  ssh_password       = "${var.gcp_ssh_password}"
  zone               = "${var.gcp_zone}"
  network_project_id = "${var.gcp_network_project_id}"
  subnetwork         = "${var.gcp_subnetwork}"
  use_internal_ip    = true
  omit_external_ip   = true
  skip_create_image  = "${var.dry_run}"
  image_labels = {
  jenkins-info = "${var.job_info}",
  git-commit = "${var.git_commit}",
  release-notes = "${var.release_notes}"
  }
  metadata = {
    windows-startup-script-cmd = "powershell Start-Sleep -s 45 & net user ${var.gcp_ssh_username} \"${var.gcp_ssh_password}\" /add /y & wmic UserAccount where Name=\"${var.gcp_ssh_username}\" set PasswordExpires=False & net localgroup administrators ${var.gcp_ssh_username} /add & powershell Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 & powershell Start-Service sshd & powershell Set-Service -Name sshd -StartupType 'Automatic' & powershell New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 & powershell.exe -NoProfile -ExecutionPolicy Bypass -Command \"Set-ExecutionPolicy -ExecutionPolicy bypass -Force\""
  }
  communicator = "ssh"
  ssh_timeout = "1h"
  state_timeout = "15m"
}

build {
  description = "Build windows base image"

  sources = ["source.googlecompute.gcp_agent"]

}
skorobogatydmitry commented 3 months ago

I have the same problem with AlmaLinux 9. In my case it's a configuraiton problem, as provisioning script the config has is very complex. Hostname gets updated to the right one after reboot. Are there any known cases causing hostname to freeze till reboot?