def test_create_temp_dir_ok(self): """ Check if create_temp_dir behaves correctly when no exception is raised from inside it. By behave correctly we mean: creates and returns a tempdir, which is deleted after we exit the context manager. """ with ansible.create_temp_dir() as temp_dir: self.assertTrue(os.path.exists(temp_dir)) self.assertTrue(os.path.isdir(temp_dir)) self.assertFalse(os.path.exists(temp_dir))
def test_create_temp_dir_exception(self): """ Check if create_temp_dir behaves correctly when an exception is raised from inside it. By behave correctly we mean: creates and returns a tempdir, which is deleted after we exit the context manager. """ saved_temp_dir = None with self.assertRaises(KeyboardInterrupt): with ansible.create_temp_dir() as temp_dir: saved_temp_dir = temp_dir self.assertTrue(os.path.exists(temp_dir)) self.assertTrue(os.path.isdir(temp_dir)) raise KeyboardInterrupt() self.assertIsNotNone(saved_temp_dir) self.assertFalse(os.path.exists(saved_temp_dir))
def activity_csv(self, out): """Generate the activity CSV.""" active_appservers = self.get_active_appservers() if not active_appservers: self.stderr.write( self.style.SUCCESS('There are no active app servers! Nothing to do.') ) sys.exit(0) self.stderr.write(self.style.SUCCESS('Running playbook...')) with ansible.create_temp_dir() as playbook_output_dir: inventory = '[apps]\n{servers}'.format(servers='\n'.join(active_appservers.keys())) playbook_path = os.path.join(settings.SITE_ROOT, 'playbooks/collect_activity/collect_activity.yml') def log_line(line): """Helper to pass to capture_playbook_output().""" self.stderr.write(self.style.SUCCESS(line)) log_line.info = log_line log_line.error = log_line # Launch the collect_activity playbook, which places a set of files into the `playbook_output_dir` # on this host. ansible.capture_playbook_output( requirements_path=os.path.join(os.path.dirname(playbook_path), 'requirements.txt'), inventory_str=inventory, vars_str=( 'local_output_dir: {output_dir}\n' 'remote_output_filename: /tmp/activity_report' ).format(output_dir=playbook_output_dir), playbook_path=playbook_path, username=settings.OPENSTACK_SANDBOX_SSH_USERNAME, logger_=log_line, ) csv_writer = csv.writer(out, quoting=csv.QUOTE_NONNUMERIC) csv_writer.writerow([ 'Appserver IP', 'Internal LMS Domain', 'Name', 'Contact Email', 'Unique Hits', 'Total Users', 'Total Courses', 'Age (Days)' ]) filenames = [os.path.join(playbook_output_dir, f) for f in os.listdir(playbook_output_dir)] data = ConfigParser() data.read(filenames) for public_ip, instance in sorted(active_appservers.items(), key=lambda tup: tup[1].id): try: section = data[public_ip] except KeyError: # Fill in stats for any instaces that failed with "N/A" section = {'hits': 'N/A', 'users': 'N/A', 'courses': 'N/A'} instance_age = datetime.now(instance.created.tzinfo) - instance.created try: email = instance.betatestapplication_set.get().user.email except BetaTestApplication.DoesNotExist: email = 'N/A' csv_writer.writerow([ public_ip, instance.internal_lms_domain, instance.ref.name, email, section['hits'], section['users'], section['courses'], instance_age.days ]) self.stderr.write(self.style.SUCCESS('Done generating CSV output.'))
def activity_csv(self, out): # pylint: disable=missing-docstring # Produce a mapping of public IPs (of active app servers) to parent instances. active_appservers = { instance.active_appserver.server.public_ip: instance for instance in OpenEdXInstance.objects.all() if not (instance.active_appserver is None or instance.active_appserver.server.public_ip is None) } if not active_appservers: self.stderr.write( self.style.SUCCESS('There are no active app servers! Nothing to do.') # pylint: disable=no-member ) sys.exit(0) self.stderr.write(self.style.SUCCESS('Running playbook...')) # pylint: disable=no-member with ansible.create_temp_dir() as playbook_output_dir: inventory = '[apps]\n{servers}'.format(servers='\n'.join(active_appservers.keys())) playbook_path = os.path.join(settings.SITE_ROOT, 'playbooks/collect_activity/collect_activity.yml') def log_line(line): """Helper to pass to capture_playbook_output().""" self.stderr.write(self.style.SUCCESS(line)) # pylint: disable=no-member log_line.info = log_line log_line.error = log_line # Launch the collect_activity playbook, which places a set of files into the `playbook_output_dir` # on this host. ansible.capture_playbook_output( requirements_path=os.path.join(os.path.dirname(playbook_path), 'requirements.txt'), inventory_str=inventory, vars_str=( 'local_output_dir: {output_dir}\n' 'remote_output_filename: /tmp/activity_report' ).format(output_dir=playbook_output_dir), playbook_path=playbook_path, username=settings.OPENSTACK_SANDBOX_SSH_USERNAME, logger_=log_line, ) csv_writer = csv.writer(out, quoting=csv.QUOTE_NONNUMERIC) csv_writer.writerow([ 'Appserver IP', 'Internal LMS Domain', 'Name', 'Contact Email', 'Unique Hits', 'Total Users', 'Total Courses', 'Age (Days)' ]) filenames = [os.path.join(playbook_output_dir, f) for f in os.listdir(playbook_output_dir)] data = ConfigParser() data.read(filenames) for public_ip in active_appservers.keys(): try: section = data[public_ip] except KeyError: # Fill in stats for any instaces that failed with "N/A" section = {'hits': 'N/A', 'users': 'N/A', 'courses': 'N/A'} instance = active_appservers[public_ip] instance_age = datetime.now(instance.created.tzinfo) - instance.created try: email = instance.betatestapplication_set.get().user.email except BetaTestApplication.DoesNotExist: email = 'N/A' csv_writer.writerow([ public_ip, instance.internal_lms_domain, instance.ref.name, email, section['hits'], section['users'], section['courses'], instance_age.days ]) self.stderr.write(self.style.SUCCESS('Done generating CSV output.')) # pylint: disable=no-member
def collect_instance_statistics( self, out, domain_name, start_date, end_date ): # pylint: disable=too-many-locals """Generate the instance statistics CSV.""" instance = self.get_instance_from_domain_name(domain_name) appserver = instance.get_active_appservers().first() name_prefix = appserver.server_name_prefix self.stderr.write(self.style.SUCCESS('Running playbook...')) with ansible.create_temp_dir() as playbook_output_dir: self.get_elasticsearch_hits_data_summary(playbook_output_dir, name_prefix, start_date, end_date) self.get_instance_usage_data(playbook_output_dir, name_prefix, appserver.server.public_ip) csv_writer = csv.writer(out, quoting=csv.QUOTE_NONNUMERIC) csv_writer.writerow([ 'Fully Qualified Domain Name', 'Server Name Prefix', 'Name', 'Contact Email', 'Unique Hits', 'Total Hits', 'Total Courses', 'Total Users', 'Age (Days)' ]) filenames = [os.path.join(playbook_output_dir, f) for f in os.listdir(playbook_output_dir)] data = ConfigParser() data.read(filenames) try: section = data[name_prefix] except KeyError: # Set the section to an empty dict section = {} instance_age = datetime.now(instance.created.tzinfo) - instance.created try: email = instance.betatestapplication_set.get().user.email except BetaTestApplication.DoesNotExist: email = 'N/A' csv_writer.writerow([ instance.domain, name_prefix, instance.ref.name, email, section.get('unique_hits', 'N/A'), section.get('total_hits', 'N/A'), section.get('courses', 'N/A'), section.get('users', 'N/A'), instance_age.days ]) self.stderr.write(self.style.SUCCESS('Done generating CSV output.'))