Skip to content

Commit

Permalink
Fmt tools
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Cameron <[email protected]>
  • Loading branch information
JasonLovesDoggo committed Oct 23, 2024
1 parent 428d963 commit 8c27b1e
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 183 deletions.
1 change: 0 additions & 1 deletion automation/Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Please install the following packages:

```bash
Expand Down
70 changes: 35 additions & 35 deletions automation/add_judge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,66 @@

set -e # exit on error
#set -x # print each command before executing it
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi

if [ "$HOSTNAME" != "general" ]
then echo "Please run on general"
if [ "$HOSTNAME" != "general" ]; then
echo "Please run on general"
exit
fi

cd "$(cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )" # cd to the directory of this script
cd "$(
cd "$(dirname "${BASH_SOURCE[0]}")"
pwd -P
)" # cd to the directory of this script

. /home/judge/docker/dmoj/scripts/utils/notify
. ./utils/commands.sh

# check if name was provided
if [ -z "$1" ]
then echo "Please provide a name for the judge"
if [ -z "$1" ]; then
echo "Please provide a name for the judge"
echo "Usage: $0 <judge_name> <droplet_name_to_run_on (optional)> "
exit
fi
JUDGE_NAME=$1

if [ -z "$2" ]
then
# Check if the droplet already exists as a safety measure
if ! doctl compute droplet list --format Name | grep -i "$2"; then
echo "Droplet $2 does not exist!" >&2
exit 1
fi
CONSTRAINT="node.name == $(doctl compute droplet list --format Name | grep -i "$2" | awk '{print $1}')"
if [ -z "$2" ]; then
# Check if the droplet already exists as a safety measure
if ! doctl compute droplet list --format Name | grep -i "$2"; then
echo "Droplet $2 does not exist!" >&2
exit 1
fi
CONSTRAINT="node.name == $(doctl compute droplet list --format Name | grep -i "$2" | awk '{print $1}')"
else
CONSTRAINT="node.role == worker"
CONSTRAINT="node.role == worker"
fi


rand=$(head -c 75 /dev/urandom | tr -d '\0')
# Convert bytes to Base64
JUDGE_AUTH_KEY=$(echo "$rand" | base64 | tr -d '\n' | cut -c -90 ) # db type is varchar(100)
JUDGE_AUTH_KEY=$(echo "$rand" | base64 | tr -d '\n' | cut -c -90) # db type is varchar(100)

notify "Attempting to create new judge $JUDGE_NAME on $2"
# Warning for future devs: THE LINE BELOW IS A PAIN TO WRITE/DEAL WITH. I KNOW IT'S UGLY. I'M SORRY.
run_single_command_on_site "python3 manage.py shell -c 'from judge.models import Judge; Judge.objects.create(name=\"'\"$JUDGE_NAME\"'\", auth_key=r\"'\"$JUDGE_AUTH_KEY\"'\")'"

notify "Judge $JUDGE_NAME's DB obj was created"


docker service create \
--name "judge_${JUDGE_NAME}" \
--env JUDGE_NAME="${JUDGE_NAME}" \
--env AUTH_KEY="${JUDGE_AUTH_KEY}" \
--replicas 1 \
--constraint "$CONSTRAINT" \
--network name=wlmoj_judge \
--cap-add SYS_PTRACE \
-e DMOJ_IN_DOCKER=1 \
--mount type=bind,src=/var/share/problems/,dst=/problems/ \
ghcr.io/mcpt/wlmoj-judge:latest \
run -p 9999 -c /judge.yml \
--skip-self-test \
--api-host=0.0.0.0 \
--api-port=9995 \
"bridged" "$JUDGE_NAME" "$JUDGE_AUTH_KEY"
echo docker service create \
--name "judge_${JUDGE_NAME}" \
--env JUDGE_NAME="${JUDGE_NAME}" \
--env AUTH_KEY="${JUDGE_AUTH_KEY}" \
--replicas 1 \
--constraint "$CONSTRAINT" \
--network name=wlmoj_judge \
--cap-add SYS_PTRACE \
-e DMOJ_IN_DOCKER=1 \
--mount type=bind,src=/var/share/problems/,dst=/problems/ \
ghcr.io/mcpt/wlmoj-judge:latest \
run -p 9999 -c /judge.yml \
--skip-self-test \
--api-host=0.0.0.0 \
--api-port=9995 \
"bridged" "$JUDGE_NAME" "$JUDGE_AUTH_KEY"
60 changes: 29 additions & 31 deletions automation/new_worker.sh
Original file line number Diff line number Diff line change
@@ -1,79 +1,77 @@
#!/usr/bin/env bash
set -e # Exit on error
cd "$(cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )"
set -e # Exit on error
cd "$(
cd "$(dirname "${BASH_SOURCE[0]}")"
pwd -P
)"

[ "$(whoami)" = root ] || { sudo "$0" "$@"; exit $?; } # enforce root as the ansible ssh key is installed under root
[ "$(whoami)" = root ] || {
sudo "$0" "$@"
exit $?
} # enforce root as the ansible ssh key is installed under root

# grab a argument of --size
if [[ $* == *--large* ]]
then
SIZE="s-2vcpu-2gb"
if [[ $* == *--large* ]]; then
SIZE="s-2vcpu-2gb"
else
SIZE="s-1vcpu-1gb"
SIZE="s-1vcpu-1gb"
fi


. ../dmoj/scripts/utils/notify
. ./swarm_info

if [ -z "$1" ]; then
echo "Usage: $0 <droplet_name> [--large]"
exit 1
echo "Usage: $0 <droplet_name> [--large]"
exit 1
fi

# Check if the droplet already exists as a safety measure
if doctl compute droplet list --format Name | grep -i "$1"; then
echo "Droplet $1 already exists!" >&2
exit 1
echo "Droplet $1 already exists!" >&2
exit 1
fi


SSH_KEYS=$(doctl compute ssh-key list --no-header --format ID)

echo "Provisioning new droplet $1..."
echo "SSH keys: $(doctl compute ssh-key list --no-header --format Name | tr '\n' ', ')"
echo "Creating droplet..."
notify "## Provisioning new droplet **$1**"


# Create the droplet
droplet_id=$(doctl compute droplet create \
--image ubuntu-22-04-x64 \
--size "$SIZE" \
--region tor1 \
--enable-monitoring \
--tag-names "worker" \
--ssh-keys "$SSH_KEYS" \
"$1" --output json | jq -r '.[0].id')


--image ubuntu-22-04-x64 \
--size "$SIZE" \
--region tor1 \
--enable-monitoring \
--tag-names "worker" \
--ssh-keys "$SSH_KEYS" \
"$1" --output json | jq -r '.[0].id')

# Wait for the droplet to be active
echo "Waiting for droplet to be active..."
while true; do
status=$(doctl compute droplet get "$droplet_id" --output json | jq -r '.[0].status')
if [ "$status" = "active" ]; then
break
fi
sleep 2
status=$(doctl compute droplet get "$droplet_id" --output json | jq -r '.[0].status')
if [ "$status" = "active" ]; then
break
fi
sleep 2
done

# Get the public and private IP addresses, guaranteed to be at index zero as we limit names
droplet_pub_ipv4=$(doctl compute droplet get "$droplet_id" --output json | jq -r '.[0].networks.v4[] | select(.type == "public") | .ip_address')
droplet_priv_ipv4=$(doctl compute droplet get "$droplet_id" --output json | jq -r '.[0].networks.v4[] | select(.type == "private") | .ip_address')


echo "Droplet is active!, public IP: $droplet_pub_ipv4, private IP: $droplet_priv_ipv4"
notify "> Droplet **$1** is active at __ $droplet_pub_ipv4 __"
# Wait for SSH to become available
echo "Waiting for SSH to be available..."
while ! ssh -o StrictHostKeyChecking=no -q root@"$droplet_priv_ipv4" exit; do
sleep 5
sleep 5
done

echo "SSH is available!"


# Run the Ansible playbook once SSH is up
echo "Running Ansible playbook..."
update_inventory
Expand Down
2 changes: 1 addition & 1 deletion automation/playbooks/initialize_worker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
module: ansible.builtin.shell
cmd: "docker swarm join-token worker -q"
register: join_token
# this works docker swarm join --token TOKLENNNNNNNNN 10.137.0.5:2377
# this works docker swarm join --token TOKLENNNNNNNNN 10.137.0.5:2377
- name: 'Join the Swarm as a worker'
run_once: true
community.docker.docker_swarm:
Expand Down
37 changes: 26 additions & 11 deletions automation/playbooks/roles/jsoncameron.docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ docker_packages:
docker_packages_state: present
```
The `docker_edition` should be either `ce` (Community Edition) or `ee` (Enterprise Edition).
You can also specify a specific version of Docker to install using the distribution-specific format:
The `docker_edition` should be either `ce` (Community Edition) or `ee` (Enterprise Edition).
You can also specify a specific version of Docker to install using the distribution-specific format:
Red Hat/CentOS: `docker-{{ docker_edition }}-<VERSION>` (Note: you have to add this to all packages);
Debian/Ubuntu: `docker-{{ docker_edition }}=<VERSION>` (Note: you have to add this to all packages).

