Решение ошибки mkdir -p
This commit is contained in:
@@ -1,27 +1,27 @@
|
|||||||
# K3s Server Configuration
|
# K3s Server Configuration
|
||||||
server_ip = "reg.benadis.org" # Replace with your server IP
|
server_ip = "reg.benadis.org" # Replace with your server IP
|
||||||
worker_ips = [ # Replace with your worker IPs
|
worker_ips = [ # Replace with your worker IPs
|
||||||
"worker1.benadis.org",
|
# "worker1.benadis.org",
|
||||||
"worker2.benadis.org"
|
"worker2.benadis.org"
|
||||||
]
|
]
|
||||||
ssh_user = "root" # Replace with your SSH username
|
ssh_user = "root" # Replace with your SSH username
|
||||||
ssh_private_key = "~/.ssh/id_rsa" # Path to your private SSH key
|
ssh_private_key = "~/.ssh/id_rsa" # Path to your private SSH key
|
||||||
replace_url = "reg.benadis.org" # Optional: URL to replace in kubeconfig, leave empty to use server_ip
|
replace_url = "reg.benadis.org" # Optional: URL to replace in kubeconfig, leave empty to use server_ip
|
||||||
|
|
||||||
# Cluster Configuration
|
# Cluster Configuration
|
||||||
domain = "reg.benadis.org" # Your domain name
|
domain = "reg.benadis.org" # Your domain name
|
||||||
k3s_version = "latest" # K3s version to install
|
k3s_version = "v1.32.3+k3s1" # K3s version to install
|
||||||
k3s_channel = "stable" # K3s release channel
|
k3s_channel = "stable" # K3s release channel
|
||||||
kubeconfig_path = "./k3s.yaml" # Where to save kubeconfig
|
kubeconfig_path = "./k3s.yaml" # Where to save kubeconfig
|
||||||
node_token_path = "./tmp/node-token" # Where to save node token
|
node_token_path = "./tmp/node-token" # Where to save node token
|
||||||
|
|
||||||
# Optional Features
|
# Optional Features
|
||||||
enable_traefik_dashboard = false # Enable Traefik dashboard
|
enable_traefik_dashboard = false # Enable Traefik dashboard
|
||||||
enable_ssl = false # Enable SSL
|
enable_ssl = false # Enable SSL
|
||||||
ssl_cert_path = "~/.tls/wildcard.benadis.org.crt" # Path to SSL certificate
|
ssl_cert_path = "~/.tls/wildcard.benadis.org.crt" # Path to SSL certificate
|
||||||
ssl_key_path = "~/.tls/wildcard.benadis.org.key" # Path to SSL key
|
ssl_key_path = "~/.tls/wildcard.benadis.org.key" # Path to SSL key
|
||||||
install_argocd = false # Install ArgoCD
|
install_argocd = false # Install ArgoCD
|
||||||
enable_ssh_tunnel = false # Enable SSH tunneling
|
enable_ssh_tunnel = false # Enable SSH tunneling
|
||||||
|
|
||||||
# Advanced Configuration
|
# Advanced Configuration
|
||||||
k3s_extra_server_args = "" # Extra args for K3s server
|
k3s_extra_server_args = "" # Extra args for K3s server
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ variable "domain" {
|
|||||||
variable "k3s_version" {
|
variable "k3s_version" {
|
||||||
description = "Version of K3s to install"
|
description = "Version of K3s to install"
|
||||||
type = string
|
type = string
|
||||||
default = "v1.27.3+k3s1"
|
default = "v1.32.3+k3s1"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "k3s_channel" {
|
variable "k3s_channel" {
|
||||||
@@ -47,7 +47,7 @@ variable "k3s_channel" {
|
|||||||
variable "kubeconfig_path" {
|
variable "kubeconfig_path" {
|
||||||
description = "Path where to save the kubeconfig file locally"
|
description = "Path where to save the kubeconfig file locally"
|
||||||
type = string
|
type = string
|
||||||
default = "~/.kube/config"
|
default = "./k3s.yaml"
|
||||||
}
|
}
|
||||||
|
|
||||||
variable "node_token_path" {
|
variable "node_token_path" {
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ terraform {
|
|||||||
}
|
}
|
||||||
|
|
||||||
locals {
|
locals {
|
||||||
ssh_config_path = "${path.module}/ssh_config"
|
temporary_dir = "/tmp"
|
||||||
temporary_dir = "/tmp/k3s-terraform"
|
ssh_config_path = "${local.temporary_dir}/ssh_config"
|
||||||
server_kubeconfig = "${local.temporary_dir}/k3s.yaml"
|
server_kubeconfig = "${local.temporary_dir}/k3s.yaml"
|
||||||
node_token_path = "${local.temporary_dir}/node-token"
|
node_token_path = "${local.temporary_dir}/node-token"
|
||||||
worker_count = length(var.worker_ips)
|
worker_count = length(var.worker_ips)
|
||||||
@@ -23,16 +23,25 @@ locals {
|
|||||||
# Create a temporary SSH config file for secure connections
|
# Create a temporary SSH config file for secure connections
|
||||||
resource "null_resource" "setup_ssh_config" {
|
resource "null_resource" "setup_ssh_config" {
|
||||||
triggers = {
|
triggers = {
|
||||||
server_ip = var.server_ip
|
server_ip = var.server_ip
|
||||||
worker_ips = join(",", var.worker_ips)
|
worker_ips = join(",", var.worker_ips)
|
||||||
ssh_user = var.ssh_user
|
ssh_user = var.ssh_user
|
||||||
ssh_private_key = var.ssh_private_key
|
ssh_private_key = var.ssh_private_key
|
||||||
ssh_config_path = local.ssh_config_path
|
ssh_config_path = local.ssh_config_path
|
||||||
}
|
}
|
||||||
|
|
||||||
provisioner "local-exec" {
|
provisioner "local-exec" {
|
||||||
command = <<-EOT
|
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'
|
cat > ${local.ssh_config_path} << 'EOF'
|
||||||
Host ${var.server_ip}
|
Host ${var.server_ip}
|
||||||
User ${var.ssh_user}
|
User ${var.ssh_user}
|
||||||
@@ -41,7 +50,7 @@ Host ${var.server_ip}
|
|||||||
UserKnownHostsFile /dev/null
|
UserKnownHostsFile /dev/null
|
||||||
|
|
||||||
${join("\n", [
|
${join("\n", [
|
||||||
for ip in var.worker_ips : <<-WORKER
|
for ip in var.worker_ips : <<-WORKER
|
||||||
Host ${ip}
|
Host ${ip}
|
||||||
User ${var.ssh_user}
|
User ${var.ssh_user}
|
||||||
IdentityFile ${var.ssh_private_key}
|
IdentityFile ${var.ssh_private_key}
|
||||||
@@ -51,16 +60,16 @@ WORKER
|
|||||||
])}
|
])}
|
||||||
EOF
|
EOF
|
||||||
EOT
|
EOT
|
||||||
interpreter = ["bash", "-c"]
|
interpreter = ["bash", "-c"]
|
||||||
}
|
}
|
||||||
|
|
||||||
# Clean up SSH config on destroy
|
# Clean up SSH config on destroy
|
||||||
provisioner "local-exec" {
|
provisioner "local-exec" {
|
||||||
when = destroy
|
when = destroy
|
||||||
command = "rm -f ${self.triggers.ssh_config_path}"
|
command = "rm -f ${self.triggers.ssh_config_path}"
|
||||||
interpreter = ["bash", "-c"]
|
interpreter = ["bash", "-c"]
|
||||||
on_failure = continue
|
on_failure = continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Install K3s on the server node
|
# Install K3s on the server node
|
||||||
@@ -87,7 +96,7 @@ resource "null_resource" "install_k3s_server" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline = [
|
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}",
|
"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",
|
"until systemctl is-active --quiet k3s; do echo 'Waiting for k3s to start...'; sleep 5; done",
|
||||||
"echo 'K3s server installation complete'"
|
"echo 'K3s server installation complete'"
|
||||||
@@ -133,7 +142,8 @@ resource "null_resource" "get_k3s_config" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline = [
|
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 cp /etc/rancher/k3s/k3s.yaml ${local.server_kubeconfig}",
|
||||||
"sudo chmod 644 ${local.server_kubeconfig}"
|
"sudo chmod 644 ${local.server_kubeconfig}"
|
||||||
]
|
]
|
||||||
@@ -141,18 +151,31 @@ resource "null_resource" "get_k3s_config" {
|
|||||||
|
|
||||||
# Download kubeconfig to local machine
|
# Download kubeconfig to local machine
|
||||||
provisioner "local-exec" {
|
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
|
# Update server URL in kubeconfig if needed
|
||||||
provisioner "local-exec" {
|
provisioner "local-exec" {
|
||||||
command = <<-EOT
|
command = <<-EOT
|
||||||
if [ -n "${var.replace_url}" ]; then
|
if [ -n "${var.replace_url}" ]; then
|
||||||
sed -i 's|https://127.0.0.1:6443|https://${var.replace_url}:6443|g' ${var.kubeconfig_path}
|
sed -i 's|https://127.0.0.1:6443|https://${var.replace_url}:6443|g' ${var.kubeconfig_path}
|
||||||
fi
|
fi
|
||||||
EOT
|
EOT
|
||||||
interpreter = ["bash", "-c"]
|
interpreter = ["bash", "-c"]
|
||||||
on_failure = continue
|
on_failure = continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +200,7 @@ resource "null_resource" "get_k3s_token" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline = [
|
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 cat /var/lib/rancher/k3s/server/node-token > ${local.node_token_path}",
|
||||||
"sudo chmod 644 ${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
|
# Download node token to local machine
|
||||||
provisioner "local-exec" {
|
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 = [
|
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