Skip to content
Merged
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
61 changes: 60 additions & 1 deletion qDup-core/src/main/java/io/hyperfoil/tools/qdup/cmd/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.hyperfoil.tools.yaup.StringUtil;
import io.hyperfoil.tools.yaup.json.Json;
import io.hyperfoil.tools.yaup.json.JsonMap;
import io.hyperfoil.tools.yaup.time.SystemTimer;
import org.jboss.logging.Logger;

import java.lang.invoke.MethodHandles;
Expand Down Expand Up @@ -623,6 +624,8 @@ public static String populateStateVariables(String command, Cmd cmd, State state
private Cmd parent;
private Cmd stateParent;

private List<Cmd> deferredCmd = new ArrayList<>();

private String idleTimer = ""+DEFAULT_IDLE_TIMER;
protected boolean silent = false;
private boolean stateScan = true;
Expand All @@ -646,6 +649,13 @@ protected Cmd(boolean silent) {
this.stateParent = null;
this.uid = uidGenerator.incrementAndGet();
}

public boolean hasDeferredCmd(){ return !deferredCmd.isEmpty();}
public void addDeferredCmd(Cmd command){
deferredCmd.add(command);
}
public List<Cmd> getDeferredCmd() {return deferredCmd;}

/**
* Set if this command should be scanned for state references.
* @param stateScan
Expand Down Expand Up @@ -966,8 +976,34 @@ public Cmd getPrevious() {
return rtrn;
}

/**
* returns the observed command or null if this command is not in an observing context
* @return
*/
public Cmd getObservedCmd(){
if(!isObserving()){
return null;
}
Cmd observer = getObservingRoot();
if(observer!=null && observer.hasStateParent()){
return observer.getStateParent();
}else{
return null;
}
}
public Cmd getObservingRoot(){
Cmd current = this;
while(current.hasParent()){
current = current.getParent();
}
return current;
}
public boolean isObserving(){
return !hasParent() && hasStateParent();
Cmd current = this;
while(current.hasParent()){
current = current.getParent();
}
return !current.hasParent() && current.hasStateParent();
}
public boolean hasParent(){return parent!=null;}
public Cmd getParent(){return parent;}
Expand Down Expand Up @@ -1126,12 +1162,35 @@ public final void doRun(String input, Context context) {

public void preRun(String input,Context context){

}
protected void runDeferred(String output, Context context){

if(hasDeferredCmd()){
if(context instanceof ScriptContext scriptContext){
SystemTimer deferredTimer = scriptContext.getContextTimer().start("deferred");
for(Cmd deferred : getDeferredCmd()){
SyncContext syncContext = new SyncContext(
scriptContext.getShell(),
scriptContext.getState(),
scriptContext.getRun(),
deferredTimer,//TODO create a deferred timer for this context?
deferred,
scriptContext
);
deferred.doRun(output,syncContext);
}
deferredTimer.stop();
}else{
//this shouldn't happen
}
}
}
public void postRun(String output,Context context){
String toLog = getLogOutput(output,context);
if(toLog != null && !toLog.isBlank()){
context.log(toLog);
}
runDeferred(output, context);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ public interface Context {
void close();
boolean isAborted();

void setCwd(String dir);
String getCwd();

void setHomeDir(String dir);
String getHomeDir();

Globals getGlobals();

}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public Json getActiveJson(){
entry.set("uid",currentCmd.getUid());
entry.set("sessionId",context.getContextId());
entry.set("script",rootCmd.getUid()+":"+rootCmd.toString());
entry.set("cwd",context.getCwd());
if(currentCmd instanceof Sh){
entry.set("input",currentCmd.getPrevious()!=null?currentCmd.getPrevious().getOutput():"");
entry.set("output",context.getShell().peekOutput());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,7 @@ public Cmd copy() {
long updateTime = -1;

private String roleName="";
private String cwd="";
private String homeDir="";
private boolean isAborted=false;
@Override
public String getCwd(){return cwd;}
@Override
public void setCwd(String cwd){
this.cwd = cwd;
}

@Override
public void setHomeDir(String dir){
this.homeDir = dir;
}
@Override
public String getHomeDir(){return homeDir;}

public boolean checkExitCode(){return checkExitCode;}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class SpyContext implements Context {
private Context context;
private Globals globals;

private String cwd="";
private String homeDir="";
public SpyContext(){
this(null,new State(""),new Coordinator(new Globals()));
}
Expand Down Expand Up @@ -230,25 +228,6 @@ public boolean isAborted(){
return context!=null ? context.isAborted() : false;
}

@Override
public void setCwd(String dir) {
this.cwd = cwd;
}

@Override
public String getCwd() {
return cwd;
}

@Override
public void setHomeDir(String dir) {
this.homeDir = dir;
}

@Override
public String getHomeDir() {
return homeDir;
}
public boolean calledAbort(){return aborted;}
public String getNext(){return next;}
public boolean hasNext(){return next!=null;}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public class SyncContext implements Context, Runnable{
private final ScriptContext scriptContext;
private final Cmd scriptActiveCmd;

private String cwd="";
private String homeDir="";

public SyncContext(AbstractShell session, State state, Run run, SystemTimer timer, Cmd currentCmd, ScriptContext scriptContext){
this.session = session;
Expand All @@ -43,31 +41,9 @@ public SyncContext(AbstractShell session, State state, Run run, SystemTimer time
this.scriptActiveCmd = scriptContext.getCurrentCmd();
}

public void setCwd(String cwd){
this.cwd = cwd;
}

public String getCwd(){
if(scriptContext!=null){
return scriptContext.getCwd();
} else {
return cwd;
}
}

@Override
public Globals getGlobals(){return run.getConfig().getGlobals();}

@Override
public void setHomeDir(String dir) {
this.homeDir = dir;
}

@Override
public String getHomeDir() {
return homeDir;
}

public Run getRun(){ return run;}

@Override
Expand Down Expand Up @@ -213,7 +189,7 @@ public SystemTimer getContextTimer() {

@Override
public SystemTimer getCommandTimer() {
return null;
return cmdTimer;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.hyperfoil.tools.qdup.cmd.Context;

import java.io.File;
import java.nio.file.Paths;
import java.util.function.Supplier;

public class Download extends Cmd {
Expand Down Expand Up @@ -76,35 +77,77 @@ public String execute(Context context, Supplier<Local> localProvider, Supplier<H
String destinationPath = context == null ? destination : populateStateVariables(basePath + File.separator +destination,this,context);


if(context!=null && context.getShell().isActive() && (
QueueDownload.hasBashEnv(remotePath) ||
remotePath.startsWith("~/") ||
!remotePath.startsWith("/")
)
){
if(isObserving()){
getObservedCmd().addDeferredCmd(
new Download(remotePath,populateStateVariables(destination,this,context),maxSize)
);
return destinationPath;
}else{
logger.error("context is busy and not observing when running "+this);
}
}else{

if(context==null && (
QueueDownload.hasBashEnv(remotePath) ||
remotePath.startsWith("~/") ||
!remotePath.startsWith("/")
)){
//This is an error condition, abort
logger.error("cannot download "+remotePath+" without a connection to resolve the full path");
return null;
}

File destinationFile = new File(destinationPath);
if(!destinationFile.exists()){
destinationFile.mkdirs();
}
boolean canDownload = true;
if(maxSize != null){
Long remoteFileSize = local.remoteFileSize(remotePath,host);
if(remoteFileSize > maxSize){
canDownload = false;
logger.warnf("Download File: `%s`; is larger %s than max size: %s bytes", remotePath, remoteFileSize, maxSize);
if(QueueDownload.hasBashEnv(remotePath)){
remotePath = context.getShell().shSync("export __qdup_ec=$?; echo "+remotePath+"; (exit $__qdup_ec)");
}
}
if(canDownload) {
boolean worked = local.download(remotePath, destinationPath, host);
if(!worked){
if(context!=null) {
context.error("failed to download " + remotePath + " to " + destinationPath);
context.abort(false);
if(remotePath.startsWith("~/")){
//export __qdup_ec=$?; echo "${__qdup_ec}"; (exit $__qdup_ec)
String homeDir = context.getShell().shSync("export __qdup_ec=$?; echo ~/; (exit $__qdup_ec)");
remotePath = homeDir+remotePath.substring("~/".length());
}else if (!remotePath.startsWith("/")){
String pwd = context.getShell().shSync("export __qdup_ec=$?; pwd; (exit $__qdup_ec)");
String normalized = Paths.get(pwd,remotePath).toAbsolutePath().normalize().toString();
if(remotePath.endsWith("/") && !normalized.endsWith("/")){
normalized = normalized + "/";
}
return null;
remotePath = normalized;
}

File destinationFile = new File(destinationPath);
if(!destinationFile.exists()){
destinationFile.mkdirs();
}
boolean canDownload = true;
if(maxSize != null){
Long remoteFileSize = local.remoteFileSize(remotePath,host);
if(remoteFileSize > maxSize){
canDownload = false;
logger.warnf("Download File: `%s`; is larger %s than max size: %s bytes", remotePath, remoteFileSize, maxSize);
}
}
if(canDownload) {
boolean worked = local.download(remotePath, destinationPath, host);
if(!worked){
if(context!=null) {
context.error("failed to download " + remotePath + " to " + destinationPath);
context.abort(false);
}
return null;
}
}
return canDownload ?
destinationPath.endsWith(File.separator) ?
destinationPath + (new File(remotePath)).getName() :
destinationPath + File.separator + (new File(remotePath)).getName()
: null;
}
return canDownload ?
destinationPath.endsWith(File.separator) ?
destinationPath + (new File(remotePath)).getName() :
destinationPath + File.separator + (new File(remotePath)).getName()
: null;
return null; // something went wrong
}

}
Loading
Loading