@@ -56,13 +56,17 @@ type IRuntimeService interface {
5656 GetPHPExtensions (runtimeID uint ) (response.PHPExtensionRes , error )
5757 InstallPHPExtension (req request.PHPExtensionInstallReq ) error
5858 UnInstallPHPExtension (req request.PHPExtensionInstallReq ) error
59+
5960 GetPHPConfig (id uint ) (* response.PHPConfig , error )
6061 UpdatePHPConfig (req request.PHPConfigUpdate ) (err error )
6162 UpdatePHPConfigFile (req request.PHPFileUpdate ) error
6263 GetPHPConfigFile (req request.PHPFileReq ) (* response.FileInfo , error )
6364 UpdateFPMConfig (req request.FPMConfig ) error
6465 GetFPMConfig (id uint ) (* request.FPMConfig , error )
6566
67+ UpdatePHPContainer (req request.PHPContainerConfig ) error
68+ GetPHPContainerConfig (id uint ) (* request.PHPContainerConfig , error )
69+
6670 GetSupervisorProcess (id uint ) ([]response.SupervisorProcessConfig , error )
6771 OperateSupervisorProcess (req request.PHPSupervisorProcessConfig ) error
6872 OperateSupervisorProcessFile (req request.PHPSupervisorProcessFileReq ) (string , error )
@@ -383,77 +387,9 @@ func (r *RuntimeService) Get(id uint) (*response.RuntimeDTO, error) {
383387 }
384388 res .AppParams = appParams
385389 case constant .RuntimeNode , constant .RuntimeJava , constant .RuntimeGo , constant .RuntimePython , constant .RuntimeDotNet :
386- res .Params = make (map [string ]interface {})
387- envs , err := gotenv .Unmarshal (runtime .Env )
388- if err != nil {
390+ if err := handleRuntimeDTO (& res , * runtime ); err != nil {
389391 return nil , err
390392 }
391- for k , v := range envs {
392- if strings .Contains (k , "CONTAINER_PORT" ) || strings .Contains (k , "HOST_PORT" ) {
393- if strings .Contains (k , "CONTAINER_PORT" ) {
394- r := regexp .MustCompile (`_(\d+)$` )
395- matches := r .FindStringSubmatch (k )
396- containerPort , err := strconv .Atoi (v )
397- if err != nil {
398- return nil , err
399- }
400- hostPort , err := strconv .Atoi (envs [fmt .Sprintf ("HOST_PORT_%s" , matches [1 ])])
401- if err != nil {
402- return nil , err
403- }
404- hostIP := envs [fmt .Sprintf ("HOST_IP_%s" , matches [1 ])]
405- if hostIP == "" {
406- hostIP = "0.0.0.0"
407- }
408- res .ExposedPorts = append (res .ExposedPorts , request.ExposedPort {
409- ContainerPort : containerPort ,
410- HostPort : hostPort ,
411- HostIP : hostIP ,
412- })
413- }
414- } else {
415- res .Params [k ] = v
416- }
417- }
418- if v , ok := envs ["CONTAINER_PACKAGE_URL" ]; ok {
419- res .Source = v
420- }
421- composeByte , err := files .NewFileOp ().GetContent (runtime .GetComposePath ())
422- if err != nil {
423- return nil , err
424- }
425- res .Environments , err = getDockerComposeEnvironments (composeByte )
426- if err != nil {
427- return nil , err
428- }
429- volumes , err := getDockerComposeVolumes (composeByte )
430- if err != nil {
431- return nil , err
432- }
433-
434- defaultVolumes := make (map [string ]string )
435- switch runtime .Type {
436- case constant .RuntimeNode :
437- defaultVolumes = constant .RuntimeDefaultVolumes
438- case constant .RuntimeJava :
439- defaultVolumes = constant .RuntimeDefaultVolumes
440- case constant .RuntimeGo :
441- defaultVolumes = constant .GoDefaultVolumes
442- case constant .RuntimePython :
443- defaultVolumes = constant .RuntimeDefaultVolumes
444- }
445- for _ , volume := range volumes {
446- exist := false
447- for key , value := range defaultVolumes {
448- if key == volume .Source && value == volume .Target {
449- exist = true
450- break
451- }
452- }
453- if ! exist {
454- res .Volumes = append (res .Volumes , volume )
455- }
456- }
457393 }
458394
459395 return & res , nil
@@ -1080,6 +1016,100 @@ func (r *RuntimeService) GetFPMConfig(id uint) (*request.FPMConfig, error) {
10801016 return res , nil
10811017}
10821018
1019+ func (r * RuntimeService ) UpdatePHPContainer (req request.PHPContainerConfig ) error {
1020+ runtime , err := runtimeRepo .GetFirst (context .Background (), repo .WithByID (req .ID ))
1021+ if err != nil {
1022+ return err
1023+ }
1024+ var (
1025+ hostPorts []string
1026+ composeContent []byte
1027+ )
1028+ for _ , export := range req .ExposedPorts {
1029+ hostPorts = append (hostPorts , strconv .Itoa (export .HostPort ))
1030+ if err = checkRuntimePortExist (export .HostPort , false , runtime .ID ); err != nil {
1031+ return err
1032+ }
1033+ }
1034+ if req .ContainerName != "" && req .ContainerName != getRuntimeEnv (runtime .Env , "CONTAINER_NAME" ) {
1035+ if err := checkContainerName (req .ContainerName ); err != nil {
1036+ return err
1037+ }
1038+ runtime .ContainerName = req .ContainerName
1039+ }
1040+ fileOp := files .NewFileOp ()
1041+ projectDir := path .Join (global .Dir .RuntimeDir , runtime .Type , runtime .Name )
1042+ composeContent , err = fileOp .GetContent (path .Join (projectDir , "docker-compose.yml" ))
1043+ if err != nil {
1044+ return err
1045+ }
1046+ envPath := path .Join (projectDir , ".env" )
1047+ if ! fileOp .Stat (envPath ) {
1048+ _ = fileOp .CreateFile (envPath )
1049+ }
1050+ envs , err := gotenv .Read (envPath )
1051+ if err != nil {
1052+ return err
1053+ }
1054+ for k := range envs {
1055+ if strings .HasPrefix (k , "CONTAINER_PORT_" ) || strings .HasPrefix (k , "HOST_PORT_" ) || strings .HasPrefix (k , "HOST_IP_" ) || strings .Contains (k , "APP_PORT" ) {
1056+ delete (envs , k )
1057+ }
1058+ }
1059+ create := request.RuntimeCreate {
1060+ Image : runtime .Image ,
1061+ Type : runtime .Type ,
1062+ Params : make (map [string ]interface {}),
1063+ NodeConfig : request.NodeConfig {
1064+ ExposedPorts : req .ExposedPorts ,
1065+ Environments : req .Environments ,
1066+ Volumes : req .Volumes ,
1067+ },
1068+ }
1069+ composeContent , err = handleCompose (envs , composeContent , create , projectDir )
1070+ if err != nil {
1071+ return err
1072+ }
1073+ newMap := make (map [string ]string )
1074+ handleMap (create .Params , newMap )
1075+ for k , v := range newMap {
1076+ envs [k ] = v
1077+ }
1078+ envStr , err := gotenv .Marshal (envs )
1079+ if err != nil {
1080+ return err
1081+ }
1082+ if err = gotenv .Write (envs , envPath ); err != nil {
1083+ return err
1084+ }
1085+ envContent := []byte (envStr )
1086+ runtime .Env = string (envContent )
1087+ runtime .DockerCompose = string (composeContent )
1088+ runtime .Status = constant .StatusReCreating
1089+ _ = runtimeRepo .Save (runtime )
1090+ go reCreateRuntime (runtime )
1091+ return nil
1092+ }
1093+
1094+ func (r * RuntimeService ) GetPHPContainerConfig (id uint ) (* request.PHPContainerConfig , error ) {
1095+ runtime , err := runtimeRepo .GetFirst (context .Background (), repo .WithByID (id ))
1096+ if err != nil {
1097+ return nil , err
1098+ }
1099+ runtimeDTO := response .NewRuntimeDTO (* runtime )
1100+ if err := handleRuntimeDTO (& runtimeDTO , * runtime ); err != nil {
1101+ return nil , err
1102+ }
1103+ res := & request.PHPContainerConfig {
1104+ ID : runtime .ID ,
1105+ ContainerName : runtime .ContainerName ,
1106+ ExposedPorts : runtimeDTO .ExposedPorts ,
1107+ Environments : runtimeDTO .Environments ,
1108+ Volumes : runtimeDTO .Volumes ,
1109+ }
1110+ return res , nil
1111+ }
1112+
10831113func (r * RuntimeService ) GetSupervisorProcess (id uint ) ([]response.SupervisorProcessConfig , error ) {
10841114 runtime , err := runtimeRepo .GetFirst (context .Background (), repo .WithByID (id ))
10851115 if err != nil {
0 commit comments