コード例 #1
0
ファイル: layout.py プロジェクト: jr0d/press
 def mount(self, path, device='none', bind=False, mount_type=''):
     full_path = self.join(path)
     command = 'mount %s%s%s %s' % (bind and '--bind ' or '',
                                    mount_type and '-t %s ' % mount_type
                                    or '', device, full_path)
     run(command, raise_exception=True)
     self.mount_points[path]['mounted'] = True
     log.info('Mounted %s' % full_path)
コード例 #2
0
def kexec(kernel, initrd, layout, kernel_type='bzImage', append=None):
    root_container = find_root(layout)
    log.info('Found root: %s' % root_container)
    root_uuid = root_container.file_system.fs_uuid
    append = append or []
    append.append('root=UUID=%s' % root_uuid)
    command = 'kexec --type=%s --initrd=%s --append="%s" %s' % (
        kernel_type, initrd, ' '.join(append), kernel)
    log.info('Kexec command: %s' % command)
    log.info('Goodbye cruel world')
    run(command)
コード例 #3
0
ファイル: parted.py プロジェクト: jr0d/press
 def remove_gpt(self):
     """
     512 Fake MBR
     512 GPT Header
     16KiB Primary Table
     16KiB Backup Table
     """
     gpt_bytes = 33792
     command = 'dd if=/dev/zero of=%s bs=%d count=1' % (self.device,
                                                        gpt_bytes)
     run(command)
コード例 #4
0
ファイル: fat.py プロジェクト: nloadholtes/press
    def create(self, device):
        command = self.full_command.format(command_path=self.command_path,
                                           device=device)

        log.info('Creating filesystem: {}'.format(command))
        result = run(command)
        if result.returncode:
            raise FileSystemCreateException(self.fs_label, command, result)
コード例 #5
0
 def set_time(ntp_server):
     # TODO: Make utility function
     # TODO: --utc/--local or setting the hardware clock at all should be configurable
     time_cmds = ['ntpdate %s' % ntp_server, 'hwclock --utc --systohc']
     for cmd in time_cmds:
         result = cli.run(cmd)
         if result.returncode:
             log.error('Failed to run %s: %s' % (cmd, result.returncode))
コード例 #6
0
ファイル: test_cli.py プロジェクト: nloadholtes/press
    def test_run(self):
        self.mock_process.communicate = mock.Mock()
        self.mock_process.communicate.return_value = (b'hello', b'')
        self.mock_process.returncode = 0

        r = cli.run('nocmd')

        self.assertEqual(r.stdout, 'hello')

        # Test dry run path
        r = cli.run('nocmd', dry_run=True)
        assert r.returncode == 0

        # Make sure we raise an exception when expected
        with pytest.raises(cli.CLIException):
            self.mock_process.returncode = 99
            cli.run('nocmd', raise_exception=True)
コード例 #7
0
    def create(self, device):
        command = self.command.format(**dict(command_path=self.command_path,
                                             label_option=self.label_option,
                                             device=device,
                                             uuid=self.fs_uuid))
        log.info("Creating filesystem: %s" % command)
        result = run(command)

        if result.returncode:
            raise FileSystemCreateException(self.fs_label, command, result)
コード例 #8
0
ファイル: lvm.py プロジェクト: nloadholtes/press
 def get_volume_groups():
     """
     :return: A list of volume group names
     """
     command = 'vgs --noheadings --rows -o vg_name'
     out = run(command, raise_exception=False)
     if out.returncode:
         return list()
     if not out.stdout:
         return list()
     return [vg.strip() for vg in out.splitlines()]
コード例 #9
0
    def create(self, device, quick_flag=True):
        command = self.full_command.format(
            command_path=self.command_path,
            quick_option="-f" if quick_flag else '',
            device=device)

        log.info('Creating filesystem: {}'.format(command))
        result = run(command)
        if result.returncode:
            raise FileSystemCreateException(self.fs_label, command, result)
        self.fs_uuid = self.blkid_uuid(device)
