Exemple #1
0
 internal static byte[] Serialize(UpdateSettings settings)
 {
     using (var stream = new MemoryStream())
     {
         var formatter = new BinaryFormatter();
         formatter.Serialize(stream, settings);
         return(stream.ToArray());
     }
 }
Exemple #2
0
        public static InstallResult SendUpdateTrigger(HttpPostedFileBase file)
        {
            InstallResult result = new InstallResult();

            try
            {
                //Check to make sure its a zip file.
                if (!file.FileName.EndsWith(".zip"))
                {
                    result.Success = false;
                    result.Message = "The update file must be a zip file.";
                    return(result);
                }

                //Read the bytes out of the files
                byte[] fileBytes;
                using (var reader = new System.IO.BinaryReader(file.InputStream))
                {
                    fileBytes = reader.ReadBytes(file.ContentLength);
                }

                //Create the update setings
                UpdateSettings settings = new UpdateSettings();
                settings.Key        = AppSettings.PrivateKey;
                settings.UpdateFile = fileBytes;

                //Send the command to the server.
                Amie.Client.AsyncClient client = new Amie.Client.AsyncClient();
                client.StartClient(AppSettings.TCPClientAddress, AppSettings.TCPClientPort);
                byte[] bytesToSend = UpdateSettings.Serialize(settings);
                client.SendBytes(bytesToSend, true);

                //TODO: Need to buffer server response and send it back to the calling function....
                return(result);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.ToString();
                return(result);
            }
        }
Exemple #3
0
        internal static void ExtractUpdateAndRun(UpdateSettings updateSettings)
        {
            Amie.Logger.Info("Checking package integrity...");
            if (updateSettings.Key != AppSettings.PrivateKey)
            {
                Amie.Logger.Error("Package integrity check failed. Update not processed.");
                return;
            }

            Amie.Logger.Info("Check for update file...");
            if (updateSettings.UpdateFile == null)
            {
                Amie.Logger.Error("Update file not found in package.  Update not processed.");
                return;
            }

            MemoryStream stream  = new MemoryStream(updateSettings.UpdateFile);
            ZipArchive   archive = new ZipArchive(stream);
            AppInfo      appInfo = GetAppInfo(archive);

            if (appInfo == null)
            {
                Amie.Logger.Error("The update package is not structured correctly. Is there a file named AppInfo.json in the package? Update not processed.");
                return;
            }


            if (!Directory.Exists(AppSettings.BaseUpdatePath))
            {
                Directory.CreateDirectory(AppSettings.BaseUpdatePath);
            }

            string defaultSavedUpdateDirectory = AppSettings.BaseUpdatePath + Path.DirectorySeparatorChar + appInfo.ProductName + "_" + appInfo.Version;

            string savedUpdateDirectory = GetSavedUpdateDirectory(defaultSavedUpdateDirectory);
            string savedUpdateFileName  = savedUpdateDirectory + ".zip";

            Amie.Logger.Info("Saving update package to {0}.", savedUpdateFileName);
            File.WriteAllBytes(savedUpdateFileName, updateSettings.UpdateFile);

            Amie.Logger.Info("Creating the unzip directory {0}.", savedUpdateDirectory);
            Directory.CreateDirectory(savedUpdateDirectory);

            Amie.Logger.Info("Unzipping update package.");
            ZipFile.ExtractToDirectory(savedUpdateFileName, savedUpdateDirectory);

            //If anything fails below here we must delete the update directory so that the user can try again.  The ExtractToDirectory will not work if there are matching files in the folder.
            try
            {
                Amie.Logger.Info("Executing update executable {0}.", appInfo.UpdateExecutableName);
                string updateExeLocation = GetUpdateExecutableLocation(appInfo, savedUpdateDirectory);
                if (updateExeLocation != "")
                {
                    var result = RunUpdateCommand(updateExeLocation);
                    if (!result.Success)
                    {
                        Amie.Logger.Error(result.Message);
                        RecursiveDelete(savedUpdateDirectory);
                    }
                }
                else
                {
                    Amie.Logger.Error("The update executable {0} could not be found at {1}. Update not processed.", appInfo.UpdateExecutableName, updateExeLocation);
                    RecursiveDelete(savedUpdateDirectory);
                }
            }
            catch (Exception)
            {
                RecursiveDelete(savedUpdateDirectory);
                throw;
            }

            return;
        }