Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/packaging/grafana-agent-flow/windows/install_script.nsis
5389 views
# This script does the following:
#
# 1. Installs grafana-agent-flow-windows-amd64.exe, grafana-agent-service-amd64.exe, and logo.ico.
# 2. Creates a Start Menu shortcut.
# 3. Builds an uninstaller.
# 4. Adds uninstall information to the registry for Add/Remove Programs.
# 5. Initializes the registry with appropriate settings.

Unicode true

!include nsDialogs.nsh
!include FileFunc.nsh
!include ./macros.nsis

!define APPNAME     "Grafana Agent Flow"
!define DESCRIPTION "Vendor-neutral programmable observability pipelines."
!define HELPURL     "https://grafana.com/docs/agent/latest/flow/"
!define UPDATEURL   "https://github.com/grafana/agent/releases"
!define ABOUTURL    "https://github.com/grafana/agent"

# Because we modify the registry and install a service that runs as
# LocalSystem, we require admin permissions.
RequestExecutionLevel admin

Name        "${APPNAME} ${VERSION}" # Shown in title bar for installer/uninstaller
Icon        "logo.ico"
InstallDir  "$PROGRAMFILES64\${APPNAME}"
LicenseData ../../../LICENSE
OutFile     "${OUT}"

# Pages during the installer.
Page license
Page directory
Page instfiles

# Automatically called when installing.
Function .onInit
  SetShellVarContext all
  !insertmacro VerifyUserIsAdmin
FunctionEnd

Section "install"
  # Calls to functions like nsExec::ExecToLog below push the exit code to the
  # stack, and must be popped after calling.

  # Preemptively stop the existing service if it's running.
  nsExec::ExecToLog 'sc stop "Grafana Agent Flow"'
  Pop $0

  # Configure the out path and copy files to it.
  SetOutPath "$INSTDIR"
  File "../../../dist.temp/grafana-agent-flow-windows-amd64.exe"
  File "../../../dist.temp/grafana-agent-service-windows-amd64.exe"
  File "logo.ico"

  # Create an uninstaller at the same pathFunctionEnd
  WriteUninstaller "$INSTDIR\uninstall.exe"

  # Registry information for Add/Remote Programs. It's OK for this to
  # overwriting existing registry entries since we want it to be relevant to
  # the current installed version.
  !define UNINSTALLKEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}"
  WriteRegStr   HKLM "${UNINSTALLKEY}" "DisplayName"          '${APPNAME} ${VERSION} - ${DESCRIPTION}'
  WriteRegStr   HKLM "${UNINSTALLKEY}" "UninstallString"      '"$INSTDIR\uninstall.exe"'
  WriteRegStr   HKLM "${UNINSTALLKEY}" "QuietUninstallString" '"$INSTDIR\uninstall.exe" /S'
  WriteRegStr   HKLM "${UNINSTALLKEY}" "InstallLocation"      '"$INSTDIR"'
  WriteRegStr   HKLM "${UNINSTALLKEY}" "DisplayIcon"          '"$INSTDIR\logo.ico"'
  WriteRegStr   HKLM "${UNINSTALLKEY}" "Publisher"            '"${ABOUTURL}"'
  WriteRegStr   HKLM "${UNINSTALLKEY}" "HelpLink"             '"${HELPURL}"'
  WriteRegStr   HKLM "${UNINSTALLKEY}" "URLUpdateInfo"        '"${UPDATEURL}"'
  WriteRegStr   HKLM "${UNINSTALLKEY}" "URLInfoAbout"         '"${ABOUTURL}"'
  WriteRegDWORD HKLM "${UNINSTALLKEY}" "NoModify" 1
  WriteRegDWORD HKLM "${UNINSTALLKEY}" "NoRepair" 1

  Call CreateConfig
  Call InitializeRegistry

  # Create the service.
  nsExec::ExecToLog 'sc create "Grafana Agent Flow" start= delayed-auto binpath= "$INSTDIR\grafana-agent-service-windows-amd64.exe"'
  Pop $0

  # Start the service.
  nsExec::ExecToLog 'sc start "Grafana Agent Flow"'
  Pop $0
SectionEnd

Function CreateConfig
  IfFileExists "$INSTDIR\config.river" Noop CreateNewConfig
  Noop:
    Return
  CreateNewConfig:
    File "config.river"
    Return
FunctionEnd

# InitializeRegistry initializes the keys in the registry that the service
# runner uses. If the registry values already exist, they are not overwritten.
Function InitializeRegistry
  !define REGKEY "HKLM\Software\Grafana\Grafana Agent Flow"

  # Define the default key, which points to the service.
  nsExec::ExecToLog 'Reg.exe query "${REGKEY}" /reg:64 /ve'
  Pop $0
  ${If} $0 == 1
    nsExec::ExecToLog 'Reg.exe add "${REGKEY}" /reg:64 /ve /d "$INSTDIR\grafana-agent-flow-windows-amd64.exe"'
    Pop $0 # Ignore return result
  ${EndIf}

  # Define the arguments key, which holds arguments to pass to the
  # service.
  nsExec::ExecToLog 'Reg.exe query "${REGKEY}" /reg:64 /v Arguments'
  Pop $0
  ${If} $0 == 1
    nsExec::ExecToLog 'Reg.exe add "${REGKEY}" /reg:64 /v Arguments /t REG_MULTI_SZ /d "run"\0"$INSTDIR\config.river"\0"--storage.path=$APPDATA\${APPNAME}\data"\0'
    Pop $0 # Ignore return result
  ${EndIf}

  Return
FunctionEnd

# Automatically called when uninstalling.
Function un.onInit
  SetShellVarContext all

  IfSilent Noop WarnUser
  Noop:
    Return
  WarnUser:
    MessageBox MB_OKCANCEL "Permanently remove ${APPNAME}? This will remove all data for the application." IDOK Continue
      Abort
    Continue:
      !insertmacro VerifyUserIsAdmin
      Return
FunctionEnd

Section "uninstall"
  DetailPrint "Starting uninstaller."

  # Stop and remove service.
  nsExec::ExecToLog 'sc stop "Grafana Agent Flow"'
  Pop $0
  nsExec::ExecToLog 'sc delete "Grafana Agent Flow"'
  Pop $0

  RMDir /r "$SMPROGRAMS\${APPNAME}"  # Start Menu folder.
  RMDir /r "$INSTDIR"                # Install directory.
  RMDir /r "$APPDATA\${APPNAME}"     # Application data.

  # Remove service and uninstaller information from the registry.
  DeleteRegKey HKLM "Software\Grafana\Grafana Agent Flow"
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}"
SectionEnd