Skip to content

Instantly share code, notes, and snippets.

@XavM
Created November 4, 2015 15:38
Show Gist options
  • Save XavM/75718c663c4484edd438 to your computer and use it in GitHub Desktop.
Save XavM/75718c663c4484edd438 to your computer and use it in GitHub Desktop.
Create an openVZ CT
#!/bin/bash
## Fail fast and be aware of exit codes
set -eo pipefail
# Guess unused CTID, by increasing the last one
get_ct_id() {
#local ctid=$(/usr/sbin/vzlist -H -a -octid \
# 2>/dev/null | tail -1)
#[ -n "$ctid" ] || ctid=100
#echo $((ctid+1))
date +%s
}
# Guess unused hostname, by increasing the last one
get_ct_hostname() {
local prefix="${1:-ct}"
local names=$(
/usr/sbin/vzlist -H -a -ohostname \
| grep ${prefix}
)
local suffix=$(
for name in ${names}; do
echo ${name//[A-Z\.]/}
done \
| sort -n \
| tail -1
)
[ -n "${suffix}" ] || suffix=0
echo "${prefix}$((suffix+1))"
}
# Configure CT interface to DHCP
set_ct_dhcp() {
local ctid="${1}"
local hostname="${2}"
local eth="${3}"
local osdist="${4}"
[[ "${osdist}" == "alpine" ]] \
&& {
cat << _EOF_ >> /vz/root/${ctid}/etc/network/interfaces
# VETH private static IP
auto ${eth}
iface ${eth} inet dhcp
hostname ${hostname}
_EOF_
echo "${hostname}" > /vz/root/${ctid}/etc/hostname
} \
|| {
cat << _EOF_ > /vz/root/${ctid}/etc/sysconfig/network-scripts/ifcfg-${eth}
DEVICE="${eth}"
BOOTPROTO="dhcp"
NM_CONTROLLED="no"
ONBOOT="yes"
DHCP_HOSTNAME="${hostname}"
_EOF_
}
}
get_ct_ip() {
local ctid="${1}"
local ctip=$(vzctl exec ${ctid} ip -o -4 a show eth0 | awk '{print $4}')
echo ${ctip/\/*/}
}
wait_for_ct_to_start() {
local ctid="${1}"
local count=0
until [[ "$(vzlist -H ${ctid} -o status)" == "running" ]]; do
echo -ne " Waiting for CT ${ctid} to be up ($((++count)) Sec)\\r"
sleep 1
done
}
wait_for_ct_network() {
local ctid="${1}"
echo -e '
count=0
until $(ip -o -4 a show eth0 | awk \x27{print $4}\x27 | grep -q "/24"); do
echo -ne " Waiting for network to be up ($((++count)) Sec)"\r; sleep 1
done' \
| vzctl exec ${ctid} - \
&& local ctip="$(get_ct_ip ${ctid})" \
&& echo -ne "\n Network is up for ${hostname} : ${ctip} \n"
}
# Configure CT authorized_keys
set_ct_authorized_keys() {
local ctid="${1}"
mkdir -p /vz/root/${ctid}/root/.ssh/
chmod 0700 /vz/root/${ctid}/root/.ssh/
cp /root/.ssh/authorized_keys /vz/root/${ctid}/root/.ssh/
chmod 0700 /vz/root/${ctid}/root/.ssh/authorized_keys
}
# Configure CT proxy
set_ct_proxy() {
local domain="${1}"
cat << _EOF_ > /vz/root/$ctid/etc/profile.d/proxy.sh
export http_proxy="http://proxy.service.${domain}:8080"
export https_proxy="http://proxy.service.${domain}:8080"
export no_proxy="localhost,127.0.0.1,.${domain}"
_EOF_
}
# Custom alias
set_custom_alias() {
local ctid="${1}"
cat << _EOF_ > /vz/root/${ctid}/etc/profile.d/alias.sh
alias ll="ls -larth"
_EOF_
}
# Set xterm for screen
set_alpine_xterm() {
local ctid="${1}"
local osdist="${2}"
[[ "${osdist}" == "alpine" ]] && {
cat << _EOF_ > /vz/root/${ctid}/etc/profile.d/screen.sh
export TERM=xterm-color
_EOF_
} || return 0
}
function set_hn_known_hosts() {
local ctid=${1}
local ctname=${2}
local ctip=${3}
ssh-keygen -R ${ctname} >/dev/null 2>&1
ssh-keygen -R ${ctip} >/dev/null 2>&1
local ctkey=$(vzctl exec ${ctid} cat /etc/ssh/ssh_host_rsa_key.pub)
echo "${ctname},${ctip} ${ctkey}" >> ~/.ssh/known_hosts
}
gen_template() {
local TMPL_list=""
[[ $# -gt 0 ]] \
&& while [ "${1:0:2}" == '--' ]; do
OPTION=${1:2}
echo ${OPTION} | grep -q "=" \
&& {
export "TMPL_${OPTION/=*/}=${OPTION/*=/}" \
TMPL_list="${TMPL_list} TMPL_${OPTION/=*/}"
}
shift
done
echo "${TMPL_template}" | envsubst
for i in ${TMPL_list}; do
#echo "${i} -> ${!i}"
unset ${i}
done
}
main() {
local ostemplate="${1}"
local osdist="${ostemplate%%-*}"
local config="vswap-256m"
local ctid="$(get_ct_id)"
local domain="dev"
local hostname_prefix="ct"
local ct_ip_range=192.168.0.{21..254}
local nameserver="ns1"
local datacenter="xav"
local ct_ip_hostname=$(/root/dhcp_ct ${datacenter} ${hostname_prefix} 192.168.0.{21..254}) \
&& local ctip=${ct_ip_hostname##* } \
&& local hostname=${ct_ip_hostname%% *} \
&& /usr/sbin/vzctl --quiet create ${ctid} \
--ostemplate ${ostemplate} \
--config ${config} \
--diskspace 2G \
--hostname "${hostname}" \
--ipadd "${ctip}" \
--name ${hostname} \
> /dev/null \
&& vzctl --quiet mount ${ctid} \
&& set_ct_authorized_keys ${ctid} \
&& set_ct_proxy ${domain} \
&& set_custom_alias ${ctid} \
&& set_alpine_xterm ${ctid} ${osdist} \
&& vzctl --quiet start ${ctid} \
&& wait_for_ct_to_start ${ctid} \
&& vzctl --quiet set ${ctid} --userpasswd root:changeMe >/dev/null \
&& echo " Container ${hostname} created" \
&& set_hn_known_hosts ${ctid} ${hostname} ${ctip} \
&& /usr/sbin/vzctl --quiet snapshot ${ctid} --skip-suspend --skip-config
}
main "${1-centos-6-x86_64-minimal-zz}"
#main "${1-alpine-3.2.3-x86_64}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment