Skip to content

Commit

Permalink
feat: introduce github actions scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed Mar 31, 2024
1 parent 3059e76 commit d9bd482
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Check

on:
pull_request:
push:
branches:
- master

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@4
with:
python-version: 3.11

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Lint and Format with Ruff
run: |
pip install ruff
ruff check
ruff format
25 changes: 25 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Deploy

on:
push:
branches:
- master

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@4
with:
python-version: 3.11

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# TODO: Set SSH config
# TODO: Acquire known_hosts from each config
# TODO: Execute the `setup.py` file
29 changes: 20 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@
"hibonite.gems.teknologiumum.com": os.getenv("HIBONITE_SUDO_PASSWORD", ""),
}


def ping(host: str) -> bool:
"""
Returns True if host (str) responds to a ping request.
Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
"""

# Option for the number of packets as a function of
param = '-n' if platform.system().lower()=='windows' else '-c'
param = "-n" if platform.system().lower() == "windows" else "-c"

# Building the command. Ex: "ping -c 1 google.com"
command = ['ping', param, '1', host]
command = ["ping", param, "1", host]

return subprocess.call(command) == 0

Expand All @@ -85,27 +86,37 @@ def ping(host: str) -> bool:

for container in containers:
# Copy directory to destination
subprocess.call(f"rsync -avz --progress {container} {server}:{container}".split(" "), stdout=sys.stdout, stderr=sys.stderr)
subprocess.call(
f"rsync -avz --progress {container} {server}:{container}".split(" "),
stdout=sys.stdout,
stderr=sys.stderr,
)

ssh_config = SSHConfig.from_path(f"{Path.home()}/.ssh/config")
server_config = ssh_config.lookup(server)

with SSHClient() as client:
client.load_system_host_keys(f"{Path.home()}/.ssh/known_hosts")
client.connect(hostname=server_config.get("hostname"),
port=server_config.as_int("port"),
username=server_config.get("user"),
key_filename=server_config.get("identityfile"))
client.connect(
hostname=server_config.get("hostname"),
port=server_config.as_int("port"),
username=server_config.get("user"),
key_filename=server_config.get("identityfile"),
)

for container in containers:
print(f"Running commands for {container} on {server}")
_, o1, e1 = client.exec_command(f"echo '{SUDO_PASSWORD[server]}' | sudo -S bash -c 'cd {container}; if [ -f \"setup.sh\" ]; then\n sudo ./setup.sh\nfi'")
_, o1, e1 = client.exec_command(
f"echo '{SUDO_PASSWORD[server]}' | sudo -S bash -c 'cd {container}; if [ -f \"setup.sh\" ]; then\n sudo ./setup.sh\nfi'"
)
for c in iter(lambda: o1.read(1), b""):
sys.stdout.buffer.write(c)
for c in iter(lambda: e1.read(1), b""):
sys.stderr.buffer.write(c)

_, o2, e2 = client.exec_command(f"echo '{SUDO_PASSWORD[server]}' | sudo -S bash -c 'cd {container}; docker compose up -d'")
_, o2, e2 = client.exec_command(
f"echo '{SUDO_PASSWORD[server]}' | sudo -S bash -c 'cd {container}; docker compose up -d'"
)
for c in iter(lambda: o2.read(1), b""):
sys.stdout.buffer.write(c)
for c in iter(lambda: e2.read(1), b""):
Expand Down

0 comments on commit d9bd482

Please sign in to comment.