Esempio n. 1
0
        internal static ForgeJsonMetadata CreateJsonMetadata(ForgeData forgeData, FilesToPack filesToPack)
        {
            if (null == forgeData)
            {
                throw new ArgumentNullException("forgeData");
            }

            if (null == filesToPack)
            {
                throw new ArgumentNullException("filesToPack");
            }

            ForgeJsonMetadata forgeJsonMetadata = null;

            if (filesToPack.Count > 0)
            {
                var forgeUserName          = forgeData[Conatants.PuppetForgeUserName];
                var forgeModuleName        = forgeData[Conatants.PuppetForgeModuleName];
                var forgeModuleVersion     = forgeData[Conatants.PuppetForgeModuleVersion];
                var forgeModuleDependency  = forgeData[Conatants.PuppetForgeModuleDependency];
                var forgeModuleSummary     = forgeData[Conatants.PuppetForgeModuleSummary];
                var forgeModuleDescription = forgeData[Conatants.PuppetForgeModuleDescription];

                forgeJsonMetadata = new ForgeJsonMetadata
                {
                    name        = string.Format("{0}-{1}", forgeUserName, forgeModuleName.ToLower()),
                    author      = forgeUserName,
                    version     = forgeModuleVersion,
                    summary     = forgeModuleSummary,
                    description = forgeModuleDescription
                };

                // Parse dependency

                forgeJsonMetadata.dependencies.Add(SELF.ParseDependency(forgeModuleDependency));

                // Add MD5 checksums to json

                using (var md5 = MD5.Create())
                {
                    foreach (var fileToPack in filesToPack)
                    {
                        var fileInfo     = fileToPack.Item1;
                        var subdir       = fileToPack.Item2;
                        var combinedName = string.IsNullOrWhiteSpace(subdir)
                            ? fileInfo.Name
                            : string.Format("{0}/{1}", subdir.Replace(Path.DirectorySeparatorChar, '/'), fileInfo.Name);

                        using (var stream = File.OpenRead(fileToPack.Item1.FullName))
                        {
                            var byteArray = md5.ComputeHash(stream);
                            forgeJsonMetadata.checksums.Add(combinedName,
                                                            string.Join("", byteArray.Select(b => b.ToString("x2"))));
                        }
                    }
                }
            }

            return(forgeJsonMetadata);
        }
Esempio n. 2
0
        private static string TarGz(ForgeData forgeData, FilesToPack filesToPack, ForgeJsonMetadata forgeJsonMetadata)
        {
            if (null == forgeData)
            {
                throw new ArgumentNullException("forgeData");
            }

            if (null == filesToPack)
            {
                throw new ArgumentNullException("filesToPack");
            }

            if (null == forgeJsonMetadata)
            {
                throw new ArgumentNullException("forgeJsonMetadata");
            }

            // Add files to a Tarball

            if (filesToPack.Count <= 0)
            {
                return(null);
            }

            var forgeUserName      = forgeData[Conatants.PuppetForgeUserName];
            var forgeModuleName    = forgeData[Conatants.PuppetForgeModuleName];
            var forgeModuleVersion = forgeData[Conatants.PuppetForgeModuleVersion];

            // Create a temp directory.

            var tmpDir       = new DirectoryInfo(Path.GetTempPath());
            var puppetTmpDir = tmpDir.CreateSubdirectory("PuppetLab");
            var rndDir       = puppetTmpDir.CreateSubdirectory(Path.GetRandomFileName());

            var moduleDirName = string.Format("{0}-{1}-{2}", forgeUserName, forgeModuleName, forgeModuleVersion).ToLower();
            var moduleDir     = rndDir.CreateSubdirectory(moduleDirName);

            Directory.SetCurrentDirectory(rndDir.ToString());

            // Create json metadata file in module directory

            MemoryStream ms = null;
            FileStream   fs = null;

            try
            {
                ms = new MemoryStream();
                fs = File.Create(Path.Combine(moduleDir.ToString(), "metadata.json"));

                var sr = new StreamReader(ms);
                var sw = new StreamWriter(fs);

                var s = new DataContractJsonSerializerSettings {
                    UseSimpleDictionaryFormat = true
                };
                var jonSerializer = new DataContractJsonSerializer(typeof(ForgeJsonMetadata), s);

                jonSerializer.WriteObject(ms, forgeJsonMetadata);

                // Remove backslashes that DataContractJsonSerializer adds before forward slashes (don't know how to disable this)
                ms.Position = 0;
                sw.WriteLine(sr.ReadToEnd().Replace("\\", string.Empty));

                sw.Close();
                sr.Close();

                ms = null;
                fs = null;
            }
            finally
            {
                if (ms != null)
                {
                    ms.Dispose();
                }
                if (fs != null)
                {
                    fs.Dispose();
                }
            }

            // Copy module's files to a tmp dir considering module dir tree

            foreach (var fileToPack in filesToPack)
            {
                var srcFileInfo = fileToPack.Item1;
                var relSubdir   = fileToPack.Item2;
                if (!string.IsNullOrEmpty(relSubdir))
                {
                    moduleDir.CreateSubdirectory(relSubdir);
                }
                var absSubdir   = Path.Combine(moduleDir.ToString(), relSubdir);
                var dstFileName = Path.Combine(absSubdir, srcFileInfo.Name);
                srcFileInfo.CopyTo(dstFileName);
            }

            // Create a TAR GZ archive

            var tarFileName = moduleDirName + ".tar";
            var gzFileName  = tarFileName + ".gz";

            CreateTarGz(gzFileName, moduleDir.ToString());

            return(gzFileName);
        }