Exemplo n.º 1
0
        private void DockerInstallGameServerContainerButton_Click(object sender, EventArgs e)
        {
            Program.ShowConsole();
            Console.Clear();

            //Creating containers
            PrintHeader("Creating Game Server container");

            int    containerPort     = Parameter.DEFAULT_GAME_SERVER_CONTAINER_PORT;
            int    startingLocalPort = Parameter.DEFAULT_GAME_SERVER_STARTING_PORT;
            string containerName     = Parameter.DEFAILT_GAME_SERVER_CONTAINER_NAME;
            string volumeName        = Parameter.DEFAULT_GAME_SERVER_VOLUME_NAME;

            Console.WriteLine($"Enter the local ports you wish to use (default is {startingLocalPort}): ");
            int.TryParse(Console.ReadLine(), out startingLocalPort);

            Console.WriteLine("Enter the server ID (copy this id on ServerID txt): ");
            int containerID = 0;

            int.TryParse(Console.ReadLine(), out containerID);

            containerName += $"-{containerID}";

            Dictionary <string, string> replacingTemplateFields
                = new Dictionary <string, string>()
                {
                { "__container_name__", containerName },
                { "__volume_name__", volumeName },
                { "__local_port__", startingLocalPort.ToString() },
                { "__container_port__", containerPort.ToString() },
                { "__context__", Parameter.DEFAULT_GAME_SERVER_CONTEXT },
                { "__dockerfile_path__", Parameter.DEFAULT_GAME_SERVER_DOCKERFILE_PATH }
                };

            Console.WriteLine("Select the base project folder. This folder contains the \".sln\" file.");

            string slnDir = PipelineHelper.SelectSLNFile();

            slnDir = slnDir.Replace("\\" + slnDir.Split("\\").Last(), "");
            string gspDir = $@"{slnDir}\OpenBound Game Server";

            List <string> files = PipelineHelper.GenerateTemplateFiles(slnDir, slnDir, replacingTemplateFields, "*.Template.yml").ToList();

            files.AddRange(PipelineHelper.GenerateTemplateFiles(gspDir, gspDir, replacingTemplateFields, "*.Template.Dockerfile").ToList());

            //Building & Starting Container
            PrintHeader("Building Containers");

            PipelineHelper.ExecuteShellCommand(@$ "docker-compose -f {slnDir}\OpenBoundServerCompose.yml build");
            PipelineHelper.ExecuteShellCommand(@$ "docker-compose -f {slnDir}\OpenBoundServerCompose.yml up -d");

            //Configuring Container
            ConfigFileManager.CreateConfigFile(RequesterApplication.GameServer, true);
            PrintHeader("Building Containers");

            Console.WriteLine("Close the notepad in order to continue the server configuration. Reminder: host.docker.internal is your ip from container's perspective.");
            Thread.Sleep(5000);

            PipelineHelper.ExecuteShellCommand($@"notepad.exe {Directory.GetCurrentDirectory()}\Config\DatabaseConfig.json");
            PipelineHelper.ExecuteShellCommand($@"notepad.exe {Directory.GetCurrentDirectory()}\Config\GameServerServerConfig.json");

            PipelineHelper.ExecuteShellCommand($"docker cp \"{Directory.GetCurrentDirectory()}\\Config\\DatabaseConfig.json\" \"{containerName}:\\OpenBound Game Server\\Config\\DatabaseConfig.json\"");
            PipelineHelper.ExecuteShellCommand($"docker cp \"{Directory.GetCurrentDirectory()}\\Config\\GameServerServerConfig.json\" \"{containerName}:\\OpenBound Game Server\\Config\\GameServerServerConfig.json\"");

            foreach (string file in files)
            {
                try { File.Delete(file); } catch { }
            }

            PipelineHelper.ExecuteShellCommand(@$ "docker restart {containerName}");

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

            Program.HideConsole();
        }