def appenv(appname, flavor='default', upgrade=False, ignore_missing_prerequisites=False): ''' install the environment for an given application usage: appname, flavor="default", upgrade=False, ignore_missing_prerequisites=False ''' upgrade = strbool(upgrade) ignore_missing_prerequisites = strbool(ignore_missing_prerequisites) package_bundle = _get_package_bundle(appname, flavor) install_packages(package_bundle, upgrade, ignore_missing_prerequisites)
def appreq(appname, flavor='default', only_list=False, use_sudo=True): ''' install the prerequisites of an environment for an given application usage: appname, flavor="default", only_list=False, use_sudo=True ''' only_list = strbool(only_list) use_sudo = strbool(use_sudo) package_bundle = _get_package_bundle(appname, flavor) install_prerequisites(package_bundle, only_list, use_sudo)
def install_prerequisites(packages, only_list=False, use_sudo=True): ''' Install the prerequisites of a package list; Usage: install_prerequisites:packages,only_list=False,use_sudo=True ''' only_list = strbool(only_list) use_sudo = strbool(use_sudo) if pkg_manager is None: abort("No pkt manager found for your system, you're on your own") if only_list: pkg_manager.check_prerequisites(packages, warn_only=only_list) else: pkg_manager.install_prerequisites(packages, use_sudo=use_sudo)
def appsys(appname, use_sudo=True, dry=False, hostname=platform.node(), ip='127.0.0.2'): ''' do the system setup for a given app ''' use_sudo = strbool(use_sudo) dry = strbool(dry) appconfig = _get_appconfig(appname) try: system_setup = appconfig.system_setup except AttributeError: pass else: system_setup(appname, use_sudo=use_sudo, dry=dry, hostname=hostname, ip=ip)
def install_packages(packages, upgrade=False, ignore_missing_prerequisites=False): ''' Install a packagelist localy; Usage: install_packages:packages,upgrade=False,ignore_missing_prerequisites=False # WARNING: pypi version using < or > needs a backslash prepended ! ''' upgrade = strbool(upgrade) ignore_missing_prerequisites = strbool(ignore_missing_prerequisites) if pkg_manager is None: warn("No pkt manager found for your system, you're on your own") else: pkg_manager.check_prerequisites(packages, warn_only=ignore_missing_prerequisites) pkg_manager.install_packages(packages)
def getshell(running=False, snapshot=False, hypervisor=VMBUILDER_HYPERVISOR): running = strbool(running) snapshot = strbool(snapshot) dirname = _targetdir() ssh = ['ssh', 'localhost', '-o', 'NoHostAuthenticationForLocalhost=yes', '-i', os.path.join(dirname, VMHOSTKEY), '-p', '42022', '-l', VM_USER, ] if not running: with RunningVM(hypervisor, snapshot=snapshot) as vm: os.system('reset && %s' % subprocess.list2cmdline(ssh)) else: os.system('reset && %s' % subprocess.list2cmdline(ssh))
def fridge_wipe(force_wipe=False): ''' wipes out fridge_store ''' fdir = os.path.abspath(_fridgedir()) force_wipe = strbool(force_wipe) if not os.path.exists(fdir): print "No fridge to wipe" return question = "Do you really want to delete the fridge under %s ?" % fdir if force_wipe or console.confirm(question, default=False): print "Deleting %s" % fdir shutil.rmtree(fdir)
def install_conf(conffile, upgrade=False, ignore_missing_prerequisites=False, only_postconfig=False): ''' install a legacy .conf file ''' def install_postconfig(postconfigdata): ''' run a postconfig string, internal ''' postconfig= 'PYTHONENV=%s; PYTHONVER=%s; FABDIR=%s; '+postconfigdata pythonver = '%d.%d' % sys.version_info[:2] postconfig= postconfig % (get_pythonenv(), pythonver, fabdir()) result= local(postconfig, capture=False) _abort_failed(result) upgrade = strbool(upgrade) ignore_missing_prerequisites = strbool(ignore_missing_prerequisites) only_postconfig = strbool(only_postconfig) with open(conffile) as f: data= f.read() depstart= re.search('#DEPENDENCIES-BEGIN', data, re.MULTILINE) depend = re.search('#DEPENDENCIES-END', data, re.MULTILINE) if (depstart is None) or (depend is None): abort("Can not find dependencies begin or end (#DEPENDENCIES-BEGIN/#DEPENDENCIES-END) in conffile") packagelist= data[depstart.end():depend.start()] configstart= re.search('#POSTCONFIG-BEGIN', data, re.MULTILINE) configend = re.search('#POSTCONFIG-END', data, re.MULTILINE) if (configstart is not None) and (configend is not None): warn("Found postconfig") postconfig= data[configstart.end():configend.start()] else: postconfig= "" if not only_postconfig: install_packages(packagelist, upgrade, ignore_missing_prerequisites) if postconfig: install_postconfig(postconfig)
def createvm(application='ecs', use_sudo=True, hypervisor=VMBUILDER_HYPERVISOR, revision=None, hostname=None, ip=None, onlychrootcreate=False, useexistingchroot=True): VMBUILDER_CONFIG = 'vmbuilder.cfg' POSTINSTALL_FILE = 'postinstall.sh' SSLEAY_CONFIG = 'ssleay.cnf' APACHE_CONFIG_TARBALL = 'apache2.tar.bz2' use_sudo = strbool(use_sudo) onlychrootcreate = strbool(onlychrootcreate) useexistingchroot = strbool(useexistingchroot) sourcedirname = _sourcedir() targetdirname = _targetdir() freesize = os.statvfs(tempfile.gettempdir()).f_bfree * os.statvfs(tempfile.gettempdir()).f_bsize if freesize < (2 ** 32): abort("sorry, less than 4 Gigabyte of freespace in temp directory %s" % tempfile.gettempdir()) if onlychrootcreate and useexistingchroot: abbort('only one of onlychrootcreate or useexistingchroot can be set to true') if not revision: revision = _get_current_revision() if not hostname: hostname = raw_input('hostname[example.com] ') if not hostname: hostname = 'example.com' if not ip: ip = raw_input('ip[127.0.0.2] ') if not ip: ip = '127.0.0.2' # create ssh key config private_key = os.path.join(targetdirname, VMHOSTKEY) public_key = os.path.join(targetdirname, VMHOSTKEY+".pub") if os.path.exists(private_key): os.remove(private_key) if os.path.exists(public_key): os.remove(public_key) ssh_keygen = ['ssh-keygen', '-t', 'rsa', '-b', '4096', '-f', VMHOSTKEY, '-N', '', '-C', 'autovm@vmhost', ] _run(ssh_keygen, cwd=targetdirname) # create postinstall config postinstall_sh = os.path.join(targetdirname, POSTINSTALL_FILE) postinstall_sh_template = os.path.join(sourcedirname, 'templates', POSTINSTALL_FILE) write_template(postinstall_sh_template, postinstall_sh, { 'applications': ' '.join(APPLICATIONS), 'vm_user': VM_USER, 'revision': revision, 'hostname': ip, 'ip': ip, 'apacheconfigtarball': APACHE_CONFIG_TARBALL, 'ssleayconfig': SSLEAY_CONFIG, }) os.chmod(postinstall_sh, stat.S_IRWXU|stat.S_IRGRP|stat.S_IXGRP|stat.S_IROTH|stat.S_IXOTH) # chmod 0755 # create ssleay config ssleay_cnf = os.path.join(targetdirname, SSLEAY_CONFIG) ssleay_cnf_template = os.path.join(sourcedirname, 'templates', SSLEAY_CONFIG) write_template(ssleay_cnf_template, ssleay_cnf, { 'hostname': hostname, }) # create vmbuilder config vmbuilder_cfg = os.path.join(targetdirname, VMBUILDER_CONFIG) vmbuilder_cfg_template = os.path.join(sourcedirname, 'templates', VMBUILDER_CONFIG) write_template(vmbuilder_cfg_template, vmbuilder_cfg, { 'dirname': targetdirname, 'hostname': hostname, 'vmhostkeypub': VMHOSTKEY+ ".pub", 'postinstallfile': POSTINSTALL_FILE, }) # create vmbuilder partition config partitions = "root 10000\nswap 1000\n---\n/opt 20000\n" with open(os.path.join(targetdirname, VMBUILDER_CONFIG+ ".partition"), "wb") as f: f.write(partitions) # copy apache config shutil.copyfile(os.path.join(sourcedirname, APACHE_CONFIG_TARBALL), os.path.join(targetdirname, APACHE_CONFIG_TARBALL)) # create actual source tarball make_tarball(targetdirname, revision=revision) # call the vmbuilder, cross fingers vmbuilder = ['sudo'] if use_sudo else [] vmbuilder += ['vmbuilder', hypervisor, VMBUILDER_OS, '--config', os.path.join(targetdirname, VMBUILDER_CONFIG), '--part', os.path.join(targetdirname, VMBUILDER_CONFIG+ ".partition"), ] if onlychrootcreate: warn('creating chroot, but dont go futher') vmbuilder += ['--only-chroot', '--destdir='+ os.path.join(targetdirname, CHROOT_TARGET),] else: vmbuilder += ['--destdir='+ os.path.join(targetdirname, VMBUILDER_OS+ "-"+ hypervisor),] if useexistingchroot and os.path.exists(os.path.join(targetdirname, CHROOT_TARGET)): warn('using prebuild chroot under %s' % os.path.join(targetdirname, CHROOT_TARGET)) vmbuilder += ['--existing-chroot='+ os.path.join(targetdirname, CHROOT_TARGET),] warn('recopy ssh authorized_keys to chroot') shutil.copy(os.path.join(targetdirname, VMHOSTKEY+".pub"), os.path.join(targetdirname, CHROOT_TARGET, 'home', VM_USER, '.ssh', 'authorized_keys')) _run(vmbuilder, cwd=targetdirname)