def run(cls, info): vagrantfile_source = os.path.join(assets, 'Vagrantfile') vagrantfile = os.path.join(info.vagrant['folder'], 'Vagrantfile') shutil.copy(vagrantfile_source, vagrantfile) import random mac_address = '080027{mac:06X}'.format(mac=random.randrange(16**6)) from common.tools import sed_i sed_i(vagrantfile, '\\[MAC_ADDRESS\\]', mac_address) metadata_source = os.path.join(assets, 'metadata.json') metadata = os.path.join(info.vagrant['folder'], 'metadata.json') shutil.copy(metadata_source, metadata) from common.tools import log_check_call disk_name = 'box-disk1.{ext}'.format(ext=info.volume.extension) disk_link = os.path.join(info.vagrant['folder'], disk_name) log_check_call(['ln', '-s', info.volume.image_path, disk_link]) ovf_path = os.path.join(info.vagrant['folder'], 'box.ovf') cls.write_ovf(info, ovf_path, mac_address, disk_name) box_files = os.listdir(info.vagrant['folder']) log_check_call([ 'tar', '--create', '--gzip', '--dereference', '--file', info.vagrant['box_path'], '--directory', info.vagrant['folder'] ] + box_files) import logging logging.getLogger(__name__).info( 'The vagrant box has been placed at {box_path}'.format( box_path=info.vagrant['box_path']))
def run(cls, info): vagrantfile_source = os.path.join(assets, 'Vagrantfile') vagrantfile = os.path.join(info.vagrant['folder'], 'Vagrantfile') shutil.copy(vagrantfile_source, vagrantfile) import random mac_address = '080027{mac:06X}'.format(mac=random.randrange(16 ** 6)) from common.tools import sed_i sed_i(vagrantfile, '\\[MAC_ADDRESS\\]', mac_address) metadata_source = os.path.join(assets, 'metadata.json') metadata = os.path.join(info.vagrant['folder'], 'metadata.json') shutil.copy(metadata_source, metadata) from common.tools import log_check_call disk_name = 'box-disk1.{ext}'.format(ext=info.volume.extension) disk_link = os.path.join(info.vagrant['folder'], disk_name) log_check_call(['ln', '-s', info.volume.image_path, disk_link]) ovf_path = os.path.join(info.vagrant['folder'], 'box.ovf') cls.write_ovf(info, ovf_path, mac_address, disk_name) box_files = os.listdir(info.vagrant['folder']) log_check_call(['tar', '--create', '--gzip', '--dereference', '--file', info.vagrant['box_path'], '--directory', info.vagrant['folder']] + box_files ) import logging logging.getLogger(__name__).info('The vagrant box has been placed at {box_path}' .format(box_path=info.vagrant['box_path']))
def run(cls, info): hostname = info.manifest.plugins['vagrant']['hostname'].format(**info.manifest_vars) hostname_file_path = os.path.join(info.root, 'etc/hostname') with open(hostname_file_path, 'w') as hostname_file: hostname_file.write(hostname) hosts_path = os.path.join(info.root, 'etc/hosts') from common.tools import sed_i sed_i(hosts_path, '^127.0.0.1\tlocalhost$', '127.0.0.1\tlocalhost\n127.0.0.1\t' + hostname)
def run(cls, info): from common.tools import sed_i inittab_path = os.path.join(info.root, 'etc/inittab') tty1 = '1:2345:respawn:/sbin/getty 38400 tty1' sed_i(inittab_path, '^' + tty1, '#' + tty1) ttyx = ':23:respawn:/sbin/getty 38400 tty' for i in range(2, 7): i = str(i) sed_i(inittab_path, '^' + i + ttyx + i, '#' + i + ttyx + i)
def run(cls, info): from common.tools import sed_i cloud_cfg = os.path.join(info.root, 'etc/cloud/cloud.cfg') username = info.manifest.plugins['cloud_init']['username'] search = '^ name: debian$' replace = (' name: {username}\n' ' sudo: ALL=(ALL) NOPASSWD:ALL\n' ' shell: /bin/bash').format(username=username) sed_i(cloud_cfg, search, replace)
def run(cls, info): hostname = info.manifest.plugins["vagrant"]["hostname"].format(**info.manifest_vars) hostname_file_path = os.path.join(info.root, "etc/hostname") with open(hostname_file_path, "w") as hostname_file: hostname_file.write(hostname) hosts_path = os.path.join(info.root, "etc/hosts") from common.tools import sed_i sed_i(hosts_path, "^127.0.0.1\tlocalhost$", "127.0.0.1\tlocalhost\n127.0.0.1\t" + hostname)
def run(cls, info): hostname = info.manifest.plugins['vagrant']['hostname'].format( **info.manifest_vars) hostname_file_path = os.path.join(info.root, 'etc/hostname') with open(hostname_file_path, 'w') as hostname_file: hostname_file.write(hostname) hosts_path = os.path.join(info.root, 'etc/hosts') from common.tools import sed_i sed_i(hosts_path, '^127.0.0.1\tlocalhost$', '127.0.0.1\tlocalhost\n127.0.0.1\t' + hostname)
def run(cls, info): if 'expand-root' not in info.initd['install']: raise TaskError('The expand-root script was not installed') from base.fs.partitionmaps.none import NoPartitions if not isinstance(info.volume.partition_map, NoPartitions): import os.path from common.tools import sed_i script = os.path.join(info.root, 'etc/init.d.expand-root') root_idx = info.volume.partition_map.root.get_index() device_path = 'device_path="/dev/xvda{idx}"'.format(idx=root_idx) sed_i(script, '^device_path="/dev/xvda$', device_path)
def run(cls, info): from subprocess import CalledProcessError from common.tools import log_check_call try: log_check_call(['chroot', info.root, 'dpkg-query', '-W', 'openssh-server']) from common.tools import sed_i sshdconfig_path = os.path.join(info.root, 'etc/ssh/sshd_config') sed_i(sshdconfig_path, 'PermitRootLogin yes', 'PermitRootLogin no') except CalledProcessError: import logging logging.getLogger(__name__).warn('The OpenSSH server has not been installed, ' 'not disabling SSH root login.')
def run(cls, info): from common.tools import sed_i from common.tools import log_check_call locale_gen = os.path.join(info.root, 'etc/locale.gen') locale_str = '{locale}.{charmap} {charmap}'.format(locale=info.manifest.system['locale'], charmap=info.manifest.system['charmap']) search = '# ' + locale_str sed_i(locale_gen, search, locale_str) log_check_call(['chroot', info.root, 'locale-gen']) lang = '{locale}.{charmap}'.format(locale=info.manifest.system['locale'], charmap=info.manifest.system['charmap']) log_check_call(['chroot', info.root, 'update-locale', 'LANG=' + lang])
def run(cls, info): from common.tools import sed_i from common.tools import log_check_call locale_gen = os.path.join(info.root, 'etc/locale.gen') locale_str = '{locale}.{charmap} {charmap}'.format( locale=info.manifest.system['locale'], charmap=info.manifest.system['charmap']) search = '# ' + locale_str sed_i(locale_gen, search, locale_str) log_check_call(['chroot', info.root, 'locale-gen']) lang = '{locale}.{charmap}'.format( locale=info.manifest.system['locale'], charmap=info.manifest.system['charmap']) log_check_call(['chroot', info.root, 'update-locale', 'LANG=' + lang])
def run(cls, info): vagrantfile_source = os.path.join(assets, "Vagrantfile") vagrantfile = os.path.join(info.vagrant["folder"], "Vagrantfile") shutil.copy(vagrantfile_source, vagrantfile) import random mac_address = "080027{mac:06X}".format(mac=random.randrange(16 ** 6)) from common.tools import sed_i sed_i(vagrantfile, "\\[MAC_ADDRESS\\]", mac_address) metadata_source = os.path.join(assets, "metadata.json") metadata = os.path.join(info.vagrant["folder"], "metadata.json") shutil.copy(metadata_source, metadata) from common.tools import log_check_call disk_name = "box-disk1.{ext}".format(ext=info.volume.extension) disk_link = os.path.join(info.vagrant["folder"], disk_name) log_check_call(["ln", "-s", info.volume.image_path, disk_link]) ovf_path = os.path.join(info.vagrant["folder"], "box.ovf") cls.write_ovf(info, ovf_path, mac_address, disk_name) box_files = os.listdir(info.vagrant["folder"]) log_check_call( [ "tar", "--create", "--gzip", "--dereference", "--file", info.vagrant["box_path"], "--directory", info.vagrant["folder"], ] + box_files ) import logging logging.getLogger(__name__).info( "The vagrant box has been placed at {box_path}".format(box_path=info.vagrant["box_path"]) )
def run(cls, info): # Fix start from common.tools import sed_i vmcontext_def = os.path.join(info.root, 'etc/init.d/vmcontext') sed_i(vmcontext_def, '# Default-Start:', '# Default-Start: 2 3 4 5') from common.tools import log_check_call log_check_call(['/usr/sbin/chroot', info.root, 'update-rc.d', 'vmcontext', 'start', '90', '2', '3', '4', '5', 'stop', '90', '0', '6']) from shutil import copy # Load all pubkeys in root authorized_keys script_src = os.path.join(assets, 'one-pubkey.sh') script_dst = os.path.join(info.root, 'etc/one-context.d/one-pubkey.sh') copy(script_src, script_dst) # If USER_EC2_DATA is a script, execute it script_src = os.path.join(assets, 'one-ec2.sh') script_dst = os.path.join(info.root, 'etc/one-context.d/one-ec2.sh') copy(script_src, script_dst)
def run(cls, info): with open(os.path.join(info.root, 'etc/hostname')) as handle: hostname = handle.read().strip() with open(os.path.join(info.root, 'etc/hosts'), 'a') as handle: handle.write('127.0.0.1\t{hostname}\n'.format(hostname=hostname)) from shutil import copy pp_manifest = info.manifest.plugins['puppet']['manifest'] manifest_rel_dst = os.path.join('tmp', os.path.basename(pp_manifest)) manifest_dst = os.path.join(info.root, manifest_rel_dst) copy(pp_manifest, manifest_dst) manifest_path = os.path.join('/', manifest_rel_dst) from common.tools import log_check_call log_check_call(['chroot', info.root, 'puppet', 'apply', manifest_path]) os.remove(manifest_dst) from common.tools import sed_i hosts_path = os.path.join(info.root, 'etc/hosts') sed_i(hosts_path, '127.0.0.1\s*{hostname}\n?'.format(hostname=hostname), '')
def run(cls, info): with open(os.path.join(info.root, 'etc/hostname')) as handle: hostname = handle.read().strip() with open(os.path.join(info.root, 'etc/hosts'), 'a') as handle: handle.write('127.0.0.1\t{hostname}\n'.format(hostname=hostname)) from shutil import copy pp_manifest = info.manifest.plugins['puppet']['manifest'] manifest_rel_dst = os.path.join('tmp', os.path.basename(pp_manifest)) manifest_dst = os.path.join(info.root, manifest_rel_dst) copy(pp_manifest, manifest_dst) manifest_path = os.path.join('/', manifest_rel_dst) from common.tools import log_check_call log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/puppet', 'apply', manifest_path]) os.remove(manifest_dst) from common.tools import sed_i hosts_path = os.path.join(info.root, 'etc/hosts') sed_i(hosts_path, '127.0.0.1\s*{hostname}\n?'.format(hostname=hostname), '')
def run(cls, info): import stat rwxr_xr_x = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH) x_all = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH grubd = os.path.join(info.root, 'etc/grub.d') for cfg in [os.path.join(grubd, f) for f in os.listdir(grubd)]: os.chmod(cfg, os.stat(cfg).st_mode & ~x_all) from shutil import copy script_src = os.path.join(assets, 'grub.d/40_custom') script_dst = os.path.join(info.root, 'etc/grub.d/40_custom') copy(script_src, script_dst) os.chmod(script_dst, rwxr_xr_x) from base.fs.partitionmaps.none import NoPartitions if not isinstance(info.volume.partition_map, NoPartitions): from common.tools import sed_i root_idx = info.volume.partition_map.root.get_index() grub_device = 'GRUB_DEVICE=/dev/xvda{idx}'.format(idx=root_idx) sed_i(script_dst, '^GRUB_DEVICE=/dev/xvda$', grub_device) grub_root = '\troot (hd0,{idx})'.format(idx=root_idx - 1) sed_i(script_dst, '^\troot \(hd0\)$', grub_root) if info.manifest.volume['backing'] == 's3': from common.tools import sed_i sed_i(script_dst, '^GRUB_DEVICE=/dev/xvda$', 'GRUB_DEVICE=/dev/xvda1') from common.tools import sed_i grub_def = os.path.join(info.root, 'etc/default/grub') sed_i(grub_def, '^GRUB_TIMEOUT=[0-9]+', 'GRUB_TIMEOUT=0\n' 'GRUB_HIDDEN_TIMEOUT=true') from common.tools import log_check_call log_check_call(['chroot', info.root, 'update-grub']) log_check_call([ 'chroot', info.root, 'ln', '--symbolic', '/boot/grub/grub.cfg', '/boot/grub/menu.lst' ])
def run(cls, info): import stat rwxr_xr_x = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH) x_all = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH grubd = os.path.join(info.root, 'etc/grub.d') for cfg in [os.path.join(grubd, f) for f in os.listdir(grubd)]: os.chmod(cfg, os.stat(cfg).st_mode & ~ x_all) from shutil import copy script_src = os.path.join(assets, 'grub.d/40_custom') script_dst = os.path.join(info.root, 'etc/grub.d/40_custom') copy(script_src, script_dst) os.chmod(script_dst, rwxr_xr_x) from base.fs.partitionmaps.none import NoPartitions if not isinstance(info.volume.partition_map, NoPartitions): from common.tools import sed_i root_idx = info.volume.partition_map.root.get_index() grub_device = 'GRUB_DEVICE=/dev/xvda{idx}'.format(idx=root_idx) sed_i(script_dst, '^GRUB_DEVICE=/dev/xvda$', grub_device) grub_root = '\troot (hd0,{idx})'.format(idx=root_idx - 1) sed_i(script_dst, '^\troot \(hd0\)$', grub_root) if info.manifest.volume['backing'] == 's3': from common.tools import sed_i sed_i(script_dst, '^GRUB_DEVICE=/dev/xvda$', 'GRUB_DEVICE=/dev/xvda1') from common.tools import sed_i grub_def = os.path.join(info.root, 'etc/default/grub') sed_i(grub_def, '^GRUB_TIMEOUT=[0-9]+', 'GRUB_TIMEOUT=0\n' 'GRUB_HIDDEN_TIMEOUT=true') from common.tools import log_check_call log_check_call(['chroot', info.root, 'update-grub']) log_check_call(['chroot', info.root, 'ln', '--symbolic', '/boot/grub/grub.cfg', '/boot/grub/menu.lst'])
def run(cls, info): from common.tools import sed_i sshd_config_path = os.path.join(info.root, 'etc/ssh/sshd_config') sed_i(sshd_config_path, '^#PasswordAuthentication yes', 'PasswordAuthentication no')
def run(cls, info): from common.tools import sed_i grub_def = os.path.join(info.root, 'etc/default/grub') sed_i(grub_def, '^#GRUB_TERMINAL=console', 'GRUB_TERMINAL=console') sed_i(grub_def, '^GRUB_CMDLINE_LINUX_DEFAULT="quiet"', 'GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0"')
def run(cls, info): puppet_defaults = os.path.join(info.root, 'etc/defaults/puppet') sed_i(puppet_defaults, 'START=no', 'START=yes')
def run(cls, info): from common.tools import sed_i getcreds_path = os.path.join(info.root, 'etc/init.d/ec2-get-credentials') username = info.manifest.plugins['admin_user']['username'] sed_i(getcreds_path, 'username=\'root\'', 'username=\'{username}\''.format(username=username))
def run(cls, info): # The dhcp client that ships with debian sets the DNS servers per default. # For dhcpcd we need to configure it to do that. from common.tools import sed_i dhcpcd = os.path.join(info.root, 'etc/default/dhcpcd') sed_i(dhcpcd, '^#*SET_DNS=.*', 'SET_DNS=\'yes\'')