Skip to content

Commit affec17

Browse files
committed
dynaml-notebook: WIP to zeppelin interpreter
- Needs to be continued
1 parent 844806a commit affec17

File tree

3 files changed

+168
-11
lines changed

3 files changed

+168
-11
lines changed

conf/shiro.ini

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
[users]
19+
# List of users with their password allowed to access Zeppelin.
20+
# To use a different strategy (LDAP / Database / ...) check the shiro doc at http://shiro.apache.org/configuration.html#Configuration-INISections
21+
admin = password1, admin
22+
23+
24+
# Sample LDAP configuration, for user Authentication, currently tested for single Realm
25+
[main]
26+
### A sample for configuring Active Directory Realm
27+
#activeDirectoryRealm = org.apache.zeppelin.realm.ActiveDirectoryGroupRealm
28+
#activeDirectoryRealm.systemUsername = userNameA
29+
30+
#use either systemPassword or hadoopSecurityCredentialPath, more details in http://zeppelin.apache.org/docs/latest/security/shiroauthentication.html
31+
#activeDirectoryRealm.systemPassword = passwordA
32+
#activeDirectoryRealm.hadoopSecurityCredentialPath = jceks://file/user/zeppelin/zeppelin.jceks
33+
#activeDirectoryRealm.searchBase = CN=Users,DC=SOME_GROUP,DC=COMPANY,DC=COM
34+
#activeDirectoryRealm.url = ldap://ldap.test.com:389
35+
#activeDirectoryRealm.groupRolesMap = "CN=admin,OU=groups,DC=SOME_GROUP,DC=COMPANY,DC=COM":"admin","CN=finance,OU=groups,DC=SOME_GROUP,DC=COMPANY,DC=COM":"finance","CN=hr,OU=groups,DC=SOME_GROUP,DC=COMPANY,DC=COM":"hr"
36+
#activeDirectoryRealm.authorizationCachingEnabled = false
37+
38+
### A sample for configuring LDAP Directory Realm
39+
#ldapRealm = org.apache.zeppelin.realm.LdapGroupRealm
40+
## search base for ldap groups (only relevant for LdapGroupRealm):
41+
#ldapRealm.contextFactory.environment[ldap.searchBase] = dc=COMPANY,dc=COM
42+
#ldapRealm.contextFactory.url = ldap://ldap.test.com:389
43+
#ldapRealm.userDnTemplate = uid={0},ou=Users,dc=COMPANY,dc=COM
44+
#ldapRealm.contextFactory.authenticationMechanism = simple
45+
46+
### A sample PAM configuration
47+
#pamRealm=org.apache.zeppelin.realm.PamRealm
48+
#pamRealm.service=sshd
49+
50+
### A sample for configuring ZeppelinHub Realm
51+
#zeppelinHubRealm = org.apache.zeppelin.realm.ZeppelinHubRealm
52+
## Url of ZeppelinHub
53+
#zeppelinHubRealm.zeppelinhubUrl = https://www.zeppelinhub.com
54+
#securityManager.realms = $zeppelinHubRealm
55+
56+
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
57+
58+
### If caching of user is required then uncomment below lines
59+
#cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
60+
#securityManager.cacheManager = $cacheManager
61+
62+
securityManager.sessionManager = $sessionManager
63+
# 86,400,000 milliseconds = 24 hour
64+
securityManager.sessionManager.globalSessionTimeout = 86400000
65+
shiro.loginUrl = /api/login
66+
67+
[roles]
68+
role1 = *
69+
role2 = *
70+
role3 = *
71+
admin = *
72+
73+
[urls]
74+
# This section is used for url-based security.
75+
# You can secure interpreter, configuration and credential information by urls. Comment or uncomment the below urls that you want to hide.
76+
# anon means the access is anonymous.
77+
# authc means Form based Auth Security
78+
# To enfore security, comment the line below and uncomment the next one
79+
/api/version = anon
80+
#/api/interpreter/** = authc, roles[admin]
81+
#/api/configurations/** = authc, roles[admin]
82+
#/api/credential/** = authc, roles[admin]
83+
#/** = anon
84+
/** = authc

conf/zeppelin-site.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999

100100
<property>
101101
<name>zeppelin.interpreter.dir</name>
102-
<value>lib</value>
102+
<value>interpreter</value>
103103
<description>Interpreter implementation base directory</description>
104104
</property>
105105

@@ -111,7 +111,7 @@
111111

112112
<property>
113113
<name>zeppelin.interpreters</name>
114-
<value>org.apache.zeppelin.spark.SparkInterpreter,org.apache.zeppelin.spark.PySparkInterpreter,org.apache.zeppelin.spark.SparkSqlInterpreter,org.apache.zeppelin.spark.DepInterpreter,io.github.mandar2812.dynaml.zeppelin.DynaMLZeppelinInterpreter</value>
114+
<value>org.apache.zeppelin.spark.SparkInterpreter,io.github.mandar2812.dynaml.zeppelin.DynaMLZeppelinInterpreter</value>
115115
</property>
116116

117117
<property>

dynaml-notebook/src/main/scala-2.11/io/github/mandar2812/dynaml/zeppelin/DynaMLZeppelinInterpreter.scala

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import java.util.Properties
66
import ammonite.interp.{Parsers, Preprocessor}
77
import ammonite.repl.Repl
88
import ammonite.runtime.{History, Storage}
9-
import ammonite.util.{Name, Res}
10-
import ammonite.util.Util.CodeSource
9+
import ammonite.util._
10+
import ammonite.util.Util.{CodeSource, VersionedWrapperId}
1111
import fastparse.core.Parsed
1212
import io.github.mandar2812.dynaml.DynaZeppelin
1313
import io.github.mandar2812.dynaml.repl.{Defaults, DynaMLInterpreter}
@@ -22,7 +22,9 @@ class DynaMLZeppelinInterpreter(properties: Properties) extends Interpreter(prop
2222

2323
protected val outputBuffer = new ByteArrayOutputStream()
2424

25-
protected val dynaml_instance = new DynaZeppelin(outputStream = outputBuffer)
25+
protected val errorBuffer = new ByteArrayOutputStream()
26+
27+
protected val dynaml_instance = new DynaZeppelin(outputStream = outputBuffer, errorStream = errorBuffer)
2628

2729
protected var CURRENT_LINE : Int = 0
2830

@@ -66,7 +68,7 @@ class DynaMLZeppelinInterpreter(properties: Properties) extends Interpreter(prop
6668
override def interpret(s: String, interpreterContext: InterpreterContext) = {
6769
addHistory(s)
6870

69-
val wrapperName = Name("cmd" + CURRENT_LINE)
71+
/*val wrapperName = Name("cmd" + CURRENT_LINE)
7072
val fileName = wrapperName.encoded + ".sc"
7173
val result = for {
7274
blocks <- Preprocessor.splitScript(ammonite.interp.Interpreter.skipSheBangLine(s), fileName)
@@ -106,14 +108,66 @@ class DynaMLZeppelinInterpreter(properties: Properties) extends Interpreter(prop
106108
new InterpreterResult(InterpreterResult.Code.SUCCESS, output)
107109
} else {
108110
new InterpreterResult(InterpreterResult.Code.ERROR, result.toString)
111+
}*/
112+
113+
Parsers.Splitter.parse(s) match {
114+
case Parsed.Success(value, idx) =>
115+
val computation_output = dynaml_interp.processLine(s, value, CURRENT_LINE, false, () => CURRENT_LINE += 1)
116+
val output = outputBuffer.toString(Charset.defaultCharset())
117+
val error = errorBuffer.toString(Charset.defaultCharset())
118+
119+
if(computation_output.isSuccess) {
120+
outputBuffer.reset()
121+
new InterpreterResult(InterpreterResult.Code.SUCCESS, output)
122+
} else {
123+
errorBuffer.reset()
124+
new InterpreterResult(InterpreterResult.Code.ERROR, "Syntax Error Mofo!")
125+
}
126+
127+
case Parsed.Failure(_, index, extra) =>
128+
new InterpreterResult(InterpreterResult.Code.ERROR, fastparse.core.ParseError.msg(extra.input, extra.traced.expected, index))
109129
}
130+
131+
110132
}
111133

