Skip to content

Instantly share code, notes, and snippets.

@VGerris
Forked from boriel/rndhostname
Created August 13, 2023 10:53
Show Gist options
  • Save VGerris/5ae5fcfb1b96a7ab176dd96359e6b1f8 to your computer and use it in GitHub Desktop.
Save VGerris/5ae5fcfb1b96a7ab176dd96359e6b1f8 to your computer and use it in GitHub Desktop.
Simple proxmox container scaling (Elastic cloud project)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import random
LEN = 5
PREFIX = 'ct-dm-core-job-'
DIGITS = ''.join(str(x) for x in range(10))
LETTERS = ''.join(chr(x) for x in range(ord('a'), ord('z') + 1))
ALPHA = LETTERS + DIGITS
def get_rnd_hostname():
random.seed(datetime.datetime.now())
return PREFIX + ''.join(random.choice(ALPHA) for x in range(LEN))
if __name__ == '__main__':
print(get_rnd_hostname())
#!/bin/bash
MAX=$1
MAX=$(($MAX+0))
i=0
while [ $i -lt $MAX ]
do
LAST=$(pct list|egrep -Ee '^4[0-9][0-9]'|sort|tail -1|sed -e 's/ .*$//') # Get last machine number 4xx
if [[ "$LAST" == "" ]]; then
echo "No containers in range 400-499 found. Nothing to do." > /dev/stderr
exit 1
fi
echo Removing LXC worker $LAST
pct shutdown $LAST -forceStop
sleep 2
pct destroy $LAST
i=$(($i+1))
done
#!/bin/bash
TEMPLATE=/var/lib/vz/dump/vzdump-lxc-400-2015_12_16-17_02_30.tar.lzo
STORAGE=pool0
GATEWAY=10.105.234.129
POOL=ceba
IFACE=eth0
LAST=$(pct list|egrep -Ee '^4[0-9][0-9]'|sort|tail -1|sed -e 's/ .*$//') # Get last machine number 4xx
if [[ "$LAST" == "" ]]; then
LAST=399 # Minimum VMID
fi
MAX=$1
MAX=$(($MAX+1))
i=1
NEW=$LAST
while [ $i -lt $MAX ]
do
NEW=$(($NEW+1))
echo Deploying new LXC container VMID $NEW
pct restore $NEW $TEMPLATE -storage=$STORAGE -pool=$POOL
#pct create $NEW $TEMPLATE -storage=$STORAGE -pool=$POOL # This will regenerate SSH keys
pct set $NEW -hostname $(./rndhostname)
pct set $NEW -net0 name=$IFACE,bridge=vmbr0
pct start $NEW
sleep 2
pct exec $NEW -- /usr/local/bin/waitforiface $IFACE >/dev/null
sleep 2
pct exec $NEW -- /bin/ping -c2 -q $GATEWAY >/dev/null
i=$(($i+1))
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment