Skip to content

Commit

Permalink
Fixed issue #54 - Nextflow installer does not work when running behin…
Browse files Browse the repository at this point in the history
…d a proxy server
  • Loading branch information
pditommaso committed Jul 20, 2015
1 parent 11cde2e commit 439b572
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 51 deletions.
43 changes: 26 additions & 17 deletions nextflow
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ function install() {
echo ''
}

function launch_nextflow() {
# the launch command line
local cmdline="${launcher[@]} ${args[@]}"

# start in daemon mode
if [[ "$bg" ]]; then
local pid_file="${NXF_PID_FILE:-.nextflow.pid}"
bash -c "exec $cmdline" &
disown
printf $! > "$pid_file"
exit 0
fi
# trampoline syntax -- https://github.com/puniverse/capsule/pull/71
exec bash -c "exec $cmdline"
exit 1
}

# check self-install
if [ "$0" = "bash" ] || [ "$0" = "/bin/bash" ]; then
if [ -d nextflow ]; then
Expand Down Expand Up @@ -234,7 +251,7 @@ fi
[[ "$cmd" == "node" && ! "$NXF_MODE" ]] && NXF_MODE='gridgain'

COLUMNS=${COLUMNS:-`tty -s && tput cols 2>/dev/null || true`}
JAVA_OPTS="-noverify -Dcapsule.trampoline"
JAVA_OPTS="-noverify -Dcapsule.trampoline -Dcapsule.java.cmd=$JAVA_CMD"
if [[ $cmd == console ]]; then bg=1;
else JAVA_OPTS+=" -Djava.awt.headless=true"
fi
Expand All @@ -254,15 +271,6 @@ fi
export JAVA_CMD
export CAPSULE_CACHE_DIR

# Start in daemon mode
if [[ "$bg" ]]; then
NXF_PID_FILE=${NXF_PID_FILE:-'.nextflow.pid'}
`"$JAVA_CMD" $JAVA_OPTS -jar "$NXF_BIN"` "${args[@]}" &
disown
printf $! > $NXF_PID_FILE
exit 0
fi

# lookup the a `md5` command
if hash md5sum 2>/dev/null; then MD5=md5sum;
elif hash gmd5sum 2>/dev/null; then MD5=gmd5sum;
Expand All @@ -272,7 +280,8 @@ fi

# when no md5 command is available fallback on default execution
if [ ! "$MD5" ] || [ "$CAPSULE_RESET" ]; then
exec `"$JAVA_CMD" $JAVA_OPTS -jar "$NXF_BIN"` "${args[@]}"
launcher=($("$JAVA_CMD" $JAVA_OPTS -jar "$NXF_BIN"))
launch_nextflow
exit 1
fi

Expand All @@ -292,14 +301,14 @@ EOF
# checked if a cached classpath file exists and it newer that the nextflow boot jar file
LAUNCH_FILE="${NXF_LAUNCHER}/classpath-$(env_md5)"
if [ -s "$LAUNCH_FILE" ] && [ "$LAUNCH_FILE" -nt "$NXF_BIN" ]; then
LAUNCHER=$(cat "$LAUNCH_FILE")
launcher=($(cat "$LAUNCH_FILE"))
else
# otherwise run the capsule and get the result classpath in the 'LAUNCHER' and save it to a file
LAUNCHER=`"$JAVA_CMD" $JAVA_OPTS -jar "$NXF_BIN"`
# otherwise run the capsule and get the result classpath in the 'launcher' and save it to a file
launcher=($("$JAVA_CMD" $JAVA_OPTS -jar "$NXF_BIN"))
[[ $? -ne 0 ]] && echo 'Unable to initialize nextflow environment' && exit $?
mkdir -p ${NXF_LAUNCHER}
echo "$LAUNCHER" > "$LAUNCH_FILE"
echo "${launcher[@]}" > "$LAUNCH_FILE"
fi

exec $LAUNCHER "${args[@]}"
exit 1
# finally run it
launch_nextflow
24 changes: 16 additions & 8 deletions src/main/groovy/nextflow/cli/Launcher.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,18 @@ class Launcher implements ExitCode {
}

