forked from ej52/proxmox-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.sh
200 lines (174 loc) · 6.02 KB
/
create.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
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
#!/usr/bin/env bash
export EPS_BASE_URL=${EPS_BASE_URL:-https://raw.githubusercontent.com/ej52/proxmox-scripts/main}
export EPS_CT_INSTALL=true
export EPS_UTILS_COMMON=$(wget --no-cache -qO- $EPS_BASE_URL/utils/common.sh)
source <(echo -n "$EPS_UTILS_COMMON")
pms_bootstrap
pms_settraps
while [ "$#" -gt 0 ]; do
case $1 in
--app)
EPS_APP_NAME=$2
shift;;
--id)
EPS_CT_ID=$2
shift;;
--os)
EPS_OS_DISTRO=$2;
shift;;
--os-version)
EPS_OS_VERSION=$2;
shift;;
--bridge)
EPS_CT_NETWORK_BRIDGE=$2
shift;;
--cores)
EPS_CT_CPU_CORES=$2
shift;;
--disksize)
EPS_CT_DISK_SIZE=$2
shift;;
--hostname)
EPS_CT_HOSTNAME=$2
shift;;
--memory)
EPS_CT_MEMORY=$2
shift;;
--storage)
EPS_CT_STORAGE_CONTAINER=$2
shift;;
--templates)
EPS_CT_STORAGE_TEMPLATES=$2
shift;;
--swap)
EPS_CT_SWAP=$2
shift;;
--cleanup)
EPS_CLEANUP=true;;
*)
log "error" "Unrecognized option: ${CLR_CYB}$1${CLR}" "" 1;;
esac
shift
done
EPS_APP_NAME=${EPS_APP_NAME:-}
if [ -z "$EPS_APP_NAME" ]; then
log "error" "No application provided" "" 1
fi
export EPS_APP_CONFIG=$(wget --no-cache -qO- $EPS_BASE_URL/apps/$EPS_APP_NAME/config.sh)
if [ $? -gt 0 ]; then
log "error" "Application config not found for ${CLR_CYB}$EPS_APP_NAME${CLR}" "" 1
fi
EPS_APP_INSTALL=$(wget --no-cache -qO- $EPS_BASE_URL/apps/$EPS_APP_NAME/install.sh)
if [ $? -gt 0 ]; then
log "error" "No install script found for ${CLR_CYB}$EPS_APP_NAME${CLR}" "" 1
fi
EPS_CT_ID=${EPS_CT_ID:-$(pvesh get /cluster/nextid)}
EPS_CT_HOSTNAME=${EPS_CT_HOSTNAME:-${EPS_APP_NAME}}
EPS_CT_NETWORK_BRIDGE=${EPS_CT_NETWORK_BRIDGE:-vmbr0}
EPS_CT_STORAGE_CONTAINER=${EPS_CT_STORAGE_CONTAINER:-local-lvm}
EPS_CT_STORAGE_TEMPLATES=${EPS_CT_STORAGE_TEMPLATES:-local}
export EPS_OS_DISTRO=${EPS_OS_DISTRO:-alpine}
EPS_OS_VERSION=${EPS_OS_VERSION:-}
export EPS_CLEANUP=${EPS_CLEANUP:-false}
EPS_CT_CPU_CORES=${EPS_CT_CPU_CORES:-1}
EPS_CT_DISK_SIZE=${EPS_CT_DISK_SIZE:-4}
EPS_CT_MEMORY=${EPS_CT_MEMORY:-512}
EPS_CT_SWAP=${EPS_CT_SWAP:-0}
[ "$EPS_CT_ID" -ge 100 ] || log "error" "ID cannot be less than 100" "" 1
if pct status $EPS_CT_ID &>$__OUTPUT; then
log "error" "ID is already in use: ${CLR_CYB}$EPS_CT_ID${CLR}" "" 1
fi
source <(echo -n "$EPS_APP_CONFIG")
pms_header
EPS_OS_NAME=$(os_name)
pms_check_os
[ "$EPS_CT_DISK_SIZE" -ge 0 ] 2>$__OUTPUT || log "error" "Disk Size should be a vaild integer" "" 1
lxc_checks
log "info" "Container will be created using the following settings.
Application: ${CLR_CYB}$EPS_APP_NAME${CLR_CY}
OS Distribution: ${CLR_CYB}$EPS_OS_DISTRO${CLR_CY}
OS Version: ${CLR_CYB}${EPS_OS_VERSION:-latest}${CLR_CY}
Container ID: ${CLR_CYB}$EPS_CT_ID${CLR_CY}
Container Hostname: ${CLR_CYB}$EPS_CT_HOSTNAME${CLR_CY}
Allocated Cores: ${CLR_CYB}$EPS_CT_CPU_CORES${CLR_CY}
Allocated Memory: ${CLR_CYB}$EPS_CT_MEMORY${CLR_CY}
Allocated Swap: ${CLR_CYB}$EPS_CT_SWAP${CLR_CY}
Allocated Disk Size: ${CLR_CYB}$EPS_CT_DISK_SIZE${CLR_CY}
Network Bridge: ${CLR_CYB}$EPS_CT_NETWORK_BRIDGE${CLR_CY}
Container Storage: ${CLR_CYB}$EPS_CT_STORAGE_CONTAINER${CLR_CY}
Template Storage: ${CLR_CYB}$EPS_CT_STORAGE_TEMPLATES${CLR_CY}
Script Cleanup: ${CLR_CYB}$EPS_CLEANUP${CLR_CY}
${CLR_YLB}If you want to abort, hit ctrl+c within 10 seconds...${CLR}"
sleep 10
pms_header
step_start "LXC templates" "Checking" "OK"
_template=""
_templates_downloaded=($(pveam list $EPS_CT_STORAGE_TEMPLATES | grep -Eo "$EPS_OS_DISTRO.*\.(gz|xz|zst)" || true))
if [ ${#_templates_downloaded[@]} -gt 0 ]; then
for t in ${_templates_downloaded[*]}; do
if [ "${t%\-*}" = "$EPS_OS_DISTRO-$EPS_OS_VERSION" ]; then
_template=$t
break;
fi
done
fi
if [ -z $_template ]; then
pveam update &>$__OUTPUT
_templates_available=($(pveam available -section system | grep -Eo "$EPS_OS_DISTRO.*\.(gz|xz|zst)" | sort -r -t - -k 2 -V))
if [ ${#_templates_available[@]} -gt 0 ]; then
if [ -z $EPS_OS_VERSION ]; then
_template=${_templates_available[0]}
else
for t in ${_templates_available[*]}; do
if [ "${t%\-*}" = "$EPS_OS_DISTRO-$EPS_OS_VERSION" ]; then
_template=$t
break;
fi
done
fi
if [[ ! "${_templates_downloaded[*]}" =~ "$_template" ]]; then
log "info" "Downloading LXC template: ${CLR_CYB}$_template${CLR}" $(__clr)
sleep 3
pveam download $EPS_CT_STORAGE_TEMPLATES $_template >$__OUTPUT
fi
fi
else
sleep 3
fi
if [ -z $_template ]; then
step_end "LXC template not found for: ${CLR_CYB}${EPS_OS_DISTRO}:${EPS_OS_VERSION:-latest}${CLR}" 1
fi
step_end "Using LXC template: ${CLR_CYB}${_template}${CLR}"
step_start "LXC container" "Creating" "Created"
_storage_type=$(pvesm status -storage $EPS_CT_STORAGE_CONTAINER >$__OUTPUT | awk 'NR>1 {print $2}')
if [ "$_storage_type" = "zfspool" ]; then
log "warn" "Some containers may not work properly due to ZFS not supporting 'fallocate'."
sleep 3
fi
_pct_options=(
-arch $(dpkg --print-architecture)
-cmode shell
-hostname $EPS_CT_HOSTNAME
-cores $EPS_CT_CPU_CORES
-memory $EPS_CT_MEMORY
-net0 name=eth0,bridge=$EPS_CT_NETWORK_BRIDGE,ip=dhcp
-onboot 1
-ostype $EPS_OS_DISTRO
-rootfs $EPS_CT_STORAGE_CONTAINER:${EPS_CT_DISK_SIZE:-4}
-swap $EPS_CT_SWAP
-tags $EPS_APP_NAME
)
pct create $EPS_CT_ID "$EPS_CT_STORAGE_TEMPLATES:vztmpl/$_template" ${_pct_options[@]} >$__OUTPUT
pct start $EPS_CT_ID
sleep 2
if [ "$EPS_OS_DISTRO" = "alpine" ]; then
pct exec "$EPS_CT_ID" -- ash -c "apk add bash >/dev/null"
fi
step_end "LXC container ${CLR_CYB}$EPS_CT_ID${CLR_GN} created successfully"
trap - ERR
_utilDistro=$EPS_OS_DISTRO
if [ "$EPS_OS_DISTRO" = "ubuntu" ]; then
_utilDistro="debian"
fi
export EPS_UTILS_DISTRO=$(wget --no-cache -qO- $EPS_BASE_URL/utils/${_utilDistro}.sh)
lxc-attach -n $EPS_CT_ID -- bash -c "$EPS_APP_INSTALL"