Skip to content

Commit 53d0d42

Browse files
aj-jaswanthzabil
authored andcommitted
Detect linux distributions properly (#2608)
1 parent fde2b5c commit 53d0d42

File tree

17 files changed

+159
-493
lines changed

17 files changed

+159
-493
lines changed

agent/src/com/thoughtworks/go/agent/service/SslInfrastructureService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ protected Registration requestRegistration(String agentHostName, AgentAutoRegist
163163
.addParameter("uuid", agentRegistry.uuid())
164164
.addParameter("location", SystemUtil.currentWorkingDirectory())
165165
.addParameter("usablespace", String.valueOf(AgentRuntimeInfo.usableSpace(SystemUtil.currentWorkingDirectory())))
166-
.addParameter("operatingSystem", new SystemEnvironment().getOperatingSystemName())
166+
.addParameter("operatingSystem", new SystemEnvironment().getOperatingSystemCompleteName())
167167
.addParameter("agentAutoRegisterKey", agentAutoRegisterProperties.agentAutoRegisterKey())
168168
.addParameter("agentAutoRegisterResources", agentAutoRegisterProperties.agentAutoRegisterResources())
169169
.addParameter("agentAutoRegisterEnvironments", agentAutoRegisterProperties.agentAutoRegisterEnvironments())

agent/test/functional/com/thoughtworks/go/agent/service/RemoteRegistrationRequesterTest.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
import org.apache.http.client.utils.URLEncodedUtils;
3030
import org.hamcrest.Description;
3131
import org.hamcrest.TypeSafeMatcher;
32-
import org.junit.After;
33-
import org.junit.Before;
3432
import org.junit.Test;
3533

3634
import java.io.IOException;
3735
import java.io.InputStream;
3836
import java.util.List;
3937
import java.util.Properties;
4038

39+
import static org.hamcrest.Matchers.not;
40+
import static org.hamcrest.Matchers.nullValue;
4141
import static org.hamcrest.core.Is.is;
4242
import static org.hamcrest.core.Is.isA;
4343
import static org.junit.Assert.assertThat;
@@ -47,18 +47,6 @@
4747
import static org.mockito.Mockito.when;
4848

4949
public class RemoteRegistrationRequesterTest {
50-
private String original;
51-
52-
@Before
53-
public void before() {
54-
original = System.getProperty("os.name");
55-
System.setProperty("os.name", "minix");
56-
}
57-
58-
@After
59-
public void after() {
60-
System.setProperty("os.name", original);
61-
}
6250

6351
@Test
6452
public void shouldPassAllParametersToPostForRegistrationOfNonElasticAgent() throws IOException, ClassNotFoundException {
@@ -107,7 +95,7 @@ public boolean matchesSafely(HttpRequestBase item) {
10795
assertThat(getParameter(params, "uuid"), is(uuid));
10896
String workingDir = SystemUtil.currentWorkingDirectory();
10997
assertThat(getParameter(params, "location"), is(workingDir));
110-
assertThat(getParameter(params, "operatingSystem"), is("minix"));
98+
assertThat(getParameter(params, "operatingSystem"), not(nullValue()));
11199
assertThat(getParameter(params, "agentAutoRegisterKey"), is("t0ps3cret"));
112100
assertThat(getParameter(params, "agentAutoRegisterResources"), is("linux, java"));
113101
assertThat(getParameter(params, "agentAutoRegisterEnvironments"), is("uat, staging"));

base/src/com/thoughtworks/go/util/FileUtil.java

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,31 @@
2020
import org.apache.commons.lang.StringUtils;
2121
import org.apache.log4j.Logger;
2222

23-
import java.io.*;
23+
import java.io.File;
24+
import java.io.FileInputStream;
25+
import java.io.FileOutputStream;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.io.OutputStream;
2429
import java.math.BigDecimal;
2530
import java.math.RoundingMode;
2631
import java.net.URI;
27-
import java.util.*;
32+
import java.util.ArrayList;
33+
import java.util.Arrays;
34+
import java.util.Collections;
35+
import java.util.List;
36+
import java.util.Stack;
37+
import java.util.StringTokenizer;
38+
import java.util.UUID;
2839

2940
import static java.lang.System.getProperty;
3041

3142
public class FileUtil {
3243
private static final String CRUISE_TMP_FOLDER = "cruise" + "-" + UUID.randomUUID().toString();
3344
private static final Logger LOGGER = Logger.getLogger(FileUtil.class);
3445

35-
private static final boolean ON_NETWARE = Os.isFamily("netware");
36-
private static final boolean ON_DOS = Os.isFamily("dos");
37-
38-
public static final FileFilter NONHIDDEN_FILE_FILTER = new FileFilter() {
39-
public boolean accept(File pathname) {
40-
return !isHidden(pathname);
41-
}
42-
};
46+
private static final boolean ON_NETWARE = OperatingSystem.isFamily("netware");
47+
private static final boolean ON_DOS = OperatingSystem.isFamily("dos");
4348

4449
public static boolean isFolderEmpty(File folder) {
4550
if (folder == null) {
@@ -237,29 +242,14 @@ public static boolean isSubdirectoryOf(File parent, File subdirectory) throws IO
237242
//CopiedFromAnt
238243

239244
public static boolean isAbsolutePath(String filename) {
240-
int len = filename.length();
241-
if (len == 0) {
242-
return false;
243-
}
244-
char sep = File.separatorChar;
245-
filename = filename.replace('/', sep).replace('\\', sep);
246-
char c = filename.charAt(0);
247-
if (!(ON_DOS || ON_NETWARE)) {
248-
return (c == sep);
249-
}
250-
if (c == sep) {
251-
// CheckStyle:MagicNumber OFF
252-
if (!(ON_DOS && len > 4 && filename.charAt(1) == sep)) {
253-
return false;
245+
File file = new File(filename);
246+
boolean absolute = file.isAbsolute();
247+
if (absolute && OperatingSystem.isFamily(OperatingSystem.WINDOWS)) {
248+
if (filename.startsWith("\\\\") && !filename.matches("\\\\\\\\.*\\\\.+")) {
249+
absolute = false;
254250
}
255-
// CheckStyle:MagicNumber ON
256-
int nextsep = filename.indexOf(sep, 2);
257-
return nextsep > 2 && nextsep + 1 < len;
258-
}
259-
int colon = filename.indexOf(':');
260-
return (Character.isLetter(c) && colon == 1
261-
&& filename.length() > 2 && filename.charAt(2) == sep)
262-
|| (ON_NETWARE && colon > 0);
251+
}
252+
return absolute;
263253
}
264254

265255
public static String[] dissect(String path) {
Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/*************************GO-LICENSE-START*********************************
2-
* Copyright 2014 ThoughtWorks, Inc.
1+
/*
2+
* Copyright 2016 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -12,44 +12,67 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*************************GO-LICENSE-END***********************************/
15+
*/
1616

1717
package com.thoughtworks.go.util;
1818

19-
/**
20-
* @understands operating system type
21-
*/
22-
public enum OperatingSystem {
23-
LINUX("Linux"), OSX("Mac OS X"), SUN_OS("SunOS"), WINDOWS("Windows"), UNKNOWN("Unknown");
24-
private String name;
19+
import java.io.File;
20+
import java.io.FileReader;
21+
import java.util.Properties;
22+
import java.util.Scanner;
2523

26-
OperatingSystem(String name) {
27-
this.name = name;
24+
import static com.thoughtworks.go.util.StringUtil.isBlank;
25+
import static com.thoughtworks.go.util.StringUtil.unQuote;
26+
27+
public class OperatingSystem {
28+
private static final String OS_FAMILY_NAME = System.getProperty("os.name");
29+
public static final String WINDOWS = "Windows";
30+
private static String OS_COMPLETE_NAME = detectCompleteName();
31+
32+
private static String detectCompleteName() {
33+
String[] command = {"python", "-c", "import platform;print(platform.linux_distribution())"};
34+
try {
35+
Process process = Runtime.getRuntime().exec(command);
36+
Scanner scanner = new Scanner(process.getInputStream());
37+
String line = scanner.nextLine();
38+
OS_COMPLETE_NAME = cleanUpPythonOutput(line);
39+
} catch (Exception e) {
40+
try {
41+
OS_COMPLETE_NAME = readFromOsRelease();
42+
} catch (Exception ignored) {
43+
OS_COMPLETE_NAME = OS_FAMILY_NAME;
44+
}
45+
}
46+
return OS_COMPLETE_NAME;
2847
}
2948

30-
public static OperatingSystem fromProperty() {
31-
String osName = new SystemEnvironment().getPropertyImpl("os.name");
32-
return parseOperatingSystem(osName);
49+
private static String readFromOsRelease() throws Exception {
50+
try (FileReader fileReader = new FileReader(new File("/etc/os-release"))) {
51+
Properties properties = new Properties();
52+
properties.load(fileReader);
53+
return unQuote(properties.getProperty("PRETTY_NAME"));
54+
}
3355
}
3456

35-
public static OperatingSystem parseOperatingSystem(String osName) {
36-
if (osName.equals("Linux")) {
37-
return LINUX;
38-
} else if (osName.equals("SunOS")) {
39-
return SUN_OS;
40-
} else if (osName.equals("Mac OS X")) {
41-
return OSX;
42-
} else if (osName.startsWith("Windows")) {
43-
return WINDOWS;
57+
private static String cleanUpPythonOutput(String str) {
58+
String output = str.replaceAll("[()',]+", "");
59+
if (isBlank(output)) {
60+
throw new RuntimeException("The linux distribution string is empty");
4461
}
45-
return UNKNOWN;
62+
return output;
63+
}
64+
65+
public static String getCompleteName() {
66+
return OS_COMPLETE_NAME;
4667
}
4768

48-
@Override public String toString() {
49-
return name;
69+
public static String getFamilyName() {
70+
if (OS_FAMILY_NAME.startsWith(WINDOWS))
71+
return WINDOWS;
72+
return OS_FAMILY_NAME;
5073
}
5174

52-
public String osName() {
53-
return name;
75+
public static boolean isFamily(String familyName) {
76+
return getFamilyName().equals(familyName);
5477
}
5578
}

0 commit comments

Comments
 (0)