diff --git a/bin/ssm b/bin/ssm index aee1b73adfc..11795a18065 100755 --- a/bin/ssm +++ b/bin/ssm @@ -1,5 +1,4 @@ #!/usr/bin/env bash -# # 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 @@ -15,7 +14,6 @@ # 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. -# Usage="ssm --config [--daemon] [--hosts --hostsend] [--daemon start|stop ] [--remote] " @@ -37,8 +35,13 @@ DAEMON_MOD= DAEMON_MOD_OP= REMOTE_MODE= SMART_VARGS= +SSM_VERSION=false while [ $# != 0 ]; do case "$1" in + "version") + SSM_VERSION=true + break + ;; "--config") shift conf_dir="$1" @@ -106,6 +109,11 @@ addNonTestJarInDir "${SMART_HOME}/smart-server/target" addNonTestJarInDir "${SMART_HOME}/smart-agent/target" addJarInDir "${SMART_HOME}/lib" +if [ $SSM_VERSION = "true" ]; then + java -cp ${SMART_CLASSPATH} org.smartdata.versioninfo.VersionInfoRead + exit 1 +fi + if [ "$SMART_CLASSPATH" = "" ]; then SMART_CLASSPATH="${SMART_CONF_DIR}" else @@ -156,5 +164,4 @@ else local_execute fi exit $? -fi - +fi \ No newline at end of file diff --git a/docs/ssm-deployment-guide.md b/docs/ssm-deployment-guide.md index 9954e9e06ad..c5a63c0e8c5 100755 --- a/docs/ssm-deployment-guide.md +++ b/docs/ssm-deployment-guide.md @@ -204,7 +204,7 @@ On the SSM service server, switch to the SSM installation directory, ready to st # Run SSM --------------------------------------------------------------------------------- -Enter into ${SMART_HOME} directory for running SSM. +Enter into ${SMART_HOME} directory for running SSM. You can type `./bin/ssm version` to show specific version information for SSM. ## **Start SSM server** SSM server requires HDFS superuser privilege to access some Namenode APIs. So please make sure the account you used to start SSM has the privilege. diff --git a/smart-common/pom.xml b/smart-common/pom.xml index 059592ed657..882de0dccf2 100644 --- a/smart-common/pom.xml +++ b/smart-common/pom.xml @@ -113,6 +113,22 @@ + + org.codehaus.mojo + exec-maven-plugin + 1.1.1 + + + compile + + java + + + org.smartdata.versioninfo.VersionInfoWrite + + + + org.apache.maven.plugins maven-checkstyle-plugin diff --git a/smart-common/src/main/java/org/smartdata/versioninfo/VersionInfoRead.java b/smart-common/src/main/java/org/smartdata/versioninfo/VersionInfoRead.java new file mode 100644 index 00000000000..5cb507f4a20 --- /dev/null +++ b/smart-common/src/main/java/org/smartdata/versioninfo/VersionInfoRead.java @@ -0,0 +1,83 @@ +/** + * 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.smartdata.versioninfo; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class VersionInfoRead { + + Properties prop = new Properties(); + + protected VersionInfoRead(String component) { + InputStream in = null; + String s = component + "-versionInfo.properties"; + try { + in = Thread.currentThread().getContextClassLoader().getResourceAsStream(s); + prop.load(in); + } catch (Exception e) { + System.out.println(e); + } finally { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + private static VersionInfoRead info = new VersionInfoRead("common"); + + protected String getVersion() { + return info.prop.getProperty("version", "Unknown"); + } + + protected String getRevision() { + return info.prop.getProperty("revision", "Unknown"); + } + + protected String getTime() { + return info.prop.getProperty("date", "Unknown"); + } + + protected String getUser() { + return info.prop.getProperty("user", "Unknown"); + } + + protected String getUrl() { + return info.prop.getProperty("url", "Unknown"); + } + + protected String getBranch() { + return info.prop.getProperty("branch", "Unknown"); + } + + public void printInfo() { + System.out.println("SSM " + getVersion()); + System.out.println("Subversion " + getUrl()); + System.out.println("Last commit " + getRevision() + " on branch " + getBranch()); + System.out.println("Compiled by " + getUser() + " on " + getTime()); + } + + public static void main(String[] args) { + VersionInfoRead t = new VersionInfoRead("common"); + t.printInfo(); + } +} diff --git a/smart-common/src/main/java/org/smartdata/versioninfo/VersionInfoWrite.java b/smart-common/src/main/java/org/smartdata/versioninfo/VersionInfoWrite.java new file mode 100644 index 00000000000..3c56617d736 --- /dev/null +++ b/smart-common/src/main/java/org/smartdata/versioninfo/VersionInfoWrite.java @@ -0,0 +1,157 @@ +/** + * 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.smartdata.versioninfo; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Properties; +import java.util.TimeZone; + +public class VersionInfoWrite { + File directory = new File(""); + String pom = directory.getAbsolutePath() + "/" + "pom.xml"; + + public void execute() { + Properties prop = new Properties(); + OutputStream output = null; + String s = this.getClass().getResource("/").getPath() + "common-versionInfo.properties"; + try { + output = new FileOutputStream(s); + prop.setProperty("version", getVersionInfo(pom)); + prop.setProperty("revision", getCommit()); + prop.setProperty("user", getUser()); + prop.setProperty("date", getBuildTime()); + prop.setProperty("url", getUri()); + prop.setProperty("branch", getBranch()); + prop.store(output, new Date().toString()); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (output != null) { + try { + output.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private String getBuildTime() { + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + return dateFormat.format(new Date()); + } + + private String getVersionInfo(String pom) throws IOException { + File file = new File(pom); + FileReader fileReader = new FileReader(file); + String st; + BufferedReader br = new BufferedReader(fileReader); + while ((st = br.readLine()) != null) { + if (st.contains("")) { + return st.trim().substring("".length(), + st.trim().length() - "".length()); + } + } + return "Not found"; + } + + private String getUri() { + List list = execCmd("git remote -v"); + String uri = "Unknown"; + for (String s : list) { + if (s.startsWith("origin") && s.endsWith("(fetch)")) { + uri = s.substring("origin".length()); + uri = uri.substring(0, uri.length() - "(fetch)".length()); + break; + } + } + return uri.trim(); + } + + private String getCommit() { + List list = execCmd("git log -n 1"); + String commit = "Unknown"; + for (String s : list) { + if (s.startsWith("commit")) { + commit = s.substring("commit".length()); + break; + } + } + return commit.trim(); + } + + private String getUser() { + List list = execCmd("whoami"); + String user = "Unknown"; + for (String s : list) { + user = s.trim(); + break; + } + return user; + } + + private String getBranch() { + List list = execCmd("git branch"); + String branch = "Unknown"; + for (String s : list) { + if (s.startsWith("*")) { + branch = s.substring("*".length()).trim(); + break; + } + } + return branch; + } + + public List execCmd(String cmd) { + String command = "/bin/sh -c " + cmd; + List list = new ArrayList(); + try { + Runtime rt = Runtime.getRuntime(); + Process proc = rt.exec(cmd, null, null); + InputStream stderr = proc.getInputStream(); + InputStreamReader isr = new InputStreamReader(stderr, "UTF-8"); + BufferedReader br = new BufferedReader(isr); + String line; + while ((line = br.readLine()) != null) { + list.add(line); + } + } catch (Exception e) { + e.printStackTrace(); + } + return list; + } + + public static void main(String[] args) { + VersionInfoWrite w = new VersionInfoWrite(); + w.execute(); + } +}