def _diff_f5(): """ Copy f5 configuration file to local sever, parse txt and create or update site items. """ f5_config_dir = "{0}/atlas/fabfile".format(path) f5_config_file = "{0}/{1}".format(f5_config_dir, f5_config_files[environment]) # If an older config file exists, copy it to a backup folder. if os.path.isfile(f5_config_file): local( "mv {0} /data/code/inventory/fabfile/backup/{1}.{2}".format( f5_config_file, f5_config_files[environment], str(time()).split(".")[0] ) ) # Copy config file from the f5 server to the Atlas server. local( "scp {0}:/config/{1} {2}/".format( serverdefs[environment]["f5_servers"][0], f5_config_files[environment], f5_config_dir ) ) # Open file from f5 with open(f5_config_file, "r") as ifile: data = ifile.read() # Use regex to parse out path values p = re.compile('"(.+/?)" := "(\w+(-\w+)?)",') sites = p.findall(data) # Iterate through sites found in f5 data for site in sites: f5only = False if site[0] in f5exceptions: f5only = True # Get path without leading slash path = site[0][1:] pool = site[1] # Set a type value based on pool if pool == "WWWLegacy": type = "legacy" elif pool == "poola-homepage" or pool == "poolb-homepage": type = "homepage" elif pool == "poolb-express": type = "express" else: type = "custom" site_query = 'where={{"path":"{0}"}}'.format(path) sites = utilities.get_eve("sites", site_query) if not sites or len(sites["_items"]) == 0: payload = {"name": path, "path": path, "pool": pool, "status": "launched", "type": type, "f5only": f5only} utilities.post_eve("sites", payload) print ("Created site record based on f5.\n{0}".format(payload)) elif pool != data["_items"][0]["pool"]: site = data["_items"][0] payload = {"pool": pool, "status": "launched", "type": type} utilities.patch_eve("sites", site["_id"], payload) print "Updated site based on f5.\n{0}".format(payload)
def available_sites_check(): site_query = 'where={"status":{"$in":["pending","available"]}}' sites = utilities.get_eve("sites", site_query) actual_site_count = sites["_meta"]["total"] if environment == "local": desired_site_count = 2 else: desired_site_count = 5 if actual_site_count < desired_site_count: needed_sites_count = desired_site_count - actual_site_count while needed_sites_count > 0: payload = {"status": "pending"} utilities.post_eve("sites", payload) needed_sites_count -= 1
def on_inserted_sites(items): """ Provision Express instances. :param items: List of dicts for instances to be provisioned. """ log.debug('site | Site objects created | sites - %s', items) for item in items: log.debug(item) log.debug('site | Site object created | site - %s', item) # Create statistics item statistics_payload = {} # Need to get the string out of the ObjectID. statistics_payload['site'] = str(item['_id']) log.debug('site | Create Statistics item - %s', statistics_payload) statistics = utilities.post_eve(resource='statistics', payload=statistics_payload) item['statistics'] = str(statistics['_id']) tasks.site_provision.delay(item)
def import_backup(): """ Import a backup to a new instance on the current version of core, profile, and any packages that are present. If a current version of a package is not available, the import will abort. """ backup_request = request.get_json() app.logger.debug('Backup | Import | %s', backup_request) # Get the backup and then the site records. # TODO Get the list of env from the config files. # TODO Verify import is from different env, recommend restore if it is the same env. if not (backup_request.get('env') and backup_request.get('id')): abort(409, 'Error: Missing env (local, dev, test, prod) and id.') elif not backup_request.get('env'): abort(409, 'Error: Missing env (local, dev, test, prod).') elif not backup_request.get('id'): abort(409, 'Error: Missing id.') elif backup_request['env'] not in ['local', 'dev', 'test', 'prod']: abort(409, 'Error: Invalid env choose from [local, dev, test, prod]') backup_record = utilities.get_single_eve('backup', backup_request['id'], env=backup_request['env']) app.logger.debug('Backup | Import | Backup record - %s', backup_record) remote_site_record = utilities.get_single_eve( 'sites', backup_record['site'], backup_record['site_version'], env=backup_request['env']) app.logger.debug('Backup | Import | Site record - %s', remote_site_record) # Get a list of packages to include try: package_list = utilities.package_import_cross_env( remote_site_record, env=backup_request['env']) except Exception as error: abort(500, error) app.logger.info('Backup | Import | Package list - %s', package_list) # Try to get the p1 record. local_p1_instance_record = utilities.get_single_eve( 'sites', remote_site_record['sid']) app.logger.debug('Backup | Import | Local instance record - %s', local_p1_instance_record) # Try to get the path record if the site is launched. local_path_instance_record = False if remote_site_record['path'] != remote_site_record['sid']: query_string = 'where={{"path":"{0}"}}'.format( remote_site_record['path']) local_path_instance_records = utilities.get_eve('sites', query_string) app.logger.info('Backup | Import | Local path instance record - %s', local_path_instance_records) if local_path_instance_records['_meta']['total'] == 1: local_path_instance_record = True if local_p1_instance_record['_error'] and local_p1_instance_record[ '_error']['code'] == 404 and not local_path_instance_record: # Create an instance with the same sid payload = { "status": remote_site_record['status'], "sid": remote_site_record['sid'], "path": remote_site_record['path'] } response_string = 'the same' else: app.logger.info('Backup | Import | Instance sid or path exists') payload = {"status": "installed"} response_string = 'a new' # Add package list to payload if it exists if package_list: payload['code'] = {"package": package_list} # Set install payload['install'] = False new_instance = utilities.post_eve('sites', payload) app.logger.debug('Backup | Import | New instance record - %s', new_instance) env = backup_request['env'] backup_id = backup_request['id'] target_instance = new_instance['_id'] tasks.import_backup.apply_async([env, backup_id, target_instance], countdown=30) return make_response( 'Attempting to import backup to {0} sid'.format(response_string))