Exemplo n.º 1
0
        public static bool UnInstall(List <Parameter> parms)
        {
            FileStream      fs       = new FileStream(GetParameter("Name", parms) + ".NAVY", FileMode.Open);
            ZipArchive      za       = new ZipArchive(fs, ZipArchiveMode.Read);
            FileStream      fs2      = new FileStream(GetParameter("Name", parms) + ".NAVY.Backup", FileMode.Open);
            ZipArchive      zbackup  = new ZipArchive(fs2, ZipArchiveMode.Read);
            ZipArchiveEntry manifest = za.GetEntry("manifest.xml");
            Package         p        = PackageFile.Load(manifest.Open());

            Console.WriteLine("Uninstalling {0} ...", p.App.Name);

            try { Directory.Delete("ORIGINAL", true); } catch { };
            Directory.CreateDirectory("ORIGINAL");

            using (PowerShell psi = PowerShell.Create())
            {
                psi.AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned");
                psi.AddScript(NAVTools);

                foreach (var delta in p.Payload.Deltas)
                {
                    string FileName = delta.DeltaFile.Split('.')[0];

                    // Extract the original from the ZIP backup
                    ZipArchiveEntry zip = zbackup.GetEntry(FileName + ".TXT");
                    zip.ExtractToFile("ORIGINAL\\" + FileName + ".TXT");

                    Console.WriteLine(" - object {0} {1}", delta.Type, delta.ID);
                    psi.AddScript(@"Import-NAVApplicationObject -Confirm:$false ORIGINAL\\" +
                                  FileName +
                                  ".TXT -DatabaseName " +
                                  GetParameter("DatabaseName", parms));
                    ExecutePowerShell(psi, true);
                }
            }
            // Compiling
            using (PowerShell psi2 = PowerShell.Create())
            {
                psi2.AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned");
                psi2.AddScript(NAVTools);
                Console.WriteLine("Compiling objects ...");
                psi2.AddScript("Compile-NAVApplicationObject " +
                               GetParameter("DatabaseName", parms) +
                               " -SynchronizeSchemaChanges Force");
                ExecutePowerShell(psi2, true);
            }
            return(true);
        }
Exemplo n.º 2
0
        public static bool Suspend(List <Parameter> parms)
        {
            FileStream      fs       = new FileStream(GetParameter("Name", parms) + ".NAVY", FileMode.Open);
            ZipArchive      za       = new ZipArchive(fs, ZipArchiveMode.Read);
            ZipArchiveEntry manifest = za.GetEntry("manifest.xml");
            Package         p        = PackageFile.Load(manifest.Open());

            try { Directory.Delete("DELTA", true); } catch { };
            Directory.CreateDirectory("DELTA");

            Console.WriteLine("Suspending {0} in database ...", p.App.Name);


            // Save data in fields
            Codeunit cu = new Codeunit(CodeunitMode.Backup);

            foreach (var delta in p.Payload.Deltas)
            {
                if (delta.Type == "Table")
                {
                    string FileName = delta.DeltaFile.Split('.')[0];

                    // Extract the delta from the ZIP
                    ZipArchiveEntry deltazip = za.GetEntry(delta.DeltaFile);
                    deltazip.ExtractToFile("DELTA\\" + delta.DeltaFile);
                    string DeltaTxt = File.ReadAllText("DELTA\\" + delta.DeltaFile);
                    if (DeltaTxt.IndexOf("ChangedElements=FieldCollection") != -1)
                    {
                        // Fields added, we are interested
                        foreach (var line in File.ReadAllLines("DELTA\\" + delta.DeltaFile))
                        {
                            int x = 0;
                            for (int i = 0; i < line.Length; i++)
                            {
                                if (line[i] == ';')
                                {
                                    x++;
                                }
                            }
                            if (line.IndexOf("                           { ") != -1 && x >= 4)
                            {
                                Match m = Regex.Match(line, @"(?<=\{.)\d{1,100}(?=\;)");
                                if (m.Success)
                                {
                                    cu.AddOperation(int.Parse(delta.ID), int.Parse(m.Groups[0].Value));
                                }
                            }
                        }
                    }
                }
            }
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            string code = cu.ExportCode(configFile.AppSettings.Settings["ObjectNoForBackup"].Value);

            File.WriteAllText("BackupCodeunit.txt", code);

            using (PowerShell psi = PowerShell.Create())
            {
                psi.AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned");
                psi.AddScript(NAVTools);

                psi.AddScript(@"Import-NAVApplicationObject -Confirm:$false BackupCodeunit.txt" +
                              " -DatabaseName " +
                              GetParameter("DatabaseName", parms));
                ExecutePowerShell(psi, true);
            }
            // Compiling
            using (PowerShell psi2 = PowerShell.Create())
            {
                psi2.AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned");
                psi2.AddScript(NAVTools);
                Console.WriteLine("Preparing backup objects ...");
                psi2.AddScript("Compile-NAVApplicationObject " +
                               GetParameter("DatabaseName", parms));
                ExecutePowerShell(psi2, true);
            }
            // - Execute codeunit
            File.Delete("NAVY.success");
            Process.Start(configFile.AppSettings.Settings["ClientInstance"].Value + "/RunCodeunit?Codeunit=50000");

            Console.WriteLine("Awaiting backup codeunit completion...");
            while (!File.Exists("NAVY.success"))
            {
                Thread.Sleep(1000);
                Console.Write(".");
            }
            ;
            File.Delete("NAVY.success");
            fs.Close();
            za.Dispose();
            // Uninstall old version
            UnInstall(parms);

            Console.WriteLine("{0} has beeen suspended.", p.App.Name);

            return(true);
        }
