MariaDB.DSC.psm1

enum Ensure {
  Absent
  Present
}

<#
   This resource manages the installation/removal of MariaDB.
#>


[DscResource()]
class MariaDB {
  <#
    The service name of the Maria DB instance that is
    expected to be installed.
  #>

  [DscProperty(Key)]
  [string]$ServiceName

  <#
    Indicates if the settings should be present or absent
    on the system. For present, the resource ensures the file pointed
    to by $Path exists. For absent, it ensures the file point to by
    $Path does not exist.
  #>

  [DscProperty(Mandatory)]
  [Ensure] $Ensure

  <#
    Defines the default Maria DB root user password
  #>

  [DscProperty(Mandatory)]
  [System.Management.Automation.PSCredential] $RootPwd

  <#
    Full file path to the Maria DB Installer .MSI
  #>

  [DscProperty(Mandatory)]
  [string] $InstallerPath

  <#
    This method is equivalent of the Set-TargetResource script function.
    It sets the resource to the desired state.
  #>

  [void] Set() {
    $serviceExists = $this.TestServiceExists($this.ServiceName)
    if ($this.ensure -eq [Ensure]::Present) {
      if (-not $serviceExists) {
        Write-Verbose "Installing Maria DB with Service Name $($this.ServiceName)"
        $ArgumentList = "PASSWORD=$($this.RootPwd.GetNetworkCredential().Password) UTF8=true SERVICENAME=$($this.ServiceName) /qn"
        Start-Process -FilePath $this.InstallerPath -ArgumentList $ArgumentList -Wait -Passthru -ErrorAction Stop
      }
      else {

        # Ensure set to present but service already exists
        Write-Verbose "Ensure set to $($this.ensure) but service $($this.ServiceName) already exists - no changes performed"
      }
    }
    else {
      if ($serviceExists) {
        Write-Verbose -Message 'Uninstalling Maria DB'
        $ArgumentList = " /x $($this.InstallerPath) /q"
        Start-Process -FilePath 'msiexec' -ArgumentList $ArgumentList -Wait -PassThru -ErrorAction Stop
      }
      else {
        Write-Verbose "Ensure set to $($this.ensure) but service $($this.ServiceName) was not found - no changes performed"
      }
    }
  }

  <#
    This method is equivalent of the Test-TargetResource script function.
    It should return True or False, showing whether the resource
    is in a desired state.
  #>

  [bool] Test() {
    $present = $this.TestServiceExists($this.ServiceName)

    if ($this.Ensure -eq [Ensure]::Present) {
      return $present
    }
    else {
      return -not $present
    }
  }

  <#
    This method is equivalent of the Get-TargetResource script function.
    The implementation should use the keys to find appropriate resources.
    This method returns an instance of this class with the updated key
      properties.
  #>

  [MariaDB] Get() {
    $present = $this.TestServiceExists($this.ServiceName)

    if ($present) {
      $Service = Get-Service $this.ServiceName
      $this.Status = $Service.Status
      $this.StartType = $Service.StartType
      $this.Ensure = [Ensure]::Present
    }
    else {
      $this.Ensure = [Ensure]::Absent
    }
    return $this
  }

  <#
    Helper method to check if a service exists
  #>

  [bool] TestServiceExists([string] $ServiceName) {
    $present = $true

    $item = Get-Service $ServiceName -ErrorAction 'Ignore'
    if ($null -eq $item) {
      $present = $false
    }
    return $present
  }
}