-
Notifications
You must be signed in to change notification settings - Fork 1.2k
FEATURE-3823: kvm agent hooks #3839
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
Merged
DaanHoogland
merged 4 commits into
apache:master
from
bwsw:feature/3823-kvm-agent-hooks
Mar 14, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
76 changes: 76 additions & 0 deletions
76
.../hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtKvmAgentHook.java
This file contains hidden or 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,76 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package com.cloud.hypervisor.kvm.resource; | ||
|
|
||
| import groovy.lang.Binding; | ||
| import groovy.lang.GroovyObject; | ||
| import groovy.util.GroovyScriptEngine; | ||
| import groovy.util.ResourceException; | ||
| import groovy.util.ScriptException; | ||
| import org.apache.log4j.Logger; | ||
| import org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
||
| public class LibvirtKvmAgentHook { | ||
| private final String script; | ||
| private final String method; | ||
| private final GroovyScriptEngine gse; | ||
| private final Binding binding = new Binding(); | ||
|
|
||
| private static final Logger s_logger = Logger.getLogger(LibvirtKvmAgentHook.class); | ||
|
|
||
| public LibvirtKvmAgentHook(String path, String script, String method) throws IOException { | ||
| this.script = script; | ||
| this.method = method; | ||
| File full_path = new File(path, script); | ||
| if (!full_path.canRead()) { | ||
| s_logger.warn("Groovy script '" + full_path.toString() + "' is not available. Transformations will not be applied."); | ||
| this.gse = null; | ||
| } else { | ||
| this.gse = new GroovyScriptEngine(path); | ||
| } | ||
| } | ||
|
|
||
| public boolean isInitialized() { | ||
| return this.gse != null; | ||
| } | ||
|
|
||
| public Object handle(Object arg) throws ResourceException, ScriptException { | ||
| if (!isInitialized()) { | ||
| s_logger.warn("Groovy scripting engine is not initialized. Data transformation skipped."); | ||
| return arg; | ||
| } | ||
|
|
||
| GroovyObject cls = (GroovyObject) this.gse.run(this.script, binding); | ||
| if (null == cls) { | ||
| s_logger.warn("Groovy object is not received from script '" + this.script + "'."); | ||
| return arg; | ||
| } else { | ||
| Object[] params = {s_logger, arg}; | ||
| try { | ||
| Object res = cls.invokeMethod(this.method, params); | ||
| return res; | ||
| } catch (MissingMethodExceptionNoStack e) { | ||
| s_logger.error("Error occured when calling method from groovy script, {}", e); | ||
| return arg; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
94 changes: 94 additions & 0 deletions
94
...ervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtKvmAgentHookTest.java
This file contains hidden or 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,94 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package com.cloud.hypervisor.kvm.resource; | ||
|
|
||
| import groovy.util.ResourceException; | ||
| import groovy.util.ScriptException; | ||
| import junit.framework.TestCase; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import java.util.UUID; | ||
|
|
||
| public class LibvirtKvmAgentHookTest extends TestCase { | ||
|
|
||
| private final String source = "<xml />"; | ||
| private final String dir = "/tmp"; | ||
| private final String script = "xml-transform-test.groovy"; | ||
| private final String method = "transform"; | ||
| private final String methodNull = "transform2"; | ||
| private final String testImpl = "package groovy\n" + | ||
| "\n" + | ||
| "class BaseTransform {\n" + | ||
| " String transform(Object logger, String xml) {\n" + | ||
| " return xml + xml\n" + | ||
| " }\n" + | ||
| " String transform2(Object logger, String xml) {\n" + | ||
| " return null\n" + | ||
| " }\n" + | ||
| "}\n" + | ||
| "\n" + | ||
| "new BaseTransform()\n" + | ||
| "\n"; | ||
|
|
||
| @Override | ||
| protected void setUp() throws Exception { | ||
| super.setUp(); | ||
| PrintWriter pw = new PrintWriter(new File(dir, script)); | ||
| pw.println(testImpl); | ||
| pw.close(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void tearDown() throws Exception { | ||
| new File(dir, script).delete(); | ||
| super.tearDown(); | ||
| } | ||
|
|
||
| public void testTransform() throws IOException, ResourceException, ScriptException { | ||
| LibvirtKvmAgentHook t = new LibvirtKvmAgentHook(dir, script, method); | ||
| assertEquals(t.isInitialized(), true); | ||
| String result = (String)t.handle(source); | ||
| assertEquals(result, source + source); | ||
| } | ||
|
|
||
| public void testWrongMethod() throws IOException, ResourceException, ScriptException { | ||
| LibvirtKvmAgentHook t = new LibvirtKvmAgentHook(dir, script, "methodX"); | ||
| assertEquals(t.isInitialized(), true); | ||
| assertEquals(t.handle(source), source); | ||
| } | ||
|
|
||
| public void testNullMethod() throws IOException, ResourceException, ScriptException { | ||
| LibvirtKvmAgentHook t = new LibvirtKvmAgentHook(dir, script, methodNull); | ||
| assertEquals(t.isInitialized(), true); | ||
| assertEquals(t.handle(source), null); | ||
| } | ||
|
|
||
| public void testWrongScript() throws IOException, ResourceException, ScriptException { | ||
| LibvirtKvmAgentHook t = new LibvirtKvmAgentHook(dir, "wrong-script.groovy", method); | ||
| assertEquals(t.isInitialized(), false); | ||
| assertEquals(t.handle(source), source); | ||
| } | ||
|
|
||
| public void testWrongDir() throws IOException, ResourceException, ScriptException { | ||
| LibvirtKvmAgentHook t = new LibvirtKvmAgentHook("/" + UUID.randomUUID().toString() + "-dir", script, method); | ||
| assertEquals(t.isInitialized(), false); | ||
| assertEquals(t.handle(source), source); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.