def create_pub(env, username, sub, pub_id): """ For debugging purposes really """ app.config.from_object(getattr(configs, env)) # Remove socket connection if env in ['staging', 'production']: remote_url = get_item('credential', f"sql-{env}").get('remote_url') secho(remote_url, fg='green') app.config['SQLALCHEMY_DATABASE_URI'] = remote_url pub_data = fetch_pub(str(pub_id)) logger.info(f"Adding {pub_data.pub_title}") if pub_data: user = User.query.filter_by(username=username).first() if not user: VARS = {'username': username, 'email': "FAKE__{}@FAKE.edu".format(random_string()), 'password': generate_password_hash('WZRfEsxjLYieTHbonP7JWig3FEBRAFCYthnJdph7fd9frs2BejHsbXWZGYnFmazT'), 'university': ""} user, created = get_or_create(User, **VARS) sub_id = Subreddit.query.filter_by(name=sub).first().id created_on = arrow.utcnow().shift(seconds=-random.randint(1,350000)).datetime thread = Thread(user_id=user.id, subreddit_id=sub_id, publication_id=pub_data.id, created_on=created_on, updated_on=created_on) db.session.add(thread) db.session.commit()
def __init__(self, name_or_obj=None): """ Args: name_or_obj - A name for a new datastore item or an existing one to initialize using the datastore_model class. """ self.exclude_from_indexes = None self._exists = False if type(name_or_obj) == Entity: # Parse JSON fields when instantiating without # loading from gcloud. result_out = {} for k, v in name_or_obj.items(): if isinstance(v, str) and v.startswith("JSON:"): result_out[k] = json.loads(v[5:]) elif v: result_out[k] = v self.__dict__.update(result_out) self.kind = name_or_obj.key.kind self.name = name_or_obj.key.name elif name_or_obj: self.name = name_or_obj item = get_item(self.kind, name_or_obj) if item: self._exists = True self.__dict__.update(item)
def get_dict(self, *keys): results = {} for key in keys: try: results.update({key: get_item('cache', key)}) except AttributeError: pass return results
def has(self, key): try: item = get_item('cache', key) expires = item.get('expires') if expires == 0 or expires > time(): return True except: return False
def send_email(data): """ Send an email to the user. """ api_key = get_item('credential', 'mailgun')['apiKey'] return requests.post( "https://api.mailgun.net/v3/mail.elegansvariation.org/messages", auth=("api", api_key), data=data)
def get_aws_client(service='ecs'): """ Retrieve AWS task account """ if not hasattr(g, service): fargate_credentials = get_item('credential', 'aws_fargate') fargate_credentials.pop("_exists") setattr(g, service, boto3.client(service, **fargate_credentials)) return getattr(g, service)
def get(self, key): try: item = get_item('cache', self.key_prefix + "/" + key) value = item.get('value') value = pickle.loads(base64.b64decode(value)) expires = item.get('expires') if expires == 0 or expires > time(): return value except AttributeError: return None
def create_subreddit(env, name, group): """ Add a new subreddit """ app.config.from_object(getattr(configs, env)) if env in ['staging', 'production']: remote_url = get_item('credential', f"sql-{env}").get('remote_url') secho(remote_url, fg='green') app.config['SQLALCHEMY_DATABASE_URI'] = remote_url secho(f"Adding subreddit -- {env}", fg='green') add_sub = Subreddit(name=name, group=group, admin_id=1) db.session.add(add_sub) db.session.commit()
def decrypt_credentials(): from base.application import create_app app = create_app() app.app_context().push() click.secho("Decrypting env_config.zip.enc", fg='green') zip_creds = get_item('credential', 'travis-ci-cred') comm = [ 'travis', 'encrypt-file', 'env_config.zip.enc', '--force', '--key', zip_creds['key'], '--iv', zip_creds['iv'], '--decrypt' ] out, err = Popen(comm, stdout=PIPE, stderr=PIPE).communicate() click.secho(str(out, 'utf-8'), fg='green') if err: exit(secho(str(err, 'utf-8'), fg='red')) click.secho("Unzipping env_config.zip", fg='green') comm = ['unzip', '-qo', 'env_config.zip'] out, err = Popen(comm, stdout=PIPE, stderr=PIPE).communicate() click.secho(str(out, 'utf-8'), fg='green') if err: exit(secho(str(err, 'utf-8'), fg='red')) os.remove("env_config.zip")
def initdb(env): """Initialize the ID database""" # Mock environment app.config.from_object(getattr(configs, env)) # Remove socket connection if env in ['staging', 'production']: remote_url = get_item('credential', f"sql-{env}").get('remote_url') secho(remote_url, fg='green') app.config['SQLALCHEMY_DATABASE_URI'] = remote_url secho(f"Init the db -- {env}", fg='green') secho(app.config['SQLALCHEMY_DATABASE_URI']) db.drop_all() db.create_all() first_user = User(username='******', email='*****@*****.**', email_verified=True, password=generate_password_hash('Chicago'), university='Northwestern University') db.session.add(first_user) db.session.commit() # Install base set of subreddits for group, subreddits in BASE_SUBREDDITS.items(): for subreddit in subreddits: if type(subreddit) is tuple: sub = Subreddit(name=subreddit[0], description=subreddit[1], group=group, admin_id=first_user.id) else: sub = Subreddit(name=subreddit, group=group, admin_id=first_user.id) db.session.add(sub) db.session.commit()
def decrypt_credentials(): secho("Decrypting env_config.zip.enc", fg='green') zip_creds = get_item('credential', 'travis-ci-cred') comm = ['travis', 'encrypt-file', 'env_config.zip.enc', '--force', '--key', zip_creds['key'], '--iv', zip_creds['iv'], '--decrypt'] out, err = Popen(comm, stdout=PIPE, stderr=PIPE).communicate() secho(str(out, 'utf-8'), fg='green') if err: exit(secho(str(err, 'utf-8'), fg='red')) secho("Unzipping env_config.zip", fg='green') comm = ['unzip', '-qo', 'env_config.zip'] out, err = Popen(comm, stdout=PIPE, stderr=PIPE).communicate() secho(str(out, 'utf-8'), fg='green') if err: exit(secho(str(err, 'utf-8'), fg='red')) os.remove("env_config.zip")
def update_credentials(): """ Update the credentials zip file """ from base.application import create_app app = create_app() app.app_context().push() click.secho("Zipping env_config", fg='green') zipdir('env_config/', 'env_config.zip') zip_creds = get_item('credential', 'travis-ci-cred') click.secho("Encrypting credentials", fg='green') if os.path.exists("env_config.zip.enc"): os.remove("env_config.zip.enc") comm = [ 'travis', 'encrypt-file', 'env_config.zip', "--org", '--key', zip_creds['key'], '--iv', zip_creds['iv'] ] print(' '.join(comm)) out, err = Popen(comm, stdout=PIPE, stderr=PIPE).communicate() secho(str(out, 'utf-8'), fg='green') if err: exit(secho(str(err, 'utf-8'), fg='red')) os.remove("env_config.zip")
def remote_redis(env): """ Launches a python terminal with redis available """ r = redis.Redis(**get_item('credential', 'redis-{}'.format(env))) IPython.embed(user_ns=locals())