HodorNV / ALOps

ALOps
55 stars 24 forks source link

BC24: shared folders #750

Closed acjdekorte closed 2 months ago

acjdekorte commented 2 months ago

Describe the bug With the introduction of BC24, we run into a problem with running a powershell script with shared folder. This script imports our license into the docker container to enable testing of the functionality. I am able to reproduce the issue with locally with BCContainerHelper. We have installed dot8 and PS7. Although I am not sure the problem is related to AlOps, I hope you can point me into the right direction.

the used yaml please provide the yaml that you used. It helps you put the yaml like this: The relevant steps

- task: ALOpsDockerStart@1
  displayName: 'ALOPS - Start Docker Container'
  condition: and(notIn(variables.pipelineTarget, 'nextminor', 'nextmajor'), ge(variables.artifactVersion, 21.0))
  inputs:
    docker_parameters: '--volume $(System.DefaultWorkingDirectory)\ShareBuild:c:\run\Build'
    licensefile:  ${{ parameters.licenseUrlNewFormat }}   # more info: https://www.youtube.com/watch?v=iVLOerdCuwA&t=3078s

parameters:
  appVersion: '17.0.[yyyyWW].*'
  appSourceFolder: ''
  disabledTestsFileName: ''
  extensionId: ''
  doNotRunTests: false

steps:
- task: ALOpsAppCompiler@1
  condition: ne(variables.doNotRunTests, true)
  displayName: 'ALOPS - Compile Extension: Test'
  inputs:
    usedocker: true
    targetproject: '${{parameters.appSourceFolder}}/app.json'
    ruleset: 'c:/Run/DevOps/${{parameters.appSourceFolder}}/MAW.ruleset.json'
    nav_app_version: ${{parameters.appVersion}}
    app_file_suffix: '_TEST'

- task: PowerShell@2
  condition: ne(variables.doNotRunTests, true)
  displayName: "Export compile log: Test"
  inputs:
    targetType: 'inline'
    script: 'Copy-Item -Path compilelog.txt -Destination ''$(Build.ArtifactStagingDirectory)/Technical/compilelog_Test.txt'''
    errorActionPreference: 'continue'

- task: ALOpsAppPublish@1
  condition: ne(variables.doNotRunTests, true)
  displayName: 'ALOPS - Publish Extension: Test'
  inputs:
    usedocker: true
    nav_artifact_app_filter: '*TEST.app'
    skip_verification: true

- task: PowerShell@2
  condition: ne(variables.doNotRunTests, true)
  displayName: "Import Agriware license"
  inputs:
    filePath: AlBuildSupport/Scripts/Import-AgriwareLicense.ps1
    arguments: '-containername ''$(ALOPS_DOCKER_CONTAINER_ID)'' -SharedFolderInContainer ''C:\run\Build'' -SharedFolderServer ''$(System.DefaultWorkingDirectory)\ShareBuild'' -SecureLicenseURL ''$(AgriwareLicense)'' -licenseFiles @{''17.1'' = ''$(AgriwareLicense)'';''17.2''=''$(AgriwareLicense172)'';''19.0''=''$(AgriwareLicense190)'';''19.1''=''$(AgriwareLicense191)'';''20.0''=''$(AgriwareLicense200)'';''20.1''=''$(AgriwareLicense201)'';''22.0''=''$(AgriwareLicense220)'';''22.1''=''$(AgriwareLicense221)'';''23.0''=''$(AgriwareLicense230)'';''23.1''=''$(AgriwareLicense231)'';''24.0''=''$(AgriwareLicense240)'';''24.1''=''$(AgriwareLicense240)''}'

- task: ALOpsAppTest@1
  condition: and(ne('${{ parameters.disabledTestsFileName }}', ''), ne(variables.doNotRunTests, true))
  displayName: "ALOPS - Run TestSuite with disabled tests from ${{parameters.disabledTestsFileName}}"
  inputs:
    usedocker: true
    disabledtests: '$(Build.SourcesDirectory)/${{parameters.appSourceFolder}}/${{parameters.disabledTestsFileName}}'
    import_testtoolkit: false
    import_action: 'Skip'
    testpage: '130455'
    extensionid: ${{parameters.extensionId}}
    failed_test_action: 'Ignore'

- task: ALOpsAppTest@1
  condition: and(eq('${{ parameters.disabledTestsFileName }}', ''), ne(variables.doNotRunTests, true))
  displayName: "ALOPS - Run TestSuite"
  inputs:
    usedocker: true
    import_testtoolkit: false
    import_action: 'Skip'
    testpage: '130455'
    extensionid: ${{parameters.extensionId}}
    failed_test_action: 'Ignore'