コード例 #10
0
ファイル: parted.py プロジェクト: jr0d/press
    def remove_partition(self, partition_number):
        """
        Uses run to spawn the process and looks for the return val.
        """
        command = self.parted + ' rm ' + str(partition_number)

        result = run(command)

        if result.returncode != 0:
            raise PartedException('Could not remove partition: %d' %
                                  partition_number)
コード例 #11
0
ファイル: lvm.py プロジェクト: nloadholtes/press
 def __execute(command, ignore_errors=False, quiet=False):
     """
     Execute a shell command using subprocess, then validate the return
     code and log either stdout or stderror to the logger.
     """
     log.debug('Running: %s' % command)
     res = run(command, ignore_error=ignore_errors, quiet=quiet)
     if res.returncode and not ignore_errors:
         log.error('stdout: %s, stderr: %s' % (res, res.stderr))
         raise LVMError('LVM command returned status {}: {}, {}'.format(
             res.returncode, res, res.stderr))
     return res
コード例 #12
0
ファイル: lvm.py プロジェクト: nloadholtes/press
    def get_physical_volumes():
        """

        :return: A list of physical volume names
        """
        command = 'pvs --noheadings --rows -o pv_name'
        out = run(command, raise_exception=False)
        if out.returncode:
            return list()
        if not out.stdout:
            return list()
        return [pv.strip() for pv in out.splitlines()]
コード例 #13
0
ファイル: xfs.py プロジェクト: jr0d/press
 def create(self, device):
     command = self.full_command.format(
         **dict(command_path=self.command_path,
                uuid=self.fs_uuid,
                label_options=self.label_options,
                inode_options=self.inode_options,
                naming_options=self.naming_options,
                global_metadata_options=self.global_metadata_options,
                device=device))
     log.info("Creating filesystem: %s" % command)
     result = run(command)
     if result.returncode:
         raise FileSystemCreateException(self.fs_label, command, result)
コード例 #14
0
ファイル: extended.py プロジェクト: nloadholtes/press
    def create(self, device):
        command = self.full_command.format(
            **dict(command_path=self.command_path,
                   superuser_reserve=self.superuser_reserve,
                   feature_options=self.feature_options,
                   extended_options=self.extended_options,
                   label_options=self.label_options,
                   device=device,
                   uuid=self.fs_uuid))
        log.info("Creating filesystem: {}".format(command))
        result = run(command)

        if result.returncode:
            raise FileSystemCreateException(self.fs_label, command, result)
コード例 #15
0
ファイル: mdadm.py プロジェクト: nloadholtes/press
 def run_mdadm(self,
               command,
               raise_on_error=True,
               ignore_error=False,
               quiet=False):
     full_command = '%s %s' % (self.mdadm, command)
     result = run(full_command,
                  ignore_error=ignore_error,
                  quiet=quiet,
                  _input='yes')
     if result and raise_on_error:
         raise MDADMError('%d: %s : %s' %
                          (result.returncode, result.stdout, result.stderr))
     return result
コード例 #16
0
ファイル: layout.py プロジェクト: nloadholtes/press
    def apply(self):
        """Lots of logging here
        """

        # TODO: now that we have some clean up operations, determine if we still need to do this
        log.info('Clearing the device mapper')
        run('dmsetup remove_all')

        # Destroy any volume groups present at boot time
        self.destroy_volume_groups()
        self.clean_software_raid()

        self.apply_standard_partitions()

        # Now that we've built a partition table, destroy any resident data
        self.destroy_volume_groups()
        self.destroy_physical_volumes()

        # Now re-apply from scratch
        self.apply_software_raid()
        self.apply_lvm()

        # we've been sent to the loony bin
        self.committed = True
コード例 #17
0
ファイル: parted.py プロジェクト: jr0d/press
 def run_parted(self,
                command,
                raise_on_error=True,
                ignore_error=False,
                quiet=False):
     """
     parted does not use meaningful return codes. It pretty much returns 1 on
     any error and then prints an error message on to standard error stream.
     """
     result = run(self.parted + command,
                  ignore_error=ignore_error,
                  quiet=quiet)
     if result and raise_on_error:
         raise PartedException(result.stderr)
     return result
