Решение ошибки mkdir -p
This commit is contained in:
@@ -13,8 +13,8 @@ terraform {
|
||||
}
|
||||
|
||||
locals {
|
||||
ssh_config_path = "${path.module}/ssh_config"
|
||||
temporary_dir = "/tmp/k3s-terraform"
|
||||
temporary_dir = "/tmp"
|
||||
ssh_config_path = "${local.temporary_dir}/ssh_config"
|
||||
server_kubeconfig = "${local.temporary_dir}/k3s.yaml"
|
||||
node_token_path = "${local.temporary_dir}/node-token"
|
||||
worker_count = length(var.worker_ips)
|
||||
@@ -23,16 +23,25 @@ locals {
|
||||
# Create a temporary SSH config file for secure connections
|
||||
resource "null_resource" "setup_ssh_config" {
|
||||
triggers = {
|
||||
server_ip = var.server_ip
|
||||
worker_ips = join(",", var.worker_ips)
|
||||
ssh_user = var.ssh_user
|
||||
server_ip = var.server_ip
|
||||
worker_ips = join(",", var.worker_ips)
|
||||
ssh_user = var.ssh_user
|
||||
ssh_private_key = var.ssh_private_key
|
||||
ssh_config_path = local.ssh_config_path
|
||||
}
|
||||
|
||||
provisioner "local-exec" {
|
||||
command = <<-EOT
|
||||
mkdir -p ${dirname(local.ssh_config_path)}
|
||||
# Check if directory exists, create only if it doesn't
|
||||
SSH_CONFIG_DIR=$(dirname "${local.ssh_config_path}")
|
||||
if [ ! -d "$SSH_CONFIG_DIR" ]; then
|
||||
echo "Creating directory $SSH_CONFIG_DIR"
|
||||
mkdir -p "$SSH_CONFIG_DIR"
|
||||
else
|
||||
echo "Directory $SSH_CONFIG_DIR already exists"
|
||||
fi
|
||||
|
||||
# Create SSH config file
|
||||
cat > ${local.ssh_config_path} << 'EOF'
|
||||
Host ${var.server_ip}
|
||||
User ${var.ssh_user}
|
||||
@@ -41,7 +50,7 @@ Host ${var.server_ip}
|
||||
UserKnownHostsFile /dev/null
|
||||
|
||||
${join("\n", [
|
||||
for ip in var.worker_ips : <<-WORKER
|
||||
for ip in var.worker_ips : <<-WORKER
|
||||
Host ${ip}
|
||||
User ${var.ssh_user}
|
||||
IdentityFile ${var.ssh_private_key}
|
||||
@@ -51,16 +60,16 @@ WORKER
|
||||
])}
|
||||
EOF
|
||||
EOT
|
||||
interpreter = ["bash", "-c"]
|
||||
}
|
||||
interpreter = ["bash", "-c"]
|
||||
}
|
||||
|
||||
# Clean up SSH config on destroy
|
||||
provisioner "local-exec" {
|
||||
when = destroy
|
||||
command = "rm -f ${self.triggers.ssh_config_path}"
|
||||
interpreter = ["bash", "-c"]
|
||||
on_failure = continue
|
||||
}
|
||||
# Clean up SSH config on destroy
|
||||
provisioner "local-exec" {
|
||||
when = destroy
|
||||
command = "rm -f ${self.triggers.ssh_config_path}"
|
||||
interpreter = ["bash", "-c"]
|
||||
on_failure = continue
|
||||
}
|
||||
}
|
||||
|
||||
# Install K3s on the server node
|
||||
@@ -87,7 +96,7 @@ resource "null_resource" "install_k3s_server" {
|
||||
}
|
||||
|
||||
inline = [
|
||||
"mkdir -p ${local.temporary_dir}",
|
||||
"if [ ! -d \"${local.temporary_dir}\" ]; then mkdir -p ${local.temporary_dir}; else echo \"Directory ${local.temporary_dir} already exists\"; fi",
|
||||
"curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${var.k3s_version} INSTALL_K3S_CHANNEL=${var.k3s_channel} sh -s - server ${var.k3s_extra_server_args}",
|
||||
"until systemctl is-active --quiet k3s; do echo 'Waiting for k3s to start...'; sleep 5; done",
|
||||
"echo 'K3s server installation complete'"
|
||||
@@ -133,7 +142,8 @@ resource "null_resource" "get_k3s_config" {
|
||||
}
|
||||
|
||||
inline = [
|
||||
"mkdir -p ${local.temporary_dir}",
|
||||
"if [ ! -d \"${local.temporary_dir}\" ]; then mkdir -p ${local.temporary_dir}; else echo \"Directory ${local.temporary_dir} already exists\"; fi",
|
||||
"if [ ! -d \"$(dirname ${local.server_kubeconfig})\" ]; then mkdir -p $(dirname ${local.server_kubeconfig}); else echo \"Directory $(dirname ${local.server_kubeconfig}) already exists\"; fi",
|
||||
"sudo cp /etc/rancher/k3s/k3s.yaml ${local.server_kubeconfig}",
|
||||
"sudo chmod 644 ${local.server_kubeconfig}"
|
||||
]
|
||||
@@ -141,18 +151,31 @@ resource "null_resource" "get_k3s_config" {
|
||||
|
||||
# Download kubeconfig to local machine
|
||||
provisioner "local-exec" {
|
||||
command = "mkdir -p ${dirname(var.kubeconfig_path)} && scp -F ${local.ssh_config_path} ${var.ssh_user}@${var.server_ip}:${local.server_kubeconfig} ${var.kubeconfig_path}"
|
||||
command = <<-EOT
|
||||
# Check if directory exists, create only if it doesn't
|
||||
KUBECONFIG_DIR=$(dirname "${var.kubeconfig_path}")
|
||||
if [ ! -d "$KUBECONFIG_DIR" ]; then
|
||||
echo "Creating directory $KUBECONFIG_DIR"
|
||||
mkdir -p "$KUBECONFIG_DIR"
|
||||
else
|
||||
echo "Directory $KUBECONFIG_DIR already exists"
|
||||
fi
|
||||
|
||||
# Copy the kubeconfig file
|
||||
scp -F ${local.ssh_config_path} ${var.ssh_user}@${var.server_ip}:${local.server_kubeconfig} ${var.kubeconfig_path}
|
||||
EOT
|
||||
interpreter = ["bash", "-c"]
|
||||
}
|
||||
|
||||
# Update server URL in kubeconfig if needed
|
||||
provisioner "local-exec" {
|
||||
command = <<-EOT
|
||||
command = <<-EOT
|
||||
if [ -n "${var.replace_url}" ]; then
|
||||
sed -i 's|https://127.0.0.1:6443|https://${var.replace_url}:6443|g' ${var.kubeconfig_path}
|
||||
fi
|
||||
EOT
|
||||
interpreter = ["bash", "-c"]
|
||||
on_failure = continue
|
||||
on_failure = continue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +200,7 @@ resource "null_resource" "get_k3s_token" {
|
||||
}
|
||||
|
||||
inline = [
|
||||
"mkdir -p ${local.temporary_dir}",
|
||||
"if [ ! -d \"$(dirname ${local.node_token_path})\" ]; then mkdir -p $(dirname ${local.node_token_path}); else echo \"Directory $(dirname ${local.node_token_path}) already exists\"; fi",
|
||||
"sudo cat /var/lib/rancher/k3s/server/node-token > ${local.node_token_path}",
|
||||
"sudo chmod 644 ${local.node_token_path}"
|
||||
]
|
||||
@@ -185,7 +208,20 @@ resource "null_resource" "get_k3s_token" {
|
||||
|
||||
# Download node token to local machine
|
||||
provisioner "local-exec" {
|
||||
command = "mkdir -p ${dirname(var.node_token_path)} && scp -F ${local.ssh_config_path} ${var.ssh_user}@${var.server_ip}:${local.node_token_path} ${var.node_token_path}"
|
||||
command = <<-EOT
|
||||
# Check if directory exists, create only if it doesn't
|
||||
TOKEN_DIR=$(dirname "${var.node_token_path}")
|
||||
if [ ! -d "$TOKEN_DIR" ]; then
|
||||
echo "Creating directory $TOKEN_DIR"
|
||||
mkdir -p "$TOKEN_DIR"
|
||||
else
|
||||
echo "Directory $TOKEN_DIR already exists"
|
||||
fi
|
||||
|
||||
# Copy the node token file
|
||||
scp -F ${local.ssh_config_path} ${var.ssh_user}@${var.server_ip}:${local.node_token_path} ${var.node_token_path}
|
||||
EOT
|
||||
interpreter = ["bash", "-c"]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +248,7 @@ resource "null_resource" "copy_token_to_workers" {
|
||||
}
|
||||
|
||||
inline = [
|
||||
"mkdir -p ${local.temporary_dir}"
|
||||
"if [ ! -d \"${local.temporary_dir}\" ]; then mkdir -p ${local.temporary_dir}; fi"
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user