/**
* set up environment and system properties
* set up environment and system properties. It checks the following
* environment variables:
* <li>http_proxy</li>
* <li>https_proxy</li>
* <li>HTTP_PROXY</li>
* <li>HTTPS_PROXY</li>
*/
private void setupEnvironment() {

setProxy('HTTP',System.getenv())
setProxy('HTTPS',System.getenv())

setProxy('http',System.getenv())
setProxy('https',System.getenv())

Expand All @@ -443,25 +451,25 @@ class Launcher implements ExitCode {
* http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
* https://github.com/nextflow-io/nextflow/issues/24
*
* @param qualifier Either {@code http} or {@code https}
* @param qualifier Either {@code http/HTTP} or {@code https/HTTPS}.
* @param env The environment variables system map
*/
@PackageScope
static void setProxy(String qualifier, Map<String,String> env ) {
assert qualifier in ['http','https']
assert qualifier in ['http','https','HTTP','HTTPS']
def str = null
def var = "${qualifier.toUpperCase()}_PROXY".toString()
def var = "${qualifier}_" + (qualifier.isLowerCase() ? 'proxy' : 'PROXY')

// -- setup HTTP proxy
try {
List<String> proxy = parseProxy(str = env.get(var))
List<String> proxy = parseProxy(str = env.get(var.toString()))
if( proxy ) {
log.debug "Setting $qualifier proxy: $proxy"
System.setProperty("${qualifier}.proxyHost", proxy[0])
if( proxy[1] ) System.setProperty("${qualifier}.proxyPort", proxy[1])
System.setProperty("${qualifier.toLowerCase()}.proxyHost", proxy[0])
if( proxy[1] ) System.setProperty("${qualifier.toLowerCase()}.proxyPort", proxy[1])
}
}
catch (MalformedURLException e ) {
catch ( MalformedURLException e ) {
log.warn "Not a valid $qualifier proxy: '$str' -- Check the value of variable `$var` in your environment"
}

Expand Down
16 changes: 14 additions & 2 deletions src/test/groovy/nextflow/cli/LauncherTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,29 @@ class LauncherTest extends Specification {
def httpsProxyPort =System.getProperty('https.proxyPort')

when:
Launcher.setProxy('http', [HTTP_PROXY: 'alpha.com:333'])
Launcher.setProxy('HTTP', [HTTP_PROXY: 'alpha.com:333'])
then:
System.getProperty('http.proxyHost') == 'alpha.com'
System.getProperty('http.proxyPort') == '333'

when:
Launcher.setProxy('https', [HTTPS_PROXY: 'beta.com:5466'])
Launcher.setProxy('http', [http_proxy: 'gamma.com:444'])
then:
System.getProperty('http.proxyHost') == 'gamma.com'
System.getProperty('http.proxyPort') == '444'

when:
Launcher.setProxy('HTTPS', [HTTPS_PROXY: 'beta.com:5466'])
then:
System.getProperty('https.proxyHost') == 'beta.com'
System.getProperty('https.proxyPort') == '5466'

when:
Launcher.setProxy('https', [https_proxy: 'zeta.com:6646'])
then:
System.getProperty('https.proxyHost') == 'zeta.com'
System.getProperty('https.proxyPort') == '6646'

cleanup:
System.setProperty('http.proxyHost', httpProxyHost ?: '')
System.setProperty('http.proxyPort', httpProxyPort ?: '')
Expand Down
4 changes: 2 additions & 2 deletions subprojects/my-capsule/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ apply plugin: 'application'

dependencies {
// Capsule manages the fat jar building process
compile 'co.paralleluniverse:capsule:1.0-rc1'
runtime 'co.paralleluniverse:capsule-maven:1.0-rc1'
compile 'co.paralleluniverse:capsule:1.0-rc2-SNAPSHOT'
runtime 'co.paralleluniverse:capsule-maven:1.0-rc2-SNAPSHOT'
testCompile "junit:junit-dep:4.10"
/* testCompile inherited from top gradle build file */
}
Expand Down
21 changes: 6 additions & 15 deletions subprojects/my-capsule/src/main/CapsuleLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,36 @@ protected CapsuleLoader(Path jarFile) {

@Override
protected ProcessBuilder prelaunch(List<String> jvmArgs, List<String> args) {
ProcessBuilder pb = super.prelaunch(jvmArgs,args);
ProcessBuilder pb = super.prelaunch(jvmArgs, args);

String drip = System.getenv().get("NXF_DRIP");
if( drip != null && !"".equals(drip) ) {
if( drip != null && !drip.isEmpty() ) {
pb.command().set(0, drip);
return pb;
}

String javaCmd = System.getenv().get("JAVA_CMD");
if( javaCmd != null && !"".equals(javaCmd) ) {
// use the Java command provided by the env variable
pb.command().set(0, javaCmd);
}

return pb;
}

@Override
@SuppressWarnings("unchecked")
protected <T> T attribute(Map.Entry<String, T> attr) {

if (ATTR_DEPENDENCIES == attr && System.getenv("NXF_GRAB") != null ) {
String customDeps = System.getenv("NXF_GRAB");
String deps = System.getenv("NXF_GRAB");
List<String> parent = super.attribute((Map.Entry<String,List<String>>)attr);
return (T)extendDepsWith(System.getenv("NXF_GRAB"), parent);
return (T)extendDepsWith(deps, parent);
}

if (ATTR_APP_CLASS_PATH == attr && System.getenv("NXF_CLASSPATH") != null) {
String classpath = System.getenv("NXF_CLASSPATH");
List<String> parent = super.attribute((Map.Entry<String, List<String>>) attr);
List<String> parent = super.attribute((Map.Entry<String, List<String>>)attr);
return (T)extendClassPathWith(classpath, parent);

}

return super.attribute(attr);
}



static List<String> extendClassPathWith(String classpath, List<String> origin) {
if( classpath == null || "".equals(classpath.trim()) ) {
return origin;
Expand All @@ -90,7 +82,6 @@ static List<String> extendClassPathWith(String classpath, List<String> origin) {
result.addAll(origin);
}


for( String lib : classpath.split(":") ) {
String trimmed = lib.trim();
if( trimmed.length()>0 )
Expand Down
11 changes: 4 additions & 7 deletions subprojects/my-capsule/src/test/CapsuleLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import static org.junit.Assert.assertEquals;

import java.nio.file.Paths;
import java.util.Arrays;

import org.junit.Test;
Expand All @@ -44,12 +43,10 @@ public void testExtendDeps () {
@Test
public void testExtendClasspath() {
assertEquals( CapsuleLoader.extendClassPathWith(null, null), null );

assertEquals( CapsuleLoader.extendClassPathWith("x", null), Arrays.asList(Paths.get("x")));
assertEquals( CapsuleLoader.extendClassPathWith("x:y ", null), Arrays.asList(Paths.get("x"), Paths.get("y")));
assertEquals( CapsuleLoader.extendClassPathWith("x :: y ", null), Arrays.asList(Paths.get("x"), Paths.get("y")));

assertEquals( CapsuleLoader.extendClassPathWith("x:y:z", Arrays.asList(Paths.get("lib1.jar"), Paths.get("lib2.jar"))), Arrays.asList( Paths.get("lib1.jar"), Paths.get("lib2.jar"), Paths.get("x"), Paths.get("y"), Paths.get("z")));
assertEquals( CapsuleLoader.extendClassPathWith("x", null), Arrays.asList("x"));
assertEquals( CapsuleLoader.extendClassPathWith("x:y ", null), Arrays.asList("x","y"));
assertEquals( CapsuleLoader.extendClassPathWith("x :: y ", null), Arrays.asList("x","y"));
assertEquals( CapsuleLoader.extendClassPathWith("x:y:z", Arrays.asList("lib1.jar","lib2.jar")), Arrays.asList( "lib1.jar","lib2.jar","x","y","z"));

}

Expand Down
43 changes: 43 additions & 0 deletions subprojects/nxf-commons/src/main/nextflow/extension/Bolts.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,49 @@ class Bolts {
StringUtils.uncapitalize(self)
}

/**
* Check if a alphabetic characters in a string are lowercase. Non alphabetic characters are ingored
* @param self The string to check
* @return {@true} if the string contains no uppercase characters, {@code false} otherwise
*/
static boolean isLowerCase(String self) {
if( self ) for( int i=0; i<self.size(); i++ ) {
if( Character.isUpperCase(self.charAt(i)))
return false
}
return true
}

/**
* Check if a alphabetic characters in a string are uppercase. Non alphabetic characters are ignored
* @param self The string to check
* @return {@true} if the string contains no lowercase characters, {@code false} otherwise
*/
static boolean isUpperCase(String self) {
if( self ) for( int i=0; i<self.size(); i++ ) {
if( Character.isLowerCase(self.charAt(i)))
return false
}
return true
}

/**
* Check if ALL characters in a string are lowercase.
* @param self The string to check
* @return {@true} when all characters are uppercase, {@code false} otherwise
*/
static boolean isAllLowerCase(String self) {
StringUtils.isAllLowerCase(self)
}

/**
* Check if ALL characters in a string are uppercase.
* @param self The string to check
* @return {@true} when all characters are uppercase, {@code false} otherwise
*/
static boolean isAllUpperCase(String self) {
StringUtils.isAllUpperCase(self)
}

static private Pattern getPattern( obj ) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,32 @@ class BoltsTest extends Specification {
salut.findBestMatchesFor('helo') == ['hello','halo']
}

def 'should check lower case strings' () {
expect:
'abc'.isLowerCase()
!'AAA'.isLowerCase()
'1a11a1a'.isLowerCase()
}

def 'should check upper case strings' () {
expect:
'ABCD'.isUpperCase()
!'aaaa'.isUpperCase()
'1A11A1A'.isUpperCase()
}

def 'should check all lower case strings' () {
expect:
'abc'.isAllLowerCase()
!'AAA'.isAllLowerCase()
!'1a11a1a'.isAllLowerCase()
}

def 'should check all upper case strings' () {
expect:
'ABCD'.isAllUpperCase()
!'aaaa'.isAllUpperCase()
!'1A11A1A'.isAllUpperCase()
}

}

0 comments on commit 439b572

Please sign in to comment.