-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
23 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
42 changes: 42 additions & 0 deletions
42
app/api/rest/minecraft/endpoints/start_minecraft_server.py
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,42 @@ | ||
from fastapi import Depends | ||
from app.db.session import get_db | ||
from app.models.mc_server import MCServer | ||
from app.schemas.mc_server import MCServerInitiateResponse | ||
from sqlalchemy.orm import Session | ||
|
||
import os | ||
from io import StringIO | ||
|
||
import paramiko | ||
|
||
def start_minecraft_server(server_id: int, db: Session = Depends(get_db)) -> MCServerInitiateResponse: | ||
""" | ||
Start a minecraft server by server_id. | ||
""" | ||
server_info = db.query(MCServer).filter(MCServer.id == server_id).first() | ||
|
||
|
||
# Establish SSH connection | ||
client = paramiko.SSHClient() | ||
client.load_system_host_keys() | ||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | ||
|
||
private_key = paramiko.RSAKey(file_obj=StringIO(os.environ.get("SSH_KEY"))) | ||
|
||
# Connect to the server | ||
client.connect( | ||
hostname=server_info.server_ip, | ||
port=server_info.ssh_port, | ||
username=server_info.ssh_username, | ||
pkey=private_key | ||
) | ||
|
||
cmd = server_info.start_cmd | ||
|
||
_, stdout, stderr = client.exec_command(cmd) | ||
|
||
# Close the connection | ||
client.close() | ||
|
||
|
||
return MCServerInitiateResponse(stdout=stdout.read().decode(), stderr=stderr.read().decode()) |
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,42 @@ | ||
from fastapi import Depends | ||
from app.db.session import get_db | ||
from app.models.mc_server import MCServer | ||
from app.schemas.mc_server import MCServerTerminateResponse | ||
from sqlalchemy.orm import Session | ||
|
||
import os | ||
from io import StringIO | ||
|
||
import paramiko | ||
|
||
def stop_minecraft_server(server_id: int, db: Session = Depends(get_db)) -> MCServerTerminateResponse: | ||
""" | ||
Stop a minecraft server by server_id. | ||
""" | ||
server_info = db.query(MCServer).filter(MCServer.id == server_id).first() | ||
|
||
|
||
# Establish SSH connection | ||
client = paramiko.SSHClient() | ||
client.load_system_host_keys() | ||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | ||
|
||
private_key = paramiko.RSAKey(file_obj=StringIO(os.environ.get("SSH_KEY"))) | ||
|
||
# Connect to the server | ||
client.connect( | ||
hostname=server_info.server_ip, | ||
port=server_info.ssh_port, | ||
username=server_info.ssh_username, | ||
pkey=private_key | ||
) | ||
|
||
cmd = server_info.stop_cmd | ||
|
||
_, stdout, stderr = client.exec_command(cmd) | ||
|
||
# Close the connection | ||
client.close() | ||
|
||
|
||
return MCServerTerminateResponse(stdout=stdout.read().decode(), stderr=stderr.read().decode()) |
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
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
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