private void buttonPlatUpdate_Click(object sender, EventArgs e) { try { TableLayoutControlCollection tableLayoutCtrlColl = this.tableLayoutPanelPlatform.Controls; System.Windows.Forms.Control labelDepVer = tableLayoutCtrlColl[this.labelPlatDeployedVer.Name]; System.Windows.Forms.Control labelCurVer = tableLayoutCtrlColl[this.labelPlatYourVer.Name]; // string hubId = checkbox.Text; string deployedVersion = labelDepVer.Text; string currentVersion = labelCurVer.Text; string platformDirPath = ""; Version latestVersion = null; string platformRootDir = this.textBoxSetupWorkingFolder.Text + "\\binaries\\Platform"; string repositoryDir = this.textBoxSetupWorkingFolder.Text + "\\HomeStore\\Repository"; string ftpHost = this.formRepoAccountInfo.RepoAccountHost; string ftpPort = this.formRepoAccountInfo.RepoAccountPort; string ftpUser = this.formRepoAccountInfo.RepoAccountLogin; string ftpPassword = this.formRepoAccountInfo.RepoAccountPassword; // make sure you are updating to a higher version Version versionDeployed = new Version(deployedVersion); Version versionCurrent = new Version(currentVersion); if (versionCurrent <= versionDeployed) { MessageBox.Show(string.Format("Only Updates to higher versions of the platform are permissable, please update the version!"), "Update Platform", MessageBoxButtons.OK, MessageBoxIcon.Exclamation ); return; } if (DialogResult.Yes == MessageBox.Show((IWin32Window)sender, string.Format("Are you sure you want to update the platform from the deployed version {0} to your current version {1}?", deployedVersion, currentVersion), "Update Platform", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2)) { string[] filePaths = new string[0]; bool success = BinaryPackagerHelper.Package(platformRootDir, MainForm.PlatformFilename, true /*singleBin*/, "exe", "platform", repositoryDir, ref filePaths, this.logger); if (!success) { throw new Exception("Failed to create platform update packages"); } Debug.Assert(filePaths.Length == 2); for (int i = 0; i < filePaths.Length; ++i) { string[] ftpFileLocations = filePaths[i].Split(new[] { repositoryDir }, StringSplitOptions.RemoveEmptyEntries); if (ftpFileLocations.Length > 0 && !string.IsNullOrWhiteSpace(ftpFileLocations[0])) { string ftpFilePath = ftpFileLocations[0].Replace("\\", "/"); string ftpDirPath = ftpFilePath.Substring(0, ftpFilePath.LastIndexOf("/")); Uri uriFile = new Uri(ftpHost + ":" + ftpPort + ftpFilePath); string subDirPath = ""; foreach (string subdir in ftpDirPath.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries)) { subDirPath += ("/" + subdir); if (platformDirPath.Length == 0 && subDirPath.ToLower().Contains(PlatformName.ToLower())) { platformDirPath = subDirPath; } if (subdir.Contains('.')) { // assume this is the version number of the type x.x.x.x latestVersion = new Version(subdir); } Uri uriDir = new Uri(ftpHost + ":" + ftpPort + subDirPath); if (!MainForm.IsFtpRemoteDirectoryPresent(uriDir, ftpUser, ftpPassword)) { SecureFtpRepoUpdate.MakeDirectory(uriDir, ftpUser, ftpPassword, true); } } SecureFtpRepoUpdate.UploadFile(uriFile, filePaths[i], ftpUser, ftpPassword, true); } } // update the latest folder for the platform, if this is highest version or if there is no latest folder at all Uri uriPlatformDir = new Uri(ftpHost + ":" + ftpPort + platformDirPath); string platformLatestDir = ftpHost + ":" + ftpPort + platformDirPath + "/Latest"; Uri uriPlatformLatestDir = new Uri(platformLatestDir); if (!MainForm.IsFtpRemoteDirectoryPresent(uriPlatformLatestDir, ftpUser, ftpPassword) || MainForm.GetFtpHighestVersionFromDir(uriPlatformDir, ftpUser, ftpPassword, this.logger) == latestVersion) { for (int i = 0; i < filePaths.Length; ++i) { string filename = filePaths[i].Substring(filePaths[i].LastIndexOf('\\') + 1); Uri uriFile = new Uri(platformLatestDir + "/" + filename); if (!MainForm.IsFtpRemoteDirectoryPresent(uriPlatformLatestDir, ftpUser, ftpPassword)) { SecureFtpRepoUpdate.MakeDirectory(uriPlatformLatestDir, ftpUser, ftpPassword, true); } SecureFtpRepoUpdate.UploadFile(uriFile, filePaths[i], ftpUser, ftpPassword, true); } } } } catch (Exception exception) { this.logger.ErrorException("Exception while trying to update platform on the ftp server", exception); outputBox.Text += "Exception while trying to update platform on the ftp server" + exception + "\r\n"; } }
private bool UpdateRepositoryWithFiles(string[] filePaths, string binType) { bool success = true; try { Version latestVersion = null; string binaryDirPath = ""; for (int i = 0; i < filePaths.Length; ++i) { string[] ftpFileLocations = filePaths[i].Split(new[] { this.repositoryDir }, StringSplitOptions.RemoveEmptyEntries); if (ftpFileLocations.Length > 0 && !string.IsNullOrWhiteSpace(ftpFileLocations[0])) { string ftpFilePath = ftpFileLocations[0].Replace("\\", "/"); string ftpDirPath = ftpFilePath.Substring(0, ftpFilePath.LastIndexOf("/")); Uri uriFile = new Uri(this.ftpHost + ":" + this.ftpPort + ftpFilePath); string subDirPath = ""; bool gotBinaryDirPath = false; foreach (string subdir in ftpDirPath.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries)) { subDirPath += ("/" + subdir); if (gotBinaryDirPath) { binaryDirPath = subDirPath; gotBinaryDirPath = false; } else if (binaryDirPath.Length == 0 && subDirPath.ToLower().Contains(binType.ToLower())) { gotBinaryDirPath = true; } else if (subdir.Contains('.')) { // assume this is the version number of the type x.x.x.x latestVersion = new Version(subdir); //fix the bug that causes incorrect path to be formed for Latest dir for x/Gadgeteer/MicrosoftResearch/X binaryDirPath = subDirPath.Substring(0, subDirPath.LastIndexOf("/")); } Uri uriDir = new Uri(this.ftpHost + ":" + this.ftpPort + subDirPath); if (!MainForm.IsFtpRemoteDirectoryPresent(uriDir, this.ftpUser, this.ftpPassword)) { SecureFtpRepoUpdate.MakeDirectory(uriDir, this.ftpUser, this.ftpPassword, true); } } SecureFtpRepoUpdate.UploadFile(uriFile, filePaths[i], this.ftpUser, this.ftpPassword, true); } } // update the latest folder for the binary, if this is highest version or if there is no latest folder at all Uri uriBinaryDir = new Uri(this.ftpHost + ":" + this.ftpPort + binaryDirPath); string binaryLatestDir = this.ftpHost + ":" + this.ftpPort + binaryDirPath + "/Latest"; Uri uriBinaryLatestDir = new Uri(binaryLatestDir); if (!MainForm.IsFtpRemoteDirectoryPresent(uriBinaryLatestDir, this.ftpUser, this.ftpPassword) || MainForm.GetFtpHighestVersionFromDir(uriBinaryDir, this.ftpUser, this.ftpPassword, this.logger) == latestVersion) { for (int i = 0; i < filePaths.Length; ++i) { string filename = filePaths[i].Substring(filePaths[i].LastIndexOf('\\') + 1); Uri uriFile = new Uri(binaryLatestDir + "/" + filename); if (!MainForm.IsFtpRemoteDirectoryPresent(uriBinaryLatestDir, this.ftpUser, this.ftpPassword)) { SecureFtpRepoUpdate.MakeDirectory(uriBinaryLatestDir, this.ftpUser, this.ftpPassword, true); } SecureFtpRepoUpdate.UploadFile(uriFile, filePaths[i], this.ftpUser, this.ftpPassword, true); } //Here we will upload the [binary].dll.config file, which contains the homeosupdate version of the binary, to the Latest dir on the repository string versionfile = filePaths[0].Substring(filePaths[0].LastIndexOf('\\') + 1).Replace("zip", "dll.config"); string binaryname = versionfile.Replace(".dll.config", ""); string versionfilepath = this.binaryRootDir + "\\" + binaryname + "\\" + versionfile; Uri uriversionfile = new Uri(binaryLatestDir + "/" + versionfile); SecureFtpRepoUpdate.UploadFile(uriversionfile, versionfilepath, this.ftpUser, this.ftpPassword, true); } } catch (Exception exception) { this.logger.ErrorException("Exception while trying to update binary package (plus hash file) on the ftp server", exception); success = false; } return(success); }
public void SecureFtpRepoUpdateTest_MakeDirectory_UsingFtps() { SecureFtpRepoUpdate.MakeDirectory(uriDir, remoteUsername, remoteUserPassword, true /*enableSSL*/); }