示例#1
0
        private async Task <int> ExecuteShellAsync(string cmd)
        {
            var task = EditorShell.Execute(cmd, new EditorShell.Options());
            int code = await task;

            return(code);
        }
        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);
        }
        /// <summary>
        /// Determine if docker is installed.
        /// </summary>
        /// <returns></returns>
        public static Task <bool> IsZODockerInstalled()
        {
            var options = new EditorShell.Options()
            {
                workDirectory = Application.dataPath
            };

            // 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 = "docker --version && docker-compose --version";
            // Execute docker command
            var shellTask = EditorShell.Execute(command, options);

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

            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;
        }
示例#6
0
        public IEnumerator KillAsyncOperation()
        {
            var operation = EditorShell.Execute("sleep 5", new EditorShell.Options());

            KillAfter1Second(operation);
            var task = GetOperationTask(operation);

            yield return(new TaskYieldable <int>(task));

            Debug.Log("exit with code = " + task.Result);
            Assert.True(task.Result == 137);
        }
示例#7
0
        public IEnumerator EchoHelloWorld()
        {
            var task = EditorShell.Execute("echo hello world", new EditorShell.Options());

            task.onLog += (logType, log) => {
                Debug.Log(log);
                LogAssert.Expect(LogType.Log, "hello world");
            };
            task.onExit += (code) => {
                Debug.Log("Exit with code = " + code);
                Assert.True(code == 0);
            };
            yield return(new ShellOperationYieldable(task));
        }
        /// <summary>
        /// Install Docker.  Note:  Probably only works for Linux.
        /// </summary>
        public static void DockerInstall()
        {
            // Find Docker install script
            string[] installScriptAssets = AssetDatabase.FindAssets(installScriptAssetName);
            if (installScriptAssets.Length == 0)
            {
                EditorUtility.DisplayDialog("Error", "Couldn't find docker_install.sh among your assets. Have you imported ZeroSim samples already?", "Ok");
                return;
            }

            string scriptPath = AssetDatabase.GUIDToAssetPath(installScriptAssets[0]);
            string directory  = System.IO.Path.GetDirectoryName(scriptPath);

            UnityEngine.Debug.Log(directory);

            // Navigate to parent directories where the docker and dockercompose files are located
            var options = new EditorShell.Options()
            {
                workDirectory = directory
            };
            string command = "./" + installScriptName;
            // Execute docker command
            var task = EditorShell.Execute(command, options);

            task.onLog += (EditorShell.LogType logType, string log) => {
                DockerLog(log);
            };
            task.onExit += (exitCode) => {
                if (exitCode == 0)
                {
                    isRunning = true;
                }

                DockerLog($"Install script exit code: {exitCode}", forceDisplay: true);
            };
        }