Skip to content

Instantly share code, notes, and snippets.

@chetanmeh
Last active February 15, 2018 19:25
Show Gist options
  • Save chetanmeh/b58deb3ce6e9729ebe9c7d2d5fbfa69a to your computer and use it in GitHub Desktop.
Save chetanmeh/b58deb3ce6e9729ebe9c7d2d5fbfa69a to your computer and use it in GitHub Desktop.
Launch OpenWhisk controller in Intellij
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def owHome = System.getenv("OPENWHISK_HOME")
assert owHome : "Cannot determine OpenWhisk home via environment variable 'OPENWHISK_HOME'"
def containerNames = 'docker ps --format {{.Names}}'.execute().text.split("\n")
def transformEnvScript = "/bin/bash $owHome/common/scala/transformEnvironment.sh"
def text = '''<component name="ProjectRunConfigurationManager">
<configuration default="false" name="${name}" type="Application" factoryName="Application">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<option name="MAIN_CLASS_NAME" value="$main" />
<option name="VM_PARAMETERS" value="$sysProps" />
<option name="PROGRAM_PARAMETERS" value="0" />
<option name="WORKING_DIRECTORY" value="file://$USER_HOME/tmp/openwhisk/$type" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="ENABLE_SWING_INSPECTOR" value="false" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<module name="${type}_main" />
<envs>
<% env.each { k,v -> %><env name="$k" value="$v" />
<% } %>
</envs>
<method />
</configuration>
</component>
'''
def meta = [
controller : [main:"whisk.core.controller.Controller"],
invoker : [main:"whisk.core.invoker.Invoker"]
]
containerNames.each{cn ->
if (cn.contains("controller") || cn.contains("invoker")){
def type = cn.contains("controller") ? "controller" : "invoker"
def inspectResult = "docker inspect $cn".execute().text
def json = new JsonSlurper().parseText(inspectResult)
def mappedPort = json.'NetworkSettings'.'Ports'.'8080/tcp'[0].'HostPort'
def env = json[0].'Config'.'Env'
def envMap = env.collectEntries {String e ->
def eqIndex = e.indexOf('=')
def k = e.substring(0, eqIndex)
def v = e.substring(eqIndex + 1).replace('"', '&quot;')
if (k == 'PORT') {v = mappedPort}
[(k):v]
}
def sysProps = transformEnvScript.execute(env, new File(".")).text
sysProps = sysProps.replace('\'','')
def templateBinding = [
main: meta[type].main,
type:type,
name:cn,
env: envMap,
sysProps : sysProps,
USER_HOME : '$USER_HOME$'
]
def engine = new groovy.text.SimpleTemplateEngine()
def template = engine.createTemplate(text).make(templateBinding)
def launchFile = new File("$owHome/.idea/runConfigurations/${cn}.xml")
launchFile.parentFile.mkdirs()
launchFile.text = template
println "Created ${launchFile.absolutePath}"
}
}
@chetanmeh
Copy link
Author

chetanmeh commented Feb 15, 2018

This script enable creation of Intellij Launch Configuration in project dir/.idea/runConfigurations with name controller0 and invoker0. To run this

  1. Setup OpenWhisk on laptop natively i.e. not using Vagrant
  2. Run this script groovy createOWRunConfigs.groovy (you would need to have Groovy installed)
  3. It would inspect the running docker containers and then generate the launch config with name 'controller0' and 'invoker0'

Key points to note

  1. Use ~/tmp/openwhisk/controller (or invoker) as working directory
  2. Changes the PORT to linked one. So controller gets started at 10001 only just like container
  3. It would only work if you Intellij project is directory based.

Now stop the docker container for controller and launch the

Note - Currently only controller can be run from IDE. Invoker posses some problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment