-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from Softawii/feat/portainer
chore: rewrite deploy to use portainer instead of podman
- Loading branch information
Showing
5 changed files
with
155 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import requests | ||
import os | ||
|
||
host = os.environ['PORTAINER_HOST'] | ||
endpointId = os.environ['PORTAINER_ENDPOINT_ID'] | ||
stack_name = 'capivara' | ||
stack_file_path = './docker-compose.yaml' | ||
access_token = os.environ['PORTAINER_ACCESS_TOKEN'] | ||
|
||
def get_envs(): | ||
env_keys = [ | ||
'CAPIVARA_IMAGE_TAG', | ||
'DISCORD_TOKEN', | ||
'LOG_CHANNEL_ID', | ||
'CURUPIRA_RESET', | ||
'DATABASE_DRIVER', | ||
'DATABASE_DIALECT', | ||
'DATABASE_URL', | ||
'DATABASE_USERNAME', | ||
'DATABASE_PASSWORD', | ||
'JAVA_ARGS', | ||
] | ||
|
||
return [{'name': key, 'value': os.environ[key]} for key in env_keys] | ||
|
||
def get_stack_id(stack_name): | ||
get_stack_res = requests.get( | ||
f'{host}/api/stacks', | ||
headers={'X-API-KEY': access_token}, | ||
verify=False | ||
) | ||
if get_stack_res.ok: | ||
payload = get_stack_res.json() | ||
for stack in payload: | ||
if stack['Name'] == stack_name: | ||
return stack['Id'] | ||
|
||
raise Exception('Stack not found') | ||
else: | ||
raise Exception('Failed to get stack id') | ||
|
||
def setup_stack(): | ||
envs = get_envs() | ||
with open(stack_file_path) as f: | ||
file_content = f.read() | ||
|
||
create_stack_res = requests.post( | ||
f'{host}/api/stacks/create/standalone/string?endpointId={endpointId}', | ||
json={ | ||
"name": stack_name, | ||
"stackFileContent": file_content, | ||
"env": envs | ||
}, | ||
headers={'X-API-KEY': access_token}, | ||
verify=False | ||
) | ||
if create_stack_res.ok: | ||
print('Created stack with success') | ||
return | ||
|
||
if create_stack_res.status_code == 401: | ||
raise Exception('Unauthorized') | ||
|
||
if create_stack_res.status_code == 409: | ||
payload = create_stack_res.json() | ||
if payload['message'] == f'A stack with the normalized name \'{stack_name}\' already exists': | ||
update_stack(file_content, envs) | ||
return | ||
|
||
raise Exception('Unable to create and then update stack') | ||
|
||
def update_stack(file_content, envs): | ||
stack_id = get_stack_id(stack_name) | ||
update_stack_res = requests.put( | ||
f'{host}/api/stacks/{stack_id}?endpointId={endpointId}', | ||
json={ | ||
"stackFileContent": file_content, | ||
"env": envs | ||
}, | ||
headers={'X-API-KEY': access_token}, | ||
verify=False | ||
) | ||
|
||
if update_stack_res.ok: | ||
print('Updated stack with success') | ||
return | ||
|
||
if update_stack_res.status_code == 401: | ||
raise Exception('Unauthorized') | ||
|
||
raise Exception('Unable to update stack', update_stack_res.status_code) | ||
|
||
if __name__ == '__main__': | ||
setup_stack() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
services: | ||
capivarabot: | ||
image: docker.io/eduardoferro/capivara:${CAPIVARA_IMAGE_TAG} | ||
restart: unless-stopped | ||
environment: | ||
- JAVA_ARGS=${JAVA_ARGS} | ||
- LOG_CHANNEL_ID=${LOG_CHANNEL_ID} | ||
- DISCORD_TOKEN=${DISCORD_TOKEN} | ||
- CURUPIRA_RESET=${CURUPIRA_RESET} | ||
- DATABASE_DRIVER=${DATABASE_DRIVER} | ||
- DATABASE_DIALECT=${DATABASE_DIALECT} | ||
- DATABASE_URL=${DATABASE_URL} | ||
- DATABASE_USERNAME=${DATABASE_USERNAME} | ||
- DATABASE_PASSWORD=${DATABASE_PASSWORD} | ||
networks: | ||
- metrics_metrics | ||
- database_database | ||
deploy: | ||
resources: | ||
limits: | ||
cpus: '0.5' | ||
memory: 400M | ||
expose: | ||
- 8080 | ||
|
||
networks: | ||
database_database: | ||
external: true | ||
metrics_metrics: | ||
external: true |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,13 @@ jobs: | |
- name: 'Check out repository' | ||
uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: '3.12' | ||
|
||
- name: 'Install Python requests' | ||
run: pip install requests | ||
|
||
- name: Log in to Docker Hub | ||
uses: docker/[email protected] | ||
with: | ||
|
@@ -32,41 +39,41 @@ jobs: | |
push: ${{ github.event_name != 'pull_request' }} | ||
tags: eduardoferro/capivara:${{ github.sha }},${{ github.event_name == 'release' && format('eduardoferro/capivara:latest,eduardoferro/capivara:{0}', github.ref_name) || '' }} | ||
|
||
- name: 'Wait for SSH' | ||
- name: 'Wait for host' | ||
run: | | ||
SLEEP=1 | ||
TRIES=0 | ||
MAX_TRIES=10 | ||
while ! nc -w5 -z $INSTANCE_IP 22; do | ||
while ! nc -w5 -z $INSTANCE_IP 9443; do | ||
echo "SSH not available..." | ||
if [[ $TRIES -eq $MAX_TRIES ]]; then | ||
echo "Max tries reached, exiting" | ||
exit 1 | ||
fi | ||
((TRIES += 1)) | ||
sleep $SLEEP | ||
done; echo "SSH ready!" | ||
done; echo "Host ready!" | ||
env: | ||
INSTANCE_IP: ${{ secrets.INSTANCE_IP }} | ||
|
||
- name: 'Push start-container.sh' | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.INSTANCE_IP }} | ||
username: ${{ secrets.SSH_USERNAME }} | ||
passphrase: ${{ secrets.VM_SSH_PRIVATE_KEY_PASSPHRASE }} | ||
key: ${{ secrets.VM_SSH_PRIVATE_KEY }} | ||
source: "./start-container.sh" | ||
target: /home/${{ secrets.SSH_USERNAME }}/capivara/ | ||
strip_components: 1 | ||
|
||
- name: 'Start BOT' | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.INSTANCE_IP }} | ||
username: ${{ secrets.SSH_USERNAME }} | ||
passphrase: ${{ secrets.VM_SSH_PRIVATE_KEY_PASSPHRASE }} | ||
key: ${{ secrets.VM_SSH_PRIVATE_KEY }} | ||
script: | | ||
cd /home/${{ secrets.SSH_USERNAME }}/capivara | ||
DISCORD_TOKEN='${{ secrets.DISCORD_BOT_TOKEN }}' LOG_CHANNEL_ID='${{ secrets.DISCORD_LOG_CHANNEL_ID }}' CURUPIRA_RESET='true' DATABASE_DRIVER='org.postgresql.Driver' DATABASE_DIALECT='org.hibernate.dialect.PostgreSQL95Dialect' DATABASE_URL='jdbc:postgresql://capivara_database:5432/capivara' DATABASE_USERNAME='${{ secrets.DATABASE_USERNAME }}' DATABASE_PASSWORD='${{ secrets.DATABASE_PASSWORD }}' JAVA_ARGS='-Xmx350M' /bin/bash start-container.sh docker.io/eduardoferro/capivara:${{ github.sha }} | ||
- name: 'Deploy to Portainer' | ||
working-directory: ./.ci | ||
run: | | ||
python3 ./deploy.py | ||
env: | ||
# Portainer | ||
PORTAINER_HOST: ${{ secrets.PORTAINER_HOST }} | ||
PORTAINER_ENDPOINT_ID: ${{ secrets.PORTAINER_ENDPOINT_ID }} | ||
PORTAINER_ACCESS_TOKEN: ${{ secrets.PORTAINER_ACCESS_TOKEN }} | ||
# Image | ||
CAPIVARA_IMAGE_TAG: ${{ github.sha }} | ||
# Bot | ||
DISCORD_TOKEN: ${{ secrets.DISCORD_BOT_TOKEN }} | ||
LOG_CHANNEL_ID: ${{ secrets.DISCORD_LOG_CHANNEL_ID }} | ||
CURUPIRA_RESET: 'true' | ||
DATABASE_DRIVER: 'org.postgresql.Driver' | ||
DATABASE_DIALECT: 'org.hibernate.dialect.PostgreSQL95Dialect' | ||
DATABASE_URL: 'jdbc:postgresql://postgres:5432/capivara' | ||
DATABASE_USERNAME: ${{ secrets.DATABASE_USERNAME }} | ||
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }} | ||
JAVA_ARGS: '-Xmx350M' |
This file was deleted.
Oops, something went wrong.