-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor Platform into smaller parts
- Loading branch information
Showing
5 changed files
with
179 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* groovylint-disable DuplicateMapLiteral, DuplicateStringLiteral, UnnecessaryGetter, UnusedVariable */ | ||
@Library('jenkins-std-lib') | ||
import org.dsty.system.Platform | ||
import org.dsty.system.System | ||
|
||
node() { | ||
// Ignore this line its for catching CPS issues. | ||
sh(script: '#!/bin/bash\nset +x; > /dev/null 2>&1\necho Test for CPS issue', returnStdout: true) | ||
|
||
// Get the current system type like WINDOWS or UNIX | ||
System currentSystem = Platform.system() | ||
|
||
List<String> validDistros = ['Ubuntu', 'CentOS', 'UNKNOWN'] | ||
|
||
String currentDistro = currentSystem.distribution() | ||
|
||
if (!validDistros.contains(currentDistro)) { | ||
|
||
error("Should be a valid distribution but was ${currentDistro}") | ||
|
||
} | ||
|
||
// Ignore this line its for catching CPS issues. | ||
sh(script: '#!/bin/bash\nset +x; > /dev/null 2>&1\necho Test for CPS issue', returnStdout: true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,40 @@ | ||
/* groovylint-disable DuplicateMapLiteral, DuplicateStringLiteral, UnnecessaryGetter, UnusedVariable */ | ||
@Library('jenkins-std-lib') | ||
import org.dsty.system.Platform | ||
import org.dsty.system.System | ||
import org.dsty.system.os.shell.Shell | ||
|
||
node() { | ||
// Ignore this line its for catching CPS issues. | ||
String cps = sh(script: '#!/bin/bash\nset +x; > /dev/null 2>&1\necho Test for CPS issue', returnStdout: true) | ||
sh(script: '#!/bin/bash\nset +x; > /dev/null 2>&1\necho Test for CPS issue', returnStdout: true) | ||
|
||
// Get the current system type like WINDOWS or UNIX | ||
String systemType = Platform.system() | ||
System currentSystem = Platform.system() | ||
|
||
List<String> validSystems = ['UNIX', 'WINDOWS', 'DARWIN'] | ||
|
||
// These tests are generic because I cant be certain where | ||
// they will be run. | ||
if (!validSystems.contains(systemType)) { | ||
error("Should be a valid system but was ${systemType}") | ||
if (!validSystems.contains(currentSystem.name())) { | ||
error("Should be a valid system but was ${currentSystem}") | ||
} | ||
|
||
// Get the current CPU architechture | ||
String architecture = Platform.architecture() | ||
|
||
List<String> validArchs = ['x86', 'x64', 'x86_x64', 'arm', 'arm64'] | ||
List<String> validArchs = ['x86', 'x64', 'amd64', 'arm', 'arm64'] | ||
|
||
if (validArchs.contains(architecture)) { | ||
if (!validArchs.contains(architecture)) { | ||
error("Should be a valid architecture but was ${architecture}") | ||
} | ||
|
||
// Get a Shell for the current system | ||
Shell shell = Platform.getShell() | ||
|
||
// Make sure the shell works by writting a msg with it. | ||
String msg = "Ran on ${architecture} ${systemType}" | ||
String msg = "Ran on ${architecture} ${currentSystem}" | ||
shell("echo ${msg}") | ||
|
||
// Ignore this line its for catching CPS issues. | ||
cps = sh(script: '#!/bin/bash\nset +x; > /dev/null 2>&1\necho Test for CPS issue', returnStdout: true) | ||
sh(script: '#!/bin/bash\nset +x; > /dev/null 2>&1\necho Test for CPS issue', returnStdout: true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* groovylint-disable ThrowException, UnnecessaryGetter */ | ||
package org.dsty.system | ||
|
||
import org.dsty.system.os.shell.Shell | ||
import org.dsty.system.os.shell.Result | ||
|
||
/** | ||
* Provides information about the version of the current {@link System}. | ||
*/ | ||
class Distribution { | ||
|
||
/** | ||
* Returns the name of the current {@link System System's} distribution. | ||
* <p> | ||
* Currently only {@link System#UNIX} is supported. The distribution is determined by | ||
* <code>lsb_release -i</code>. If the {@link System} doesn't have <code>lsb_release</code> | ||
* installed than <code>UNKNOWN</code> is returned. | ||
* | ||
* @param system The current {@link System}. | ||
* @return The name of the current distribution or <code>UNKNOWN</code>. | ||
*/ | ||
static String name(System system) { | ||
|
||
String dist | ||
|
||
switch (system) { | ||
case system.UNIX: | ||
dist = unixDist(system) | ||
break | ||
default: | ||
throw new Exception("Distribution name is not supported for ${system}.") | ||
} | ||
|
||
return dist | ||
} | ||
|
||
/** | ||
* Get the distribution name for <code>UNIX</code> {@link System Systems}. | ||
* <p> | ||
* On <code>UNIX</code> {@link System Systems} this value comes from | ||
* <code>lsb_release -i</code>. | ||
* | ||
* @param system The current {@link System}. | ||
* @return The name of the current <code>UNIX</code> distribution or <code>UNKNOWN</code>. | ||
*/ | ||
private static String unixDist(System system) { | ||
|
||
String distName = 'UNKNOWN' | ||
|
||
Shell shell = system.getShell() | ||
|
||
Result result = shell.ignoreErrors('lsb_release -i', true) | ||
|
||
if (!result.exitCode) { | ||
distName = result.stdOut.split()[-1] | ||
} | ||
|
||
return distName | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* groovylint-disable UnnecessaryPackageReference */ | ||
package org.dsty.system | ||
|
||
import org.dsty.system.os.shell.Shell | ||
|
||
/** | ||
* The type of agent the current build is running on. | ||
*/ | ||
enum System { | ||
|
||
/** | ||
* A system derived from <code>UNIX</code>. | ||
*/ | ||
UNIX, | ||
|
||
/** | ||
* A system made by Microsoft. | ||
*/ | ||
WINDOWS, | ||
|
||
/** | ||
* A system made by Apple. | ||
*/ | ||
DARWIN | ||
|
||
/** | ||
* Gets the current {@link Shell} for the {@link System}. | ||
* <p> | ||
* Most {@link System Systems} can have more than one {@link Shell}. The default shell is | ||
* determine by {@link Platform#unixShell}, {@link Platform#winShell} and {@link Platform#darwinShell}. | ||
* | ||
* @return The {@link Shell} for the current {@link System}. | ||
*/ | ||
Shell getShell() { | ||
|
||
Class<Shell> shell | ||
|
||
switch (this) { | ||
case UNIX: | ||
shell = Platform.unixShell | ||
break | ||
case WINDOWS: | ||
shell = Platform.winShell | ||
break | ||
case DARWIN: | ||
shell = Platform.darwinShell | ||
break | ||
} | ||
|
||
return shell.newInstance() | ||
} | ||
|
||
/** | ||
* The name of the current {@link System System's} OS. | ||
* <p> | ||
* For {@link #UNIX} this might be <code>Ubuntu</code> or <code>CentOS</code>. | ||
* | ||
* @return The name of the OS. | ||
*/ | ||
String distribution() { | ||
return Distribution.name(this) | ||
} | ||
|
||
/** | ||
* The architecture value that is returned by the | ||
* <code>os.arch</code> java property. | ||
* | ||
* @return The architecture type. | ||
*/ | ||
String architecture() { | ||
return java.lang.System.getProperty('os.arch') | ||
} | ||
|
||
} |