- task: PublishTestResults@2
  condition: ne(variables.doNotRunTests, true)
  displayName: "Publish Test Results **/TestResults.xml"
  inputs:
    testResultsFormat: XUnit
    testResultsFiles: "**/TestResults.xml"
    failTaskOnFailedTests: true
    testRunTitle: "BC Test Results: $(Build.BuildId)"

- task: PowerShell@2
  condition: ne(variables.doNotRunTests, true)
  displayName: "Create DisabledTests.json"
  inputs:
    filePath: 'AlBuildSupport/Scripts/ConvertTo-DisabledTests.ps1'
    arguments: '-inputFolderPath ''$(System.DefaultWorkingDirectory)/'''
    errorActionPreference: 'continue'

- task: CopyFiles@2
  condition: ne(variables.doNotRunTests, true)
  displayName: 'Copy Test Artifacts to $(Agent.BuildDirectory)/TestResults'
  inputs:
    SourceFolder: '$(Build.Sourcesdirectory)'
    Contents: ?(TestResults.xml|DisabledTests.json)
    TargetFolder: '$(Agent.BuildDirectory)/TestResults'

- task: PublishPipelineArtifact@1
  condition: ne(variables.doNotRunTests, true)
  displayName: 'Publish Test Artifacts: TestResults.xml and DisabledTests.json'
  inputs:
    targetPath: '$(Agent.BuildDirectory)/TestResults'
    artifact: 'TestResults'
    publishLocation: 'pipeline'
param
(
    [Parameter()]
    [string]$containername = 'Test17',

    [Parameter()]
    [string] $SecureLicenseURL,

    [Parameter()]
    [string] $SharedFolderInContainer = 'C:\run\Build',

    [Parameter()]
    [string] $SharedFolderServer = "",

    [Parameter()]
    [HashTable] $licenseFiles = @{}
)

$filenameLicense = 'license.mlf'

Get-BcContainerSharedFolders -containerName $containername

$versiontext=''
$app = Get-BcContainerAppInfo -containerName $containername -installedOnly | where {$_.AppId -eq '41602dbb-b7fe-4ce5-9eb9-0783dda7f345'}
if($app){
    $versiontext = "$($app.version.Major).$($app.version.minor)"
    Write-Host "Version determined based on App: $versiontext"
}
if($versiontext -in $licenseFiles.Keys){
    Write-Host "Version found in hashtable for license files and replacing SecureLicenseURL"
    $SecureLicenseURL = $licensefiles[$versiontext]
}
else{
    Write-Host "Version not found in hashtable"
}

Invoke-WebRequest $SecureLicenseURL -OutFile (join-path $SharedFolderServer $filenameLicense)