コード例 #18
0
    print('Can do validation..')
    if dl.validate():
        print('Checksums match up!')
    else:
        print('Checksums do NOT MATCH!')
else:
    print("Validation can't be performed")

log.info('Extracting image.')

new_root = '/mnt/press'

dl.extract(new_root)

# Nessy adding some POST action
run('cp /etc/resolv.conf %s/etc/resolv.conf' % new_root)
run('mv %s/etc/fstab_rs %s/etc/fstab' % (new_root, new_root))

# A example config
config = {
    'auth': {
        'algorythim': 'sha512',
        'users': {
            'rack': {
                'gid': 1000,
                'group': 'rack',
                'home': '/home/rack',
                'password': '******',
                'password_options': [{
                    'encrypted': False
                }],
コード例 #19
0
ファイル: yaml_test.py プロジェクト: nloadholtes/press
                               expected_hash,
                               chunk_size=1024 * 1024)
dl.download(my_callback)

if dl.can_validate():
    print('Can do validation..')
    if dl.validate():
        print('Checksums match up!')
    else:
        print('Checksums do NOT MATCH!')
else:
    print("Validation can't be performed")

log.info('Extracting image.')

new_root = '/mnt/press'

dl.extract(new_root)

# Nessy adding some POST action
run('mv %s/etc/fstab_rs %s/etc/fstab' % (new_root, new_root))

network = Network(new_root, config)
network.apply()

chroot = DebianChroot(new_root, config, l1.disks)
chroot.apply()
chroot.__exit__()

log.info('Done!')
コード例 #20
0
ファイル: mdadm.py プロジェクト: nloadholtes/press
 def zero_4k(device):
     log.info('Removing 4K from the beginning of the device: 1.2 metadata')
     command = 'dd if=/dev/zero of=%s bs=%d count=10' % (device, 4096)
     run(command)
コード例 #21
0
ファイル: parted.py プロジェクト: jr0d/press
 def set_label(self, label='gpt'):
     result = run(self.parted + ' mklabel ' + label)
     if result.returncode != 0:
         raise PartedException('Could not create filesystem label')
コード例 #22
0
ファイル: parted.py プロジェクト: jr0d/press
 def remove_mbr(self):
     mbr_bytes = 512
     command = 'dd if=/dev/zero of=%s bs=%d count=1' % (self.device,
                                                        mbr_bytes)
     run(command)
コード例 #23
0
ファイル: layout.py プロジェクト: jr0d/press
 def clear_device_mapper(self):
     log.info('Clearing the device mapper')
     run('dmsetup remove_all')
コード例 #24
0
ファイル: layout.py プロジェクト: jr0d/press
 def umount(self, path):
     full_path = self.join(path)
     command = 'umount %s' % full_path
     run(command, raise_exception=True)
     self.mount_points[path]['mounted'] = True
     log.info('Unmounted %s' % full_path)
コード例 #25
0
 def blkid_uuid(device):
     return run("blkid -s UUID -o value {}".format(device)).stdout.strip()
コード例 #26
0
 def get_product_name(self):
     res = cli.run('dmidecode -s system-product-name', raise_exception=True)
     for line in res.splitlines():
         if line.lstrip().startswith('#'):
             continue
         return line.strip()
コード例 #27
0
ファイル: post_test.py プロジェクト: nloadholtes/press
import logging

from press.logger import setup_logging
from press.chroot.debian import DebianPost
from press.helpers.cli import run

setup_logging()

log = logging.getLogger(__name__)

new_root = '/mnt/press'
run('cp /etc/resolv.conf %s/etc/resolv.conf' % new_root)

post = DebianPost(new_root)
post.useradd('rack')
post.passwd('rack', 'password')

# Install Kernel
post.install_packages(['linux-generic'])

post.grub_install('/dev/loop')

# After we complete lets delete the Post to call __exit__ function.
post.__exit__()