-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
165 lines (141 loc) · 3.94 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
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
#!/bin/bash
set -e
# Configuration
DOCKER_IMAGE="marianbasti/buenarda-worker:latest"
WORKERS_PER_INDEX=3
STORAGE_SIZE="100Gi" # Default value
NAMESPACE="default"
NFS_PATH="/mnt/buenarda"
TEST_MODE=""
PATTERN="*.ar"
while getopts "i:tp:s:" opt; do
case $opt in
i) NFS_SERVER="$OPTARG";;
t) TEST_MODE="--test";;
p) PATTERN="$OPTARG";;
s) STORAGE_SIZE="$OPTARG";;
*) echo "Usage: $0 -i <nfs_server_ip> [-t] [-p pattern] [-s storage_size]" >&2; exit 1;;
esac
done
if [ -z "$NFS_SERVER" ]; then
echo "Error: NFS server IP required. Usage: $0 -i <nfs_server_ip> [-t] [-p pattern] [-s storage_size]" >&2
exit 1
fi
# Check prerequisites
check_command() {
if ! command -v $1 &> /dev/null; then
echo "Error: $1 is required but not installed."
exit 1
fi
}
check_prerequisites() {
echo "Checking prerequisites..."
check_command kubectl
check_command docker
if ! kubectl cluster-info &> /dev/null; then
echo "Error: Cannot connect to Kubernetes cluster"
exit 1
fi
}
get_node_name() {
NODE_NAME=$(kubectl get nodes --no-headers -o custom-columns=":metadata.name" | head -n 1)
if [ -z "$NODE_NAME" ]; then
echo "Error: Could not get node name"
exit 1
fi
echo $NODE_NAME
}
check_resource_exists() {
local resource=$1
local name=$2
kubectl get ${resource} ${name} &> /dev/null
}
setup_nfs_storage_class() {
echo "Setting up NFS StorageClass..."
if check_resource_exists storageclass nfs-storage; then
echo "StorageClass nfs-storage already exists, skipping creation"
return
fi
cat <<EOF | kubectl apply -f -
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-storage
provisioner: kubernetes.io/nfs
parameters:
server: ${NFS_SERVER}
path: ${NFS_PATH}
EOF
}
create_persistent_volume() {
echo "Setting up persistent volume..."
if check_resource_exists pv crawler-data-pv; then
echo "PersistentVolume crawler-data-pv already exists, skipping creation"
return
fi
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:
name: crawler-data-pv
spec:
capacity:
storage: ${STORAGE_SIZE}
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs-storage
nfs:
server: ${NFS_SERVER}
path: ${NFS_PATH}
EOF
}
create_storage() {
echo "Setting up PVC..."
if check_resource_exists pvc -n ${NAMESPACE} crawler-data-pvc; then
echo "PVC crawler-data-pvc already exists, skipping creation"
return
fi
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: crawler-data-pvc
namespace: ${NAMESPACE}
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: ${STORAGE_SIZE}
storageClassName: nfs-storage
EOF
# Esto no anda? kubectl no detecta bound cuando sí está bound. HOTFIX: sleep
# kubectl wait --for=condition=bound pvc/crawler-data-pvc -n ${NAMESPACE} --timeout=30s
sleep 5
}
# Deploy crawler jobs
deploy_jobs() {
echo "Deploying crawler jobs..."
if [ -n "$TEST_MODE" ]; then
echo "Running in test mode with single job"
fi
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export PYTHONPATH="${SCRIPT_DIR}:${PYTHONPATH}"
python3 -m scripts.buenarda_job_controller --workers ${WORKERS_PER_INDEX} ${TEST_MODE} --pattern "${PATTERN}"
}
# Main deployment flow
main() {
echo "Starting deployment..."
check_prerequisites
# Create namespace if it doesn't exist
kubectl create namespace ${NAMESPACE} --dry-run=client -o yaml | kubectl apply -f -
setup_nfs_storage_class
create_persistent_volume
create_storage
deploy_jobs
echo "Deployment completed successfully!"
echo "Monitor jobs with: kubectl get jobs -n ${NAMESPACE}"
echo "View logs with: kubectl logs -f job/buenarda-crawler-* -n ${NAMESPACE}"
}
main "$@"