138 lines
5.2 KiB
Go
138 lines
5.2 KiB
Go
package task
|
|
|
|
import (
|
|
"fmt"
|
|
"gitlink.org.cn/cloudream/common/pkgs/logger"
|
|
cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
|
|
schglb "gitlink.org.cn/cloudream/scheduler/common/globals"
|
|
"gitlink.org.cn/cloudream/scheduler/common/pkgs/mq/executor"
|
|
exectsk "gitlink.org.cn/cloudream/scheduler/common/pkgs/mq/executor/task"
|
|
"gitlink.org.cn/cloudream/scheduler/executor/internal/config"
|
|
"gitlink.org.cn/cloudream/scheduler/executor/internal/globals"
|
|
"gitlink.org.cn/cloudream/scheduler/executor/internal/task/create_ecs"
|
|
)
|
|
|
|
type ScheduleCreateECS struct {
|
|
*exectsk.ScheduleCreateECS
|
|
}
|
|
|
|
func NewScheduleCreateECS(info *exectsk.ScheduleCreateECS) *ScheduleCreateECS {
|
|
return &ScheduleCreateECS{
|
|
ScheduleCreateECS: info,
|
|
}
|
|
}
|
|
|
|
func (t *ScheduleCreateECS) Execute(task *Task, ctx TaskContext) {
|
|
log := logger.WithType[ScheduleCreateECS]("Task")
|
|
log.Debugf("begin")
|
|
defer log.Debugf("end")
|
|
|
|
err := t.do(task, ctx)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return
|
|
}
|
|
|
|
log.Info("ScheduleCreateECS...")
|
|
}
|
|
|
|
func (t *ScheduleCreateECS) do(task *Task, ctx TaskContext) error {
|
|
stgCli, err := schglb.CloudreamStoragePool.Acquire()
|
|
if err != nil {
|
|
return fmt.Errorf("new cloudream storage client: %w", err)
|
|
}
|
|
defer schglb.CloudreamStoragePool.Release(stgCli)
|
|
|
|
resp, err := stgCli.Package().Get(cdssdk.PackageGetReq{
|
|
PackageID: t.PackageID,
|
|
UserID: t.UserID,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
println(resp.Name)
|
|
|
|
CDSRcloneID := schglb.CloudreamStorageConfig.URL + "/object/download?userID=1&objectID=" + schglb.CDSRclone.CDSRcloneID
|
|
CDSRcloneConfigID := schglb.CloudreamStorageConfig.URL + "/object/download?userID=1&objectID=" + schglb.CDSRclone.CDSRcloneConfigID
|
|
println("CDSRcloneID: " + CDSRcloneID)
|
|
println("CDSRcloneConfigID: " + CDSRcloneConfigID)
|
|
|
|
var commands []string
|
|
commandContent := "yum install -y fuse3"
|
|
commands = append(commands, commandContent)
|
|
commandContent = "mkdir -p /opt/rclone/ \n mkdir -p /mnt/cds/"
|
|
commands = append(commands, commandContent)
|
|
commandContent = "cd /opt/rclone \n python3 -c 'import requests;response=requests.get(\"" + CDSRcloneID + "\",stream=True);response.raise_for_status();boundary=response.headers.get(\"Content-Type\").split(\"boundary=\")[-1].encode();content=response.content;body=[part.split(b\"\\r\\n\\r\\n\",1)[1].rsplit(b\"\\r\\n--\",1)[0] for part in content.split(b\"--\"+boundary+b\"\\r\\n\") if b\"filename=\" in part][0];open(\"rclone\",\"wb\").write(body);print(\"success\")'\n"
|
|
commands = append(commands, commandContent)
|
|
commandContent = "cd /opt/rclone \n python3 -c 'import requests;response=requests.get(\"" + CDSRcloneConfigID + "\",stream=True);response.raise_for_status();boundary=response.headers.get(\"Content-Type\").split(\"boundary=\")[-1].encode();content=response.content;body=[part.split(b\"\\r\\n\\r\\n\",1)[1].rsplit(b\"\\r\\n--\",1)[0] for part in content.split(b\"--\"+boundary+b\"\\r\\n\") if b\"filename=\" in part][0];open(\"rclone.conf\",\"wb\").write(body);print(\"success\")'\n"
|
|
commands = append(commands, commandContent)
|
|
commandContent = "cd /opt/rclone \n chmod +x rclone"
|
|
commands = append(commands, commandContent)
|
|
commandContent = "cd /opt/rclone \n nohup ./rclone mount cds: /mnt/cds --vfs-cache-mode full --vfs-read-wait 0 --vfs-read-chunk-size 128M --cache-db-purge -vv > rclone.log 2>&1 &"
|
|
commands = append(commands, commandContent)
|
|
commandContent = "cd /mnt/cds/bkt1/tiny_model/ \n sh execute.sh"
|
|
commands = append(commands, commandContent)
|
|
|
|
// 创建云主机
|
|
factory := create_ecs.GetFactory(config.CloudName)
|
|
provider := factory.CreateProvider()
|
|
|
|
instanceID, err := provider.CreateServer()
|
|
if err != nil {
|
|
task.SendStatus(exectsk.NewScheduleCreateECSStatus("", t.ModelID, err.Error()))
|
|
return err
|
|
}
|
|
|
|
address, err := provider.RunCommand(commands, instanceID)
|
|
if err != nil {
|
|
task.SendStatus(exectsk.NewScheduleCreateECSStatus("", t.ModelID, err.Error()))
|
|
return err
|
|
}
|
|
|
|
// 返回执行结果
|
|
task.SendStatus(exectsk.NewScheduleCreateECSStatus(address, t.ModelID, ""))
|
|
println("create ECS success, waiting msg...")
|
|
|
|
// 监听更新操作
|
|
for {
|
|
taskOperate, err := task.taskChan.Chan.Receive()
|
|
if err != nil {
|
|
task.SendStatus(exectsk.NewScheduleCreateECSStatus("", t.ModelID, err.Error()))
|
|
return err
|
|
}
|
|
|
|
info, ok := taskOperate.(executor.TaskOperateInfo)
|
|
if !ok {
|
|
task.SendStatus(exectsk.NewScheduleCreateECSStatus("", t.ModelID, "invalid task operate info"))
|
|
return fmt.Errorf("invalid task operate info")
|
|
}
|
|
|
|
switch info.Command {
|
|
case globals.RESTART:
|
|
var commands []string
|
|
commandContent := "yum install -y fuse3"
|
|
commands = append(commands, commandContent)
|
|
result, err := provider.RunCommand(commands, instanceID)
|
|
if err != nil {
|
|
task.SendStatus(exectsk.NewScheduleCreateECSStatus("", t.ModelID, err.Error()))
|
|
return err
|
|
}
|
|
task.SendStatus(exectsk.NewScheduleCreateECSStatus(result, t.ModelID, ""))
|
|
case globals.STOP:
|
|
println("STOP")
|
|
case globals.DESTROY:
|
|
result, err := provider.DestroyServer(instanceID)
|
|
if err != nil {
|
|
task.SendStatus(exectsk.NewScheduleCreateECSStatus("", t.ModelID, err.Error()))
|
|
return err
|
|
}
|
|
task.SendStatus(exectsk.NewScheduleCreateECSStatus(result, t.ModelID, ""))
|
|
}
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
Register(NewScheduleCreateECS)
|
|
}
|