Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File provisioner upload file and startup-script - not synchronised? #423

Open
vfedoriv opened this issue Aug 22, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@vfedoriv
Copy link

vfedoriv commented Aug 22, 2024

What I have: terraform code that deploys VM on GCP
It contains a startup script

 metadata = {
   ssh-keys = "${var.ssh_username}:${var.ssh_public_key}"
   startup-script = templatefile("${path.module}/startup-script.sh", {..}

and this template has code

sudo tar -xvf "/tmp/${my_archive_name}"

that should unpack the file from /tmp dir, then put some files in another place.

those my_archive_name file should be uploaded by the file provisioner.

What I see:

Terraform apply executed successfully, and an instance was created.
The file provisioner successfully uploaded the file in /tmp dir, but SOMETIME there are a few missing files (expected as the result of the unpack operation)
If I manually unpack the archive from /tmp dir, files are present
After I recalled that tar can unpack an archive even if it's incomplete, and missing files seem to be at the end of the archive, I added a delay before the unpack command to check if the arch file size is not changing
Log results:
Arch file size: 210403328
Arch file size: 235031211
Arch file size: 235031211

So it seems that or file provisioner "finished" before upload was fully completed (async?) or startup-script started prematurely

Expected behavior

startup-script should be executed after file provisioner completely uploaded the file

Observed behavior

startup-script starts before file provisioner completely uploaded the file

Terraform Configuration

resource "google_compute_instance" "my_vm" {
  provider     = google-beta
  count        = var.instance_count
  name         = "${var.vm_name}-${count.index + 1}"
  machine_type = var.vm_machine_type
  zone         = var.zone
  tags = ["my-vm-instance"]

  boot_disk {
    initialize_params {
      image = "${var.vm_image_project}/${var.vm_image_family}"
    }
  }

  attached_disk {
    source      = google_compute_disk.persistent_disk.id
    device_name = google_compute_disk.persistent_disk.name
    mode        = "READ_WRITE"
  }

  network_interface {
    network    = var.private_network
    subnetwork = var.private_subnet
    access_config {
      // Ephemeral public IP
    }
  }

  service_account {
    email = var.service_acc_email
    scopes = ["https://www.googleapis.com/auth/cloud-platform"]
  }

  metadata = {
    ssh-keys = "${var.ssh_username}:${var.ssh_public_key}"
    startup-script = templatefile("${path.module}/startup-script.sh", {
      postgres_user         = var.db_user,
      postgres_password     = var.db_password, postgres_db_name = var.db_name,
      postgres_host         = var.db_host, postgres_port = var.db_port,
      path_to_license_file  = var.dest_license_file_path,
      path_to_my_archive = var.local_my_archive_dir,
 my_archive_name = var.my_archive_name
    })

  }

  connection {
    type        = "ssh"
    user        = var.ssh_username
    private_key = file(var.ssh_private_key)
    host        = self.network_interface[0].access_config[0].nat_ip
    timeout     = "10m"
    agent       = false
  }

  provisioner "file" {
    source      = var.local_license_file
    destination = var.dest_license_file_path
  }

  provisioner "file" {
    source      = "${var.local_my_archive_dir}/${var.my_archive_name}"
    destination = "/tmp/${var.my_archive_name}"
  }

  # Ignore changes for persistent disk attachments
  lifecycle {
    ignore_changes = [attached_disk]
  }
}

Terraform Version

Terraform v1.4.6
on darwin_arm64
+ provider registry.terraform.io/hashicorp/google v5.41.0
+ provider registry.terraform.io/hashicorp/google-beta v5.41.0
+ provider registry.terraform.io/hashicorp/null v3.2.2

Additional information

No response

@vfedoriv vfedoriv added the bug Something isn't working label Aug 22, 2024
@vfedoriv
Copy link
Author

nvm, seems like upgrading Terraform to Terraform v1.9.5 version solved the issue

@vfedoriv
Copy link
Author

UPD. Still the issue with the new terraform version (flaky)
from logs:

Arch file size: 161841152
Arch file size: 177012736
Arch file size: 192774144
Arch file size: 208732160
Arch file size: 222494720
Arch file size: 231899136
Arch file size: 235031211
Arch file size: 235031211
Arch file size: 235031211

@vfedoriv vfedoriv reopened this Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant