public static Task <bool> IsZODockerRunning()
        {
            ZOSettings settings = ZOSettings.GetOrCreateSettings();

            var options = new EditorShell.Options()
            {
                workDirectory   = settings.ComposeWorkingDirectory,
                environmentVars = new Dictionary <string, string>()
                {
                }
            };

            // Create a task and return it so clients can use async/await
            // Use TaskCompletionSource so that we can manually fulfill the task when
            // the shell script executes the onExit callback
            TaskCompletionSource <bool> taskCompletionSource = new TaskCompletionSource <bool>();

            string command = "if [ $(docker inspect -f '{{.State.Running}}' zosim) = \"true\" ]; then exit 0; else exit 1; fi";
            // Execute docker command
            var shellTask = EditorShell.Execute(command, options);

            DockerLog($"Checking if docker running...");
            shellTask.onLog += (EditorShell.LogType logType, string log) => {
                DockerLog(log);
            };
            shellTask.onExit += (exitCode) => {
                UnityEngine.Debug.Log("Check if docker running exit code: " + exitCode);
                isRunning = exitCode == 0;
                taskCompletionSource.SetResult(isRunning);
            };

            return(taskCompletionSource.Task);
        }
        public static void DockerComposeDown()
        {
            ZOSettings settings = ZOSettings.GetOrCreateSettings();

            // Navigate to parent directories where the docker and dockercompose files are located
            var options = new EditorShell.Options()
            {
                workDirectory   = settings.ComposeWorkingDirectory,
                environmentVars = new Dictionary <string, string>()
                {
                }
            };
            string command = "docker-compose down";
            // Execute docker command
            var task = EditorShell.Execute(command, options);

            DockerLog($"Stopping Docker, please wait...", forceDisplay: true);
            task.onLog += (EditorShell.LogType logType, string log) => {
                DockerLog(log);
            };
            task.onExit += (exitCode) => {
                DockerLog($"Docker compose down exit: {exitCode}", forceDisplay: true);
            };

            isRunning = false;
        }
        public static void DockerComposeUp()
        {
            ZOSettings settings = ZOSettings.GetOrCreateSettings();

            // Navigate to parent directories where the docker and dockercompose files are located
            var options = new EditorShell.Options()
            {
                workDirectory   = settings.ComposeWorkingDirectory,
                environmentVars = new Dictionary <string, string>()
                {
                }
            };

            // need to set environment variable so that we execute container as current user in host machine
            string command = "CURRENT_UID=$(id -u):$(id -g) docker-compose up";
            // Execute docker command
            var task = EditorShell.Execute(command, options);

            DockerLog($"Starting docker container, please wait...");
            task.onLog += (EditorShell.LogType logType, string log) => {
                DockerLog(log);
            };
            task.onExit += (exitCode) => {
                if (exitCode == 0)
                {
                    // we won't get this until the server is stopped
                }

                DockerLog($"Docker compose up exit: {exitCode}", forceDisplay: true);
            };

            // TODO: poll to check if the server was started successfully
            // here we assume it will
            isRunning = true;
        }
        /// <summary>
        /// Docker compose run command.
        /// <see>https://docs.docker.com/compose/reference/run/</see>
        /// </summary>
        /// <param name="service">The Docker Service.  For example "zosim".</param>
        /// <param name="command">The docker command to execute.</param>
        /// <param name="additionalVolumes">Additional volumes to mount.</param>
        /// <param name="servicePorts">Run command with the service's ports enabled and mapped to the host.</param>
        /// <param name="setUser">Run as specified username or uid.</param>
        /// <param name="callback">The on exit callback.</param>
        public static void DockerRun(string service,
                                     string command,
                                     string[] additionalVolumes = null,
                                     bool doServicePorts        = true,
                                     bool setUser          = true,
                                     Action <int> callback = null)
        {
            ZOSettings settings = ZOSettings.GetOrCreateSettings();

            // docker-compose -f ./docker/docker-compose.yml run --rm
            // zosim_tools python ./zo-asset-tools/zo_convex_decomposition/zo_convex_decomposition.py
            var options = new EditorShell.Options()
            {
                workDirectory   = settings.ComposeWorkingDirectory,
                environmentVars = new Dictionary <string, string>()
                {
                }
            };

            // set up the volumes to mount
            string volumes = BuildVolumesString(additionalVolumes);

            // set up server ports
            string servicePorts = doServicePorts ? "--service-ports" : "";

            // set up user
            // --user=$(id -u $USER):$(id -g $USER)
            string userName  = System.Environment.GetEnvironmentVariable("USER");
            string userParam = "";

            if (setUser == true)
            {
                int    exitCode = -99;
                string userId   = ZOSystem.RunProcessAndGetOutput("./", "id", $"-u {userName}", out exitCode);
                userId = Regex.Replace(userId, @"\s+", string.Empty);
                string userGroup = ZOSystem.RunProcessAndGetOutput("./", "id", $"-g {userName}", out exitCode);
                userGroup = Regex.Replace(userGroup, @"\s+", string.Empty);
                userParam = $"--user={userId}:{userGroup}";
            }

            // Run command in a new container, and delete after execution with --rm
            string dockerCommand = $"docker-compose run --rm {volumes} {servicePorts} {userParam} {service} {command}";

            UnityEngine.Debug.Log(dockerCommand);

            string     arguments = $"run --rm {volumes} {servicePorts} {userParam} {service} {command}";
            Task <int> t         = ZOSystem.RunProcessAsync("docker-compose", arguments, settings.ComposeWorkingDirectory);
        }
        private void OnGUIDocker()
        {
            EditorGUILayout.LabelField("Docker running", ZODockerManager.isRunning.ToString());

            _ZOSettings = ZOSettings.GetOrCreateSettings();

            EditorGUILayout.LabelField("Docker-compose directory", _ZOSettings.ComposeWorkingDirectory);

            if (!ZODockerManager.isRunning && GUILayout.Button("Start Docker service"))
            {
                ZODockerManager.DockerComposeUp();
            }

            if (ZODockerManager.isRunning && GUILayout.Button("Stop Docker service"))
            {
                ZODockerManager.DockerComposeDown();
            }

            ZODockerManager.showLogs = EditorGUILayout.Toggle("Show docker-compose logs", ZODockerManager.showLogs);
        }
示例#6
0
    public static SettingsProvider CreateZOSettingsProvider()
    {
        // First parameter is the path in the Settings window.
        // Second parameter is the scope of this setting: it only appears in the Project Settings window.
        var provider = new SettingsProvider("Project/ZeroSim Settings", SettingsScope.Project)
        {
            // By default the last token of the path is used as display name if no label is provided.
            label = "ZeroSim",
            // Create the SettingsProvider and initialize its drawing (IMGUI) function in place:
            guiHandler = (searchContext) =>
            {
                var settings = ZOSettings.GetSerializedSettings();

                if (GUILayout.Button("Select docker-compose.yml working directory"))
                {
                    string pathDockerCompose = EditorUtility.OpenFolderPanel("Select docker-compose.yml directory", "", "");
                    if (!string.IsNullOrEmpty(pathDockerCompose))
                    {
                        settings.FindProperty("_dockerComposeWorkingDirectory").stringValue = pathDockerCompose;

                        Debug.Log(settings.FindProperty("_dockerComposeWorkingDirectory").stringValue);
                        if (settings.ApplyModifiedProperties())
                        {
                            Debug.Log("Updated setting docker dir");
                        }
                    }
                }
                EditorGUILayout.LabelField("Docker-compose workdir:", settings.FindProperty("_dockerComposeWorkingDirectory").stringValue);
            },

            // Populate the search keywords to enable smart search filtering and label highlighting:
            keywords = new HashSet <string>(new[] { "docker", "compose" })
        };

        return(provider);
    }