112134
def evaluate(s: String) = {
113135
addHistory(s)
114136

115137
val wrapperName = Name("cmd" + CURRENT_LINE)
116138
val fileName = wrapperName.encoded + ".sc"
139+
140+
def compileRunBlock(
141+
leadingSpaces: String, hookInfo: ImportHookInfo,
142+
codeSource: CodeSource,
143+
eval: (Preprocessor.Output, Name) => Res[(Evaluated, Tag)],
144+
indexedWrapperName: Name,
145+
wrapperIndex: Int = 1) = {
146+
147+
val printSuffix = if (wrapperIndex == 1) "" else " #" + wrapperIndex
148+
dynaml_interp.printer.info("Compiling " + codeSource.printablePath + printSuffix)
149+
for{
150+
processed <- dynaml_interp.compilerManager.preprocess(codeSource.fileName).transform(
151+
hookInfo.stmts,
152+
"",
153+
leadingSpaces,
154+
codeSource.pkgName,
155+
indexedWrapperName,
156+
dynaml_interp.predefImports ++ dynaml_interp.frameImports ++ hookInfo.imports,
157+
_ => "scala.Iterator[String]()",
158+
extraCode = "",
159+
skipEmpty = false
160+
)
161+
162+
(ev, tag) <- eval(processed, indexedWrapperName)
163+
} yield ScriptOutput.BlockMetadata(
164+
VersionedWrapperId(ev.wrapper.map(_.encoded).mkString("."), tag),
165+
leadingSpaces,
166+
hookInfo,
167+
ev.imports
168+
)
169+
}
170+
117171
val result = for {
118172
blocks <- Preprocessor.splitScript(ammonite.interp.Interpreter.skipSheBangLine(s), fileName)
119173

@@ -124,7 +178,26 @@ class DynaMLZeppelinInterpreter(properties: Properties) extends Interpreter(prop
124178
Some(dynaml_interp.wd/"(console)")
125179
)
126180

127-
metadata <- dynaml_interp.processAllScriptBlocks(
181+
indexedWrapperName = ammonite.interp.Interpreter.indexWrapperName(codeSource.wrapperName, 1)
182+
183+
184+
allSplittedChunks <- Res.Success(blocks)
185+
(leadingSpaces, stmts) = allSplittedChunks(1 - 1)
186+
(hookStmts, importTrees) = dynaml_interp.parseImportHooks(codeSource, stmts)
187+
hookInfo <- dynaml_interp.resolveImportHooks(importTrees, hookStmts, codeSource)
188+
189+
res <- compileRunBlock(leadingSpaces, hookInfo, codeSource,
190+
(processed, indexedWrapperName) =>
191+
dynaml_interp.evaluateLine(
192+
processed, dynaml_interp.printer, fileName,
193+
indexedWrapperName, silent = false, () => CURRENT_LINE += 1),
194+
indexedWrapperName
195+
)
196+
197+
198+
199+
200+
/*metadata <- dynaml_interp.processAllScriptBlocks(
128201
blocks.map(_ => None),
129202
Res.Success(blocks),
130203
dynaml_interp.predefImports ++ dynaml_interp.frameImports,
@@ -135,19 +208,19 @@ class DynaMLZeppelinInterpreter(properties: Properties) extends Interpreter(prop
135208
indexedWrapperName, silent = false, () => CURRENT_LINE += 1),
136209
autoImport = true,
137210
""
138-
)
211+
)*/
139212
} yield {
140-
metadata
213+
res
141214
}
142215

143216
if(result.isSuccess) {
144217

145218
val output = outputBuffer.toString(Charset.defaultCharset())
146219

147220
outputBuffer.reset()
148-
val resStr = result.flatMap(d => {
221+
/*val resStr = result.flatMap(d => {
149222
Res(Some(d.blockInfo.map(blockm => blockm.finalImports.value.map(d => d.fromName.raw).mkString("\n")).mkString("\n")), "")
150-
})
223+
})*/
151224

152225
output
153226
} else {

0 commit comments

Comments
 (0)