2013-11-28 13:18:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2014-01-10 09:46:09 +00:00
|
|
|
# 2013-2014 Nico Schottelius (nico-cdist at schottelius.org)
|
2013-11-28 13:18:24 +00:00
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
2014-01-11 20:05:14 +00:00
|
|
|
import glob
|
2013-11-28 13:18:24 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2014-01-09 23:04:46 +00:00
|
|
|
import stat
|
2014-01-11 21:34:44 +00:00
|
|
|
import sys
|
2014-01-11 20:05:14 +00:00
|
|
|
import shutil
|
2014-01-09 23:04:46 +00:00
|
|
|
import tempfile
|
2013-11-28 13:18:24 +00:00
|
|
|
|
|
|
|
import cdist.config
|
2014-01-09 23:04:46 +00:00
|
|
|
import cdist.exec.local
|
|
|
|
import cdist.exec.remote
|
2013-11-28 13:18:24 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2014-01-11 21:34:44 +00:00
|
|
|
DEFAULT_MANIFEST = """
|
|
|
|
for pkg in \
|
|
|
|
file \
|
|
|
|
linux-image-amd64 \
|
2016-07-06 08:30:13 +00:00
|
|
|
lsb-release \
|
2014-01-20 08:42:53 +00:00
|
|
|
openssh-server curl \
|
2014-01-20 08:06:02 +00:00
|
|
|
syslinux grub2 \
|
2014-01-20 07:50:41 +00:00
|
|
|
gdisk util-linux lvm2 mdadm \
|
2014-01-11 21:34:44 +00:00
|
|
|
btrfs-tools e2fsprogs jfsutils reiser4progs xfsprogs; do
|
|
|
|
__package $pkg --state present
|
|
|
|
done
|
|
|
|
|
|
|
|
# initramfs requires /init
|
|
|
|
__link /init --source /sbin/init --type symbolic
|
|
|
|
|
|
|
|
__file /etc/network/interfaces --source - --mode 0644 << eof
|
|
|
|
# The loopback network interface
|
|
|
|
auto lo
|
|
|
|
iface lo inet loopback
|
|
|
|
|
|
|
|
# The primary network interface
|
|
|
|
auto eth0
|
|
|
|
allow-hotplug eth0
|
2014-01-11 21:45:38 +00:00
|
|
|
iface eth0 inet dhcp
|
2014-01-11 21:34:44 +00:00
|
|
|
eof
|
2014-01-11 21:48:47 +00:00
|
|
|
|
|
|
|
# Steven found this out - coyping it 1:1
|
|
|
|
# fix the bloody 'stdin: is not a tty' problem
|
|
|
|
__line /root/.profile --line 'mesg n' --state absent
|
2014-06-13 12:56:24 +00:00
|
|
|
|
2016-07-06 08:15:34 +00:00
|
|
|
__hostname --name preos
|
2014-01-11 21:34:44 +00:00
|
|
|
"""
|
|
|
|
|
2014-01-09 23:04:46 +00:00
|
|
|
class PreOSExistsError(cdist.Error):
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'Path %s already exists' % self.path
|
|
|
|
|
2014-01-10 09:46:09 +00:00
|
|
|
class PreOSBootstrapError(cdist.Error):
|
|
|
|
pass
|
|
|
|
|
2014-01-09 23:04:46 +00:00
|
|
|
|
2013-11-28 13:18:24 +00:00
|
|
|
class PreOS(object):
|
2014-01-09 23:04:46 +00:00
|
|
|
|
2013-11-28 13:18:24 +00:00
|
|
|
def __init__(self, target_dir, arch="amd64"):
|
|
|
|
|
|
|
|
self.target_dir = target_dir
|
|
|
|
self.arch = arch
|
|
|
|
|
|
|
|
self.command = "debootstrap"
|
2016-07-06 08:47:28 +00:00
|
|
|
self.suite = "stable"
|
2013-11-28 13:18:24 +00:00
|
|
|
self.options = [ "--include=openssh-server",
|
|
|
|
"--arch=%s" % self.arch ]
|
|
|
|
|
2014-01-10 09:46:09 +00:00
|
|
|
self.pxelinux = "/usr/lib/syslinux/pxelinux.0"
|
2014-01-10 16:21:42 +00:00
|
|
|
self.pxelinux_cfg = """
|
2014-01-11 20:05:14 +00:00
|
|
|
DEFAULT preos
|
|
|
|
LABEL preos
|
|
|
|
KERNEL kernel
|
2014-01-10 09:46:09 +00:00
|
|
|
INITRD initramfs
|
2014-01-10 16:21:42 +00:00
|
|
|
"""
|
2014-01-10 09:46:09 +00:00
|
|
|
|
2014-01-09 23:04:46 +00:00
|
|
|
def _init_helper(self):
|
|
|
|
self.helper = {}
|
2014-01-11 21:34:44 +00:00
|
|
|
self.helper["manifest"] = self.initial_manifest
|
2014-01-09 23:04:46 +00:00
|
|
|
self.helper["remote_exec"] = """#!/bin/sh
|
|
|
|
# echo $@
|
|
|
|
# set -x
|
|
|
|
chroot="$1"; shift
|
|
|
|
|
|
|
|
script=$(mktemp "${chroot}/tmp/chroot-${0##*/}.XXXXXXXXXX")
|
|
|
|
trap cleanup INT TERM EXIT
|
|
|
|
cleanup() {
|
|
|
|
[ $__cdist_debug ] || rm "$script"
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "#!/bin/sh -l" > "$script"
|
|
|
|
echo "$@" >> "$script"
|
|
|
|
chmod +x "$script"
|
|
|
|
|
|
|
|
relative_script="${script#$chroot}"
|
|
|
|
|
2014-01-10 09:46:09 +00:00
|
|
|
# ensure PATH is setup
|
|
|
|
export PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin
|
|
|
|
|
2014-01-09 23:04:46 +00:00
|
|
|
# run in chroot
|
|
|
|
chroot "$chroot" "$relative_script"
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.helper["remote_copy"] = """#!/bin/sh
|
2014-01-10 09:46:09 +00:00
|
|
|
# echo $@
|
|
|
|
# set -x
|
2014-01-09 23:04:46 +00:00
|
|
|
src=$1; shift
|
|
|
|
dst=$1; shift
|
|
|
|
real_dst=$(echo $dst | sed 's,:,,')
|
|
|
|
cp -L "$src" "$real_dst"
|
|
|
|
"""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def exists(self):
|
|
|
|
return os.path.exists(self.target_dir)
|
|
|
|
|
2013-11-29 15:02:00 +00:00
|
|
|
def bootstrap(self):
|
2014-01-09 23:04:46 +00:00
|
|
|
if self.exists:
|
|
|
|
raise PreOSExistsError(self.target_dir)
|
|
|
|
|
2013-11-28 13:18:24 +00:00
|
|
|
cmd = [ self.command ]
|
|
|
|
cmd.extend(self.options)
|
|
|
|
cmd.append(self.suite)
|
|
|
|
cmd.append(self.target_dir)
|
|
|
|
|
|
|
|
log.debug("Bootstrap: %s" % cmd)
|
|
|
|
|
2014-09-14 11:51:13 +00:00
|
|
|
try:
|
|
|
|
subprocess.check_call(cmd)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
raise cdist.Error("Debootstrap failed (root priviliges required)")
|
2013-11-28 13:18:24 +00:00
|
|
|
|
2014-01-10 09:46:09 +00:00
|
|
|
# Required to run this - otherwise apt-get install fails
|
|
|
|
cmd = [ "chroot", self.target_dir, "/usr/bin/apt-get", "update" ]
|
2014-09-14 11:51:13 +00:00
|
|
|
try:
|
|
|
|
subprocess.check_call(cmd)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
raise cdist.Error("chrooted apt-get update failed (root priviliges required)")
|
2014-01-09 23:38:29 +00:00
|
|
|
|
2014-01-09 23:04:46 +00:00
|
|
|
def create_helper_files(self, base_dir):
|
|
|
|
for key, val in self.helper.items():
|
|
|
|
filename = os.path.join(base_dir, key)
|
|
|
|
with open(filename, "w") as fd:
|
|
|
|
fd.write(val)
|
|
|
|
os.chmod(filename, stat.S_IRUSR | stat.S_IXUSR)
|
|
|
|
|
2014-01-10 16:21:42 +00:00
|
|
|
def create_kernel(self):
|
2014-01-11 20:05:14 +00:00
|
|
|
dst = os.path.join(self.out_dir, "kernel")
|
|
|
|
srcglob = glob.glob("%s/boot/vmlinuz-*" % self.target_dir)
|
|
|
|
src = srcglob[0]
|
2014-01-10 16:21:42 +00:00
|
|
|
|
2014-01-11 20:14:04 +00:00
|
|
|
log.info("Creating kernel ...")
|
2014-01-11 20:05:14 +00:00
|
|
|
shutil.copyfile(src, dst, follow_symlinks=True)
|
|
|
|
|
|
|
|
def create_pxelinux(self):
|
|
|
|
dst = os.path.join(self.out_dir, "pxelinux.0")
|
|
|
|
src = "%s/usr/lib/syslinux/pxelinux.0" % self.target_dir
|
|
|
|
|
2014-01-11 20:14:04 +00:00
|
|
|
log.info("Creating pxelinux.0 ...")
|
2014-01-11 20:05:14 +00:00
|
|
|
shutil.copyfile(src, dst, follow_symlinks=True)
|
|
|
|
|
|
|
|
def create_pxeconfig(self):
|
|
|
|
configdir = os.path.join(self.out_dir, "pxelinux.cfg")
|
|
|
|
configfile = os.path.join(configdir, "default")
|
2014-01-11 20:14:04 +00:00
|
|
|
log.info("Creating pxe configuration ...")
|
2014-01-11 20:05:14 +00:00
|
|
|
if not os.path.isdir(configdir):
|
|
|
|
os.mkdir(configdir)
|
|
|
|
|
|
|
|
with open(configfile, "w") as fd:
|
|
|
|
fd.write(self.pxelinux_cfg)
|
2014-01-10 16:21:42 +00:00
|
|
|
|
|
|
|
def create_initramfs(self):
|
2014-01-11 20:14:04 +00:00
|
|
|
out_file = os.path.join(self.out_dir, "initramfs")
|
|
|
|
cmd="cd {target_dir}; find . -print0 | cpio --null -o --format=newc | gzip -9 > {out_file}".format(target_dir = self.target_dir, out_file = out_file)
|
2014-01-10 16:21:42 +00:00
|
|
|
|
2014-01-11 20:14:04 +00:00
|
|
|
log.info("Creating initramfs ...")
|
|
|
|
subprocess.check_call(cmd, shell=True)
|
2014-01-10 09:46:09 +00:00
|
|
|
|
2014-01-20 08:21:50 +00:00
|
|
|
def ensure_out_dir_exists(self):
|
|
|
|
os.makedirs(self.out_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
2014-01-10 16:21:42 +00:00
|
|
|
def create_iso(self, out_dir):
|
|
|
|
self.out_dir = out_dir
|
|
|
|
|
2014-01-20 08:21:50 +00:00
|
|
|
self.ensure_out_dir_exists()
|
|
|
|
|
2014-01-10 16:21:42 +00:00
|
|
|
raise cdist.Error("Generating ISO is not yet supported")
|
|
|
|
|
|
|
|
def create_pxe(self, out_dir):
|
|
|
|
self.out_dir = out_dir
|
|
|
|
|
2014-01-20 08:21:50 +00:00
|
|
|
self.ensure_out_dir_exists()
|
2014-01-10 16:21:42 +00:00
|
|
|
self.create_kernel()
|
2014-01-11 20:14:04 +00:00
|
|
|
self.create_initramfs()
|
2014-01-10 16:21:42 +00:00
|
|
|
self.create_pxeconfig()
|
2014-01-11 20:05:14 +00:00
|
|
|
self.create_pxelinux()
|
2014-01-10 16:21:42 +00:00
|
|
|
|
2014-01-11 21:34:44 +00:00
|
|
|
|
|
|
|
def setup_initial_manifest(self, user_initial_manifest, replace_manifest):
|
|
|
|
if user_initial_manifest:
|
|
|
|
if user_initial_manifest == '-':
|
|
|
|
user_initial_manifest_content = sys.stdin.read()
|
|
|
|
else:
|
|
|
|
with open(user_initial_manifest, "r") as fd:
|
|
|
|
user_initial_manifest_content = fd.read()
|
|
|
|
else:
|
|
|
|
user_initial_manifest_content = ""
|
|
|
|
|
|
|
|
if replace_manifest:
|
|
|
|
self.initial_manifest = user_initial_manifest_content
|
|
|
|
else:
|
|
|
|
self.initial_manifest = "{default}\n# User supplied manifest\n{user}".format(default=DEFAULT_MANIFEST, user=user_initial_manifest_content)
|
|
|
|
|
2014-01-09 23:04:46 +00:00
|
|
|
def config(self):
|
2014-01-11 21:34:44 +00:00
|
|
|
self._init_helper()
|
|
|
|
|
2014-01-09 23:04:46 +00:00
|
|
|
handle, path = tempfile.mkstemp(prefix='cdist.stdin.')
|
|
|
|
with tempfile.TemporaryDirectory() as tempdir:
|
|
|
|
host = self.target_dir
|
|
|
|
|
|
|
|
self.create_helper_files(tempdir)
|
|
|
|
|
|
|
|
local = cdist.exec.local.Local(
|
|
|
|
target_host=host,
|
|
|
|
initial_manifest=os.path.join(tempdir, "manifest")
|
|
|
|
)
|
|
|
|
|
|
|
|
remote = cdist.exec.remote.Remote(
|
|
|
|
target_host=host,
|
|
|
|
remote_exec=os.path.join(tempdir, "remote_exec"),
|
|
|
|
remote_copy=os.path.join(tempdir, "remote_copy"),
|
|
|
|
)
|
|
|
|
|
|
|
|
config = cdist.config.Config(local, remote)
|
|
|
|
config.run()
|
2013-11-28 13:18:24 +00:00
|
|
|
|
2014-02-03 14:29:48 +00:00
|
|
|
def cleanup(self):
|
|
|
|
# Remove cruft from chroot
|
|
|
|
for action in 'autoclean clean autoremove'.split():
|
|
|
|
cmd = [ 'chroot', self.target_dir, '/usr/bin/apt-get', action]
|
|
|
|
subprocess.check_call(cmd)
|
|
|
|
|
2013-11-28 13:18:24 +00:00
|
|
|
@classmethod
|
|
|
|
def commandline(cls, args):
|
|
|
|
self = cls(target_dir=args.target_dir[0],
|
|
|
|
arch=args.arch)
|
2014-01-09 23:04:46 +00:00
|
|
|
|
2014-01-11 21:34:44 +00:00
|
|
|
# read initial manifest first - it may come from stdin
|
|
|
|
if args.config:
|
|
|
|
self.setup_initial_manifest(args.initial_manifest, args.replace_manifest)
|
|
|
|
|
|
|
|
# Bootstrap: creates base directory
|
2014-01-09 23:04:46 +00:00
|
|
|
if args.bootstrap:
|
|
|
|
self.bootstrap()
|
2014-01-11 21:34:44 +00:00
|
|
|
|
|
|
|
# Configure the OS
|
2014-01-09 23:04:46 +00:00
|
|
|
if args.config:
|
|
|
|
self.config()
|
2014-01-11 21:34:44 +00:00
|
|
|
|
2014-02-03 14:29:48 +00:00
|
|
|
# Cleanup chroot
|
|
|
|
self.cleanup()
|
|
|
|
|
2014-01-11 21:34:44 +00:00
|
|
|
# Output pxe files
|
|
|
|
if args.pxe_boot_dir:
|
|
|
|
self.create_pxe(args.pxe_boot_dir)
|
|
|
|
|
|
|
|
#if args.iso_boot_dir:
|
|
|
|
# self.create_iso(args.iso_boot)
|