-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·216 lines (187 loc) · 5.7 KB
/
deploy.sh
File metadata and controls
executable file
·216 lines (187 loc) · 5.7 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ANSIBLE_DIR="${SCRIPT_DIR}/nimaora-infrastructure/ansible"
TERRAFORM_DIR="${SCRIPT_DIR}/nimaora-infrastructure/terraform/arvancloud"
ENVIRONMENT="${1:-production}"
ACTION="${2:-deploy}"
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_banner() {
echo -e "${BLUE}"
echo "╔══════════════════════════════════════════════════════════════════╗"
echo "║ 🚀 Nimaora Deployment Tool ║"
echo "╠══════════════════════════════════════════════════════════════════╣"
echo "║ Environment: ${ENVIRONMENT}"
echo "║ Action: ${ACTION}"
echo "╚══════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
check_dependencies() {
log_info "Checking dependencies..."
local missing=()
if ! command -v ansible &> /dev/null; then
missing+=("ansible")
fi
if ! command -v terraform &> /dev/null; then
missing+=("terraform")
fi
if [ ${#missing[@]} -ne 0 ]; then
log_error "Missing dependencies: ${missing[*]}"
echo ""
echo "Install missing dependencies:"
echo " - Ansible: pip install ansible"
echo " - Terraform: https://www.terraform.io/downloads"
exit 1
fi
log_success "All dependencies found"
}
terraform_init() {
log_info "Initializing Terraform..."
cd "${TERRAFORM_DIR}"
terraform init
log_success "Terraform initialized"
}
terraform_plan() {
log_info "Planning infrastructure changes..."
cd "${TERRAFORM_DIR}"
terraform plan -out=tfplan
log_success "Plan created"
}
terraform_apply() {
log_info "Applying infrastructure changes..."
cd "${TERRAFORM_DIR}"
terraform apply tfplan
log_success "Infrastructure deployed"
}
terraform_destroy() {
log_warning "This will destroy all infrastructure!"
read -p "Are you sure? (yes/no): " confirm
if [ "$confirm" = "yes" ]; then
cd "${TERRAFORM_DIR}"
terraform destroy -auto-approve
log_success "Infrastructure destroyed"
else
log_info "Destroy cancelled"
fi
}
ansible_setup() {
log_info "Running initial server setup..."
cd "${ANSIBLE_DIR}"
ansible-playbook \
-i "inventories/${ENVIRONMENT}/hosts.ini" \
playbooks/setup.yml \
-e "environment=${ENVIRONMENT}"
log_success "Server setup complete"
}
ansible_deploy() {
log_info "Deploying application..."
cd "${ANSIBLE_DIR}"
ansible-playbook \
-i "inventories/${ENVIRONMENT}/hosts.ini" \
playbooks/deploy.yml \
-e "environment=${ENVIRONMENT}"
log_success "Application deployed"
}
ansible_rollback() {
log_warning "Rolling back to previous version..."
cd "${ANSIBLE_DIR}"
ansible-playbook \
-i "inventories/${ENVIRONMENT}/hosts.ini" \
playbooks/rollback.yml \
-e "environment=${ENVIRONMENT}"
log_success "Rollback complete"
}
full_deploy() {
log_info "Starting full deployment..."
cd "${TERRAFORM_DIR}"
if ! terraform state list &> /dev/null; then
log_info "No existing infrastructure found. Creating..."
terraform_init
terraform_plan
terraform_apply
else
log_info "Infrastructure exists. Updating if needed..."
terraform_plan
terraform_apply
fi
log_info "Waiting for servers to be ready..."
sleep 30
ansible_setup
ansible_deploy
log_success "Full deployment complete!"
}
show_help() {
echo "Usage: $0 [environment] [action]"
echo ""
echo "Environments:"
echo " production Production environment (default)"
echo " staging Staging environment"
echo " development Development environment"
echo ""
echo "Actions:"
echo " deploy Deploy or update the application (default)"
echo " setup Initial server setup only"
echo " full Full deployment (infrastructure + app)"
echo " rollback Rollback to previous version"
echo " infra Deploy infrastructure only"
echo " destroy Destroy all infrastructure"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " $0 production deploy # Deploy to production"
echo " $0 staging setup # Setup staging servers"
echo " $0 production rollback # Rollback production"
}
print_banner
case "$ACTION" in
deploy)
check_dependencies
ansible_deploy
;;
setup)
check_dependencies
ansible_setup
;;
full)
check_dependencies
full_deploy
;;
rollback)
check_dependencies
ansible_rollback
;;
infra)
check_dependencies
terraform_init
terraform_plan
terraform_apply
;;
destroy)
check_dependencies
terraform_destroy
;;
help|--help|-h)
show_help
;;
*)
log_error "Unknown action: $ACTION"
show_help
exit 1
;;
esac