def wsgi_config(self): write_template(os.path.join(self.dirname, "templates", "apache2", self.appname, "apache.wsgi", "ecs-main.wsgi"), os.path.join(self.dirname, "main.wsgi"), {'appdir': os.path.join(self.dirname), 'appname': self.appname,} ) write_template(os.path.join(self.dirname, "templates", "apache2", self.appname, "apache.wsgi", "ecs-service.wsgi"), os.path.join(self.dirname, "service.wsgi"), {'appdir': os.path.join(self.dirname), 'appname': self.appname,} )
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)