How to use _get_env_variables method in avocado

Best Python code snippet using avocado_python

fabfile.py

Source:fabfile.py Github

copy

Full Screen

...191 update_conf_files(deploy_to=deploy_to)192 update_permissions(setup=True)193def compile_messages(mode=DEFAULT_MODE):194 return195 env_variables = _get_env_variables(mode=mode) 196 with shell_env(**env_variables):197 # with cd(DJANGO_PROJECT_PATH):198 # run('django-admin makemessages -x js -l fr')199 # with cd(os.path.join(DJANGO_PROJECT_PATH, 'static', 'javascripts')):200 # run('django-admin makemessages -d djangojs -l fr')201 with cd(DJANGO_PROJECT_PATH):202 run('django-admin compilemessages')203def _update_permissions(debug=False, setup=False, only_static=False):204 """205 An exhaustive fail-proof permission setup. I tried to give the least possible 206 permissions.207 """208 print 'Updating permissions'209 with settings():210 with cd('/home'):211 sudo("find -type d -exec chmod a+x {} \;"); # makre sure all directories are executable212 if setup and not only_static:213 with settings(warn_only=True):214 sudo('groupadd staticusers')215 sudo('adduser www-data staticusers')216 sudo('adduser django staticusers')217 if setup:218 with cd(DJANGO_PROJECT_PATH):219 run('mkdir -p media')220 run('mkdir -p assets')221 run('mkdir -p app/migrations')222 # change permissions to static files223 sudo('chown -R django %s' % (DJANGO_PROJECT_PATH))224 sudo('chgrp -R staticusers %s' % (os.path.join(DJANGO_PROJECT_PATH, 'assets')))225 sudo('chgrp -R staticusers %s' % (os.path.join(DJANGO_PROJECT_PATH, 'media')))226 with cd(DJANGO_PROJECT_PATH): 227 # all files under the project dir are owned by django (gunicorn's uid) is the owner228 if not only_static:229 sudo("chmod -R 500 .") # r-x --- --- : django can only read and execute files by default230 with settings(warn_only=True):231 sudo("chmod -R 700 app/migrations") # rwx --- --- : django can write new migrations232 233 sudo("chmod -R 644 assets") # rw- r-- r-- : assets can be read by nginx (var-www) as well as everyone else234 sudo("chmod -R 644 media") # rw- r-- r--235 if not only_static:236 sudo("chmod -R 200 server_files/logs") # -w- r-- r--237 blacklist = (238 'scripts',239 # '%s/settings/*_private.py' % (DJANGO_PROJECT_NAME),240 )241 for f in blacklist:242 sudo("chmod -R 000 %s" % (f))243 sudo("find -type d -exec chmod a+x {} \;") # set all directories to executable 244 run("ls -la")245 if debug:246 with settings(warn_only=True):247 env.user = 'django'248 run('python manage.py runserver')249 250def update_permissions(deploy_to=DEFAULT_DEPLOY_TO, mode=DEFAULT_MODE, setup=False):251 _update_permissions(setup=setup)252 restart_nginx()253 restart_gunicorn()254def update_private_files(deploy_to=DEFAULT_DEPLOY_TO):255 env.hosts = DEPLOYMENT_HOSTS[deploy_to]256 _update_private_settings_file(deploy_to=deploy_to)257def update_conf_files(deploy_to=DEFAULT_DEPLOY_TO, restart=True):258 """259 Updates private django settings file as well as .profile, nginx and gunicorn configs260 261 Options262 -------263 deploy_to [DEFAULT_DEPLOY_TO]264 restart [True]265 Whether or not to restart nginx and gunicorn266 """267 env.hosts = DEPLOYMENT_HOSTS[deploy_to]268 _update_private_settings_file(deploy_to=deploy_to)269 print 'Modifying ~/.profile'270 _write_file('.profile', '.profile',271 {272 'DJANGO_PROJECT_PATH': DJANGO_PROJECT_PATH273 })274 print 'Modifying nginx config'275 _write_file('nginx.sh', '/etc/nginx/sites-enabled/django',276 {277 'DJANGO_PROJECT_PATH': DJANGO_PROJECT_PATH,278 'DOMAIN_NAME': DOMAIN_NAME279 })280 print 'Modifying gunicorn config'281 _write_file('gunicorn.sh', '/etc/init/gunicorn.conf',282 {283 'DJANGO_PROJECT_DIR': DJANGO_PROJECT_DIR,284 'DJANGO_PROJECT_NAME': DJANGO_PROJECT_NAME,285 'DJANGO_APP_NAME': DJANGO_APP_NAME286 })287 if restart:288 print 'Restarting nginx'289 sudo('nginx -t')290 sudo('service nginx reload')291 print 'Restarting gunicorn'292 run('service gunicorn restart')293def test_models(mode=DEFAULT_MODE, deploy_to=DEFAULT_DEPLOY_TO):294 """295 Generate random data using app/management/comands/generate_models.py.296 """297 print '\nTesting models'298 env_variables = _get_env_variables(mode=mode) 299 with cd(DJANGO_PROJECT_PATH):300 with shell_env(**env_variables):301 print '> Check database backend'302 run('echo "from django.db import connection;connection.vendor" | python manage.py shell ')303 print '> Generate random apps'304 run('python manage.py generate_models 10 --reset')305def _get_private_settings(deploy_to=DEFAULT_DEPLOY_TO):306 private_file = _get_private_settings_file(deploy_to=DEFAULT_DEPLOY_TO, local=True)307 import imp308 private = imp.load_source('', private_file)309 return private310def reset_db(mode=DEFAULT_MODE, deploy_to=DEFAULT_DEPLOY_TO):311 """312 Delete database and perform migrations (see migrate).313 Options314 -------315 mode [DEFAULT_MODE]316 deploy_to [DEFAULT_DEPLOY_TO]317 """318 with (hide('stdout')):319 pull_changes(mode=mode, deploy_to=deploy_to)320 migrate(mode=mode, deploy_to=deploy_to, reset_db=True)321def migrate(mode=DEFAULT_MODE, deploy_to=DEFAULT_DEPLOY_TO, env_variables=None,322 setup=False, reset_db=False, generate_dummy_data=True, create_super_user=True):323 """324 Perform migrations.325 Options326 -------327 mode [DEFAULT_MODE]328 deploy_to [DEFAULT_DEPLOY_TO]329 reset_db [False]330 If True, delete the database.331 generate_dummy_data [True]332 Generate dummy data (see app/management/)333 createsuperuser [True]334 If True and reset_db is True, create admin super user.335 """336 if not env_variables:337 env_variables = _get_env_variables(mode=mode)338 print '\nMigrating database as user django'339 with shell_env(**env_variables):340 with cd(DJANGO_PROJECT_PATH):341 if reset_db:342 with settings(warn_only=True):343 run('rm -rf app/migrations')344 with settings(warn_only=True):345 sudo("chmod -R 700 app") # rwx --- --- : django can write new migrations346 env.user = 'django'347 env.password = DJANGO_PASS348 # get django database pass, this is kind of hacky but wtv349 private_settings = _get_private_settings(deploy_to=deploy_to)350 django_db_pass = private_settings.DB_PASS351 print '> Checking database backend'352 with settings(prompts={353 "Login password for 'django': ": django_db_pass}):354 run('echo "from django.db import connection; connection.vendor" | python manage.py shell')355 with settings(prompts={356 "Login password for 'django': ": django_db_pass}):357 if reset_db:358 print '> Deleting database'359 if mode == 'dev':360 run('rm -rf %s/db.sqlite3' % (DJANGO_PROJECT_NAME))361 else:362 with settings(warn_only=True):363 run('rm -rf app/migrations')364 run('python manage.py sqlclear app | python manage.py dbshell ')365 run('python manage.py makemigrations')366 if setup:367 run('python manage.py migrate --fake-initial') 368 run('python manage.py makemigrations app')369 run('python manage.py migrate app') 370 elif reset_db:371 run('python manage.py migrate --fake') 372 run('python manage.py makemigrations app')373 run('python manage.py migrate --fake-initial')374 run('python manage.py migrate')375 else:376 run('python manage.py migrate')377 if mode == 'dev' or reset_db:378 if generate_dummy_data or reset_db:379 run('python manage.py generate_models 3 --reset')380 env.user = 'root'381 382 if mode == 'prod':383 print '> Checking postgresql status'384 run('service postgresql status')385 run('sudo netstat -nl | grep postgres')386 387 print '> Creating super user with login admin/pass'388 with settings(warn_only=True):389 with hide('stderr', 'stdout', 'warnings'):390 sudo('chmod u+x scripts/createsuperuser.sh')391 run('./scripts/createsuperuser.sh')392def update_requirements(branch=DEFAULT_BRANCH):393 """394 Update pip and bower requirements395 """396 print '\nUpdate pip and bower requirements'397 with cd(DJANGO_PROJECT_PATH):398 print 'Installing python requirements..'399 run('pip install -r requirements.txt')400 401 print 'Installing bower requirements..'402 run('bower install --allow-root')403def pull_changes(mode=DEFAULT_MODE, deploy_to=DEFAULT_DEPLOY_TO, branch=DEFAULT_BRANCH,404 collectstatic=False):405 """406 Update conf files, pull changes from repo and update requirements.407 Options408 -------409 mode [DEFAULT_MODE]410 deploy_to [DEFAULT_DEPLOY_TO]411 branch [DEFAULT_BRANCH]412 git branch to pull from413 See414 -------415 update_requirements416 """417 _update_private_settings_file(deploy_to=deploy_to)418 with cd(DJANGO_PROJECT_PATH):419 print '\nPulling changes from %s repo' % (branch)420 run('git config --global core.filemode false')421 if False: #branch == 'stable':422 run('git fetch --all')423 run('git reset --hard origin/%s' % (branch))424 else:425 run('git checkout .') # discard changes in working directory426 run('git pull origin %s' % (branch))427 run('git checkout %s' % (branch))428 update_requirements()429 if collectstatic:430 compile_messages(mode=mode)431 collect_staticfiles(mode=mode, deploy_to=deploy_to)432 restart_gunicorn()433def _update_private_settings_file(deploy_to=DEFAULT_DEPLOY_TO):434 print '\nUpdating private settings'435 local_private_file = _get_private_settings_file(local=True, deploy_to=deploy_to)436 remote_private_file = _get_private_settings_file(local=False, deploy_to=deploy_to)437 put(local_path=local_private_file,remote_path=remote_private_file)438def _get_private_settings_file(deploy_to=DEFAULT_DEPLOY_TO, local=True):439 if deploy_to not in DEPLOYMENT_PRIVATE_FILES.keys():440 print 'Unknown deployment option %s' % (deploy_to)441 print 'Possible options:', DEPLOYMENT_PRIVATE_FILES.keys()442 sys.exit()443 basename = DEPLOYMENT_PRIVATE_FILES[deploy_to]444 if local:445 django_path = LOCAL_DJANGO_PATH446 private_file = os.path.join(django_path, DJANGO_PROJECT_NAME,447 'settings', basename + '.py')448 else:449 django_path = DJANGO_PROJECT_PATH450 private_file = os.path.join(django_path, DJANGO_PROJECT_NAME,451 'settings', 'private.py')452 return private_file453def _get_env_variables(mode=DEFAULT_MODE, deploy_to=DEFAULT_DEPLOY_TO):454 ev = dict(ENV_VARIABLES)455 if mode not in DEPLOYMENT_MODES:456 print 'Invalid mode option %s' % (mode)457 print 'Possible options:', DEPLOYMENT_MODES458 sys.exit()459 ev['APP_ENV'] = mode460 env.hosts = DEPLOYMENT_HOSTS[deploy_to]461 return ev462def hard_reboot(**kwargs):463 """464 Reboot server and reset database (see reboot)465 """466 kwargs["reset_db"] = True467 reboot(**kwargs)468def restart_gunicorn():469 print 'Restarting gunicorn'470 run('service gunicorn restart')471def restart_nginx():472 print 'Restarting nginx'473 sudo('nginx -t')474 sudo('service nginx restart')475def collect_staticfiles(mode=DEFAULT_MODE, deploy_to=DEFAULT_DEPLOY_TO,476 env_variables=None):477 if not env_variables:478 env_variables = _get_env_variables(mode=mode, deploy_to=deploy_to)479 with cd(DJANGO_PROJECT_PATH):480 with shell_env(**env_variables):481 with settings(prompts=prompts):482 compile_messages(mode=mode)483 run('python manage.py collectstatic')484 _update_permissions(only_static=True, setup=setup)485 restart_nginx()486def reboot(mode=DEFAULT_MODE, deploy_to=DEFAULT_DEPLOY_TO, env_variables=None, 487 setup=False, reset_db=False, branch=DEFAULT_BRANCH):488 """489 Reboot server.490 Prerequisites491 -------------492 fab setup493 494 Overview495 -------------496 Update conf files (see update_conf_files)497 Pull changes from repo (see pull_chages)498 Migrate database (see migrate)499 Collects static files500 Restart nginx501 If mode is dev, stop gunicorn and run localhost502 If mode is prod, restart gunicorn503 Updates logs (see get_logs)504 Options505 -------------506 mode [DEFAULT_MODE]507 deploy_to [DEFAULT_DEPLOY_TO]508 env_variables [None]509 dictionary of env variables to pass to remote shell env510 see: _get_env_variables511 reset_db [False]512 whether or not to delete database513 branch [DEFAULT_BRANCH]514 git branch to pull from515 """516 if not env_variables:517 env_variables = _get_env_variables(mode=mode, deploy_to=deploy_to)518 519 with cd(DJANGO_PROJECT_PATH):520 print 'Stopping gunicorn' 521 with settings(warn_only=True):522 run('service gunicorn stop')523 pull_changes(mode=mode, deploy_to=deploy_to)524 with shell_env(**env_variables):525 with settings(prompts=prompts):526 compile_messages(mode=mode)527 run('python manage.py collectstatic')528 _update_permissions(only_static=True, setup=setup)529 530 restart_nginx()531 migrate(mode=mode, deploy_to=deploy_to, env_variables=env_variables, 532 setup=setup, reset_db=reset_db)533 if mode == 'prod':534 if deploy_to == 'alpha':535 with shell_env(**env_variables):536 print 'Generating 3 random app...'537 run('python manage.py generate_models 3')538 print 'Restarting gunicorn'539 run('service gunicorn restart')540 get_logs(deploy_to=deploy_to)541 elif mode == 'dev':542 get_logs(deploy_to=deploy_to)543 with shell_env(**env_variables):544 print 'Running on localhost'545 run('python manage.py runserver localhost:9000')546 else:547 print 'Invalid mode %s' % (mode)548def get_media(deploy_to=DEFAULT_DEPLOY_TO):549 """550 Copy server's media folder551 """552 print '\nCopying media to server_files/media/'553 log_dir = os.path.join(LOCAL_DJANGO_PATH, 'server_files', 'media', deploy_to)554 if not os.path.exists(log_dir):555 local('mkdir -p %s' % (log_dir))556 with settings(hide('warnings')): 557 get(remote_path="%s/media" % (DJANGO_PROJECT_PATH), local_path="%s" % (log_dir))558def put_media(deploy_to=DEFAULT_DEPLOY_TO):559 """560 Copy local media folder to server561 """562 print '\nCopying media from local to server'563 local_media_dir = os.path.join(LOCAL_DJANGO_PATH, 'media')564 remote_media_dir = os.path.join(DJANGO_PROJECT_PATH, 'media')565 with cd(DJANGO_PROJECT_PATH):566 with settings(hide('warnings')): 567 put(local_media_dir, remote_media_dir)568def generate_models(n=3, mode=DEFAULT_MODE):569 env_variables = _get_env_variables(mode=mode) 570 with shell_env(**env_variables):571 with cd(DJANGO_PROJECT_PATH):572 print 'Generating %i random app...' % (n)573 run('python manage.py generate_models %i' % (n))574def update_challenge_questions(deploy_to=DEFAULT_DEPLOY_TO, reset=False, mode=DEFAULT_MODE):575 # update csv in tmp576 print os.path.join(LOCAL_DJANGO_PATH, 'tmp/crypto_texts.csv')577 with cd(DJANGO_PROJECT_PATH):578 run('mkdir -p tmp')579 _write_file(580 os.path.join(LOCAL_DJANGO_PATH, 'tmp/crypto_texts.csv'), 581 os.path.join(DJANGO_PROJECT_PATH, 'tmp/crypto_texts.csv'), {})582 _write_file(583 os.path.join(LOCAL_DJANGO_PATH, 'app/management/commands/encrypter.py'), 584 os.path.join(DJANGO_PROJECT_PATH, 'app/management/commands/encrypter.py'), {})585 env_variables = _get_env_variables(mode=mode) 586 with cd(DJANGO_PROJECT_PATH):587 with shell_env(**env_variables):588 if reset:589 run('python manage.py generate_challenges tmp/crypto_texts.csv --reset')590 else:591 run('python manage.py generate_challenges tmp/crypto_texts.csv') 592def get_fixtures(deploy_to=DEFAULT_DEPLOY_TO, mode=DEFAULT_MODE):593 from datetime import datetime594 env_variables = _get_env_variables(mode=mode) 595 apps = ('app', 'event')596 for app in apps:597 fixture_path = '%s/fixtures/%s-data-%s.json' % (598 app, app, datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))599 with shell_env(**env_variables):600 with cd(DJANGO_PROJECT_PATH):601 run('mkdir -p %s/fixtures' % (app))602 run('python manage.py dumpdata --format=json --indent=2 %s > %s' % (app, fixture_path))603 log_dir = os.path.join(LOCAL_DJANGO_PATH, 'server_files', 'fixtures', deploy_to)604 if not os.path.exists(log_dir):605 local('mkdir -p %s' % (log_dir))606 with settings(hide('warnings')): 607 get(remote_path="%s/%s" % (DJANGO_PROJECT_PATH, fixture_path), local_path="%s" % (log_dir))608def get_logs(deploy_to=DEFAULT_DEPLOY_TO):609 """610 Copy django, nginx and gunicorn log files from remote to server_files/logs611 """612 print '\nCopying logs to server_files/logs/'613 log_dir = os.path.join(LOCAL_DJANGO_PATH, 'server_files', 'logs', deploy_to)614 if not os.path.exists(log_dir):615 local('mkdir -p %s' % (log_dir))616 with settings(hide('warnings')): 617 get(remote_path="/var/log/nginx/error.log", local_path="%s/nginx.error.log" % (log_dir))618 get(remote_path="/var/log/nginx/access.log", local_path="%s/nginx.access.log" % (log_dir))619 get(remote_path="%s/server_files/logs/local/django.debug.log" % (DJANGO_PROJECT_PATH), local_path="%s/django.debug.log" % (log_dir))620 get(remote_path="%s/server_files/logs/local/django.request.debug.log" % (DJANGO_PROJECT_PATH), local_path="%s/django.request.debug.log" % (log_dir))621 get(remote_path="/var/log/upstart/gunicorn.log", local_path="%s/gunicorn.log" % (log_dir))622 get(remote_path="/var/log/postgresql/postgresql-9.3-main.log", local_path="%s/psql.main.log" % (log_dir))623def test():624 deploy_to=DEFAULT_DEPLOY_TO625 env_variables = _get_env_variables(mode='prod')626 print '\nMigrating database as user django'627 with shell_env(**env_variables):628 with cd(DJANGO_PROJECT_PATH):629 env.user = 'django'630 env.password = DJANGO_PASS631 # get django database pass, this is kind of hacky but wtv632 private_settings = _get_private_settings(deploy_to=deploy_to)633 django_db_pass = private_settings.DB_PASS634 with settings(prompts={635 "Login password for 'django': ": django_db_pass}):636 run('python manage.py migrate --fake app')637def all(deploy_to=DEFAULT_DEPLOY_TO, mode=DEFAULT_MODE):638 """Setup and reboot."""639 setup(deploy_to=deploy_to, mode=mode)...

