def create_posts(user): access_token, refresh_token = obtain_token_pair(user['username'], config['default_password']) num_posts = random.randint(1, config['max_number_of_posts']) headers = { 'Authorization': 'Bearer {}'.format(access_token), } for i in range(num_posts): title = "{0} {1}".format(user['username'], str(i)) content = "{0} says: {1}".format(user['username'], config['post_content']) payload = { "title": title, "content": content } resp = requests.post(url="{}/posts/".format(base_url), json=payload, headers=headers) if resp.status_code == 401 and resp.json()['code'] == 'token_not_valid': LOGGER.warning("{}. Obtaining new token pair...".format(resp.json()['messages'][0]['message'])) access_token, refresh_token = obtain_token_pair(user['username'], config['default_password']) headers = { 'Authorization': 'Bearer {}'.format(access_token) } resp = requests.post(url="{}/posts/".format(base_url), json=payload, headers=headers) if resp.status_code != 201: LOGGER.error("Failed to create post. Response code: {}".format(resp.status_code))
def open_mdb_connection(): for host in MARIA_DB_CONFIGURATION['hosts']: try: connection = pymysql.connect( user=MARIA_DB_CONFIGURATION['user'], password=MARIA_DB_CONFIGURATION['password'], database=MARIA_DB_CONFIGURATION['database'], host=host, cursorclass=pymysql.cursors.DictCursor, autocommit=True, ) except: if host == MARIA_DB_CONFIGURATION['hosts'][-1]: log_str = "Failed to connect to any MariaDB host" LOGGER.exception(log_str, exc_info=True) raise DBError(log_str) else: LOGGER.warning( 'Failed to connect to MariaDB on host {}. Trying next host.' .format(host)) else: if connection.open: LOGGER.debug( 'mariadb connection to host {} successful.'.format(host)) return connection else: err_str = 'Connection to MariaDB failed.' LOGGER.error(err_str) raise DBError(err_str)
def start_servers(processes, ami_id, user_data, setup_disks, instance_type, obs_ids, created_by, name, instance_details, spot_price, frequency_channels, force): cvel_data = get_cvel() # Create the queue tasks = multiprocessing.JoinableQueue() # Start the consumers for x in range(processes): consumer = Consumer(tasks) consumer.start() counter = 1 for obs_id in obs_ids: snapshot_id = OBS_IDS.get(obs_id) if snapshot_id is None: LOGGER.warning( 'The obs-id: {0} does not exist in the settings file') else: obs_id_dashes = obs_id.replace('_', '-') for frequency_groups in get_frequency_groups( frequency_channels, obs_id_dashes, cvel_data, force): tasks.put( Task(ami_id, user_data, setup_disks, instance_type, obs_id, snapshot_id, created_by, name, spot_price, instance_details, frequency_groups, counter)) counter += 1 # Add a poison pill to shut things down for x in range(processes): tasks.put(None) # Wait for the queue to terminate tasks.join()
def start_servers( processes, ami_id, user_data, setup_disks, instance_type, obs_ids, created_by, name, instance_details, spot_price, frequency_channels, force): cvel_data = get_cvel() # Create the queue tasks = multiprocessing.JoinableQueue() # Start the consumers for x in range(processes): consumer = Consumer(tasks) consumer.start() counter = 1 for obs_id in obs_ids: snapshot_id = OBS_IDS.get(obs_id) if snapshot_id is None: LOGGER.warning('The obs-id: {0} does not exist in the settings file') else: obs_id_dashes = obs_id.replace('_', '-') for frequency_groups in get_frequency_groups(frequency_channels, obs_id_dashes, cvel_data, force): tasks.put( Task( ami_id, user_data, setup_disks, instance_type, obs_id, snapshot_id, created_by, name, spot_price, instance_details, frequency_groups, counter ) ) counter += 1 # Add a poison pill to shut things down for x in range(processes): tasks.put(None) # Wait for the queue to terminate tasks.join()
def like_em_all(users): iterator = PostIterator() for user in users: access, refresh = obtain_token_pair(user['username'], config['default_password']) iterator.set_auth(access, refresh) iterator.set_user(user) iterator.set_posts(get_all_posts()) while True: try: post_to_like = next(iterator) perform_like(post=post_to_like, user=user, token=access) except StopIteration: break except BotClientError as e: LOGGER.warning("Failed to perform a like: {}".format(str(e)))
def start_servers( ami_id, user_data, setup_disks, instance_type, obs_id, created_by, name, instance_details, spot_price): snapshot_id = OBS_IDS.get(obs_id) if snapshot_id is None: LOGGER.warning('The obs-id: {0} does not exist in the settings file') else: ec2_helper = EC2Helper() iops = None if instance_details.iops_support: iops = 500 zone = ec2_helper.get_cheapest_spot_price(instance_type, spot_price) if zone is not None: volume, snapshot_name = ec2_helper.create_volume(snapshot_id, zone, iops=iops) LOGGER.info('obs_id: {0}, volume_name: {1}'.format(obs_id, snapshot_name)) now = datetime.datetime.now() user_data_mime = get_mime_encoded_user_data(volume.id, setup_disks, user_data, now.strftime('%Y-%m-%dT%H-%M-%S')) if spot_price is not None: ec2_helper.run_spot_instance( ami_id, spot_price, user_data_mime, instance_type, volume.id, created_by, '{1}-{0}'.format(name, snapshot_name), instance_details, zone, ephemeral=True) else: LOGGER.error('Cannot get a spot instance of {0} for ${1}'.format(instance_type, spot_price))
def start_servers(ami_id, user_data, setup_disks, instance_type, obs_id, created_by, name, instance_details, spot_price): snapshot_id = OBS_IDS.get(obs_id) if snapshot_id is None: LOGGER.warning('The obs-id: {0} does not exist in the settings file') else: ec2_helper = EC2Helper() iops = None if instance_details.iops_support: iops = 500 zone = ec2_helper.get_cheapest_spot_price(instance_type, spot_price) if zone is not None: volume, snapshot_name = ec2_helper.create_volume(snapshot_id, zone, iops=iops) LOGGER.info('obs_id: {0}, volume_name: {1}'.format( obs_id, snapshot_name)) now = datetime.datetime.now() user_data_mime = get_mime_encoded_user_data( volume.id, setup_disks, user_data, now.strftime('%Y-%m-%dT%H-%M-%S')) if spot_price is not None: ec2_helper.run_spot_instance(ami_id, spot_price, user_data_mime, instance_type, volume.id, created_by, '{1}-{0}'.format( name, snapshot_name), instance_details, zone, ephemeral=True) else: LOGGER.error('Cannot get a spot instance of {0} for ${1}'.format( instance_type, spot_price))