Skip to content

Commit

Permalink
fix different issues on sheel script and more meaningful names
Browse files Browse the repository at this point in the history
Signed-off-by: est-suse <[email protected]>
  • Loading branch information
est-suse committed Oct 3, 2023
1 parent 3c08b3b commit e98e88e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
18 changes: 17 additions & 1 deletion modules/install/rke2_master.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ mkdir -p /etc/rancher/rke2
create_config() {
local fake_fqdn="${1}"
hostname=$(hostname -f)

cat << EOF >>/etc/rancher/rke2/config.yaml
write-kubeconfig-mode: "0644"
tls-san:
- ${fake_fqdn}
- ${fake_fqdn}
node-name: ${hostname}
EOF
}

add_config() {
local server_flags="${1}"
local public_ip="${2}"

if [ -n "$server_flags" ] && [[ "$server_flags" == *":"* ]]
then
echo "$server_flags"
Expand All @@ -45,6 +47,19 @@ network_manager() {
subscription-manager repos --enable=rhel-7-server-extras-rpms
fi

if [[ "$node_os" = *"centos"* ]] || [[ "$node_os" = *"rhel"* ]] || [[ "$node_os" = *"oracle"* ]]
then
NM_CLOUD_SETUP_SERVICE_ENABLED=$(systemctl status nm-cloud-setup.service | grep -i enabled)
NM_CLOUD_SETUP_TIMER_ENABLED=$(systemctl status nm-cloud-setup.timer | grep -i enabled)

if [ "${NM_CLOUD_SETUP_SERVICE_ENABLED}" ]; then
systemctl disable nm-cloud-setup.service
fi

if [ "${NM_CLOUD_SETUP_TIMER_ENABLED}" ]; then
systemctl disable nm-cloud-setup.timer
fi

if [ "$node_os" = "centos8" ] || [ "$node_os" = "rhel8" ] || [ "$node_os" = "oracle8" ]
then
yum install tar -y
Expand Down Expand Up @@ -91,6 +106,7 @@ install() {
systemctl restart systemd-sysctl
useradd -r -c "etcd user" -s /sbin/nologin -M etcd -U
fi

sudo systemctl enable rke2-server
sudo systemctl start rke2-server
}
Expand Down
9 changes: 2 additions & 7 deletions modules/install/rke2_stop_start_instance.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
#!/bin/bash
#set -x
#echo "$@"
#instanceState=$1
#resource_name=$2


output_files() {
local instanceState="${1}"
local resource_name="${2}"

cd /tmp || exit
if [ "${1}" == "stop" ]; then
#output instances IDs

aws ec2 describe-instances --filters "Name=tag:Name,Values=${resource_name}-server" \
"Name=instance-state-name,Values=running" \
--output text --query 'Reservations[*].Instances[*].InstanceId' > /tmp/ids_server_1_2.txt

aws ec2 describe-instances --filters "Name=tag:Name,Values=${resource_name}-worker" \
"Name=instance-state-name,Values=running" \
--output text --query 'Reservations[*].Instances[*].InstanceId' > /tmp/ids_master_worker.txt

cat /tmp/ids_server_1_2.txt /tmp/ids_master_worker.txt > /tmp/ids_all.txt
fi
}

assign_file(){
local instanceState="${1}"

if [ "${1}" == "stop" ]; then
file="ids_all.txt"
elif [ "${1}" == "start_s1_s2" ]; then
Expand All @@ -38,6 +32,7 @@ assign_file(){

stop_start_nodes(){
local instanceState="${1}"

i=1
while read -r line; do
if [ "${instanceState}" == "stop" ]; then
Expand Down
10 changes: 5 additions & 5 deletions modules/rke2/master/instances_server.tf
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ resource "aws_eip" "master_with_eip2" {
}
depends_on = [aws_eip.master_with_eip ]
}

resource "aws_eip_association" "master-stop-association" {
resource "aws_eip_association" "master_eip_association" {
count = var.create_eip ? 1 : 0
instance_id = aws_instance.master.id
allocation_id = aws_eip.master_with_eip[0].id
depends_on = [aws_eip.master_with_eip]
}

resource "aws_eip_association" "master2-stop-association" {
resource "aws_eip_association" "master2_eip_association" {
for_each = local.master_with_eip
instance_id = aws_instance.master2[each.key].id
allocation_id = aws_eip.smaster_with_eip2[each.key].id
Expand Down Expand Up @@ -133,7 +133,7 @@ resource "null_resource" "master_eip" {
}

depends_on = [aws_instance.master,
aws_eip_association.master-stop-association]
aws_eip_association.master_eip_association]
}

resource "null_resource" "master2_eip" {
Expand All @@ -154,7 +154,7 @@ resource "null_resource" "master2_eip" {
}

depends_on = [null_resource.master_eip,
aws_eip_association.master2-stop-association]
aws_eip_association.master2_eip_association]
}

resource "aws_instance" "master2" {
Expand Down
24 changes: 12 additions & 12 deletions modules/rke2/worker/instances_worker.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,39 @@ resource "aws_instance" "worker" {
}

locals {
eip_index = { for i, v in aws_instance.worker : tonumber(i) => v.id if var.create_eip}
worker_with_eip = { for i, v in aws_instance.worker : tonumber(i) => v.id if var.create_eip}
}

resource "aws_eip" "worker-eip" {
resource "aws_eip" "worker_with_eip" {
depends_on = [var.dependency]
for_each = local.eip_index
for_each = local.worker_with_eip
vpc = true
tags = {
Name ="${var.resource_name}-worker-${each.key}"
}
}

resource "aws_eip_association" "worker-association" {
for_each = local.eip_index
resource "aws_eip_association" "worker_eip_association" {
for_each = local.worker_with_eip
instance_id = aws_instance.worker[each.key].id
allocation_id = aws_eip.worker-eip[each.key].id
allocation_id = aws_eip.worker_with_eip[each.key].id
depends_on = [aws_instance.worker]
}

resource "null_resource" "worker_eip" {

for_each = local.eip_index
for_each = local.worker_with_eip
connection {
type = "ssh"
user = var.aws_user
host = aws_eip.worker-eip[each.key].public_ip
host = aws_eip.worker_with_eip[each.key].public_ip
private_key = "${file(var.access_key)}"
}

provisioner "remote-exec" {
inline = [
"sudo sed -i s/${local.master_ip_prev}/${local.master_ip}/g /etc/rancher/rke2/config.yaml",
"sudo sed -i s/-ip:.*/\"-ip: ${aws_eip.worker-eip[each.key].public_ip}\"/g /etc/rancher/rke2/config.yaml",
"sudo sed -i s/-ip:.*/\"-ip: ${aws_eip.worker_with_eip[each.key].public_ip}\"/g /etc/rancher/rke2/config.yaml",
"sudo systemctl restart rke2-agent"
]
}
Expand All @@ -82,7 +82,7 @@ provisioner "local-exec" {
}

depends_on = [aws_instance.worker,
aws_eip_association.worker-association]
aws_eip_association.worker_eip-association]
}

data "local_file" "master_ip" {
Expand Down Expand Up @@ -112,7 +112,7 @@ locals {
node_token = trimspace("${data.local_file.token.content}")
}

resource "null_resource" "stop_resource" {
resource "null_resource" "stop_all_nodes" {
count = var.create_eip ? 1 : 0
depends_on = [null_resource.worker_eip]
provisioner "local-exec" {
Expand All @@ -126,7 +126,7 @@ resource "null_resource" "stop_resource" {
resource "time_sleep" "wait_for_stop" {
count = var.create_eip ? 1 : 0
create_duration = "400s"
depends_on = [null_resource.stop_resource]
depends_on = [null_resource.stop_all_nodes]
}

resource "null_resource" "start_server1_server2" {
Expand Down

0 comments on commit e98e88e

Please sign in to comment.