You can control whether the package is installed, uninstalled, or at the latest version by setting `docker_packages_state` to `present`, `absent`, or `latest`, respectively. Note that the Docker daemon will be automatically restarted if the Docker package is updated. This is a side effect of flushing all handlers (running any of the handlers that have been notified by this and any other role up to this point in the play).
You can control whether the package is installed, uninstalled, or at the latest version by setting
`docker_packages_state` to `present`, `absent`, or `latest`, respectively. Note that the Docker daemon will be
automatically restarted if the Docker package is updated. This is a side effect of flushing all handlers (running any of
the handlers that have been notified by this and any other role up to this point in the play).

```yaml
docker_obsolete_packages:
Expand All @@ -39,7 +42,9 @@ docker_obsolete_packages:
- runc
```

A list of packages to be uninstalled prior to running this role. See [Docker's installation instructions](https://docs.docker.com/engine/install/debian/#uninstall-old-versions) for an up-to-date list of old packages that should be removed.
A list of packages to be uninstalled prior to running this role.
See [Docker's installation instructions](https://docs.docker.com/engine/install/debian/#uninstall-old-versions) for an
up-to-date list of old packages that should be removed.

```yaml
docker_service_manage: true
Expand All @@ -48,15 +53,17 @@ docker_service_enabled: true
docker_restart_handler_state: restarted
```

Variables to control the state of the `docker` service, and whether it should start on boot. If you're installing Docker inside a Docker container without systemd or sysvinit, you should set `docker_service_manage` to `false`.
Variables to control the state of the `docker` service, and whether it should start on boot. If you're installing Docker
inside a Docker container without systemd or sysvinit, you should set `docker_service_manage` to `false`.

```yaml
docker_install_compose_plugin: false
docker_compose_package: docker-compose-plugin
docker_compose_package_state: present
```

Docker Compose Plugin installation options. These differ from the below in that docker-compose is installed as a docker plugin (and used with `docker compose`) instead of a standalone binary.
Docker Compose Plugin installation options. These differ from the below in that docker-compose is installed as a docker
plugin (and used with `docker compose`) instead of a standalone binary.

```yaml
docker_install_compose: true
Expand All @@ -71,7 +78,8 @@ Docker Compose installation options.
docker_add_repo: true
```

Controls whether this role will add the official Docker repository. Set to `false` if you want to use the default docker packages for your system or manage the package repository on your own.
Controls whether this role will add the official Docker repository. Set to `false` if you want to use the default docker
packages for your system or manage the package repository on your own.

```yaml
docker_repo_url: https://download.docker.com/linux
Expand All @@ -91,7 +99,10 @@ docker_apt_filename: "docker"
(Used only for Debian/Ubuntu.) You can switch the channel to `nightly` if you want to use the Nightly release.

You can change `docker_apt_gpg_key` to a different url if you are behind a firewall or provide a trustworthy mirror.
Usually in combination with changing `docker_apt_repository` as well. `docker_apt_filename` controls the name of the source list file created in `sources.list.d`. If you are upgrading from an older (<7.0.0) version of this role, you should change this to the name of the existing file (e.g. `download_docker_com_linux_debian` on Debian) to avoid conflicting lists.
Usually in combination with changing `docker_apt_repository` as well. `docker_apt_filename` controls the name of the
source list file created in `sources.list.d`. If you are upgrading from an older (<7.0.0) version of this role, you
should change this to the name of the existing file (e.g. `download_docker_com_linux_debian` on Debian) to avoid
conflicting lists.

```yaml
docker_yum_repo_url: "{{ docker_repo_url }}/{{ (ansible_distribution == 'Fedora') | ternary('fedora','centos') }}/docker-{{ docker_edition }}.repo"
Expand Down Expand Up @@ -124,7 +135,9 @@ Custom `dockerd` options can be configured through this dictionary representing

## Use with Ansible (and `docker` Python library)

Many users of this role wish to also use Ansible to then _build_ Docker images and manage Docker containers on the server where Docker is installed. In this case, you can easily add in the `docker` Python library using the `geerlingguy.pip` role:
Many users of this role wish to also use Ansible to then _build_ Docker images and manage Docker containers on the
server where Docker is installed. In this case, you can easily add in the `docker` Python library using the
`geerlingguy.pip` role:

```yaml
- hosts: all
Expand Down Expand Up @@ -158,8 +171,10 @@ MIT / BSD

* [We Manage](https://we-manage.de): Helping start-ups and grown-ups scaling their infrastructure in a sustainable way.

The above sponsor(s) are supporting Jeff Geerling on [GitHub Sponsors](https://github.com/sponsors/geerlingguy). You can sponsor Jeff's work too, to help him continue improving these Ansible open source projects!
The above sponsor(s) are supporting Jeff Geerling on [GitHub Sponsors](https://github.com/sponsors/geerlingguy). You can
sponsor Jeff's work too, to help him continue improving these Ansible open source projects!

## Author Information

This role was created in 2017 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/).
This role was created in 2017 by [Jeff Geerling](https://www.jeffgeerling.com/), author
of [Ansible for DevOps](https://www.ansiblefordevops.com/).
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ docker_yum_repo_enable_test: '0'
docker_yum_gpg_key: "{{ docker_repo_url }}/centos/gpg"

# A list of users who will be added to the docker group.
docker_users: []
docker_users: [ ]

# Docker daemon options as a dict
docker_daemon_options: {}
docker_daemon_options: { }
14 changes: 7 additions & 7 deletions automation/playbooks/update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
- name: Remove outdated docker artifacts
ansible.builtin.include_tasks: "tasks/cleanup.yml"

# - name: Update docker stack
# docker_stack:
# state: present
# name: wlmoj
# compose:
# - /home/judge/docker/dmoj/docker-stack.yml
# when: ansible_hostname == "general"
# - name: Update docker stack
# docker_stack:
# state: present
# name: wlmoj
# compose:
# - /home/judge/docker/dmoj/docker-stack.yml
# when: ansible_hostname == "general"

- name: Update packages
apt:
Expand Down
Loading

0 comments on commit 8c27b1e

Please sign in to comment.