Updated Branches: refs/heads/master c852a28ac -> f02cc2bf9
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f02cc2bf/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/internal/RepositoryContext.java ---------------------------------------------------------------------- diff --git a/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/internal/RepositoryContext.java b/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/internal/RepositoryContext.java new file mode 100644 index 0000000..5f11077 --- /dev/null +++ b/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/internal/RepositoryContext.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.stratos.deployment.synchronizer.git.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.lib.Repository; + +import java.io.File; + +/** + * Git Repository Context class. Keeps track of git configurations per tenant. + */ +public class RepositoryContext { + + private static final Log log = LogFactory.getLog(RepositoryContext.class); + + private String gitRemoteRepoUrl; + private String gitLocalRepoPath; + private Repository localRepo; + private Git git; + private boolean cloneExists; + private int tenantId; + private File gitRepoDir; + private boolean keyBasedAuthentication; + private String repoUsername; + private String repoPassword; + + public RepositoryContext () { + + } + + public String getGitRemoteRepoUrl() { + return gitRemoteRepoUrl; + } + + public void setGitRemoteRepoUrl(String gitRemoteRepoUrl) { + this.gitRemoteRepoUrl = gitRemoteRepoUrl; + } + + public String getGitLocalRepoPath() { + return gitLocalRepoPath; + } + + public void setGitLocalRepoPath(String gitLocalRepoPath) { + this.gitLocalRepoPath = gitLocalRepoPath; + } + + public Repository getLocalRepo() { + return localRepo; + } + + public void setLocalRepo(Repository localRepo) { + this.localRepo = localRepo; + } + + public Git getGit() { + return git; + } + + public void setGit(Git git) { + this.git = git; + } + + public boolean cloneExists() { + return cloneExists; + } + + public void setCloneExists(boolean cloneExists) { + this.cloneExists = cloneExists; + } + + public int getTenantId() { + return tenantId; + } + + public void setTenantId(int tenantId) { + this.tenantId = tenantId; + } + + public File getGitRepoDir() { + return gitRepoDir; + } + + public void setGitRepoDir(File gitRepoDir) { + this.gitRepoDir = gitRepoDir; + } + + public boolean getKeyBasedAuthentication() { + return keyBasedAuthentication; + } + + public void setKeyBasedAuthentication(boolean keyBasedAuthentication) { + this.keyBasedAuthentication = keyBasedAuthentication; + } + + public String getRepoUsername() { + return repoUsername; + } + + public void setRepoUsername(String repoUsername) { + this.repoUsername = repoUsername; + } + + public String getRepoPassword() { + return repoPassword; + } + + public void setRepoPassword(String repoPassword) { + this.repoPassword = repoPassword; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f02cc2bf/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/util/GitDeploymentSyncronizerConfiguration.java ---------------------------------------------------------------------- diff --git a/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/util/GitDeploymentSyncronizerConfiguration.java b/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/util/GitDeploymentSyncronizerConfiguration.java new file mode 100644 index 0000000..d05642e --- /dev/null +++ b/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/util/GitDeploymentSyncronizerConfiguration.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.stratos.deployment.synchronizer.git.util; + +/** + * Configuration class + */ +public class GitDeploymentSyncronizerConfiguration { + + //public static String CARTRIDGE_SHORT_NAME; + //can take the server key from the name itself as default +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f02cc2bf/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/util/Utilities.java ---------------------------------------------------------------------- diff --git a/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/util/Utilities.java b/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/util/Utilities.java new file mode 100644 index 0000000..d09a76d --- /dev/null +++ b/products/cartridge-agent/modules/deployment-synchronizer/src/main/java/org/apache/stratos/deployment/synchronizer/git/util/Utilities.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.stratos.deployment.synchronizer.git.util; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.File; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * String Utility methods + */ +public class Utilities { + + private static final Log log = LogFactory.getLog(Utilities.class); + + /** + * Searches for a match in a input String against a regex + * + * @param input input String + * @param regex regex to match + * @param group grouping, + * + * @return result of the match if found, else empty String + */ + public static String getMatch (String input, String regex, int group) { + + String whitespaceRemovedJsonString = input.replaceAll("\\s+",""); + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(whitespaceRemovedJsonString); + if(!matcher.find()) + return ""; + else + return matcher.group(group).trim(); + } + + /** + * Deletes a folder structure recursively + * + * @param existingDir folder to delete + */ + public static void deleteFolderStructure (File existingDir) { + + try { + FileUtils.deleteDirectory(existingDir); + + } catch (IOException e) { + log.error("Deletion of existing non-git repository structure failed"); + e.printStackTrace(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f02cc2bf/products/cartridge-agent/modules/event-subscriber/pom.xml ---------------------------------------------------------------------- diff --git a/products/cartridge-agent/modules/event-subscriber/pom.xml b/products/cartridge-agent/modules/event-subscriber/pom.xml index 869e6e4..0093976 100644 --- a/products/cartridge-agent/modules/event-subscriber/pom.xml +++ b/products/cartridge-agent/modules/event-subscriber/pom.xml @@ -77,16 +77,11 @@ <artifactId>commons-io</artifactId> <version>2.0</version> </dependency> - <!-- <dependency> - <groupId>org.eclipse.jgit</groupId> - <artifactId>org.eclipse.jgit</artifactId> - <version>2.1.0.wso2v1</version> + <dependency> + <groupId>org.apache.stratos</groupId> + <artifactId>org.apache.stratos.deployment.synchronizer</artifactId> + <version>4.0.0-SNAPSHOT</version> </dependency> - <dependency> - <groupId>com.jcraft</groupId> - <artifactId>jsch</artifactId> - <version>0.1.49.wso2v1</version> - </dependency> --> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f02cc2bf/products/cartridge-agent/modules/event-subscriber/src/main/bin/event-subscriber.sh ---------------------------------------------------------------------- diff --git a/products/cartridge-agent/modules/event-subscriber/src/main/bin/event-subscriber.sh b/products/cartridge-agent/modules/event-subscriber/src/main/bin/event-subscriber.sh index a372410..01971ba 100644 --- a/products/cartridge-agent/modules/event-subscriber/src/main/bin/event-subscriber.sh +++ b/products/cartridge-agent/modules/event-subscriber/src/main/bin/event-subscriber.sh @@ -22,7 +22,7 @@ echo "Starting event subscriber..." lib_path=./../lib/ -class_path=${lib_path}commons-io-2.0.jar:${lib_path}andes-client-0.13.wso2v8.jar:${lib_path}apache-stratos-event-subscriber-4.0.0-SNAPSHOT.jar:${lib_path}commons-codec-1.8.jar:${lib_path}commons-logging-1.1.1.jar:${lib_path}geronimo-jms_1.1_spec-1.1.0.wso2v1.jar:${lib_path}geronimo-jms_1.1_spec-1.1.jar:${lib_path}gson-2.2.4.jar:${lib_path}log4j-1.2.13.jar:${lib_path}org.apache.log4j-1.2.13.v200706111418.jar:${lib_path}org.apache.stratos.messaging-4.0.0-SNAPSHOT.jar:${lib_path}org.wso2.carbon.logging-4.1.0.jar:${lib_path}slf4j-api-1.7.5.jar:${lib_path}slf4j-log4j12-1.7.5.jar +class_path=${lib_path}commons-io-2.0.jar:${lib_path}andes-client-0.13.wso2v8.jar:${lib_path}apache-stratos-event-subscriber-4.0.0-SNAPSHOT.jar:${lib_path}commons-codec-1.8.jar:${lib_path}commons-logging-1.1.1.jar:${lib_path}geronimo-jms_1.1_spec-1.1.0.wso2v1.jar:${lib_path}geronimo-jms_1.1_spec-1.1.jar:${lib_path}gson-2.2.4.jar:${lib_path}log4j-1.2.13.jar:${lib_path}org.apache.log4j-1.2.13.v200706111418.jar:${lib_path}org.apache.stratos.messaging-4.0.0-SNAPSHOT.jar:${lib_path}org.wso2.carbon.logging-4.1.0.jar:${lib_path}slf4j-api-1.7.5.jar:${lib_path}slf4j-log4j12-1.7.5.jar:${lib_path}org.apache.stratos.deployment.synchronizer-4.0.0-SNAPSHOT.jar java -cp $class_path org.apache.stratos.cartridge.agent.event.subscriber.Main ../conf echo "Event subscriber completed" http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f02cc2bf/products/cartridge-agent/modules/event-subscriber/src/main/java/org/apache/stratos/cartridge/agent/event/subscriber/ArtifactListener.java ---------------------------------------------------------------------- diff --git a/products/cartridge-agent/modules/event-subscriber/src/main/java/org/apache/stratos/cartridge/agent/event/subscriber/ArtifactListener.java b/products/cartridge-agent/modules/event-subscriber/src/main/java/org/apache/stratos/cartridge/agent/event/subscriber/ArtifactListener.java index e5a085b..3b1fc93 100644 --- a/products/cartridge-agent/modules/event-subscriber/src/main/java/org/apache/stratos/cartridge/agent/event/subscriber/ArtifactListener.java +++ b/products/cartridge-agent/modules/event-subscriber/src/main/java/org/apache/stratos/cartridge/agent/event/subscriber/ArtifactListener.java @@ -1,17 +1,40 @@ -/** - * +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ + package org.apache.stratos.cartridge.agent.event.subscriber; import java.io.File; import java.util.Scanner; +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.stratos.deployment.synchronizer.ArtifactRepository; +import org.apache.stratos.deployment.synchronizer.RepositoryInformation; +import org.apache.stratos.deployment.synchronizer.git.impl.GitBasedArtifactRepository; import org.apache.stratos.messaging.event.artifact.synchronization.ArtifactUpdatedEvent; import org.apache.stratos.messaging.util.Util; @@ -22,7 +45,6 @@ import org.apache.stratos.messaging.util.Util; public class ArtifactListener implements MessageListener{ private static final Log log = LogFactory.getLog(ArtifactListener.class); - private String script = "/opt/apache-stratos-cartridge-agent/git.sh"; private String launchParams = "/opt/apache-stratos-cartridge-agent/payload/launch-params"; @Override @@ -39,32 +61,35 @@ public class ArtifactListener implements MessageListener{ log.error("Exception is occurred " + e.getMessage(), e); } - // if(ArtifactUpdatedEvent.class.getName().equals(type)) { ArtifactUpdatedEvent event = (ArtifactUpdatedEvent) Util.jsonToObject(json, ArtifactUpdatedEvent.class); - String clusterIdInPayload = readClusterIdFromPayload(); + String clusterIdInPayload = readParamValueFromPayload("CLUSTER_ID"); + String localRepoPath = readParamValueFromPayload("APP_PATH"); String clusterIdInMessage = event.getClusterId(); String repoURL = event.getRepoURL(); - String repoPassword = event.getRepoPassword(); + String repoPassword = decryptPassword(event.getRepoPassword()); String repoUsername = event.getRepoUserName(); - - // execute script + String tenantId = event.getTenantId(); + + log.info("cluster id in payload " + clusterIdInPayload); + log.info("cluster id in message " + clusterIdInMessage); + if(clusterIdInPayload != null && clusterIdInPayload.equals(clusterIdInMessage)) { - try { - String command = script + " " + repoUsername+ " " +repoPassword+ " "+repoURL+ " /"; - log.info("Executing command " + command); - Process proc = Runtime.getRuntime().exec(command); - proc.waitFor(); - } catch (Exception e) { - //e.printStackTrace(); - log.error("Exception is occurred in executing script. " + e.getMessage(), e); - } + ArtifactRepository gitBasedArtifactRepository = new GitBasedArtifactRepository(); + RepositoryInformation repoInformation = new RepositoryInformation(); + repoInformation.setRepoUsername(repoUsername); + repoInformation.setRepoPassword(repoPassword); + repoInformation.setRepoUrl(repoURL); + repoInformation.setRepoPath(localRepoPath); + repoInformation.setTenantId(tenantId); + gitBasedArtifactRepository.checkout(repoInformation); } } - private String readClusterIdFromPayload() { - String clusterId = null; + + private String readParamValueFromPayload(String param) { + String paramValue = null; // read launch params File file = new File(launchParams); @@ -76,8 +101,8 @@ public class ArtifactListener implements MessageListener{ String[] params = line.split(","); for (String string : params) { String[] var = string.split("="); - if("CLUSTER_ID".equals(var[0])){ - clusterId = var[1]; + if(param.equals(var[0])){ + paramValue = var[1]; } } } @@ -87,7 +112,28 @@ public class ArtifactListener implements MessageListener{ log.error("Exception is occurred in reading file. ", e); } - return clusterId; + return paramValue; + } + + private String decryptPassword(String repoUserPassword) { + + String decryptPassword = ""; + String secret = readParamValueFromPayload("CARTRIDGE_KEY"); + SecretKey key; + Cipher cipher; + Base64 coder; + key = new SecretKeySpec(secret.getBytes(), "AES"); + try { + cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE"); + coder = new Base64(); + byte[] encrypted = coder.decode(repoUserPassword.getBytes()); + cipher.init(Cipher.DECRYPT_MODE, key); + byte[] decrypted = cipher.doFinal(encrypted); + decryptPassword = new String(decrypted); + } catch (Exception e) { + e.printStackTrace(); + } + return decryptPassword; } } http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f02cc2bf/products/cartridge-agent/modules/event-subscriber/src/main/java/org/apache/stratos/cartridge/agent/event/subscriber/Main.java ---------------------------------------------------------------------- diff --git a/products/cartridge-agent/modules/event-subscriber/src/main/java/org/apache/stratos/cartridge/agent/event/subscriber/Main.java b/products/cartridge-agent/modules/event-subscriber/src/main/java/org/apache/stratos/cartridge/agent/event/subscriber/Main.java index 6edff03..559d102 100644 --- a/products/cartridge-agent/modules/event-subscriber/src/main/java/org/apache/stratos/cartridge/agent/event/subscriber/Main.java +++ b/products/cartridge-agent/modules/event-subscriber/src/main/java/org/apache/stratos/cartridge/agent/event/subscriber/Main.java @@ -35,6 +35,7 @@ public class Main { log.info("Strating cartridge agent event subscriber"); System.setProperty("jndi.properties.dir", args[0]); + //initialting the subscriber TopicSubscriber subscriber = new TopicSubscriber(Constants.ARTIFACT_SYNCHRONIZATION_TOPIC); subscriber.setMessageListener(new ArtifactListener()); http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f02cc2bf/products/cartridge-agent/pom.xml ---------------------------------------------------------------------- diff --git a/products/cartridge-agent/pom.xml b/products/cartridge-agent/pom.xml index 797828a..99edcf2 100644 --- a/products/cartridge-agent/pom.xml +++ b/products/cartridge-agent/pom.xml @@ -34,6 +34,7 @@ <url>http://apache.org</url> <modules> + <module>modules/deployment-synchronizer</module> <module>modules/event-publisher</module> <module>modules/event-subscriber</module> <module>modules/health-stats</module>
