如何使用 Terraform 在 Google Cloud Container Optimized OS 下执行脚本(远程执行)
How to execute scripts (remote-exec) under Google Cloud Container Optimized OS using Terraform
我知道容器优化 OS 主要是“noexec”。我有一个用例,当我需要在我的主目录中执行一些简单的脚本时,将文件从我的 docker 图像复制到主机等。当我使用 SSH 登录实例时,这没有问题。但是对于 terraform 它似乎不起作用:
resource "null_resource" "test_upload2" {
count = length(var.nodes)
provisioner "remote-exec" {
connection {
type = "ssh"
host = google_compute_address.static[count.index].address
private_key = file("keys/private_key")
user = var.admin_username
script_path = "/home/hyperledger/provision.sh"
}
inline = [
"ls"
]
}
depends_on = [google_compute_instance.peer-blockchain-vm, null_resource.test_upload]
}
我收到以下错误消息:
null_resource.test_upload[0] (remote-exec): bash: /home/hyperledger/provision.sh: Permission denied
Error: error executing "/home/hyperledger/provision.sh": Process exited with status 126
有没有一种方法可以完全使用 Terraform 来执行此操作?
将此逻辑外包给某些本地 shell 脚本并使用“local-exec”实现目标似乎不太好。
目前我已经找到了解决方案。
当我创建实例时,我设置了以下 startup-script:
resource "google_compute_instance" "my_vm" {
...
metadata_startup_script = "mkdir -p /home/hyperledger/tmp/;sudo mount -t tmpfs -o size=100M tmpfs /home/hyperledger/tmp/"
}
它创建一个 in-memory 磁盘。 resource 中的 script-path 应该重新定义如下:
script_path = "/home/hyperledger/tmp/provision.sh"
这个临时目录下的所有脚本都可以执行。
第一个执行的配置程序应该更改主目录的所有者,因为它是用上面的 root 所有者创建的:
provisioner "remote-exec" {
connection {
type = "ssh"
private_key = file(var.private_key)
user = var.admin_username
script_path = "/home/hyperledger/tmp/provision.sh"
}
inline = [
"sudo chown -R hyperledger:hyperledger /home/hyperledger"
]
}
我知道容器优化 OS 主要是“noexec”。我有一个用例,当我需要在我的主目录中执行一些简单的脚本时,将文件从我的 docker 图像复制到主机等。当我使用 SSH 登录实例时,这没有问题。但是对于 terraform 它似乎不起作用:
resource "null_resource" "test_upload2" {
count = length(var.nodes)
provisioner "remote-exec" {
connection {
type = "ssh"
host = google_compute_address.static[count.index].address
private_key = file("keys/private_key")
user = var.admin_username
script_path = "/home/hyperledger/provision.sh"
}
inline = [
"ls"
]
}
depends_on = [google_compute_instance.peer-blockchain-vm, null_resource.test_upload]
}
我收到以下错误消息:
null_resource.test_upload[0] (remote-exec): bash: /home/hyperledger/provision.sh: Permission denied
Error: error executing "/home/hyperledger/provision.sh": Process exited with status 126
有没有一种方法可以完全使用 Terraform 来执行此操作? 将此逻辑外包给某些本地 shell 脚本并使用“local-exec”实现目标似乎不太好。
目前我已经找到了解决方案。 当我创建实例时,我设置了以下 startup-script:
resource "google_compute_instance" "my_vm" {
...
metadata_startup_script = "mkdir -p /home/hyperledger/tmp/;sudo mount -t tmpfs -o size=100M tmpfs /home/hyperledger/tmp/"
}
它创建一个 in-memory 磁盘。 resource 中的 script-path 应该重新定义如下:
script_path = "/home/hyperledger/tmp/provision.sh"
这个临时目录下的所有脚本都可以执行。
第一个执行的配置程序应该更改主目录的所有者,因为它是用上面的 root 所有者创建的:
provisioner "remote-exec" {
connection {
type = "ssh"
private_key = file(var.private_key)
user = var.admin_username
script_path = "/home/hyperledger/tmp/provision.sh"
}
inline = [
"sudo chown -R hyperledger:hyperledger /home/hyperledger"
]
}