diff --git a/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/download/DownloadFileUtilities.java b/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/download/DownloadFileUtilities.java index 20716964a08..47562e73237 100644 --- a/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/download/DownloadFileUtilities.java +++ b/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/download/DownloadFileUtilities.java @@ -26,11 +26,11 @@ public static File getDpDownloadFile(DeploymentPackageInstallOptions options) th String downloadDirectory = options.getDownloadDirectory(); String packageFilename; if (!options.getSystemUpdate()) { - String dpName = FileUtilities.getFileName(options.getDpName(), options.getDpVersion(), ".dp"); + String dpName = FileUtilities.getFileName(options.getDpName(), options.getDpVersion(), ".dp", "_"); packageFilename = new StringBuilder().append(downloadDirectory).append(File.separator).append(dpName) .toString(); } else { - String shName = FileUtilities.getFileName(options.getDpName(), options.getDpVersion(), ".sh"); + String shName = FileUtilities.getFileName(options.getDpName(), options.getDpVersion(), ".sh", "-"); packageFilename = new StringBuilder().append(downloadDirectory).append(File.separator).append(shName) .toString(); } diff --git a/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/download/impl/DownloadImpl.java b/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/download/impl/DownloadImpl.java index 43da4a16f4f..40fa7eb0fd2 100644 --- a/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/download/impl/DownloadImpl.java +++ b/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/download/impl/DownloadImpl.java @@ -211,7 +211,8 @@ private void incrementalDownloadFromURL(File dpFile, String url, int downloadInd } s_logger.info("--> Going to verify hash signature!"); try { - // these things should be checked beforehand, so that hash() has a chance to succeed + // these things should be checked beforehand, so that hash() has a chance to + // succeed if (hashAlgorithm == null || "".equals(hashAlgorithm) || hashValue == null || "".equals(hashValue)) { throw new KuraException(KuraErrorCode.INTERNAL_ERROR, null, "Failed to verify checksum with empty algorithm: " + hashAlgorithm); @@ -278,7 +279,7 @@ private void downloadFailedAsync(int downloadIndex, Exception e) { private File getDpVerifierFile(DeploymentPackageInstallOptions options) { - String shName = FileUtilities.getFileName(options.getDpName(), options.getDpVersion(), ".sh_verifier.sh"); + String shName = FileUtilities.getFileName(options.getDpName(), options.getDpVersion(), ".sh_verifier.sh", "-"); String packageFilename = new StringBuilder().append(this.verificationDirectory).append(File.separator) .append(shName).toString(); diff --git a/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/install/InstallImpl.java b/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/install/InstallImpl.java index 7670d3d0d52..771e30c5465 100644 --- a/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/install/InstallImpl.java +++ b/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/install/InstallImpl.java @@ -39,6 +39,7 @@ import org.eclipse.kura.core.deployment.DeploymentPackageOptions; import org.eclipse.kura.core.deployment.InstallStatus; import org.eclipse.kura.core.deployment.download.DeploymentPackageDownloadOptions; +import org.eclipse.kura.core.deployment.util.FileUtilities; import org.eclipse.kura.core.linux.executor.LinuxSignal; import org.eclipse.kura.executor.Command; import org.eclipse.kura.executor.CommandExecutorService; @@ -272,36 +273,35 @@ private DeploymentPackage installDeploymentPackageInternal(File fileReference) throws DeploymentException, IOException { DeploymentPackage dp = null; - File dpPersistentFile = null; File downloadedFile = fileReference; - File packagesFolder = new File(this.packagesPath); + StringBuilder dpPersistentFilePath = new StringBuilder(); try (InputStream dpInputStream = new FileInputStream(downloadedFile);) { - StringBuilder pathSB = new StringBuilder(); - pathSB.append(this.packagesPath); - pathSB.append(File.separator); dp = this.deploymentAdmin.installDeploymentPackage(dpInputStream); - pathSB.append(dp.getName()).append("_"); - pathSB.append(dp.getVersion()).append(".dp"); - String dpPersistentFilePath = pathSB.toString(); - dpPersistentFile = new File(pathSB.toString()); - - // Now we need to copy the deployment package file to the Kura - // packages directory unless it's already there. - if (!downloadedFile.getCanonicalPath().startsWith(packagesFolder.getCanonicalPath())) { - logger.debug("dpFile.getCanonicalPath(): {}", downloadedFile.getCanonicalPath()); - logger.debug("dpPersistentFile.getCanonicalPath(): {}", dpPersistentFile.getCanonicalPath()); - FileUtils.copyFile(downloadedFile, dpPersistentFile); - addPackageToConfFile(dp.getName(), "file:" + dpPersistentFilePath); + dpPersistentFilePath.append(this.packagesPath); + dpPersistentFilePath.append(File.separator); + dpPersistentFilePath + .append(FileUtilities.getFileName(dp.getName(), dp.getVersion().toString(), ".dp", "_")); + File dpPersistentFile = new File(dpPersistentFilePath.toString()); + + boolean isDownloadedInPersistentPath = downloadedFile.getCanonicalPath() + .equals(dpPersistentFile.getCanonicalPath()); + + if (!isDownloadedInPersistentPath) { + logger.info("Moving downloaded DP from '{}' to '{}'.", downloadedFile.getCanonicalPath(), + dpPersistentFile.getCanonicalPath()); + Files.deleteIfExists(dpPersistentFile.toPath()); + FileUtils.moveFile(downloadedFile, dpPersistentFile); } - } catch (IOException ex) { + addPackageToConfFile(dp.getName(), "file:" + dpPersistentFilePath.toString()); + } catch (IOException ex) { + logger.error("Unable to move downloaded DP from '" + downloadedFile.getCanonicalPath() + "' to '" + + dpPersistentFilePath.toString() + "'.", ex); } finally { - // The file from which we have installed the deployment package will be deleted - // unless it's a persistent deployment package file. - if (!downloadedFile.getCanonicalPath().startsWith(packagesFolder.getCanonicalPath())) { + if (!downloadedFile.getCanonicalPath().equals(dpPersistentFilePath.toString())) { downloadedFile.delete(); } } diff --git a/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/util/FileUtilities.java b/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/util/FileUtilities.java index c0fa93805bb..fa0d429ad37 100644 --- a/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/util/FileUtilities.java +++ b/kura/org.eclipse.kura.core.deployment/src/main/java/org/eclipse/kura/core/deployment/util/FileUtilities.java @@ -16,8 +16,10 @@ public class FileUtilities { public static String getFileName(String dpName, String dpVersion, String extension) { - String packageFilename = new StringBuilder().append(dpName).append("-").append(dpVersion).append(extension) - .toString(); - return packageFilename; + return getFileName(dpName, dpVersion, extension, "-"); + } + + public static String getFileName(String dpName, String dpVersion, String extension, String separator) { + return dpName + separator + dpVersion + extension; } } diff --git a/kura/org.eclipse.kura.deployment.agent/src/main/java/org/eclipse/kura/deployment/agent/impl/DeploymentAgent.java b/kura/org.eclipse.kura.deployment.agent/src/main/java/org/eclipse/kura/deployment/agent/impl/DeploymentAgent.java index 3fa37fd83cd..67a5c644ba2 100644 --- a/kura/org.eclipse.kura.deployment.agent/src/main/java/org/eclipse/kura/deployment/agent/impl/DeploymentAgent.java +++ b/kura/org.eclipse.kura.deployment.agent/src/main/java/org/eclipse/kura/deployment/agent/impl/DeploymentAgent.java @@ -380,13 +380,15 @@ private DeploymentPackage installDeploymentPackageInternal(String urlSpec) if (!dpFile.getCanonicalPath().equals(dpPersistentFile.getCanonicalPath())) { logger.debug("dpFile.getCanonicalPath(): {}", dpFile.getCanonicalPath()); logger.debug("dpPersistentFile.getCanonicalPath(): {}", dpPersistentFile.getCanonicalPath()); - FileUtils.copyFile(dpFile, dpPersistentFile); - addPackageToConfFile(dp.getName(), "file:" + dpPersistentFilePath); + Files.deleteIfExists(dpPersistentFile.toPath()); + FileUtils.moveFile(dpFile, dpPersistentFile); } + + addPackageToConfFile(dp.getName(), "file:" + dpPersistentFilePath); } finally { // The file from which we have installed the deployment package will be deleted // unless it's a persistent deployment package file. - if (dpPersistentFile != null && dpPersistentFile.exists() + if (dpPersistentFile != null && dpPersistentFile.exists() && dpFile.exists() && !dpFile.getCanonicalPath().equals(dpPersistentFile.getCanonicalPath())) { Files.delete(dpFile.toPath()); logger.debug("Deleted file: {}", dpFile.getName()); diff --git a/kura/test/org.eclipse.kura.core.deployment.test/src/test/java/org/eclipse/kura/core/deployment/install/InstallImplTest.java b/kura/test/org.eclipse.kura.core.deployment.test/src/test/java/org/eclipse/kura/core/deployment/install/InstallImplTest.java index f3d0f3711ce..ed44ef7b470 100644 --- a/kura/test/org.eclipse.kura.core.deployment.test/src/test/java/org/eclipse/kura/core/deployment/install/InstallImplTest.java +++ b/kura/test/org.eclipse.kura.core.deployment.test/src/test/java/org/eclipse/kura/core/deployment/install/InstallImplTest.java @@ -48,6 +48,7 @@ import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.osgi.framework.Version; import org.osgi.service.deploymentadmin.DeploymentAdmin; import org.osgi.service.deploymentadmin.DeploymentException; import org.osgi.service.deploymentadmin.DeploymentPackage; @@ -89,12 +90,19 @@ public void testGetDeployedPackages() throws IOException { } @Test - public void testInstallDpSuccessMessage() throws KuraException { + public void testInstallDpSuccessMessage() throws KuraException, DeploymentException { CloudDeploymentHandlerV2 callbackMock = mock(CloudDeploymentHandlerV2.class); String kuraDataDir = "/tmp"; InstallImpl ii = new InstallImpl(callbackMock, kuraDataDir, null); ii.setPackagesPath(kuraDataDir); + DeploymentAdmin mockDeploymentAdmin = mock(DeploymentAdmin.class); + DeploymentPackage dpMock = mock(DeploymentPackage.class); + when(dpMock.getName()).thenReturn("dpname"); + when(mockDeploymentAdmin.installDeploymentPackage(Mockito.any())).thenReturn(dpMock); + ii.setDeploymentAdmin(mockDeploymentAdmin); + ii.setDpaConfPath("/tmp/dpa.properties"); + final String clientId = "clientid"; final long jobid = 1234; @@ -113,8 +121,8 @@ public Object answer(InvocationOnMock invocation) throws Throwable { KuraInstallPayload kp = (KuraInstallPayload) arguments[1]; assertEquals("", clientId, kp.getClientId()); - assertEquals("", 100, kp.getInstallProgress()); assertEquals("", InstallStatus.COMPLETED.getStatusString(), kp.getInstallStatus()); + assertEquals("", 100, kp.getInstallProgress()); assertEquals("", jobid, kp.getJobId().longValue()); return null; @@ -195,6 +203,8 @@ public void testInstallDeploymentPackageInternal() throws Throwable { ii.setPackagesPath(kuraDataDir); + ii.setDpaConfPath("/tmp/dpa.properties"); + File persDir = new File(kuraDataDir, "persistance"); persDir.mkdirs(); File veriDir = new File(persDir, "verification"); @@ -205,6 +215,8 @@ public void testInstallDeploymentPackageInternal() throws Throwable { dpFile.createNewFile(); DeploymentPackage dpMock = mock(DeploymentPackage.class); + when(dpMock.getName()).thenReturn("dp"); + when(dpMock.getVersion()).thenReturn(new Version("1.0.0")); when(deploymentAdminMock.installDeploymentPackage(any())).thenReturn(dpMock); Object dp = TestUtil.invokePrivate(ii, "installDeploymentPackageInternal", dpFile); @@ -246,6 +258,7 @@ public Properties getDeployedPackages() { when(deploymentAdminMock.installDeploymentPackage(any())).thenReturn(dpMock); when(dpMock.getName()).thenReturn("dpname"); + when(dpMock.getVersion()).thenReturn(new Version("1.0.0")); ii.setDpaConfPath(null); // make sure this is null, so that we don't test too much ii.setPackagesPath(pkgDir.getCanonicalPath());