Stack Template: Python
If you are using Docker or Kubernetes you can check specific instructions for these at the bottom of this page.
Building and running Python based apps is quite straightforward in Squash. There are basically three elements to accomplish this:
- Pick the base OS image from Docker Hub, this is usually a flavor of a major distribution (Ubuntu, CentOS, etc) or a basic image tailored for your specific programming language. You may also use an image from your own private registry.
- Install & build the necessary libraries/packages to run your app.
- Run your app and services used by your app (database, caching, etc).
This is all done in a Squash YAML file.
Example #1: basic app, no database
This is a basic
.squash.yml file for a Django application that doesn’t require installing any additional services:
deployments: MyWebApp: dockerimage: ubuntu:14.04 build_steps: - apt-get update && apt-get install -y libssl-dev \ libpq-dev git \ python python-dev python-pip python-virtualenv - pip install setuptools pip - pip install ~/code launch_steps: - cd ~/code/mysite - python manage.py runserver 0.0.0.0:80
- A Squash YAML needs to start with “deployments:”
- “MyWebApp”: you can change this and use the name of your application. If you have multiple apps in one repo you can define multiple Apps using this same format.
- Important: each application runs on its own unique virtual machine (with flexible sizes and storage) in Squash. However, you can define as many apps as needed within the same Squash YAML file and even create a chain of dependencies between apps or services. See the 3rd example below for more details.
- Squash also supports apps that are made of several repos (multi-repository apps).
- dockerimage: this is where you define the base OS image.
- Squash automatically downloads your branch’s code into
/home/test-instance/code, thus we are using the base path
~/codeto build the app.
- build_steps: install any required packages here. In the example above we are using “apt” to download and install packages. This is because the base OS image is Ubuntu. You would use different package managers depending on the OS image of your choice.
- launch_steps: This is where you run any required services and your app. For this specific example the app is listening on port 80, which is the default port used by Squash for routing, so no additional routing information is needed.
Example #2: app + database + port_forwarding
Here we are installing the necessary packages to build the database service (PostgreSQL). Then the database service is launched inside of “launch_steps”. Need to load test data into this database? please check: how to import data into a database.
deployments: MyWebApp: dockerimage: ubuntu:16.04 build_steps: - apt-get update && apt-get install -y libssl-dev \ libpq-dev git \ python python-dev python-pip python-virtualenv \ locales gcc postgresql postgresql-contrib - pip install setuptools pip - pip install ~/code launch_steps: - service postgresql start - cd ~/code/mysite - python manage.py runserver 0.0.0.0:8080 port_forwarding: 80:8080
Example #3: using a separate VM for the database
In the example below we first define the database service itself, note that it has its own separate app at the top, named “DatabaseService”. Then we define the web app (InventoryApp), note the depends_on field. This will tell Squash to make sure the Database service is available before starting the web app.
This feature allows you to also share the same database VM with multiple feature branches as needed. You can either tell Squash to always spin up a new database VM for each feature branch or just use a shared model. For more information please check the deployment dependencies page.
deployments: DatabaseService: dockerimage: ubuntu:16.04 build_steps: - DEBIAN_FRONTEND=noninteractive apt-get update - DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common - DEBIAN_FRONTEND=noninteractive apt-get install -y locales gcc postgresql postgresql-contrib sudo supervisor launch_steps: - service postgresql start # Squash will ping/telnet this port number as a # way to ensure the service is up and running check_service_ports: 5432 port_forwarding: 5432:5432 InventoryApp: dockerimage: ubuntu:16.04 build_steps: - apt-get update && apt-get install -y libssl-dev \ libpq-dev git \ python python-dev python-pip python-virtualenv - pip install setuptools pip - pip install ~/code launch_steps: - cd ~/code/mysite - python manage.py runserver 0.0.0.0:80 depends_on: - DATABASE_SERVICE: app: myrepo/master:DatabaseService