This guide will help you get started with standing up a sample appcli application.
- python
- docker
- docker-compose
Run the following commands to create the required files and folders
mkdir -p src/resources/templates/{baseline,configurable}
touch src/resources/settings.yml
touch src/resources/stack-settings.ymltouch src/myapp.py
chmod +x src/myapp.py
cat <<EOF >src/myapp.py
#!/usr/bin/env python3
# # -*- coding: utf-8 -*-
# Standard libraries.
import sys
from pathlib import Path
# Vendor libraries.
from appcli.cli_builder import create_cli
from appcli.models.configuration import Configuration
from appcli.orchestrators import DockerComposeOrchestrator
# ------------------------------------------------------------------------------
# CONSTANTS
# ------------------------------------------------------------------------------
# directory containing this script
BASE_DIR = Path(__file__).parent
# ------------------------------------------------------------------------------
# PRIVATE METHODS
# ------------------------------------------------------------------------------
def main():
configuration = Configuration(
app_name='myapp',
docker_image='brightsparklabs/myapp',
seed_app_configuration_file=BASE_DIR / 'resources/settings.yml',
application_context_files_dir=BASE_DIR / 'resources/templates/appcli/context',
stack_configuration_file=BASE_DIR / 'resources/stack-settings.yml',
baseline_templates_dir=BASE_DIR / 'resources/templates/baseline',
configurable_templates_dir=BASE_DIR / 'resources/templates/configurable',
orchestrator=DockerComposeOrchestrator(
# NOTE: These paths are relative to 'resources/templates/baseline'.
docker_compose_file = Path('docker-compose.yml')
),
)
cli = create_cli(configuration)
cli()
# ------------------------------------------------------------------------------
# ENTRYPOINT
# ------------------------------------------------------------------------------
if __name__ == '__main__':
main()
EOFcat <<EOF >Dockerfile
FROM brightsparklabs/appcli-docker-compose:<version>
ENTRYPOINT ["./myapp.py"]
WORKDIR /app
COPY requirements.txt .
RUN pip install --requirement requirements.txt
COPY src .
ARG APP_VERSION=latest
ENV APP_VERSION=\${APP_VERSION}
EOFWe can see in the Dockerfile, it is referring to a requirements.txt file which we will need to
create:
APPCLI_VERSION=<version>
echo "bsl-appcli==${APPCLI_VERSION}" >> requirements.txtcat <<EOF >src/resources/templates/baseline/docker-compose.yml
version: '3'
services:
echo-server:
image: ealen/echo-server:0.5.1
EOFThe above uses Echo-Server as our example application.
Before we build our application please make sure your directory reflects the following structure:
├── src
│ ├── resources
│ │ ├── settings.yml
│ │ ├── stack-settings.yml
│ │ └── templates
│ │ ├── baseline
│ │ │ └── docker-compose.yml
│ │ └── configurable
│ └── myapp.py
├── requirements.txt
└── Dockerfile
docker build -t brightsparklabs/myapp:latest --build-arg APP_VERSION=latest .
While it is not mandatory to view the script before running, it is highly recommended.
docker run --rm brightsparklabs/myapp:latest installOnce you can successfully see the script printed on the command line, you are ready to install
docker run --rm brightsparklabs/myapp:latest install | sudo bashBy default, appcli will auto init and auto apply itself. It is possible to manually perform
these steps by disabling auto_configure in either the my-app.py script, or when generating
the install script:
# Disable auto_configure in the `myapp.py` script.
configuration = Configuration(
app_name="myapp",
auto_configure_on_install=False,
)# Disable auto_configure in the `install` script.
docker run --rm brightsparklabs/myapp:latest install | sudo bash --auto-configure=false
# Manually apply the connfigurations.
/opt/brightsparklabs/myapp/production/myapp configure init
/opt/brightsparklabs/myapp/production/myapp configure applyIf you type in service, it will list all the commands you can use to control the services. The main
ones are service start and service stop:
/opt/brightsparklabs/myapp/production/myapp service startYou can check to see if your service is still running by typing in the following:
docker psMore commands can be found in the appcli README.
Instead of fetching appcli from the PIP repository, you can import appcli from the local filesystem into your project (this can help during development).
First, clone the appcli repository into the same directory as the requirements.txt file:
git clone git@github.com:brightsparklabs/appcli.gitChange the requirements.txt file to reference the local appcli repository:
-e . /appcli
Finally, copy the appcli repo to the docker image (in the Dockerfile):
COPY appcli .