Skip to content

Commit

Permalink
fix: consolidated env vars, deploy usage, command invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Jan 12, 2023
1 parent dc5d0ff commit cf22e44
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v0.10.0 (2023-01-12)

- Consolidates `SRVINFRA_WEBSITES_DIR` and `SRVINFRA_SERVICES_DIR` into `SRVINFRA_SERVICES_DIR`
- srvinfra will now determine if there are prod `docker-compose` files in the directory specified and use those when possible and do a normal `docker compose up` when not
- When deploying a service, you will no longer need to specify `website` or `service` since srvinfra will determine which kind of `docker-compose` files are present and change the underlying command accordingly

## v0.9.1 (2022-11-28)

- Fixes a command that missed the force recreate flag
Expand Down
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# srvinfra

`srvinfra` is a tool to deploy, update, and maintain services and websites on a server hosted by Docker.
`srvinfra` is a tool to deploy, update, and maintain Docker projects hosted on a server.

[![Build](https://github.com/Justintime50/srvinfra/workflows/build/badge.svg)](https://github.com/Justintime50/srvinfra/actions)
[![Version](https://img.shields.io/github/v/tag/justintime50/srvinfra)](https://github.com/justintime50/srvinfra/releases)
Expand All @@ -12,7 +12,9 @@

</div>

I store all my services and websites in the same place and have similar commands to update and deploy them but found myself needing to constantly navigate all over the filesystem to run Docker commands. With `srvinfra`, I can deploy and manage my services and websites with ease using a unified CLI syntax.
I store all my Docker services in the same place and have similar commands to update and deploy them but found myself needing to constantly navigate all over the filesystem to run Docker commands. With `srvinfra`, I can deploy and manage my services with ease from anywhere using a unified CLI syntax.

`srvinfra` will stash any local changes of a project, pull down new changes via `git`, then deploy the service via Docker. For services that have a production configuration, it will need to be titled `docker-compose-prod.yaml` and have a normal `docker-compose.yaml` file as well. Either `.yml` or `.yaml` extensions are supported. When a service does not have a production configuration, `srvinfra` will deploy using the base config.

## Install

Expand All @@ -24,25 +26,22 @@ brew tap justintime50/formulas
brew install srvinfra
```

**NOTE:** `srvinfra` assumes `Docker Compose v2` is active and not `v1`.
**NOTE:** `srvinfra` assumes `Docker Compose v2` is installed.

Once you have `srvinfra` installed, you'll need to setup two environment variables:
Once you have `srvinfra` installed, you'll need to setup an environment variable:

```bash
echo 'export SRVINFRA_SERVICES_DIR=path/to/dir' >> ~/.zshrc
echo 'export SRVINFRA_WEBSITES_DIR=path/to/dir' >> ~/.zshrc
```

## Usage

```bash
# Deploy a service
srvinfra deploy service plex

# Deploy a website
srvinfra deploy website justintime50/justinpaulhammond
# Deploy a service (relative from $SRVINFRA_SERVICES_DIR), subdirectories are possible
srvinfra deploy justintime50/justinpaulhammond
srvinfra deploy justintime50/server-infra/plex

# Deploy all services and websites (great for server cold-start)
# Deploy all services (great for server cold-start)
srvinfra deploy_all

# Decrypt a compressed SQL backup file
Expand Down Expand Up @@ -71,7 +70,7 @@ srvinfra import_encrypted_database DATABASE_CONTAINER_NAME ROOT_PASSWORD DATABAS
srvinfra status justinpaulhammond

# Update a service
srvinfra update plex
srvinfra update justintime50/server-infra/plex

# Update all services
srvinfra update_all
Expand Down
49 changes: 23 additions & 26 deletions src/srvinfra.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Srvinfra is a tool to deploy and update services and websites on a server hosted by Docker
# srvinfra is a tool to deploy and update services on a server hosted by Docker

### Databases

Expand Down Expand Up @@ -70,27 +70,27 @@ import_encrypted_database() {

### Services

# Deploy a service or website depending on context
# Deploy a service
deploy() {
# Parameters
# 1. enum: service | website
# 2. service/website directory path (eg: justintime50/justinpaulhammond)
if [[ "$1" = "service" ]]; then
cd "$SRVINFRA_SERVICES_DIR"/"$2" || exit 1
docker compose -f docker-compose.yml up -d --build --force-recreate --quiet-pull
elif [[ "$1" = "website" ]]; then
cd "$SRVINFRA_WEBSITES_DIR"/"$2" || exit 1
# 1. service directory path (eg: justintime50/justinpaulhammond)
cd "$SRVINFRA_SERVICES_DIR"/"$1" || exit 1
git stash && git pull

if [[ -f "docker-compose-prod.yml" ]]; then
docker compose -f docker-compose.yml -f docker-compose-prod.yml up -d --build --force-recreate --quiet-pull
elif [[ -f "docker-compose-prod.yaml" ]]; then
docker compose -f docker-compose.yaml -f docker-compose-prod.yaml up -d --build --force-recreate --quiet-pull
else
echo "$1 isn't a valid action, try again."
docker compose -d --build --force-recreate --quiet-pull
fi
}

# Deploy all is perfect for a cold-start server deployment
deploy_all() {
echo "You are about to deploy all production services and websites. Press any button to continue."
echo "You are about to deploy all production services. Press any button to continue."
read -r
echo "Deploying all services and websites..."
echo "Deploying all services..."

# Deploy Traefik before other services
cd "$SRVINFRA_SERVICES_DIR/traefik" || exit 0 # don't fail if traefik doesn't exist
Expand All @@ -101,18 +101,15 @@ deploy_all() {
cd "$SRVINFRA_SERVICES_DIR" || exit 1
for DIR in */; do
echo "Deploying $DIR..."
docker compose -f "$DIR"/docker-compose.yml up -d --build --force-recreate --quiet-pull
done

# Deploy websites
cd "$SRVINFRA_WEBSITES_DIR" || exit 1
for TOP_DIR in */; do
cd "$TOP_DIR" || exit 1
for DIR in */; do
echo "Deploying $DIR..."
docker compose -f "$DIR"/docker-compose.yml -f "$DIR"/docker-compose-prod.yml up -d --build --force-recreate --quiet-pull
done
cd .. || exit 1
git stash && git pull

if [[ -f "docker-compose-prod.yml" ]]; then
docker compose -f docker-compose.yml -f docker-compose-prod.yml up -d --build --force-recreate --quiet-pull
elif [[ -f "docker-compose-prod.yaml" ]]; then
docker compose -f docker-compose.yaml -f docker-compose-prod.yaml up -d --build --force-recreate --quiet-pull
else
docker compose -d --build --force-recreate --quiet-pull
fi
done
}

Expand All @@ -127,7 +124,7 @@ update() {
# 1. service name
echo "Updating $1..."
cd "$SRVINFRA_SERVICES_DIR"/"$1" || exit 1
docker compose pull && docker-compose up -d --build --force-recreate --quiet-pull || exit 1
docker compose pull && docker compose up -d --build --force-recreate --quiet-pull || exit 1
echo "$1 updated!"
}

Expand All @@ -137,7 +134,7 @@ update_all() {
cd "$SRVINFRA_SERVICES_DIR" || exit 1
for DIR in */; do
printf '%s\n' "$DIR"
cd "$DIR" && docker compose pull && docker-compose up -d --build --force-recreate --quiet-pull
cd "$DIR" && docker compose pull && docker compose up -d --build --force-recreate --quiet-pull
echo "$DIR updating..."
cd .. || exit 1
done
Expand Down

0 comments on commit cf22e44

Please sign in to comment.