Full Screen

Full Screen

eventhandler.py

Source:eventhandler.py Github

copy

Full Screen

...55 output_to_logger=True,56 fatal_error=True57 )58 if event_type == events.type.CW_SCHEDULED_EVENT:59 scheduler = events.scheduler.Scheduler(self._get_env_variables())60 response = scheduler.automated_schedule()61 elif event_type == events.type.API_S3_PUT_CONFIG:62 response = events.put_config.put_config_into_dynamo(self._get_env_variables())63 elif event_type == events.type.API_RETRIEVE_DYNAMO_AS_CONFIG:64 response = events.retrieve_config.retrieve_dynamo_as_config(self._get_env_variables())65 else:66 automated.exceptions.log_error(67 self,68 error_message=f"Event type '{event_type}' not found.",69 include_in_http_response=True,70 http_status_code=http_response.NOT_FOUND,71 output_to_logger=True,72 fatal_error=True73 )74 return response75 def _get_env_variables(self) -> dict:76 """77 Returns Lambda environment variables common to majority of application functionality.78 These variables are defined/expected by AWS CDK synth/deploy79 :return: dict: Environment variables retrieved from Lambda environment80 """81 try:82 region = os.environ["scheduler_region"]83 logger.debug(f"Discovered dynamodb region name [{region}] from environment variable 'scheduler_region'")84 except KeyError:85 automated.exceptions.log_error(86 automation_component=self,87 error_message=f"Unable to discover region name from environment variable 'scheduler_region'",88 output_to_logger=True,89 include_in_http_response=True,...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful