.tfstate文件在文件供应器期间被锁定;Process Explorer表示没有其他锁定进程;部署不带文件供应器的works

2024-04-29 00:45:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的个人Twitch流开发一个小型python应用程序,并决定使用Terraform来处理我的基础设施。每次尝试应用main.tf时,它都会在主文件provisioner上失败。我基本上是将python代码所在的当前文件夹(使用我的.tfvvars中的绝对路径),移动到远程服务器上的/tmp/,然后在代码放在框中后将其移动到最终代码位置

file provisioner error

(在某些上下文中,这是一个旧的0.11之前的文件,在正常运行一段时间后更新)

这是我的主要任务

terraform {
  required_providers {
    digitalocean = {
      source = "digitalocean/digitalocean"
      version = "~> 2.0"
    }
  }
}


variable "private_key_file" {
  # sensitive = true
  # sensitive temp disabled for debugging
}
variable "twitch_oauth_token" {
  # sensitive = true
}
variable "twitch_client_id" {
  # sensitive = true
}
variable "auth_token" {
  # sensitive = true
}
variable "ssh_key" {
  # sensitive = true
}
variable "unix_user" {
  # sensitive = true
}
variable "unix_user_priv" {
  # sensitive = true
}

variable "local_code_directory" {}
variable "remote_code_directory" {}
variable "tmp_remote_code_directory" {
  default = "/tmp/tmp_code"
}

variable "systemd_service_file_full_path" {}
variable "systemd_service_file_dest_name" {}


# Configure the DigitalOcean Provider
provider "digitalocean" {
  token = var.auth_token
}

# Create a web server
resource "digitalocean_droplet" "bot" {
  image  = "ubuntu-18-04-x64"
  name   = "twitch-bot"
  region = "nyc2"
  size   = "s-1vcpu-1gb"
  ssh_keys = [var.ssh_key]

connection {
      user = "var.unix_user_priv"
      type = "ssh"
      private_key = file(var.private_key_file)
      host = digitalocean_droplet.bot.ipv4_address
  }

  provisioner "remote-exec" {
    inline = [
      "adduser --disabled-password -gecos \"\" ${var.unix_user}",
      "mkdir ${var.remote_code_directory}",
      "mkdir /tmp/twitch-bot/",
      "mkdir /etc/systemd/system/${var.systemd_service_file_full_path}.d/",
      "echo \"[Service]\nEnvironment=TWITCH_OAUTH_TOKEN=${var.twitch_oauth_token}\nEnvironment=TWITCH_API_CLIENT_ID=${var.twitch_client_id}\" >> /etc/systemd/system/${var.systemd_service_file_full_path}.d/override.conf",
      "mkdir -p ${var.tmp_remote_code_directory}",
      "mkdir -p ${var.remote_code_directory}"
    ]
  }


  provisioner "file" {
    source = var.systemd_service_file_full_path
    destination = "/etc/systemd/system/${var.systemd_service_file_dest_name}"
  }

# Error happens here
  provisioner "file" {
    source = var.local_code_directory
    destination = var.tmp_remote_code_directory
  }

  provisioner "remote-exec" {
    inline = [
      "mv ${var.tmp_remote_code_directory} ${var.remote_code_directory}",
      "sed -i.bak -e \"s/remote_directory/${var.remote_code_directory}/\" /etc/systemd/system/${var.systemd_service_file_full_path}",
      "systemd enable twitchbot.service"
    ]
  }
}

我已将文件夹目标(临时和最终)调整为在用户文件夹内外输出。不管怎样,结果都是一样的。Process Explorer还显示TF的PID是锁定文件的唯一PID

process explorer handle search showing only one process is locking .tfstate

showing all process in process explorer

我正在Windows终端上的Powershell中运行我的tf apply。我以管理员的身份运行了所有相关软件。不管怎样,结果都是一样的。我还重新安装了TF的最新版本,并删除/重新创建了.tfstate和while .terraform文件夹。还是不走运

注释掉文件provisioner允许框完成创建并优雅地退出Terraform

请让我知道,如果有什么愚蠢的事情,我正在做,或者如果我的谷歌是薄弱的,有一个简单的链接,为这个确切的问题。我计划尽可能地远离供应器,转而使用cloud-init或仅仅使用Packer,因为我无论如何都会将该软件用于不相关的项目。不过,我更愿意在>;0.11地形,然后从那里迭代,如果可能的话

先谢谢你