invoke-scriptInBcContainer -containerName $containername -scriptblock {
    $SharedFolderInContainer = 'C:\run\Build'
    $filenameLicense = 'license.mlf'
    $licenseInContainer = join-path $SharedFolderInContainer $filenameLicense 

    $me = whoami
    Write-host "whoami $($me)"

    $serverinstance = 'BC'
    $userexist = Get-NAVServerUser -ServerInstance $ServerInstance | Where-Object username -eq $me
    if (!($userexist)) {
        Write-host 'create user'
        New-NAVServerUser -ServerInstance $ServerInstance -WindowsAccount $me
        New-NAVServerUserPermissionSet -ServerInstance $ServerInstance -WindowsAccount $me -PermissionSetId SUPER
    } elseif ($userexist.state -eq "Disabled") {
        Write-host 'enable user'
        Set-NAVServerUser -ServerInstance $ServerInstance -WindowsAccount $me -state Enabled
    }

    Try{
        write-host 'Try using new method for agriware license import'
        Invoke-NAVCodeunit `
            -serverinstance BC `
            -CodeunitId 11269442 `
            -MethodName ImportLicenseTextFromCode `
            -Argument (get-content -path $licenseInContainer -raw -encoding utf8) `
            -ErrorAction Stop
    }
    catch{
        write-host 'Using old method for agriware license import, since new method failed'
        Invoke-NAVCodeunit `
            -ServerInstance BC `
            -CodeunitId 11269442 `
            -MethodName ImportLicenseFileFromCode `
            -Argument ($licenseInContainer) 
    }                
    Write-Host "Imported license from path '$($licenseInContainer)'"
} 

the output Also the complete output is necessary for us to see what is going on. Also use backtics:

Starting: Import Agriware license
==============================================================================
Task         : PowerShell
Description  : Run a PowerShell script on Linux, macOS, or Windows
Version      : 2.237.5
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
Formatted command: . 'C:\ADO Agents\Agent2\_work\4191\s\AlBuildSupport\Scripts\Import-AgriwareLicense.ps1' -containername 'd71dc1c64bed9690bed40130b645fc28836cf6e2a09ddf4929040e7f6c4a4d07' -SharedFolderInContainer 'C:\run\Build' -SharedFolderServer 'C:\ADO Agents\Agent2\_work\4191\s\ShareBuild' -SecureLicenseURL '***' -licenseFiles @{'17.1' = '***';'17.2'='***';'19.0'='***';'19.1'='***';'20.0'='***';'20.1'='***';'22.0'='***';'22.1'='***';'23.0'='***';'23.1'='***';'24.0'='***';'24.1'='***'}
========================== Starting Command Output ===========================
"C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:\ADO Agents\Agent2\_work\_temp\a1df55a4-e37f-4b34-b30d-7bbcb2c33d87.ps1'"
BcContainerHelper version 6.0.15
BC.HelperFunctions emits usage statistics telemetry to Microsoft
Running on Windows, PowerShell 5.1.20348.2227

Name                           Value                                                                                   
----                           -----                                                                                   
C:\ADO Agents\Agent2\_work\... c:\run\Build                                                                            
Get-BcContainerAppInfo Telemetry Correlation Id: 87c9a7a0-8c9a-4031-a7c1-290272f4627c
The path C:\ProgramData\BcContainerHelper\ebc1c261-e351-4233-955b-14cd92037bae.ps1 is not shared with the container d71
dc1c64bed9690bed40130b645fc28836cf6e2a09ddf4929040e7f6c4a4d07 (nor is any of it's parent folders)
At C:\Program Files\WindowsPowerShell\Modules\BcContainerHelper\6.0.15\ContainerInfo\Get-NavContainerPath.ps1:48 char:1
3
+             throw "The path $path is not shared with the container $c ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (The path C:\Pro...parent folders):String) [], RuntimeException
    + FullyQualifiedErrorId : The path C:\ProgramData\BcContainerHelper\ebc1c261-e351-4233-955b-14cd92037bae.ps1 is no 
   t shared with the container d71dc1c64bed9690bed40130b645fc28836cf6e2a09ddf4929040e7f6c4a4d07 (nor is any of it's p  
  arent folders)

##[error]PowerShell exited with code '1'.
Finishing: Import Agriware license

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem. You can easily paste them in here.

Additional context Add any other context about the problem here.

waldo1001 commented 2 months ago

Hi Ariën,

Seems very custom, but I'd suggest not to use PowerShell-step, but to use "ALOpsDockerExec": https://docs.alops.be/ALOpsSteps/Buildstepsv1/#alops_docker_execute

This will execute a script on the docker container and avoid "invoke-scriptInBcContainer", which is failing at the moment.

acjdekorte commented 2 months ago

Thank you for your suggestion

waldo1001 commented 2 months ago

@acjdekorte did this help?

acjdekorte commented 2 months ago

Last week it did not help. I was running into other problems. For the moment decided to disable the automatic tests, since I have insufficient time to fix it now.

epernst commented 4 weeks ago

This issue is marked as closed, but we are still getting the same errors, so does it mean that it will not be fixed?

waldo1001 commented 4 weeks ago

@acjdekorte reported the issue, and closed it as well. We didn't.

If you have this issue, it needs to be re-opened or re-created, including a decent repro-scenario.

epernst commented 4 weeks ago

Thank you. For now we have just added a "--volume" to the Docker Parameters to share the missing BcContainerHelper folder with the container. Seems to work fine.

waldo1001 commented 4 weeks ago

Hm, interesting - could you share the complete step?

epernst commented 4 weeks ago

We have a Yaml Template that sets all the Container parameters like version, type, country etc. Here I added:

      if (-not $version.application.contains(".")) {
          $version.application = $version.application + ".0"
      }
      $navVersion = [system.version] ($version.application)
      if ($navVersion.Major -ge 24) {
          Write-Host "NavVersion: $($NavVersion.major)"
          $ShareFolder = "C:\ProgramData\BcContainerHelper:C:\ProgramData\BcContainerHelper"
          $additionalParameters += @("--volume ""$ShareFolder""")
      }
Write-Host "##vso[task.setvariable variable=docker_parameters;]$additionalParameters"

($version.application may be full version, or only major i.e. 23.4 or 24, and system.version fails if it doesn't contain at least .0).