Skip to content

Commit

Permalink
SSM detailed version support (#1847)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaohuan771634 authored and PHILO-HE committed Jul 10, 2018
1 parent 94174e4 commit 48ccb0c
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 5 deletions.
15 changes: 11 additions & 4 deletions bin/ssm
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <config dir> [--daemon] [--hosts <hostA> <hostB> --hostsend] [--daemon start|stop ] [--remote] <command> <command args>"

Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -156,5 +164,4 @@ else
local_execute
fi
exit $?
fi

fi
2 changes: 1 addition & 1 deletion docs/ssm-deployment-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions smart-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.smartdata.versioninfo.VersionInfoWrite</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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();
}
}
Original file line number Diff line number Diff line change
@@ -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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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("<version>")) {
return st.trim().substring("<version>".length(),
st.trim().length() - "</version>".length());
}
}
return "Not found";
}

private String getUri() {
List<String> 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<String> 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<String> list = execCmd("whoami");
String user = "Unknown";
for (String s : list) {
user = s.trim();
break;
}
return user;
}

private String getBranch() {
List<String> list = execCmd("git branch");
String branch = "Unknown";
for (String s : list) {
if (s.startsWith("*")) {
branch = s.substring("*".length()).trim();
break;
}
}
return branch;
}

public List<String> execCmd(String cmd) {
String command = "/bin/sh -c " + cmd;
List<String> list = new ArrayList<String>();
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();
}
}

0 comments on commit 48ccb0c

Please sign in to comment.