diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..df3c6c3 --- /dev/null +++ b/.github/workflows/check.yml @@ -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 diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..9724802 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -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 diff --git a/setup.py b/setup.py index 9ff5273..6a1da46 100644 --- a/setup.py +++ b/setup.py @@ -48,6 +48,7 @@ "hibonite.gems.teknologiumum.com": os.getenv("HIBONITE_SUDO_PASSWORD", ""), } + def ping(host: str) -> bool: """ Returns True if host (str) responds to a ping request. @@ -55,10 +56,10 @@ def ping(host: str) -> bool: """ # 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 @@ -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""):