Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions example/daemon.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
from mcsmapi import MCSMAPI
from mcsmapi.models.daemon import DaemonConfig

mcsm = MCSMAPI("http://localhost:23333")

mcsm.login("admin", "547cABC9bf88@")

# mcsm.login_with_apikey("apikey")

daemon_object = mcsm.daemon()
daemon_object = mcsm.daemon

# show Daemon list

print(daemon_object.config())

# 创建节点
daemonId = daemon_object.add(
{
"ip": "localhost",
"port": 24444,
"prefix": "",
"remarks": "Unnamed Node",
"available": True,
}
DaemonConfig(
ip="localhost",
port=24444,
prefix="",
remarks="Unnamed Node",
)
)
# 删除节点
daemon_object.delete(daemonId)
4 changes: 2 additions & 2 deletions example/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

api.login("admin", "547cABC9bf88@")

instance_object = api.instance()
instance_object = api.instance

instance_list = instance_object.search("xxx")

Expand Down Expand Up @@ -37,7 +37,7 @@
f.rename("new_name")

# copy file
f.copy("new_path")
f.copy_to("new_path")
# move file
f.move("new_path")

Expand Down
16 changes: 9 additions & 7 deletions example/overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# mcsm.login_with_apikey("apikey")

# Get dashboard data
overview = mcsm.overview()
overview = mcsm.overview
overview_data = overview.overview()

mcsm_version = overview_data.version
Expand All @@ -19,12 +19,14 @@
print(remote.ip)
print(remote.port)
print(remote.prefix)
print(remote.available)
print(remote.version)
print(remote.process.cpu)
print(remote.system.freemem)
print(remote.system.hostname)
print(remote.system.loadavg)
if remote.available:
print(remote.version)
print(remote.process.cpu) # type: ignore
print(remote.system.freemem) # type: ignore
print(remote.system.hostname) # type: ignore
print(remote.system.loadavg) # type: ignore
else:
print("Not available")


remote = remotes[0]
Expand Down
14 changes: 6 additions & 8 deletions example/use_case/get_all_instance_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
# Get all instance info
def get_all_instance_info(daemon_id: str):
# Get all instance info from the daemon
instance_object = mcsm.instance()
instance_object = mcsm.instance
instance_list = instance_object.search(daemonId=daemon_id).data

# Create a dictionary to store instance info
instance_dict = {}
for instance in instance_list:
instance_dict[instance.instanceUuid] = {

return {
instance.instanceUuid: {
"name": instance.config.nickname,
"status": instance.status,
"daemonId": instance.daemonId,
"uuid": instance.instanceUuid,
}
return instance_dict
for instance in instance_list
}

# Example usage
daemon_id = "your_daemon_id" # Please change to your MCSM panel daemon id.
Expand All @@ -41,4 +40,3 @@ def get_all_instance_info(daemon_id: str):
# Optional: Save info to a file in the current directory
with open("instance_info.json", "w") as f:
content = json.dump(instance_info, f, indent=4)
f.write(content)
12 changes: 5 additions & 7 deletions example/use_case/search_instance_by_name.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from mcsmapi import MCSMAPI
import json

baseurl = "http://localhost:23333" # Change to your MCSM panel address and port(include http or https etc.).

Expand All @@ -17,7 +16,7 @@
# Get instance
def get_instance(daemon_id: str, instance_name: str):
# Get specific instance info from the daemon by name
instance_object = mcsm.instance()
instance_object = mcsm.instance
instance_list = instance_object.search(daemonId=daemon_id, instance_name=instance_name).data

# Error handling
Expand All @@ -41,17 +40,16 @@ def get_instance(daemon_id: str, instance_name: str):
# Print instance status
print(instance.config.nickname)
print(instance.status)
print(instance.space)

# Optional:
# start
#instance.start()
instance.start()

# stop
#instance.stop()
instance.stop()

# restart
#instance.restart()
instance.restart()

# kill
#instance.kill()
instance.kill()
4 changes: 2 additions & 2 deletions example/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

api.login("admin", "547cABC9bf88@")

userManager = api.user()
userManager = api.user

users = userManager.search()

Expand All @@ -13,7 +13,7 @@
print(user.userName)

# 查找第一个管理员账号
user = api.user().search(role=10).data[0]
user = userManager.search(role=10).data[0]

# 封禁管理员账号
user.update({"permission": -1})
Expand Down
6 changes: 6 additions & 0 deletions mcsmapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,26 @@ def login_with_apikey(self, apikey: str):
self.authentication = "apikey"
return self

@property
def overview(self):
return Overview()

@property
def instance(self):
return Instance()

@property
def user(self) :
return User()

@property
def daemon(self):
return Daemon()

@property
def file(self):
return File()

@property
def image(self):
return Image()
6 changes: 3 additions & 3 deletions mcsmapi/apis/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ def system() -> list[DaemonSystemInfo]:
return [DaemonSystemInfo(**daemon) for daemon in daemons]

@staticmethod
def add(config: dict[str, Any]) -> str:
def add(config: DaemonConfig) -> str:
"""
新增一个节点

:params config: 节点的配置信息,以字典形式提供,缺失内容由DaemonConfig模型补全
:params config: 节点的配置信息

:returns: 新增节点的UUID
"""
return send(
"POST",
f"{ApiPool.SERVICE}/remote_service",
data=DaemonConfig(**config).model_dump(),
data=config.model_dump(),
)

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions mcsmapi/apis/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async def upload(daemonId: str, uuid: str, file: bytes, upload_dir: str) -> bool
return True

@staticmethod
def copy(daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
def copy_to(daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
"""
复制多个文件夹或文件到指定位置

Expand Down Expand Up @@ -156,7 +156,7 @@ def copyOne(daemonId: str, uuid: str, source: str, target: str) -> bool:

:returns: 操作成功后返回True
"""
return File.copy(daemonId, uuid, {source: target})
return File.copy_to(daemonId, uuid, {source: target})

@staticmethod
def move(daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
Expand Down
71 changes: 71 additions & 0 deletions mcsmapi/apis/schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from mcsmapi.pool import ApiPool
from mcsmapi.request import send
from mcsmapi.models.schedule import ScheduleDetail, SchedulePostBody


class Schedule:

@staticmethod
def list(daemonId: str, uuid: str) -> list[ScheduleDetail]:
"""
获取实例计划任务列表

:param daemonId: 节点ID
:param uuid: 实例ID

:returns: 计划任务列表
"""
result = send(
"GET",
f"{ApiPool.SCHEDULE}",
params={"daemonId": daemonId, "uuid": uuid},
)
return [ScheduleDetail(**r, daemonId=daemonId) for r in result]

@staticmethod
def delete(daemonId: str, uuid: str, task_name: str) -> bool:
"""
删除计划任务

:param daemonId: 节点ID
:param uuid: 实例ID
:param task_name: 计划任务名称

:returns: 是否成功
"""
return send(
"DELETE",
f"{ApiPool.SCHEDULE}",
params={"daemonId": daemonId, "uuid": uuid, "task_name": task_name},
)

@staticmethod
def create(daemonId: str, uuid: str, config: SchedulePostBody) -> bool:
"""
创建计划任务

:param daemonId: 节点ID
:param uuid: 实例ID
:param config: 计划任务配置

:returns: 是否成功
"""
return send(
"POST",
f"{ApiPool.SCHEDULE}",
params={"daemonId": daemonId, "uuid": uuid},
data=config.model_dump(),
)

@staticmethod
def update(daemonId: str, uuid: str, config: SchedulePostBody) -> bool:
"""
更新计划任务

:param daemonId: 节点ID
:param uuid: 实例ID
:param config: 计划任务配置

:returns: 是否成功
"""
return Schedule.create(daemonId, uuid, config)
Empty file removed mcsmapi/models/__init__.py
Empty file.
46 changes: 29 additions & 17 deletions mcsmapi/models/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,8 @@ class DaemonSetting(BaseModel):
"""未知"""
port: int
"""节点监听端口"""


class DaemonSystemInfo(BaseModel):
"""节点系统信息"""

version: str | None = None
"""远程节点版本"""
process: ProcessInfo | None = None
"""远程节点的基本信息"""
instance: InstanceStat | None = None
"""远程节点实例基本信息"""
system: SystemInfo | None = None
"""远程节点系统信息"""
cpuMemChart: list[CpuMemChart] | None = None
"""cpu和内存使用趋势"""
config: DaemonSetting
"""节点系统配置信息"""
maxDownloadFromUrlFileCount: int
"""允许同时下载的远程下载任务数量"""


class DaemonOperation(BaseModel):
Expand Down Expand Up @@ -150,6 +135,33 @@ class DaemonConfig(BaseModel):
"""远程节点的apiKey"""


class DaemonSystemInfo(DaemonOperation):
"""节点信息"""

version: str | None = None
"""节点版本"""
process: ProcessInfo | None = None
"""节点进程信息"""
instance: InstanceStat | None = None
"""节点实例统计信息"""
system: SystemInfo | None = None
"""节点系统信息"""
cpuMemChart: list[CpuMemChart] | None = None
"""cpu和内存使用趋势"""
config: DaemonSetting | None = None
"""节点配置信息"""
available: bool
"""节点可用状态"""
ip: str
"""节点ip"""
port: int
"""节点端口"""
prefix: str
"""节点路径前缀"""
remarks: str
"""节点备注"""


class DaemonStatus(DaemonOperation):
"""节点状态信息"""

Expand Down
2 changes: 1 addition & 1 deletion mcsmapi/models/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def delete(self):
self.daemonId, self.uuid, [os.path.join(self.target, self.name)]
)

def copy2(self, target: str):
def copy_to(self, target: str):
"""
复制该文件或文件夹到目标路径

Expand Down
Loading