def _install_from_buildout(self): """ Fetches buildout configuration from the server, run buildout with it. If it fails, we notify the server. """ root_stat = os.stat(self.software_root) os.environ = getCleanEnvironment(logger=self.logger, home_path=pwd.getpwuid(root_stat.st_uid).pw_dir) if not os.path.isdir(self.software_path): os.mkdir(self.software_path) self._set_ownership(self.software_path) extends_cache = tempfile.mkdtemp() self._set_ownership(extends_cache) try: buildout_cfg = os.path.join(self.software_path, 'buildout.cfg') if not os.path.exists(buildout_cfg): self._create_buildout_profile(buildout_cfg, self.url) additional_parameters = list(self._additional_buildout_parameters(extends_cache)) additional_parameters.extend(['-c', buildout_cfg]) utils.bootstrapBuildout(path=self.software_path, buildout=self.buildout, logger=self.logger, additional_buildout_parameter_list=additional_parameters) utils.launchBuildout(path=self.software_path, buildout_binary=os.path.join(self.software_path, 'bin', 'buildout'), logger=self.logger, additional_buildout_parameter_list=additional_parameters) finally: shutil.rmtree(extends_cache)
def destroy(self): """Destroys the partition and makes it available for subsequent use." """ self.logger.info("Destroying Computer Partition %s..." % self.computer_partition.getId()) # Launches "destroy" binary if exists destroy_executable_location = os.path.join(self.instance_path, 'sbin', 'destroy') if os.path.exists(destroy_executable_location): uid, gid = self.getUserGroupId() self.logger.debug('Invoking %r' % destroy_executable_location) process_handler = SlapPopen([destroy_executable_location], preexec_fn=lambda: dropPrivileges(uid, gid, logger=self.logger), cwd=self.instance_path, env=getCleanEnvironment(logger=self.logger, home_path=pwd.getpwuid(uid).pw_dir), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, logger=self.logger) if process_handler.returncode is None or process_handler.returncode != 0: message = 'Failed to destroy Computer Partition in %r.' % \ self.instance_path self.logger.error(message) raise subprocess.CalledProcessError(message, process_handler.output) # Manually cleans what remains try: for f in [self.key_file, self.cert_file]: if f: if os.path.exists(f): os.unlink(f) # better to manually remove symlinks because rmtree might choke on them sr_symlink = os.path.join(self.instance_path, 'software_release') if os.path.islink(sr_symlink): os.unlink(sr_symlink) for root, dirs, file_list in os.walk(self.instance_path): for directory in dirs: shutil.rmtree(os.path.join(self.instance_path, directory)) for file in file_list: os.remove(os.path.join(self.instance_path, file)) if os.path.exists(self.supervisord_partition_configuration_path): os.remove(self.supervisord_partition_configuration_path) self.updateSupervisor() except IOError as exc: raise IOError("I/O error while freeing partition (%s): %s" % (self.instance_path, exc))
def destroy(self): """Destroys the partition and makes it available for subsequent use." """ self.logger.info("Destroying Computer Partition %s..." % self.computer_partition.getId()) self.createRetentionLockDate() if not self.checkRetentionIsAuthorized(): return False # Launches "destroy" binary if exists destroy_executable_location = os.path.join(self.instance_path, 'sbin', 'destroy') if os.path.exists(destroy_executable_location): uid, gid = self.getUserGroupId() self.logger.debug('Invoking %r' % destroy_executable_location) process_handler = SlapPopen([destroy_executable_location], preexec_fn=lambda: dropPrivileges(uid, gid, logger=self.logger), cwd=self.instance_path, env=getCleanEnvironment(logger=self.logger, home_path=pwd.getpwuid(uid).pw_dir), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, logger=self.logger) if process_handler.returncode is None or process_handler.returncode != 0: message = 'Failed to destroy Computer Partition in %r.' % \ self.instance_path self.logger.error(message) raise subprocess.CalledProcessError(message, process_handler.output) # Manually cleans what remains try: for f in [self.key_file, self.cert_file]: if f: if os.path.exists(f): os.unlink(f) # better to manually remove symlinks because rmtree might choke on them sr_symlink = os.path.join(self.instance_path, 'software_release') if os.path.islink(sr_symlink): os.unlink(sr_symlink) data_base_link = os.path.join(self.instance_path, CP_STORAGE_FOLDER_NAME) if self.instance_storage_home and os.path.exists(data_base_link) and \ os.path.isdir(data_base_link): for filename in os.listdir(data_base_link): data_symlink = os.path.join(data_base_link, filename) partition_data_path = os.path.join(self.instance_storage_home, filename, self.partition_id) if os.path.lexists(data_symlink): os.unlink(data_symlink) if os.path.exists(partition_data_path): self.cleanupFolder(partition_data_path) self.cleanupFolder(self.instance_path) # Cleanup all Data storage location of this partition if os.path.exists(self.supervisord_partition_configuration_path): os.remove(self.supervisord_partition_configuration_path) self.updateSupervisor() except IOError as exc: raise IOError("I/O error while freeing partition (%s): %s" % (self.instance_path, exc)) return True
def install(self): """ Creates configuration file from template in software_path, then installs the software partition with the help of buildout """ self.logger.info("Installing Computer Partition %s..." % self.computer_partition.getId()) self.check_free_space() # Checks existence and permissions of Partition directory # Note : Partitions have to be created and configured before running slapgrid if not os.path.isdir(self.instance_path): raise PathDoesNotExistError('Please create partition directory %s' % self.instance_path) sr_symlink = os.path.join(self.instance_path, 'software_release') self.updateSymlink(sr_symlink, self.software_path) instance_stat_info = os.stat(self.instance_path) permission = stat.S_IMODE(instance_stat_info.st_mode) if permission != REQUIRED_COMPUTER_PARTITION_PERMISSION: raise WrongPermissionError('Wrong permissions in %s: actual ' 'permissions are: 0%o, wanted are 0%o' % (self.instance_path, permission, REQUIRED_COMPUTER_PARTITION_PERMISSION)) os.environ = getCleanEnvironment(logger=self.logger, home_path=pwd.getpwuid(instance_stat_info.st_uid).pw_dir) # Check that Software Release directory is present if not os.path.exists(self.software_path): # XXX What should it raise? raise IOError('Software Release %s is not present on system.\n' 'Cannot deploy instance.' % self.software_release_url) # Generate buildout instance profile from template in Software Release template_location = os.path.join(self.software_path, 'instance.cfg') if not os.path.exists(template_location): # Backward compatibility: "instance.cfg" file was named "template.cfg". if os.path.exists(os.path.join(self.software_path, 'template.cfg')): template_location = os.path.join(self.software_path, 'template.cfg') else: # No template: Software Release is either inconsistent or not correctly installed. # XXX What should it raise? raise IOError('Software Release %s is not correctly installed.\nMissing file: %s' % ( self.software_release_url, template_location)) config_location = os.path.join(self.instance_path, 'buildout.cfg') self.logger.debug("Copying %r to %r" % (template_location, config_location)) shutil.copy(template_location, config_location) # fill generated buildout with additional information buildout_text = open(config_location).read() buildout_text += '\n\n' + pkg_resources.resource_string(__name__, 'templates/buildout-tail.cfg.in') % { 'computer_id': self.computer_id, 'partition_id': self.partition_id, 'server_url': self.server_url, 'software_release_url': self.software_release_url, 'key_file': self.key_file, 'cert_file': self.cert_file, 'storage_home': self.instance_storage_home, 'global_ipv4_network_prefix': self.ipv4_global_network, } open(config_location, 'w').write(buildout_text) os.chmod(config_location, 0o640) # Try to find the best possible buildout: # *) if software_root/bin/bootstrap exists use this one to bootstrap # locally # *) as last resort fallback to buildout binary from software_path bootstrap_candidate_dir = os.path.abspath(os.path.join(self.software_path, 'bin')) if os.path.isdir(bootstrap_candidate_dir): bootstrap_candidate_list = [q for q in os.listdir(bootstrap_candidate_dir) if q.startswith('bootstrap')] else: bootstrap_candidate_list = [] uid, gid = self.getUserGroupId() os.chown(config_location, -1, int(gid)) if len(bootstrap_candidate_list) == 0: buildout_binary = os.path.join(self.software_path, 'bin', 'buildout') self.logger.info("Falling back to default buildout %r" % buildout_binary) else: if len(bootstrap_candidate_list) != 1: raise ValueError('More than one bootstrap candidate found.') # Reads uid/gid of path, launches buildout with thoses privileges bootstrap_file = os.path.abspath(os.path.join(bootstrap_candidate_dir, bootstrap_candidate_list[0])) first_line = open(bootstrap_file, 'r').readline() invocation_list = [] if first_line.startswith('#!'): invocation_list = first_line[2:].split() invocation_list.append(bootstrap_file) self.logger.debug('Invoking %r in %r' % (' '.join(invocation_list), self.instance_path)) process_handler = SlapPopen(invocation_list, preexec_fn=lambda: dropPrivileges(uid, gid, logger=self.logger), cwd=self.instance_path, env=getCleanEnvironment(logger=self.logger, home_path=pwd.getpwuid(uid).pw_dir), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, logger=self.logger) if process_handler.returncode is None or process_handler.returncode != 0: message = 'Failed to bootstrap buildout in %r.' % (self.instance_path) self.logger.error(message) raise BuildoutFailedError('%s:\n%s\n' % (message, process_handler.output)) buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout') if not os.path.exists(buildout_binary): # use own buildout generation utils.bootstrapBuildout(path=self.instance_path, buildout=self.buildout, logger=self.logger, additional_buildout_parameter_list= ['buildout:bin-directory=%s' % os.path.join(self.instance_path, 'sbin')]) buildout_binary = os.path.join(self.instance_path, 'sbin', 'buildout') # Launches buildout utils.launchBuildout(path=self.instance_path, buildout_binary=buildout_binary, logger=self.logger) self.generateSupervisorConfigurationFile() self.createRetentionLockDelay()