1818
1919import hudson .model .Node ;
2020import hudson .model .TaskListener ;
21- import hudson .tools .DownloadFromUrlInstaller ;
21+ import hudson .tools .ToolInstaller ;
2222import hudson .tools .ToolInstallation ;
2323import hudson .tools .ToolInstallerDescriptor ;
2424
4545import org .kohsuke .stapler .QueryParameter ;
4646import org .kohsuke .stapler .verb .POST ;
4747
48- public class MatlabInstaller extends DownloadFromUrlInstaller {
48+ public class MatlabInstaller extends ToolInstaller {
4949
50- private String version ;
50+ private String release ;
5151 private String products ;
5252 private static String DEFAULT_PRODUCT = "MATLAB" ;
5353
5454 @ DataBoundConstructor
55- public MatlabInstaller (String id ) {
56- super (id );
55+ public MatlabInstaller (String id ) {
56+ super (id );
5757 }
5858
59- public String getVersion () {
60- return this .version ;
59+ public String getRelease () {
60+ return this .release ;
6161 }
6262
6363 @ DataBoundSetter
64- public void setVersion (String version ) {
65- this .version = version ;
64+ public void setVersion (String release ) {
65+ this .release = release ;
6666 }
6767
68- public String getProducts () {
68+ public String getProducts () {
6969 return this .products ;
7070 }
7171
7272 @ DataBoundSetter
73- public void setProducts (String products ) {
73+ public void setProducts (String products ) {
7474 this .products = products ;
7575 }
7676
7777 @ Override
78- public FilePath performInstallation (ToolInstallation tool , Node node , TaskListener log )
78+ public FilePath performInstallation (ToolInstallation tool , Node node , TaskListener log )
7979 throws IOException , InterruptedException {
80- FilePath supportingExecutable = preferredLocation (tool , node );
80+ FilePath destination = preferredLocation (tool , node );
8181 String [] systemProperties = getSystemProperties (node );
82- FilePath expectedPath ;
83- if (systemProperties [0 ].toLowerCase ().contains ("os x" )) {
84- expectedPath = new FilePath ( supportingExecutable , this .getVersion ()+".app" );
82+ FilePath matlabRootPath ;
83+ if (systemProperties [0 ].toLowerCase ().contains ("os x" )) {
84+ matlabRootPath = new FilePath ( destination , this .getRelease ()+".app" );
8585 } else {
86- expectedPath = new FilePath ( supportingExecutable , this .getVersion ());
86+ matlabRootPath = new FilePath ( destination , this .getRelease ());
8787 }
88- MatlabInstallable installable ;
89- try {
90- installable = (MatlabInstallable ) getInstallable (systemProperties );
91- } catch (Exception e ) {
92- throw new InstallationFailedException (e .getMessage ());
88+ String platform = getPlatform (systemProperties [0 ], systemProperties [1 ]);
89+ getFreshCopyOfExecutables (platform , destination );
90+
91+ makeDir (matlabRootPath );
92+ int result = installUsingMpm (node , this .getRelease (), matlabRootPath , this .getProducts (), log );
93+ if (result != 0 ) {
94+ throw new InstallationFailedException ("Unable to install MATLAB using mpm." );
9395 }
94-
95- getFreshCopyOfExecutables (installable , supportingExecutable );
96- makeDir (expectedPath );
97-
98- int result = installUsingMpm (node , expectedPath , log );
99- if (result == 0 ) {
100- log .getLogger ().println (
101- "MATLAB installation of version " + this .getVersion ()
102- + " using mpm completed successfully!" );
103- }
104- return expectedPath ;
96+ return matlabRootPath ;
10597 }
10698
107- private int installUsingMpm (Node node , FilePath destination , TaskListener log )
99+ private int installUsingMpm (Node node , String release , FilePath destination , String products , TaskListener log )
108100 throws IOException , InterruptedException {
109101
110- Launcher matlabInstaller = node .createLauncher (log );
102+ Launcher matlabInstaller = node .createLauncher (log );
111103 ProcStarter installerProc = matlabInstaller .launch ();
112104
113- ArgumentListBuilder args = new ArgumentListBuilder ();
114- args .add (destination .getParent ().getRemote () + getNodeSpecificMPMExecutor (node ));
115- args .add ("install" );
116- appendReleaseToArguments ( args , log );
117- args .add ("--destination=" + destination .getRemote ());
118- addMatlabProductsToArgs (args );
119- installerProc .pwd (destination ).cmds (args ).stdout (log );
105+ ArgumentListBuilder args = new ArgumentListBuilder ();
106+ args .add (destination .getParent ().getRemote () + getNodeSpecificMPMExecutor (node ));
107+ args .add ("install" );
108+ appendReleaseToArguments ( release , args , log );
109+ args .add ("--destination=" + destination .getRemote ());
110+ addMatlabProductsToArgs (args , products );
111+ installerProc .pwd (destination ).cmds (args ).stdout (log );
120112 int result ;
121113 try {
122- result = installerProc .join ();
114+ result = installerProc .join ();
123115 } catch (Exception e ) {
124- log .getLogger ().println ("MATLAB installation failed " + e .getMessage ());
125- throw new InstallationFailedException (e .getMessage ());
116+ log .getLogger ().println ("MATLAB installation failed " + e .getMessage ());
117+ throw new InstallationFailedException (e .getMessage ());
126118 }
127119 return result ;
128120 }
129121
122+
130123 private void makeDir (FilePath path ) throws IOException , InterruptedException {
131- if (!path .exists ()){
132- path .mkdirs ();
133- path .chmod (0777 );
124+ if (!path .exists ()){
125+ path .mkdirs ();
126+ path .chmod (0777 );
134127 }
135128 }
136129
137- private void appendReleaseToArguments ( ArgumentListBuilder args , TaskListener log ) {
138- String trimmedRelease = this . getVersion (). trim ();
130+ private void appendReleaseToArguments ( String release , ArgumentListBuilder args , TaskListener log ) {
131+ String trimmedRelease = release . trim ();
139132 String actualRelease = trimmedRelease ;
140133
141- if (trimmedRelease .equalsIgnoreCase ("latest" ) || trimmedRelease .equalsIgnoreCase (
134+ if (trimmedRelease .equalsIgnoreCase ("latest" ) || trimmedRelease .equalsIgnoreCase (
142135 "latest-including-prerelease" )) {
143136 String releaseInfoUrl =
144- Message .getValue ("matlab.release.info.url" ) + trimmedRelease ;
137+ Message .getValue ("matlab.release.info.url" ) + trimmedRelease ;
145138 String releaseVersion = null ;
146139 try {
147- releaseVersion = IOUtils .toString (new URL (releaseInfoUrl ),
148- StandardCharsets .UTF_8 ).trim ();
140+ releaseVersion = IOUtils .toString (new URL (releaseInfoUrl ),
141+ StandardCharsets .UTF_8 ).trim ();
149142 } catch (IOException e ) {
150- log .getLogger ().println ("Failed to fetch release version: " + e .getMessage ());
143+ log .getLogger ().println ("Failed to fetch release version: " + e .getMessage ());
151144 }
152145
153- if (releaseVersion != null && releaseVersion .contains ("prerelease" )) {
154- actualRelease = releaseVersion .replace ("prerelease" , "" );
146+ if (releaseVersion != null && releaseVersion .contains ("prerelease" )) {
147+ actualRelease = releaseVersion .replace ("prerelease" , "" );
155148 args .add ("--release-status=Prerelease" );
156149 } else {
157150 actualRelease = releaseVersion ;
158151 }
159152 }
160- args .add ("--release=" + actualRelease );
153+ args .add ("--release=" + actualRelease );
161154 }
162155
163- private void getFreshCopyOfExecutables ( MatlabInstallable installable , FilePath expectedPath )
156+ private void getFreshCopyOfExecutables ( String platform , FilePath expectedPath )
164157 throws IOException , InterruptedException {
165- FilePath mpmPath = installable .getMpmInstallable (expectedPath );
166- FilePath mbatchPath = installable .getBatchInstallable (expectedPath );
167- mpmPath .copyFrom (new URL (installable .url ).openStream ());
168- mpmPath .chmod (0777 );
169- mbatchPath .copyFrom (new URL (installable .batchURL ).openStream ());
170- mbatchPath .chmod (0777 );
158+ FilePath matlabBatchPath = new FilePath (expectedPath , "matlab-batch" );
159+ FilePath mpmPath = new FilePath (expectedPath , "mpm" );
160+
161+ URL mpmUrl ;
162+ URL matlabBatchUrl ;
163+
164+ switch (platform ) {
165+ case "glnxa64" :
166+ mpmUrl = new URL (Message .getValue ("tools.matlab.mpm.installer.linux" ));
167+ matlabBatchUrl = new URL (Message .getValue ("tools.matlab.batch.executable.linux" ));
168+ break ;
169+ case "maci64" :
170+ mpmUrl = new URL (Message .getValue ("tools.matlab.mpm.installer.maci64" ));
171+ matlabBatchUrl = new URL (Message .getValue ("tools.matlab.batch.executable.maci64" ));
172+ break ;
173+ case "maca64" :
174+ mpmUrl = new URL (Message .getValue ("tools.matlab.mpm.installer.maca64" ));
175+ matlabBatchUrl = new URL (Message .getValue ("tools.matlab.batch.executable.maca64" ));
176+ break ;
177+ default :
178+ throw new InstallationFailedException ("Unsupported OS" );
179+ }
180+
181+ mpmPath .copyFrom (mpmUrl .openStream ());
182+ mpmPath .chmod (0777 );
183+ matlabBatchPath .copyFrom (matlabBatchUrl .openStream ());
184+ matlabBatchPath .chmod (0777 );
171185 }
172186
173187 @ SuppressFBWarnings (value = {"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE" },
174188 justification =
175189 "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE: Its false positive scenario for sport bug which is fixed in later versions "
176190 + "https://github.com/spotbugs/spotbugs/issues/1843" )
177- private String getNodeSpecificMPMExecutor (Node node ) {
178- if (!node .toComputer ().isUnix ()) {
191+ private String getNodeSpecificMPMExecutor (Node node ) {
192+ if (!node .toComputer ().isUnix ()) {
179193 return "\\ mpm.exe" ;
180194 }
181195 return "/mpm" ;
182196 }
183197
184- private void addMatlabProductsToArgs (ArgumentListBuilder args )
198+ private void addMatlabProductsToArgs (ArgumentListBuilder args , String products )
185199 throws IOException , InterruptedException {
186- args .add ("--products" );
187- if (this . getProducts (). isEmpty ()) {
188- args .add (DEFAULT_PRODUCT );
200+ args .add ("--products" );
201+ if (products . isEmpty ()) {
202+ args .add (DEFAULT_PRODUCT );
189203
190204 } else {
191- if (!this . getProducts (). contains (DEFAULT_PRODUCT )) {
192- args .add (DEFAULT_PRODUCT );
205+ if (!products . contains (DEFAULT_PRODUCT )) {
206+ args .add (DEFAULT_PRODUCT );
193207 }
194- String [] productList = this . getProducts (). split (" " );
208+ String [] productList = products . split (" " );
195209 for (String prod : productList ) {
196- args .add (prod );
210+ args .add (prod );
197211 }
198212 }
199213 }
200214
201- public Installable getInstallable (String [] systemProperties ) throws IOException {
202- // Gather properties for the node to install on
203- return getInstallCandidate (systemProperties [0 ], systemProperties [1 ]);
204- }
205-
206- public MatlabInstallable getInstallCandidate (String osName , String architecture )
207- throws InstallationFailedException {
208- String platform = getPlatform (osName , architecture );
209- return new MatlabInstallable (platform );
210- }
211-
212- public String getPlatform (String os , String architecture ) throws InstallationFailedException {
213- String value = os .toLowerCase (Locale .ENGLISH );
214- if (value .contains ("linux" )) {
215+ public String getPlatform (String os , String architecture ) throws InstallationFailedException {
216+ String value = os .toLowerCase (Locale .ENGLISH );
217+ if (value .contains ("linux" )) {
215218 return "glnxa64" ;
216- } else if (value .contains ("os x" )) {
217- if (architecture .equalsIgnoreCase ("aarch64" ) || architecture .equalsIgnoreCase (
219+ } else if (value .contains ("os x" )) {
220+ if (architecture .equalsIgnoreCase ("aarch64" ) || architecture .equalsIgnoreCase (
218221 "arm64" )) {
219222 return "maca64" ;
220223 } else {
221224 return "maci64" ;
222225 }
223226 } else {
224- throw new InstallationFailedException ("Unsupported OS" );
227+ throw new InstallationFailedException ("Unsupported OS" );
225228 }
226229 }
227230
@@ -230,30 +233,30 @@ public String getPlatform (String os, String architecture) throws InstallationFa
230233 "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE: Its false positive scenario for sport bug which is fixed in later versions "
231234 + "https://github.com/spotbugs/spotbugs/issues/1843" )
232235 private String [] getSystemProperties (Node node ) throws IOException , InterruptedException {
233- String [] properties = node .getChannel ()
234- .call (new GetSystemProperties ("os.name" , "os.arch" , "os.version" ));
236+ String [] properties = node .getChannel ()
237+ .call (new GetSystemProperties ("os.name" , "os.arch" , "os.version" ));
235238 return properties ;
236239 }
237240
238241 @ Extension
239242 public static final class DescriptorImpl extends ToolInstallerDescriptor <MatlabInstaller > {
240243
241- public String getDisplayName () {
242- return Message .getValue ("matlab.tools.auto.install.display.name" );
244+ public String getDisplayName () {
245+ return Message .getValue ("matlab.tools.auto.install.display.name" );
243246 }
244247
245248 @ Override
246- public boolean isApplicable (Class <? extends ToolInstallation > toolType ) {
249+ public boolean isApplicable (Class <? extends ToolInstallation > toolType ) {
247250 return toolType == MatlabInstallation .class ;
248251 }
249252
250253 @ POST
251- public FormValidation doCheckVersion (@ QueryParameter String value ) {
252- Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
253- if (value .isEmpty ()) {
254- return FormValidation .error (Message .getValue ("tools.matlab.empty.version .error" ));
254+ public FormValidation doCheckRelease (@ QueryParameter String value ) {
255+ Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
256+ if (value .isEmpty ()) {
257+ return FormValidation .error (Message .getValue ("tools.matlab.empty.release .error" ));
255258 }
256- return FormValidation .ok ();
259+ return FormValidation .ok ();
257260 }
258261 }
259- }
262+ }
0 commit comments