Created
January 13, 2020 00:42
-
-
Save Hidendra/85813b37e745ed28a1db808a5a0a5dab to your computer and use it in GitHub Desktop.
Helper script for creating LXC containers on Proxmox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import os | |
import subprocess | |
import sys | |
import time | |
def execute_shell(cmd) -> str: | |
""" | |
Executes cmd on shell and returns stdout output | |
""" | |
sys.stderr.write('[shell] running: %s ..' % ' '.join(cmd)) | |
start = time.time() | |
output = subprocess.check_output(cmd, encoding='utf-8').strip() | |
took = time.time() - start | |
sys.stderr.write('. took %.2f sec\n' % took) | |
return output | |
def main(): | |
parser = argparse.ArgumentParser(description='Creates & bootstraps an LXC CT') | |
parser.add_argument('--vmid', required=False, default=None, type=int, help='Override the vmid for the created CT (default: nextid)') | |
parser.add_argument('--hostname', required=True, help='The vm hostname') | |
parser.add_argument('--cores', default=2, type=int, help='Number of cores to assign') | |
parser.add_argument('--memory', default=2048, type=int, help='Amount of RAM (in MB) to assign') | |
parser.add_argument('--rootfs', required=True, help='The rootfs to create the CT on; e.g. local:16 creates the storage on local with 16 GB') | |
parser.add_argument('--template', required=True, help='The fully qualified path to the OS template; e.g. local:vztmpl/ubuntu-18.04-standard_18.04-1_amd64.tar.gz') | |
parser.add_argument('--features', default='mount=nfs', help='The features to enable on the CT (default: NFS)') | |
parser.add_argument('--net0', default='name=eth0,bridge=vmbr0,ip=dhcp,ip6=auto,type=veth', help='The configuration for net0 (default: vmbr0 dhcp)') | |
parser.add_argument('--root-pub-key', help='If provided, the path to the public key to store in root\'s authorized_keys file') | |
parser.add_argument('--apt-upgrade', action='store_true', help='If enabled, runs apt update and apt upgrade to bring the base system up to date') | |
args = parser.parse_args() | |
if args.vmid: | |
vmid = args.vmid | |
else: | |
vmid = execute_shell(['pvesh', 'get', '/cluster/nextid']) | |
sys.stderr.write('creating ct with vmid: %s\n' % vmid) | |
cmd_args = [ | |
'pct', 'create', vmid, args.template, | |
'--hostname', args.hostname, | |
'--cores', str(args.cores), | |
'--memory', str(args.memory), | |
'--features', args.features, | |
'--rootfs', args.rootfs, | |
] | |
if args.net0: | |
cmd_args.extend(['--net0', args.net0]) | |
# Create the CT | |
execute_shell(cmd_args) | |
# Start the CT | |
sys.stderr.write('starting ct %s\n' % vmid) | |
execute_shell(['pct', 'start', vmid]) | |
if args.root_pub_key: | |
if not os.path.exists(args.root_pub_key): | |
sys.stderr.write('root pub key does not exist: %s\n' % args.root_pub_key) | |
sys.stderr.write('installing root pub key from file: %s\n' % args.root_pub_key) | |
execute_shell(['pct', 'exec', vmid, 'mkdir', '/root/.ssh']) | |
execute_shell(['pct', 'exec', vmid, 'chmod', '700', '/root/.ssh']) | |
execute_shell(['pct', 'push', vmid, args.root_pub_key, '/root/.ssh/authorized_keys']) | |
execute_shell(['pct', 'exec', vmid, 'chmod', '600', '/root/.ssh/authorized_keys']) | |
if args.apt_upgrade: | |
sys.stderr.write('upgrading base system to latest pkgs with apt\n') | |
execute_shell(['pct', 'exec', vmid, 'apt', 'update']) | |
execute_shell(['pct', 'exec', vmid, '--', 'apt', 'upgrade', '-y']) | |
execute_shell(['pct', 'exec', vmid, '--', 'shutdown', '-r', 'now']) | |
return 1 | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment