-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_deploy.sh
More file actions
135 lines (111 loc) · 3.99 KB
/
container_deploy.sh
File metadata and controls
135 lines (111 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
# Source the environment file to load the variables
source ./env_vars.sh
NODE_EXPORTER_MACHINES=(
backend
frontend
)
NETWORK=${NETWORK:-ansible-net}
# Function to split string
split_string() {
local input_string="$1" # Input string
local delimiter="$2" # Delimiter character or string
local -n output_array="$3" # Output array variable (passed by reference)
IFS="$delimiter" read -r -a output_array <<<"$input_string"
}
# Remove old containers
info header "Remove old containers"
info "Removing..."
echo
docker rm -f ansible-controller
for machine in ${!REMOTE_MACHINE_PORT_MAPPING[@]}; do
docker rm -f $machine
done
info "Complete!"
# Create network
info header "Create network '${NETWORK}'"
info "Creating..."
docker network create --driver=bridge ${NETWORK}
info "Complete!"
# Create Ansible container
info header "Create Ansible controller container"
info "Creating..."
docker build -t ansible-controller:v1 -f ./dockerfile/Dockerfile-ansible-machine .
docker run -itd \
--name ansible-controller \
--network ${NETWORK} \
--volume ./ansible/:/etc/ansible/ \
ansible-controller:v1
rsa_pub=$(docker exec -it ansible-controller sh -c 'cat ~/.ssh/id_rsa.pub')
info "Complete!"
# Build REMOTE MACHINE container
info header "Create Remote Machine container"
info "Building image..."
docker build -t ubuntu-node:20.04 -f ./dockerfile/Dockerfile-remote-machine .
info "Complete!"
# Create REMOTE MACHINE container
for machine in ${!REMOTE_MACHINE_PORT_MAPPING[@]}; do
info "Creating ${machine} VPS... "
docker run -itd \
--name=${machine} \
--publish=${REMOTE_MACHINE_PORT_MAPPING[$machine]}:${REMOTE_MACHINE_PORT_MAPPING[$machine]} \
--network=${NETWORK} \
--hostname=${machine} \
ubuntu-node:20.04
docker exec -it ${machine} sh -c "mkdir -p /root/.ssh && echo '${rsa_pub}' > /root/.ssh/authorized_keys -vvv"
info "Complete!"
done
info "Complete!"
# Get IP address of containers
info header "Get IP address of containers"
info "Getting..."
delimiter=";"
ipv4=$(docker network inspect --format="{{range .Containers}}{{.Name}}:{{.IPv4Address}}{{\"$delimiter\"}}{{end}}" $NETWORK)
split_string "$ipv4" "$delimiter" ipv4_array
info "Complete!"
# Generate inventory content
info header "Generate inventory content"
info "Generating..."
inventory=""
node_exporter_inventory="[node_exporter]"
for element in "${ipv4_array[@]}"; do
split_string "$element" ":" machine_props
split_string "${machine_props[1]}" "/" ip
machine_name=${machine_props[0]}
machine_ip=${ip[0]}
inventory+="[${machine_name}]"
inventory+="\n${machine_name}_host ansible_host=${machine_ip}\n\n"
echo "machine_name: ${machine_name}"
echo "machine_ip: ${machine_ip}"
if [ "${machine_name}" == "backend" ] || [ "${machine_name}" == "frontend" ]; then
node_exporter_inventory+="\n${machine_name}_host ansible_host=${machine_ip}"
fi
if [ ${machine_name} != "ansible" ]; then
# check connection from Controller to Nodes
info "Checking connection from Controller to ${machine_name}..."
printf "exit\n" | docker exec -i ansible-controller sh -c "ssh -o 'StrictHostKeyChecking=no' root@${machine_ip}"
info "Complete!"
fi
done
inventory+=$node_exporter_inventory
info "Complete!"
# Write inventory file
info header "Write inventory file"
info "Writing..."
echo -e $inventory >$INVENTORY
info "Complete!"
# Check Inventory file
info header "List all hosts Inventory file"
info "Listing..."
docker exec -it ansible-controller sh -c "ansible-inventory -i $CONTROLLER_PATH_TO_INVNETORY --list"
info "Complete!"
# Test ping to Grafana host
info header "Test ping to Grafana host"
info "Checking..."
docker exec -it ansible-controller sh -c "ansible -i $CONTROLLER_PATH_TO_INVNETORY -m ping grafana"
info "Complete!"
# Check connection from Controller to node (client)
info header "Play playbook"
info "Playing..."
docker exec -it ansible-controller sh -c "ansible-playbook -i $CONTROLLER_PATH_TO_INVNETORY $CONTROLLER_PATH_TO_PLAYBOOK"
info "Complete!"