forked from ungleich-public/cdist
		
	preos: seperate parameters, create remote_exec, remote_copy and manifest on the fly
Signed-off-by: Nico Schottelius <nico@bento.schottelius.org>
This commit is contained in:
		
					parent
					
						
							
								e461961efd
							
						
					
				
			
			
				commit
				
					
						8af1add2a6
					
				
			
		
					 3 changed files with 120 additions and 7 deletions
				
			
		| 
						 | 
					@ -22,14 +22,24 @@
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
 | 
					import stat
 | 
				
			||||||
 | 
					import tempfile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# initialise cdist
 | 
					 | 
				
			||||||
import cdist.exec.local
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import cdist.config
 | 
					import cdist.config
 | 
				
			||||||
 | 
					import cdist.exec.local
 | 
				
			||||||
 | 
					import cdist.exec.remote
 | 
				
			||||||
 | 
					
 | 
				
			||||||
log = logging.getLogger(__name__)
 | 
					log = logging.getLogger(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class PreOSExistsError(cdist.Error):
 | 
				
			||||||
 | 
					    def __init__(self, path):
 | 
				
			||||||
 | 
					        self.path = path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __str__(self):
 | 
				
			||||||
 | 
					        return 'Path %s already exists' % self.path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PreOS(object):
 | 
					class PreOS(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, target_dir, arch="amd64"):
 | 
					    def __init__(self, target_dir, arch="amd64"):
 | 
				
			||||||
| 
						 | 
					@ -42,7 +52,53 @@ class PreOS(object):
 | 
				
			||||||
        self.options = [ "--include=openssh-server",
 | 
					        self.options = [ "--include=openssh-server",
 | 
				
			||||||
            "--arch=%s" % self.arch ]
 | 
					            "--arch=%s" % self.arch ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self._init_helper()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _init_helper(self):
 | 
				
			||||||
 | 
					        self.helper = {}
 | 
				
			||||||
 | 
					        self.helper["manifest"]  = """
 | 
				
			||||||
 | 
					for pkg in linux-image-amd64 openssh-server; do
 | 
				
			||||||
 | 
					    __package $pkg --state present
 | 
				
			||||||
 | 
					done
 | 
				
			||||||
 | 
					"""
 | 
				
			||||||
 | 
					        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}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# run in chroot
 | 
				
			||||||
 | 
					chroot "$chroot" "$relative_script"
 | 
				
			||||||
 | 
					"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.helper["remote_copy"]  = """#!/bin/sh
 | 
				
			||||||
 | 
					        echo $@
 | 
				
			||||||
 | 
					        set -x
 | 
				
			||||||
 | 
					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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def bootstrap(self):
 | 
					    def bootstrap(self):
 | 
				
			||||||
 | 
					        if self.exists:
 | 
				
			||||||
 | 
					            raise PreOSExistsError(self.target_dir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        cmd = [ self.command ]
 | 
					        cmd = [ self.command ]
 | 
				
			||||||
        cmd.extend(self.options)
 | 
					        cmd.extend(self.options)
 | 
				
			||||||
        cmd.append(self.suite)
 | 
					        cmd.append(self.suite)
 | 
				
			||||||
| 
						 | 
					@ -52,12 +108,40 @@ class PreOS(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        subprocess.call(cmd)
 | 
					        subprocess.call(cmd)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def run(self):
 | 
					    def create_helper_files(self, base_dir):
 | 
				
			||||||
        self.bootstrap()
 | 
					        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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def config(self):
 | 
				
			||||||
 | 
					        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()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def commandline(cls, args):
 | 
					    def commandline(cls, args):
 | 
				
			||||||
        print(args)
 | 
					 | 
				
			||||||
        self = cls(target_dir=args.target_dir[0],
 | 
					        self = cls(target_dir=args.target_dir[0],
 | 
				
			||||||
            arch=args.arch)
 | 
					            arch=args.arch)
 | 
				
			||||||
        self.run()
 | 
					
 | 
				
			||||||
 | 
					        if args.bootstrap:
 | 
				
			||||||
 | 
					            self.bootstrap()
 | 
				
			||||||
 | 
					        if args.config:
 | 
				
			||||||
 | 
					            self.config()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										20
									
								
								docs/dev/logs/2014-01-09.preos
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								docs/dev/logs/2014-01-09.preos
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,20 @@
 | 
				
			||||||
 | 
					- debootstrap
 | 
				
			||||||
 | 
					    x setup arch
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    - include trigger
 | 
				
			||||||
 | 
					        - replace with cdist config later?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    - get kernel
 | 
				
			||||||
 | 
					    - create initramfs
 | 
				
			||||||
 | 
					    - later:
 | 
				
			||||||
 | 
					        - configure chroot using cdist
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    packages:
 | 
				
			||||||
 | 
					        linux-image-amd64
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    - temporary manifest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    - bugs with sudo
 | 
				
			||||||
 | 
					        [22:50:04] bento:~# ln -s ~nico/.cdist/ ~
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -89,6 +89,15 @@ def commandline():
 | 
				
			||||||
        parents=[parser['loglevel']])
 | 
					        parents=[parser['loglevel']])
 | 
				
			||||||
    parser['preos'].add_argument('-a', '--arch',
 | 
					    parser['preos'].add_argument('-a', '--arch',
 | 
				
			||||||
         help='Select architecture for preos', default="amd64")
 | 
					         help='Select architecture for preos', default="amd64")
 | 
				
			||||||
 | 
					    parser['preos'].add_argument('-b', '--bootstrap',
 | 
				
			||||||
 | 
					         help='Bootstrap directory with OS',  action="store_true")
 | 
				
			||||||
 | 
					    parser['preos'].add_argument('-c', '--configure',
 | 
				
			||||||
 | 
					         help='Configure previously bootstrapped directory', action="store_true",
 | 
				
			||||||
 | 
					         dest="config")
 | 
				
			||||||
 | 
					    parser['preos'].add_argument('-i', '--initramfs',
 | 
				
			||||||
 | 
					         help='Create Linux initramfs',  action="store_true")
 | 
				
			||||||
 | 
					    parser['preos'].add_argument('-k', '--kernel',
 | 
				
			||||||
 | 
					         help='Create Linux kernel',  action="store_true")
 | 
				
			||||||
    parser['preos'].add_argument('target_dir', nargs=1,
 | 
					    parser['preos'].add_argument('target_dir', nargs=1,
 | 
				
			||||||
        help='Select target directory')
 | 
					        help='Select target directory')
 | 
				
			||||||
    parser['preos'].set_defaults(func=cdist.preos.PreOS.commandline)
 | 
					    parser['preos'].set_defaults(func=cdist.preos.PreOS.commandline)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue