Пример #1
0
        public Manifest Install(string directory)
        {
            if (!Directory.Exists(directory))
            {
                throw new ArgumentException(string.Format("Illegal path to cartridge source: {0}", directory));
            }

            if (directory == path)
            {
                throw new ArgumentException(string.Format("Source cannot be: {0}", path));
            }

            string manifestPath = Path.Combine(directory, "metadata", "manifest.yml");

            if (!File.Exists(manifestPath))
            {
                throw new ArgumentException(string.Format("Cartridge manifest.yml missing: {0}", manifestPath));
            }

            Manifest entry = null;

            lock (OpenShiftCartridgeRepositorySemaphore)
            {
                entry = Insert(new Manifest(manifestPath, null, "file", path));
                if (Directory.Exists(entry.RepositoryPath))
                {
                    Directory.Delete(entry.RepositoryPath, true);
                }
                Directory.CreateDirectory(entry.RepositoryPath);
                DirectoryUtil.DirectoryCopy(directory, entry.RepositoryPath, true);
            }
            return(entry);
        }
Пример #2
0
        public void Test_DirectoryandFileUtil()
        {
            bool result = true;

            try
            {
                if (!Directory.Exists("Test1"))
                {
                    Directory.CreateDirectory("Test1");
                    Directory.CreateDirectory(@"Test1\Testing");
                }
                File.WriteAllText(@"Test1\test1.txt", "Testing");
                DirectoryUtil.DirectoryCopy("Test1", "Test2", true);
                DirectoryUtil.EmptyDirectory("Test2");
                if (Directory.GetFiles("Test2").Length == 0)
                {
                    Directory.Delete("Test2");
                }
                DirectoryUtil.CreateSymLink(@"Test1\TestLink", @"Test1\test1.txt", DirectoryUtil.SymbolicLink.File);
                string location = FileUtil.GetSymlinkTargetLocation(@"Test1\TestLink");
                if (location == string.Empty)
                {
                    result = false;
                }
                DirectoryUtil.EmptyDirectory("Test1");
                Directory.Delete("Test1");
            }
            catch
            {
                result = false;
            }
            Assert.AreEqual(true, result);
        }
Пример #3
0
        private static void Extract(string method, string source, string target)
        {
            // use intermediate temp directory to reset cygwin directories ACLs
            string tempPath = target + ".temp";

            Directory.CreateDirectory(tempPath);
            Path.Combine(NodeConfig.Values["SSHD_BASE_DIR"], @"bin\tar.exe");
            Logger.Debug("Extracting {0} to {2} using {1} method", source, method, target);
            switch (method)
            {
            case "zip":
            {
                ZipFile.ExtractToDirectory(source, tempPath);
                break;
            }

            case "tgz":
            {
                ProcessResult result = ProcessExtensions.RunCommandAndGetOutput(Path.Combine(NodeConfig.Values["SSHD_BASE_DIR"], @"bin\tar.exe"), string.Format("-C {0} -zxpf {1}", LinuxFiles.Cygpath(tempPath), LinuxFiles.Cygpath(source)));
                Logger.Debug(result.StdOut + result.StdErr);
                break;
            }

            case "tar":
            {
                ProcessResult result = ProcessExtensions.RunCommandAndGetOutput(Path.Combine(NodeConfig.Values["SSHD_BASE_DIR"], @"bin\tar.exe"), string.Format("-C {0} -xpf {1}", LinuxFiles.Cygpath(tempPath), LinuxFiles.Cygpath(source)));
                Logger.Debug(result.StdOut + result.StdErr);
                break;
            }

            default:
            {
                throw new Exception(string.Format("Packaging method {0} not yet supported.", method));
            }
            }

            DirectoryUtil.DirectoryCopy(tempPath, target, true);
            Directory.Delete(tempPath, true);

            string[] files = Directory.GetFileSystemEntries(target);
            if (files.Length == 1)
            {
                Logger.Debug(string.Join(",", files));
                // A directory of one file is not legal move everyone up a level (github zip's are this way)
                string to_delete = files[0] + ".to_delete";
                Directory.Move(files[0], to_delete);
                DirectoryUtil.DirectoryCopy(to_delete, target, true);
                Directory.Delete(to_delete);
            }
        }
Пример #4
0
        private void BuildBare(string path)
        {
            string template = Path.Combine(this.Container.ContainerDir, "git", "template");

            if (Directory.Exists(template))
            {
                Directory.Delete(template, true);
            }
            Directory.CreateDirectory(template);
            string gitPath = Path.Combine(this.Container.ContainerDir, "git");

            DirectoryUtil.DirectoryCopy(path, template, true);
            RunCmd(string.Format(GIT_INIT, GIT), template);
            RunCmd(string.Format(GIT_LOCAL_CLONE, GIT, this.Container.ApplicationName), gitPath);
        }
Пример #5
0
        public string PopulateFromCartridge(string cartridgeName)
        {
            if (Exists())
            {
                return(null);
            }

            Directory.CreateDirectory(Path.Combine(this.Container.ContainerDir, "git"));

            string[] locations = new string[] {
                Path.Combine(this.Container.ContainerDir, cartridgeName, "template"),
                Path.Combine(this.Container.ContainerDir, cartridgeName, "template.git"),
                Path.Combine(this.Container.ContainerDir, cartridgeName, "usr", "template"),
                Path.Combine(this.Container.ContainerDir, cartridgeName, "usr", "template.git")
            };

            string template = null;

            foreach (string dir in locations)
            {
                if (Directory.Exists(dir))
                {
                    template = dir;
                    break;
                }
            }
            if (template == null)
            {
                return(null);
            }

            this.cartridgeName   = cartridgeName;
            this.applicationName = this.Container.ApplicationName;

            if (template.EndsWith(".git"))
            {
                DirectoryUtil.DirectoryCopy(template, RepositoryPath, true);
            }
            else
            {
                BuildBare(template);
            }
            Configure();

            return(template);
        }
