R/Utils.R

Defines functions remote.execute.cmd remote.copy.to Read_Campaign_template Read_param_template edit_list_obj

Documented in edit_list_obj Read_Campaign_template Read_param_template remote.copy.to remote.execute.cmd

#' edit_list_obj
#'
#' @param param_list
#' @param tmp_yml
#'
#' @return
#' @export
#'
#' @examples
edit_list_obj <- function(param_list, tmp_yml) {

  for (nn in names(param_list)) {
    if (is.list(param_list[[nn]])) {
      tmp_yml[[nn]] <- edit_list_obj(param_list[[nn]], tmp_yml[[nn]])
    } else{
      tmp_yml[[nn]] <- param_list[[nn]]
    }

  }
  return(tmp_yml)
}


#' Read_param_template
#'
#' @return
#' @export
#'
#' @examples
Read_param_template <-function(){
  yaml_fname <- "params.apsim.sample"
  tmp_yml_path <- system.file("templates", yaml_fname, package = "pSIMSSiteMaker")
  param_yml <- yaml::read_yaml(tmp_yml_path)
  return(param_yml)
}

#' Read_Campaign_template
#'
#' @return
#' @export
#'
#' @examples
Read_Campaign_template <-function(){
  json_fname <- "exp_template.json"
  tmp_json_path <- system.file("templates", json_fname, package = "pSIMSSiteMaker")
  camp_json <- jsonlite::fromJSON(tmp_json_path)
  return(camp_json)
}

#' Copy file/dir to remote server from local server
#'
#' Copies the file/dir to the remote server from the local server. If the dst
#' is a folder it will copy the file into that folder.
#'
#' @inheritParams remote.execute.cmd
#' @param src local file/dir to copy
#' @param dst remote file/dir to copy to
#' @param options additional arguments to be passed to rsync command
#' @param delete in case of local dir should all non-existent files be removed
#' @return output of command executed
#'
#' @author Rob Kooper
#' @export
#' @examples
#' \dontrun{
#'
#'  # ssh -n -N -f -o ControlMaster=yes -S ~/tunnel/tunnel  hamzed`@`cc-login.campuscluster.illinois.edu

#'   host <- list(name='cc-login.campuscluster.illinois.edu', user='hamzed', tunnel='~/tunnel')
#'   remote.copy.to(host, '~/pSIMS', '/projects/aces/hamzed/psims/Data/NewP', delete=TRUE)
#' }
remote.copy.to <- function(host, src, dst, options = NULL, delete = FALSE, stderr = FALSE) {
  args <- c("-a", "-q", options)

  if (as.logical(delete)) {
    args <- c(args, "--delete")
  }


    tunnel <- host$tunnel

    if (!is.null(host$data_tunnel)) {
      tunnel <- host$data_tunnel
    }

    hostname <- host$name
    if (!is.null(host$data_hostname)) {
      hostname <- host$data_hostname
    }

    if (!is.null(tunnel)) {
      if (!file.exists(tunnel)) {
        print("Could not find tunnel")
      }
      args <- c(args, "-e", paste0("ssh -o ControlPath=\"", tunnel, "\"",
                                   collapse = ""))
      args <- c(args, src, paste0(hostname, ":", dst))
    } else if (!is.null(host$user)) {
      args <- c(args, src, paste0(host$user, "@", hostname, ":", dst))
    } else {
      args <- c(args, src, paste0(hostname, ":", dst))
    }

  system2("rsync", shQuote(args), stdout = TRUE, stderr = as.logical(stderr))
} # remote.copy.to



#' Execute command remotely
#'
#' Executes the given command on the remote host using ssh. If the user is set
#' the system will login as the given user. If the host given is the local
#' machine it will execute the command locally without ssh.
#'
#' @title Execute command remotely
#' @param command the system command to be invoked, as a character string.
#' @param host host structure to execute command on
#' @param args a character vector of arguments to command.
#' @param stderr should stderr be returned as well.
#' @return the captured output of the command (both stdout and stderr)
#' @author Rob Kooper
#' @export
#' @examples
#' \dontrun{
#' #' host <- list(name='cc-login.campuscluster.illinois.edu', user='hamzed', tunnel='~/tunnel')
#'   print(remote.execute.cmd(host, 'ls', c('-l', '/'), stderr=TRUE))
#' }
remote.execute.cmd <- function(host, cmd, args = character(), stderr = FALSE) {
  if (is.character(host)) {
    host <- list(name = host)
  }


    remote <- host$name
    if (!is.null(host$tunnel)) {
      if (!file.exists(host$tunnel)) {
        print("Could not find tunnel")
      }
      remote <- c("-o", paste0("ControlPath=\"", host$tunnel, "\""), remote)
    } else if (!is.null(host$user)) {
      remote <- c("-l", host$user, remote)
    }
    print(paste(c("ssh", "-T", remote, cmd, args), collapse = " "))
    system2("ssh", c("-T", remote, cmd, args), stdout = TRUE, stderr = as.logical(stderr))
} # remote.execute.cmd
AgronomicForecastingLab/pSIMSSiteMaker documentation built on March 6, 2021, 2:27 p.m.