-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·129 lines (116 loc) · 3.13 KB
/
deploy.sh
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
#!/usr/bin/bash
# Basic directories
ANSIBLE_DIR="ansible"
CONFIG_DIR="config"
FUNCTIONS_DIR="functions"
SCRIPTS_DIR="scripts"
TERRAFORM_DIR="terraform"
VAGRANT_DIR="vagrant"
# Basic files
ANSIBLE_INVENTORY="${ANSIBLE_DIR}/inventory/hosts"
ANSIBLE_PLAYBOOK="${ANSIBLE_DIR}/playbooks/main.yml"
CONFIG_FILE="${CONFIG_DIR}/main.conf"
# Function to display script usage
function show_usage() {
echo "Usage: ./deploy.sh [tool] [action] [extra]"
echo " [tool]: The tool to use (vagrant, terraform, ansible)"
echo " [action]: The action to perform for the specified tool"
echo " [extra]: Additional parameters or arguments for specific actions"
echo ""
echo "Example:"
echo " To start Vagrant VM: ./deploy.sh vagrant up"
echo " To run Terraform plan: ./deploy.sh terraform plan"
echo " To run Playbook everything-playbook.yml using the development inventory: ./deploy.sh ansible run development.ini everything-playbook.yml"
}
# Function to display error messages and exit
function display_error() {
echo "ERROR: $1" >&2
exit 1
}
# Function to detect the environment and set options accordingly
function set_options() {
if [[ "$ENVIRONMENT" == "production" ]]; then
set -euxo pipefail
else
set -e
fi
}
# Source functions from the functions directory
source "${FUNCTIONS_DIR}/vagrant_actions.sh"
source "${FUNCTIONS_DIR}/terraform_actions.sh"
source "${FUNCTIONS_DIR}/ansible_actions.sh"
# Main Script
# If you are running the script locally, you can change this to local
ENVIRONMENT="production"
set_options
# Check if required tools are installed
command -v vagrant >/dev/null 2>&1 || display_error "Vagrant is not installed. Please install Vagrant."
command -v terraform >/dev/null 2>&1 || display_error "Terraform is not installed. Please install Terraform."
command -v ansible >/dev/null 2>&1 || display_error "Ansible is not installed. Please install Ansible."
# Check for the correct number of arguments
if [ $# -lt 2 ]; then
show_usage
display_error "Invalid number of arguments."
fi
# Parse command-line arguments
tool="$1"
action="$2"
extra="${@:3}"
case "$tool" in
"vagrant")
case "$action" in
"destroy")
halt_vagrant
destroy_vagrant
;;
"up")
start_vagrant
;;
"halt")
halt_vagrant
;;
"ssh")
ssh_vagrant
;;
*)
show_usage
display_error "Invalid action for Vagrant. Use [start|halt|ssh]."
;;
esac
;;
"terraform")
case "$action" in
"init")
terraform_init
;;
"plan")
terraform_plan
;;
"apply")
terraform_apply
;;
"destroy")
terraform_destroy
;;
*)
show_usage
display_error "Invalid action for Terraform. Use [init|plan|apply|destroy]."
;;
esac
;;
"ansible")
case "$action" in
"run")
run_playbook "$extra"
;;
*)
show_usage
display_error "Invalid action for Ansible. Use [run <inventory> playbook.yml]."
;;
esac
;;
*)
show_usage
display_error "Invalid tool. Use [ansible|terraform|vagrant]."
;;
esac