Пример #6
0
        public void SyncFiles(string from, string to)
        {
            // TODO use rsync
            if (!Directory.Exists(from))
            {
                return;
            }

            if (!Directory.Exists(to))
            {
                Directory.CreateDirectory(to);
            }

            if (Directory.Exists(to))
            {
                DirectoryUtil.EmptyDirectory(to);
            }

            DirectoryUtil.DirectoryCopy(from, to, true);
        }
Пример #7
0
        public static void InstantiateCartridge(Manifest cartridge, string target, bool failureRemove = true)
        {
            Directory.CreateDirectory(target);
            try
            {
                bool downloadable = cartridge.ManifestPath == "url";

                if (downloadable)
                {
                    Uri    uri       = new Uri(cartridge.SourceUrl);
                    string temporary = Path.Combine(target, Path.GetFileName(uri.LocalPath));

                    if (uri.Scheme == "git" || cartridge.SourceUrl.EndsWith(".git"))
                    {
                        // use intermediate temp directory to reset cygwin directories ACLs
                        string tempRepo    = cartridge.Name + ".temp";
                        string tempRepoDir = Path.Combine(new DirectoryInfo(target).Parent.FullName, tempRepo);
                        try
                        {
                            string template = @"{0} clone {1} {2}
set GIT_DIR=./{2}/.git
{0} repack";
                            string file     = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + "cmd.bat");
                            File.WriteAllText(file, string.Format(template, Path.Combine(NodeConfig.Values["SSHD_BASE_DIR"], @"bin\git.exe"), cartridge.SourceUrl, tempRepo));
                            ProcessResult result = ProcessExtensions.RunCommandAndGetOutput("cmd.exe", string.Format("/c {0}", file), new DirectoryInfo(target).Parent.FullName);
                            if (result.ExitCode != 0)
                            {
                                throw new Exception(string.Format("Unable to clone cartridge from {0} stdout: {1} stderr {2}", cartridge.SourceUrl, result.StdOut, result.StdErr));
                            }

                            DirectoryUtil.DirectoryCopy(tempRepoDir, target, true);
                        }
                        finally
                        {
                            if (Directory.Exists(tempRepoDir))
                            {
                                Directory.Delete(tempRepoDir, true);
                            }
                        }
                    }
                    else if (Regex.IsMatch(uri.Scheme, @"^https*") && Regex.IsMatch(cartridge.SourceUrl, @"\.zip"))
                    {
                        try
                        {
                            UriCopy(new Uri(cartridge.SourceUrl), temporary, cartridge.SourceMD5);
                            Extract("zip", temporary, target);
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex.ToString());
                            throw ex;
                        }
                        finally
                        {
                            if (File.Exists(temporary))
                            {
                                File.Delete(temporary);
                            }
                        }
                    }
                    else if (Regex.IsMatch(uri.Scheme, @"^https*") && Regex.IsMatch(cartridge.SourceUrl, @"(\.tar\.gz|\.tgz)$"))
                    {
                        try
                        {
                            UriCopy(new Uri(cartridge.SourceUrl), temporary, cartridge.SourceMD5);
                            Extract("tgz", temporary, target);
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex.ToString());
                            throw ex;
                        }
                        finally
                        {
                            if (File.Exists(temporary))
                            {
                                File.Delete(temporary);
                            }
                        }
                    }
                    else if (Regex.IsMatch(uri.Scheme, @"^https*") && Regex.IsMatch(cartridge.SourceUrl, @"\.tar$"))
                    {
                        try
                        {
                            UriCopy(new Uri(cartridge.SourceUrl), temporary, cartridge.SourceMD5);
                            Extract("tar", temporary, target);
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex.ToString());
                            throw ex;
                        }
                        finally
                        {
                            if (File.Exists(temporary))
                            {
                                File.Delete(temporary);
                            }
                        }
                    }
                    else if (uri.Scheme == "file")
                    {
                        DirectoryUtil.DirectoryCopy(uri.LocalPath, target, true);
                    }
                    else
                    {
                        throw new ArgumentException(string.Format("CLIENT_ERROR: Unsupported URL({0}) for downloading a private cartridge", cartridge.SourceUrl));
                    }
                }
                else
                {
                    // TODO exclude usr folder and use link
                    DirectoryUtil.DirectoryCopy(cartridge.RepositoryPath, target, true);
                }

                ValidateCartridgeHome(cartridge, target);

                if (downloadable)
                {
                    string manifestOnDisk = Path.Combine(target, "metadata", "manifest.yml");
                    using (StreamWriter sw = new StreamWriter(manifestOnDisk))
                    {
                        Serializer ser = new Serializer(SerializationOptions.None);
                        ser.Serialize(sw, cartridge.ManifestSpec);
                    }
                }
            }
            catch (Exception e)
            {
                if (failureRemove)
                {
                    if (Directory.Exists(target))
                    {
                        Directory.Delete(target);
                    }
                }
                throw e;
            }
        }