Skip to content

Add on-demand licensing support #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion WolframLanguageForJupyter/Resources/Initialization.wl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ If[
(* warnings to display in kernel information *)
bannerWarning =
If[
Length[$CommandLine] > 4,
MemberQ[$CommandLine, "ScriptInstall"],
"\\n\\nNote: This Jupyter kernel was installed through the WolframScript install method. Accordingly, updates to a WolframLanguageForJupyter paclet will not affect this kernel.",
""
];
Expand Down
45 changes: 36 additions & 9 deletions WolframLanguageForJupyter/WolframLanguageForJupyter.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@
(* START: Helper symbols *)

projectHome = DirectoryName[$InputFileName];
defaultOnDemandLicenseServer = "cloudlm.wolfram.com";

(* establishes link with Wolfram Engine at mathBin and evaluates $Version/$VersionNumber *)
(* returns string form *)
getVersionFromKernel[mathBin_String] :=
Module[{link, res},
link =
LinkLaunch[
StringJoin[
{
"\"",
mathBin,
"\" -wstp"
}
StringRiffle[
Flatten@{
"\"" <> mathBin <> "\"",
"-wstp",
onDemandLicensingArguments[]
},
" "
]
];
If[FailureQ[link],
Expand Down Expand Up @@ -136,6 +138,29 @@
],
pathSeperator];

(* returns a list of WolframKernel CLI args for on-demand licensing based on the
WOLFRAMSCRIPT_ENTITLEMENTID envar; empty list if the variable is absent *)
onDemandLicensingArguments[] := onDemandLicensingArguments[Association@GetEnvironment[]]
onDemandLicensingArguments[processEnvironment_Association] := Module[{
entitlementID = processEnvironment["WOLFRAMSCRIPT_ENTITLEMENTID"],
licenseServer = Lookup[
processEnvironment,
"WOLFRAMSCRIPT_CLOUDLICENSESERVER",
defaultOnDemandLicenseServer
]
},
If[
(* if the envar is absent or too short *)
!StringQ[entitlementID] || StringLength[entitlementID] < 2,
(* then return an empty list *)
Return[{}]
];

Return@{
"-pwfile", "\"!" <> licenseServer <> "\"",
"-entitlement", "\"" <> entitlementID <> "\""
}
]

(* find Jupyter installation path *)
(* returns above *)
Expand Down Expand Up @@ -288,14 +313,16 @@
Export[
FileNameJoin[{tempDir, "kernel.json"}],
Association[
"argv" -> {
"argv" -> Flatten@{
mathBin,

(* TODO: automatically find the kernel script
(only) if the Wolfram Engine being installed is the same as the one used to execute this command *)
"-script",
FileNameJoin[{projectHome, "Resources", "KernelForWolframLanguageForJupyter.wl"}],
"{connection_file}"
(* , "-noprompt" *)
"{connection_file}",
(* "-noprompt", *)
StringTrim[onDemandLicensingArguments[processEnvironment], "\""]
},
"display_name" -> displayName,
"language" -> "Wolfram Language"
Expand Down
46 changes: 39 additions & 7 deletions configure-jupyter.wls
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ nolink = "configure-jupyter.wls: Communication with provided Wolfram Engine bina
(* START: Helper symbols *)

projectHome = If[StringQ[$InputFileName] && $InputFileName != "", DirectoryName[$InputFileName], Directory[]];
defaultOnDemandLicenseServer = "cloudlm.wolfram.com";

(* establishes link with Wolfram Engine at mathBin and evaluates $Version/$VersionNumber *)
(* returns string form *)
getVersionFromKernel[mathBin_String] :=
Module[{link, res},
link =
LinkLaunch[
StringJoin[
{
"\"",
mathBin,
"\" -wstp"
}
StringRiffle[
Flatten@{
"\"" <> mathBin <> "\"",
"-wstp",
onDemandLicensingArguments[]
},
" "
]
];
If[FailureQ[link],
Expand Down Expand Up @@ -158,6 +160,30 @@ attemptPathRegeneration[] := If[
];
];

(* returns a list of WolframKernel CLI args for on-demand licensing based on the
WOLFRAMSCRIPT_ENTITLEMENTID envar; empty list if the variable is absent *)
onDemandLicensingArguments[] := onDemandLicensingArguments[Association@GetEnvironment[]]
onDemandLicensingArguments[processEnvironment_Association] := Module[{
entitlementID = processEnvironment["WOLFRAMSCRIPT_ENTITLEMENTID"],
licenseServer = Lookup[
processEnvironment,
"WOLFRAMSCRIPT_CLOUDLICENSESERVER",
defaultOnDemandLicenseServer
]
},
If[
(* if the envar is absent or too short *)
!StringQ[entitlementID] || StringLength[entitlementID] < 2,
(* then return an empty list *)
Return[{}]
];

Return@{
"-pwfile", "\"!" <> licenseServer <> "\"",
"-entitlement", "\"" <> entitlementID <> "\""
}
]

(* find Jupyter installation path *)
(* returns kernel IDs in Jupyter *)
findJupyterPath[] :=
Expand Down Expand Up @@ -310,7 +336,13 @@ configureJupyter[specs_Association, removeQ_?BooleanQ, removeAllQ_?BooleanQ] :=
Export[
FileNameJoin[{tempDir, "kernel.json"}],
Association[
"argv" -> {mathBin, "-script", kernelScript, "{connection_file}", "ScriptInstall" (* , "-noprompt" *)},
"argv" -> Flatten@{
mathBin,

"-script", kernelScript,
"{connection_file}", "ScriptInstall", (* "-noprompt", *)
StringTrim[onDemandLicensingArguments[processEnvironment], "\""]
},
"display_name" -> displayName,
"language" -> "Wolfram Language"
]
Expand Down