Exemplo n.º 3
0
        public static bool Install(List <Parameter> parms, bool InstallObjectPayload)
        {
            FileStream      fs       = new FileStream(GetParameter("Name", parms) + ".NAVY", FileMode.Open);
            ZipArchive      za       = new ZipArchive(fs, ZipArchiveMode.Update);
            FileStream      fs2      = new FileStream(GetParameter("Name", parms) + ".NAVY.Backup", FileMode.Create);
            ZipArchive      zbackup  = new ZipArchive(fs2, ZipArchiveMode.Update);
            ZipArchiveEntry manifest = za.GetEntry("manifest.xml");
            Package         p        = PackageFile.Load(manifest.Open());

            using (PowerShell psi = PowerShell.Create())
            {
                psi.AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned");
                psi.AddScript(NAVTools);

                File.Delete("object.txt");
                try { Directory.Delete("ORIGINAL", true); } catch { };
                try { Directory.Delete("DELTA", true); } catch { };
                try { Directory.Delete("RESULT", true); } catch { };
                Directory.CreateDirectory("ORIGINAL");
                Directory.CreateDirectory("DELTA");
                Directory.CreateDirectory("RESULT");
                Console.WriteLine("Exporting original objects from database...");
                foreach (var delta in p.Payload.Deltas)
                {
                    string FileName = delta.DeltaFile.Split('.')[0];

                    // Extract the delta from the ZIP
                    ZipArchiveEntry deltazip = za.GetEntry(delta.DeltaFile);
                    deltazip.ExtractToFile("DELTA\\" + delta.DeltaFile);

                    Console.WriteLine(" - object {0} {1}", delta.Type, delta.ID);
                    psi.AddScript("Export-NAVApplicationObject ORIGINAL\\" + FileName + ".TXT -DatabaseName " +
                                  GetParameter("DatabaseName", parms) +
                                  " -Force -Filter 'Type=" + delta.Type + ";Id=" + delta.ID + "'");
                    ExecutePowerShell(psi, false);
                    zbackup.CreateEntryFromFile("ORIGINAL\\" + FileName + ".TXT", FileName + ".TXT");
                }
                zbackup.Dispose();
                fs2.Close();

                Console.WriteLine("Applying deltas to objects...");
                psi.AddScript(@"Update-NAVApplicationObject -VersionListProperty FromModified -TargetPath ORIGINAL\*.txt -DeltaPath DELTA\*.delta –ResultPath RESULT\");
                ExecutePowerShell(psi, false);
            }

            if (InstallObjectPayload)
            {
                // Importing - First objects FOBs
                p.Payload.Objects.Sort(delegate(NAVObject a, NAVObject b)
                {
                    return(a.ImportOrder.CompareTo(b.ImportOrder));
                });
                foreach (var fob in p.Payload.Objects)
                {
                    ZipArchiveEntry fobzip = za.GetEntry(fob.FileName);
                    fobzip.ExtractToFile(fob.FileName, true);

                    using (PowerShell psi2 = PowerShell.Create())
                    {
                        psi2.AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned");
                        psi2.AddScript(NAVTools);

                        Console.WriteLine("Importing FOB objects to database ...");

                        Console.WriteLine(" - fob file {0}", fob.FileName);
                        psi2.AddScript(@"Import-NAVApplicationObject -Confirm:$false " +
                                       fob.FileName +
                                       " -ImportAction Overwrite -DatabaseName " +
                                       GetParameter("DatabaseName", parms));
                        ExecutePowerShell(psi2, true);
                    }
                }
            }
            using (PowerShell psi2 = PowerShell.Create())
            {
                psi2.AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned");
                psi2.AddScript(NAVTools);

                Console.WriteLine("Importing new objects to database ...");
                foreach (var delta in p.Payload.Deltas)
                {
                    string FileName = delta.DeltaFile.Split('.')[0];
                    Console.WriteLine(" - object {0} {1}", delta.Type, delta.ID);
                    psi2.AddScript(@"Import-NAVApplicationObject -Confirm:$false RESULT\\" +
                                   FileName +
                                   ".TXT -DatabaseName " +
                                   GetParameter("DatabaseName", parms));
                    ExecutePowerShell(psi2, true);
                }
            }

            // Compiling
            using (PowerShell psi2 = PowerShell.Create())
            {
                psi2.AddScript("Set-ExecutionPolicy -ExecutionPolicy RemoteSigned");
                psi2.AddScript(NAVTools);
                Console.WriteLine("Compiling objects ...");
                psi2.AddScript("Compile-NAVApplicationObject " +
                               GetParameter("DatabaseName", parms) + " -SynchronizeSchemaChanges Force");
                ExecutePowerShell(psi2, true);
            }
            za.Dispose();
            fs.Close();
            return(true);
        }