-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·171 lines (151 loc) · 5.11 KB
/
deploy.sh
File metadata and controls
executable file
·171 lines (151 loc) · 5.11 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# Script location
export SCALEHUB_BASEDIR=$(cd "$(dirname "$0")"; pwd)
IMAGE_NAME="scalehub"
SERVICE_NAME="scalehub"
## Retrieve the current user IDs and name
export UID=$(id -u)
export GID=$(id -g)
function gen_env_file(){
echo "SCALEHUB_BASEDIR=$SCALEHUB_BASEDIR" > $SCALEHUB_BASEDIR/setup/scalehub/.env
echo "UID=$UID" >> $SCALEHUB_BASEDIR/setup/scalehub/.env
echo "GID=$GID" >> $SCALEHUB_BASEDIR/setup/scalehub/.env
echo "USER=$USER" >> $SCALEHUB_BASEDIR/setup/scalehub/.env
}
# Set credentials path
export g5k_creds_path="$SCALEHUB_BASEDIR/setup/scalehub/secrets/enos/Grid5000_creds.yaml"
# Function to display help message
function display_help() {
echo "Usage: ./deploy.sh [option]"
echo " "
echo "This script helps build and deploy a containerized environment with docker."
echo "The built image contains ansible and enoslib."
echo "At runtime, a python script is loaded in the container, which allows to reserve and provision nodes on Grid5000."
echo " "
echo "Options:"
echo " generate Generate credentials file for grid5000"
echo " create Create the Docker container"
echo " remove Remove the Docker container"
echo " restart Restart the Docker container"
echo " restart_ss <service_name> Restart a specific service"
echo " shell Spawn an interactive shell in the container"
echo " push <registry> Push the Docker image to a private registry"
echo " help Display this help message"
}
# Function to generate Docker secret with credentials
function generate_creds() {
if [ -f "$g5k_creds_path" ]; then
read -p "File exists at $g5k_creds_path. Do you want to modify it? (y/n): " answer
else
read -p "File doesn't exist at $g5k_creds_path. Do you want to create it? (y/n): " answer
fi
if [ "$answer" = "y" ]; then
read -p "Enter username: " username
read -s -p "Enter password: " password
echo "username: $username" > "$g5k_creds_path"
echo -e "\npassword: $password" >> "$g5k_creds_path"
# if [ -f "$g5k_creds_path" ]; then
# echo -e "\nFile modified successfully."
# git update-index --assume-unchanged $g5k_creds_path
# else
# echo -e "\nFile created successfully."
# fi
else
if [ -f "$g5k_creds_path" ]; then
echo "No modifications made."
else
echo "No file created."
fi
fi
}
function generate_scalehub_keys(){
# These kays are used to expose the container via SSH to Pycharm or VSCode
# This behavior is intended to be used during development to facilitate code editing and debugging with IDEs that support remote development over SSH.
if [ ! -f "$SCALEHUB_BASEDIR/setup/scalehub/secrets/scalehub" ]; then
echo "Generating SSH keys for ScaleHub..."
ssh-keygen -t rsa -b 4096 -f "$SCALEHUB_BASEDIR/setup/scalehub/secrets/scalehub" -N ""
echo "SSH keys generated at $SCALEHUB_BASEDIR/setup/scalehub/secrets/scalehub and scalehub.pub"
else
echo "SSH keys already exist at $SCALEHUB_BASEDIR/setup/scalehub/secrets/scalehub. Skipping generation."
fi
}
function restart_service_ss(){
service_name="$1"
docker compose -p scalehub -f $SCALEHUB_BASEDIR/setup/scalehub/docker-compose.yml up --build --no-deps -d --force-recreate $service_name
}
function restart_service(){
if is_service_running; then
remove_service
create_service
else
echo "Service is not running."
fi
}
# Function to create the Docker container
function create_service() {
gen_env_file
generate_scalehub_keys
docker compose -p scalehub -f $SCALEHUB_BASEDIR/setup/scalehub/docker-compose.yml up --build -d
# Prune docker image to clean unnecessary cached layers
# docker image prune -f
}
function remove_service() {
docker compose -p scalehub -f $SCALEHUB_BASEDIR/setup/scalehub/docker-compose.yml down --rmi all --remove-orphans
}
# Function to check if the service is running
function is_service_running() {
if docker ps --format '{{.Names}}' | grep -q "$SERVICE_NAME"; then
echo "Container $SERVICE_NAME is running."
else
echo "Container $SERVICE_NAME is not running."
fi
}
# Function to get an interactive shell for the service
function get_shell() {
if is_service_running; then
docker exec -it $SERVICE_NAME fish
else
create_service
get_shell
fi
}
# Function to push the Docker image to a private registry
function push_image() {
registry="$1"
docker tag myproject "$registry/$IMAGE_NAME"
docker push "$registry/$IMAGE_NAME"
}
# Parse the command line argument
case "$1" in
build)
build_image
;;
generate)
generate_creds
;;
create)
create_service
;;
remove)
remove_service
;;
restart)
restart_service
;;
restart_ss)
restart_service_ss "$2"
;;
shell)
get_shell
;;
push)
push_image "$2"
;;
help)
display_help
;;
*)
display_help
exit 1
;;
esac