How to ignore files when running `gcloud app deploy`? - python

When I run
gcloud app deploy app.yaml
which files actually get uploaded?
The project folder contains folders and files such as .git, .git_ignore, Makefile or venv that are irrelevant for the deployed application.
How does gcloud app deploy decide which files get uploaded?

tl;dr: you should use a .gcloudignore file, not skip_files in app.yaml.
While the prior two answers make use of skip_files in the app.yaml file. There is now a .gcloudignore that is created when using gcloud deploy or upload commands. The default will depend on the detected language that you are using but here is automatically created .gcloudignore that I found in my Python project:
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore
# Python pycache:
__pycache__/
Note: These commands will not work when both skip_files is defined and .gcloudignore is present. This is not mentioned in the skip_filesdefinition of theapp.yaml` reference.
It seems better to have a globally recognized standard across gcloud commands and makes more sense to adopt the .gcloudignore versus using the skip_files which is only relevant without App Engine. Additionally, it works pretty much like a .gitignore file which the reference mentions:
The syntax of .gcloudignore borrows heavily from that of .gitignore;
see https://git-scm.com/docs/gitignore or man gitignore for a full
reference.
https://cloud.google.com/sdk/gcloud/reference/topic/gcloudignore

EDIT Aug 2018: Google has since introduced .gcloudignore, which is now preferred, see dalanmiller's answer.
They're all uploaded, unless you use the skip_files instruction in app.yaml. Files with a dot like .git are ignored by default. If you want to add more, beware that you're overriding these defaults and almost certainly want to keep them around.
skip_files:
- ^Makefile$
- ^venv$
# Defaults
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\..*$
Note also that they are uploaded to different places if you use a static handler. Static files are sent to a CDN and are not available to your language run time (although there are ways around that, too).
Make sure to read the docs:
https://cloud.google.com/appengine/docs/standard/python/config/appref#skip_files

How does gcloud app deploy decide which files get uploaded?
It doesn't. It uploads everything by default. As mentioned in another response you can use the skip_files section in app.yaml as follows:
skip_files:
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\..*$
- ^(.*/)?\.bak$
- ^\.idea$
- ^\.git$
You can also use the --verbosity param to see what files are being deployed, i.e. gcloud app deploy app.yaml --verbosity=debug or gcloud app deploy app.yaml --verbosity=info per docs.

Related

Django keep different database setting in each servers

I am deploying capistrano to Django project.
And database settings differs from local to server.
So at first I set linked_files in deploy.rb.
append :linked_files, "app/settings.py"
With this setting, deployment doesn't update the app/settings.py on server.
However sometimes, I need to add app setting to this file, so it should be synced to server's.
Is there any good practice to deploy django project by tools and keep only database setting in each server??
In such setups one way could be to use app/settings/local.local and app/settings/prod.py files. a make target should copy the app/settings/local.local file to app/settings/local.py and use this file. This way in settings you always have one .py file. all the changes to local.local are synced with git.
local.py should be added to gitignore. __init__.py in settings folder should import from all the py files.
settings
|
| -- __init__.py
| -- local.local
| -- prod.py
somethings like this.

Elastic Beanstalk doesn't accept my changes to WSGIPath

I have an app where I want to run it from aws_wsgi.py instead of application.py, as there are several different entry points depending on where we are hosting it. For this reason, I would like to be able to change the WSGIPath variable to point to the correct location.
This, in an .ebextensions .config file, does not work:
option_settings:
- namespace: "aws:elasticbeanstalk:container:python"
option_name: WSGIPath
value: "/opt/python/current/app/aws_wsgi.py"
The environment attempts to use 'application.py' despite the above lines. No error appears to be emitted. Other parts of that same config file work perfectly, such as packages commands to get the system to install some yum packages. I can confirm the config files are getting uploaded in the logs:
INFO: Creating new application version using project code
WARNING: You have uncommitted changes.
INFO: Getting version label from git with git-describe
Creating application version archive "0_3_0-507-ga36f".
INFO: creating zip using git archive HEAD
INFO: git archive output: .ebextensions/
.ebextensions/01-weave_server_eb.config
.ebextensions/02-weave_server_eb_lxml_dependencies.config
.ebextensions/03-weave_server_eb_nltk_data.config
.ebextensions/04-weave_server_eb_entity_data.config
.ebextensions/05-weave_server_eb_geography_data.config
.gitattributes
.gitignore
...etc.
We run with a saved configuration, i.e. via eb create --cfg Live, and in the dashboard, that configuration shows that WSGIPath is "application.py". But there is nowhere to change that value in the dashboard. It seems like it is a built-in value that overrides the data we send with the above config file.
I tried adding it as an environment variable via the dashboard, but that goes in the aws:elasticbeanstalk:application:environment namespace and does not affect how the application is started up in the first place. (I checked this by using eb config to download the configuration.)
Maybe I could add a section in the file retrieved by eb config, but I heard that doing that will start to override .ebextensions files, and I have several important commands in my .ebextensions files that I need to continue using. (But see the comment below.) It's not clear from the documentation how .ebextensions data translates to and compares with config files used by eb config but the .ebextensions files are well-documented and reasonably convenient so I'd rather not break those if possible!
If I retrieve the configuration on the server via eb config get Live, it contains the following (numerous API keys removed):
EnvironmentConfigurationMetadata:
Description: Includes API keys for live operation
DateModified: '1437734273000'
DateCreated: '1437734273000'
AWSConfigurationTemplateVersion: 1.1.0.0
EnvironmentTier:
Name: WebServer
Type: Standard
SolutionStack: 64bit Amazon Linux 2015.03 v1.4.3 running Python 2.7
OptionSettings:
aws:elb:loadbalancer:
CrossZone: true
aws:elasticbeanstalk:command:
BatchSize: '30'
BatchSizeType: Percentage
aws:autoscaling:launchconfiguration:
IamInstanceProfile: aws-elasticbeanstalk-ec2-role
EC2KeyName: aws-eb
InstanceType: t2.micro
aws:elb:policies:
ConnectionDrainingEnabled: true
aws:autoscaling:updatepolicy:rollingupdate:
RollingUpdateType: Health
RollingUpdateEnabled: true
aws:elasticbeanstalk:application:environment:
DATA_DIR: /opt/python/current/app/data
WSGIPath: /opt/python/current/aws_wsgi.py
aws:elb:healthcheck:
Interval: '30'
(NB. The WSGIPath environment variable there is invalid - but I am unable to remove it from the configuration due to bugs in the AWS Dashboard. It appears to have no effect anyway.)
How do I get AWS to respect my chosen WSGIPath?

How to deploy structured Flask app on AWS elastic beanstalk

After successfully deploying a test app using the steps outlined here:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Python_flask.html
I tried to deploy my actual flask application which has the following structure:
myApp/
runServer.py
requirements.txt
myApp/
__init__.py
helpers.py
clean.sh
static/
myApp.css
handlers/
__init__.py
views.py
templates/
layout.html
viewOne.html
viewTwo.html
Where views.py contains my url mappings.
I have tried initializing the eb instance in the root directory as well as within the myApp module and git aws.push but I get the following error on the AWS dashboard:
ERROR Your WSGIPath refers to a file that does not exist. and the application does not work (404 for any path).
How can I deploy the above Flask application to elastic beanstalk?
I encountered a similar problem deploying a Flask application to EB, with a similar directory structure, and had to do 2 things:
Update my manage.py to create an object of name application, not app
import os
from application import create_app, db
from flask.ext.script import Manager, Shell
application = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(application)
Create .ebextensions/myapp.config, and define the following block to point to manage.py
option_settings:
"aws:elasticbeanstalk:container:python":
WSGIPath: manage.py
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "application/static/"
This let Elastic Beanstalk find the application callable correctly.
This is described briefly at the official docs, and is described in more detail in this blog post
EDIT - see project structure below
ProjectRoot
.ebextensions
application.config
application
main
forms.py
views.py
static
templates
tests
manage.py
requirements.txt
config.py
etc, etc
Add the following to .ebextensions/<env-name>.config:
option_settings:
"aws:elasticbeanstalk:container:python":
WSGIPath: myApp/handlers/views.py
Update:
If you don't have .ebextensions directory, please create one for the project. You can find more information of what can be done regarding the container configuration in Customizing and Configuring AWS Elastic Beanstalk Environments guide.
Your WSGIPath refers to a file that does not exist.
This error appears because Beanstalk, by default, looks for application.py. Check at Beanstalk web UI, Configuration > Software Configuration, WSGIPath is mapped to application.py
Update the WSGIPath as shown in the previous replies or rename to application.py file.
As of awsebcli 3.0, you can actually edit your configuration settings to represent your WSGI path via eb config. The config command will then pull (and open it in your default command line text editor, i.e nano) an editable config based on your current configuration settings. You'll then search for WSGI and update it's path that way. After saving the file and exiting, your WSGI path will be updated automatically.
WSGI configuration was painful for me. I did changed WSCI settings using eb config command but it did not work. Below you can fix this in 5 easy steps.
1- Moved app.py function to the root of the directory (where I runned eb init command.
2- Also renamed app.py as application.py and in that initilized application as application = Flask(__name__) not app = Flask(__name__)
3- eb deploy did not worked after this (in the same project) I tried to fix config by using eb config but it was too hairy to sort it out. Delete all .extensions, .gitignore etc from your project.
4- re initialize your project on EB with eb init and follow the prompts. when deployment is done, eb open would launch your webapp (hopefully!)
When I encountered this problem it was because I was using the GUI to upload a zip of my project files. Initially I was zipping the project level directory and uploading that zip to EB.
Then I switched to simply uploading a zip of the project files themselves-ie select all files and send those to a zip-and then the GUI upload utility was able to find my application.py file without a problem because the application.py file was not in a subfolder.
Well, In my case I followed the entire process and conventions but was still getting 404. The problem was my virtual environment. I was ignoring all environment config related folders/files in my .gitignore but not in .ebignore. After creating .ebignore and ignoring all the folders/files which were not related to project code, fixed the issue.

Multiple settings files with Google App Engine project

A note before the question
We generally have a settings.py file and a local_settings.py file in standard Django project layouts.
settings.py: for production settings
local_settings.py: for local settings that override production settings when running the project locally
The local_settings.py file is added to .gitignore to avoid being pushed into production via git push.
The question
In appengine when we push the application using:
appcfg.py update exampleproject
The local_settings.py file also gets pushed up even though it has been added to .gitignore.
At first adding the following lines to app.yaml looks like a possible solution:
skip_files:
- ^(.*/)?local_settings.py
These lines tell app engine to ignore the local_settings.py file.
But, then we face another problem ->
The local_settings.py file is totally excluded. ie. It is not even used when running the application locally along with dev_appserver.py.
Is there any good solution that can help define multiple settings files in a Google App Engine based Django project?

Using Python Flask, AngularJS, test/production directories and serving requests

Here is the setup:
Using ngBoilerplate (grunt, bower, angular, the works...) to create a SAP application. On my localhost, it launches a NodeJS server so I can test the app. This all works fine minus the database/apis. Using Grunt, it will create a /build folder (which is all the non-minified source,assets, for debugging) and a /bin folder with the production code.
For the backend I have a Python flask app (which I'll use for REST API's) on Heroku. Inside the main Python script:
#app.route("/")
def index():
#index.html has the angular SAP
return make_response( open('build/index.html').read() )
I push the code to Heroku, it detects a Python app (which I believe is good as I will need Python to make my api requests), and it serves the correct index.html. I see Angular making requests to /vendor/angular.js /css/angular.css etc, when those files technically live in /build/vendor/angular.js.
I'm not sure if I'm suppose to tell Angular where to grab the files or if it's Python related.
Am I suppose to change the DOCROOT (WWW) like in LAMP land?
Do I change the routeprovider/urlrouterprovider in Angular to tell it to serve the files to a different location?
Or do I change what I'm doing in Python?
The project directory looks like:
hellworld.py
requirements.txt
runp-heroku.py
procfile
Gruntfile.js
build/ //test code
assets/
index.html
vendor/
bin/ //production code
assets/
index.html
vendor/
src/ //original code
assets/
index.html
vendor/
I never used Heroku, I usually get the Ubuntu server somewhere in the cloud and setup production manually. But the main point is that you production differs from development and you need a distinct config for production. You have two options:
Configure Flask's static_path
Or configure nginx to serve js/css you've built from the right dir

Categories