Skip to content

Package Manager

posit-pm

Reports on Package Manager. It prints the state of the rstudio-pm service and rstudio-pm.gcfg, then Server.Address. It checks whether anything listens on HTTP.Listen and HTTPS.Listen and whether the address answers. It also prints the version from the __api__/status endpoint and the HTTPS certificate if one is set. It prints DelaySyncDuration for CRAN, Bioconductor, and PyPI and warns about any SyncSchedule setting in those sections. No SyncSchedule setting appears in the current configuration reference.1 It also prints Git.AllowUnsandboxedGitBuilds and checks that each Server.RVersion directory exists. It lists the repositories from the __api__/repos endpoint, and finishes with free space for Server.DataDir (default /var/lib/rstudio-pm) and the log directory.

Package Manager documents its API in the Server API Guide.2 The two endpoints answered without a token when this page was written, on a server with authentication turned off. A server that requires authentication for its API may refuse them.

Usage

--compare URL also fetches the repository list from another Package Manager and prints the names found on only one of them. Use it during a migration to confirm the new server publishes the same repositories as the old one.

posit-pm
posit-pm --compare https://ppm-old.example.com

The rspm CLI does not need root; members of the rstudio-pm group can use it.3 The script defers sudo rspm list; if you are in the group, run /opt/rstudio-pm/bin/rspm list yourself.

The script needs posit-lib.sh in the same directory.

Source: scripts/posit-pm
#!/bin/bash
#
# posit-pm: report on Posit Package Manager without root.
#
# Shows the version, service, configuration, listening ports, the public
# address, sync schedules, and the repositories the server publishes.
# With --compare URL it also compares the repository names with another
# Package Manager server. Checks that need root are printed at the end as
# commands for the sudo administrator.
#
# Usage: posit-pm [--compare https://other-ppm.example.com]
#

# Report scripts keep going when one check fails, so -e is not set
set -uo pipefail

# shellcheck source=posit-lib.sh
. "$(dirname "$(readlink -f "$0")")/posit-lib.sh" || exit 1

readonly CONF="${POSIT_PM_CONF:-/etc/rstudio-pm/rstudio-pm.gcfg}"
readonly RSPM='/opt/rstudio-pm/bin/rspm'

#######################################
# Print the value of every "KEY": "VALUE" pair in JSON on STDIN.
# Arguments:
#   Key.
#######################################
json_strings() {
  grep -o "\"$1\": *\"[^\"]*\"" | sed 's/.*"\([^"]*\)"$/\1/'
}

#######################################
# Print the repository names a Package Manager server publishes.
# Arguments:
#   Server URL.
#######################################
repo_names() {
  curl -fsS --max-time 15 -- "${1%/}/__api__/repos" 2>/dev/null \
    | json_strings name \
    | sort -u
}

#######################################
# Print the addresses, sync, and build settings from the config file.
# Globals:
#   CONF
# Arguments:
#   Server.Address, already read from CONF.
#######################################
config_report() {
  local address="$1"
  local section
  local listen
  local v
  local k
  local path

  header "Addresses"
  item "Server.Address" "${address:-not set}"
  for section in HTTP HTTPS; do
    listen="$(gcfg_value "${CONF}" "${section}" Listen)"
    [[ -z "${listen}" ]] && continue
    check_listen "${section}.Listen" "${listen%:*}" "${listen##*:}"
  done
  if [[ -n "${address}" ]]; then
    http_check "Address reachable" "${address}"
    item "Version" "$(curl -fsS --max-time 10 -- \
      "${address%/}/__api__/status" 2>/dev/null | json_strings version)"
  fi
  cert_check "HTTPS certificate" \
    "$(gcfg_value "${CONF}" HTTPS Certificate)" \
    "$(gcfg_value "${CONF}" HTTPS Key)"

  header "Sync and builds"
  for section in CRAN Bioconductor PyPI; do
    v="$(gcfg_value "${CONF}" "${section}" DelaySyncDuration)"
    item "${section}.DelaySyncDuration" "${v:-not set}"
    v="$(gcfg_value "${CONF}" "${section}" SyncSchedule)"
    if [[ -n "${v}" ]]; then
      warn "${section}.SyncSchedule = ${v} is not in the current" \
        "configuration reference"
    fi
  done
  v="$(gcfg_value "${CONF}" Git AllowUnsandboxedGitBuilds)"
  item "Git.AllowUnsandboxedGitBuilds" "${v:-not set}"
  for k in RVersion PythonVersion; do
    while read -r path; do
      if [[ -d "${path}" ]]; then
        item "Server.${k}" "${path}"
      else
        warn "Server.${k} ${path}: directory not found"
      fi
    done < <(gcfg_values "${CONF}" Server "${k}")
  done
}

#######################################
# Print the repositories, and compare them with another server.
# Arguments:
#   Server.Address, URL to compare with (may be empty).
#######################################
repo_report() {
  local address="$1"
  local compare_url="$2"
  local names
  local other

  header "Repositories"
  if [[ -z "${address}" ]]; then
    item "Repositories" "Server.Address not set; cannot query the API"
    return
  fi
  names="$(repo_names "${address}")"
  if [[ -n "${names}" ]]; then
    printf '    %s\n' "${names//$'\n'/$'\n'    }"
  else
    warn "no repositories returned by ${address%/}/__api__/repos"
  fi
  [[ -z "${compare_url}" ]] && return
  header "Compared with ${compare_url}"
  other="$(repo_names "${compare_url}")"
  if [[ -z "${other}" ]]; then
    warn "no repositories returned by ${compare_url%/}/__api__/repos"
    return
  fi
  comm -23 <(echo "${names}") <(echo "${other}") | sed 's/^/    only here: /'
  comm -13 <(echo "${names}") <(echo "${other}") | sed 's/^/    only there: /'
  if [[ "${names}" == "${other}" ]]; then
    echo "    Same repository names on both servers."
  fi
}

main() {
  local compare_url=""
  local address=""
  local data_dir=""

  case "${1:-}" in
    "") ;;
    --compare)
      compare_url="${2:?--compare needs a Package Manager URL}"
      ;;
    -h|--help)
      sed -n '3,12p' "$0" | sed 's/^# \{0,1\}//'
      exit 0
      ;;
    *)
      echo "Unknown option: $1" >&2
      exit 1
      ;;
  esac

  report_start "Posit Package Manager report"

  header "Service"
  unit_status rstudio-pm

  header "Configuration"
  config_file "rstudio-pm.gcfg" "${CONF}"

  if [[ -r "${CONF}" ]]; then
    address="$(gcfg_value "${CONF}" Server Address)"
    config_report "${address}"
  fi

  repo_report "${address}" "${compare_url}"
  defer "Repositories and sources" "sudo ${RSPM} list"
  note "rspm also works without root for members of the rstudio-pm group"

  header "Data and logs"
  if [[ -r "${CONF}" ]]; then
    data_dir="$(gcfg_value "${CONF}" Server DataDir)"
  fi
  dir_size "Package Manager data" "${data_dir:-/var/lib/rstudio-pm}"
  log_dir "Package Manager logs" /var/log/rstudio/rstudio-pm

  print_deferred
}

main "$@"