자바스크립트를 활성화 해주세요

p014 Powershell Webserver PServeREST 테스트해봤습니다

 ·  ☕ 4 min read

    일반적으로는

    PowerShell로 웹 인터페이스를 만드는 일은 좀처럼 하지 않습니다. Stateless하고, 짧은 동작을 수행해야하는 REST APi라면 좀 더 퍼포먼스와 확장도 생각해 두는 것이 좋습니다. 일반적이라면 Powershell보다는 좀 더 Web Service에 필요한 프레임웍을 지원하는 언어를 선택하는 것이 일반적입니다.

    하지만, 경우에 따라서는

    하지만 경우에 따라서는 달리 생각을 해 볼 수도 있습니다. 예를 들면

    • REST API의 프로토타입을 테스트하고 싶은 경우
    • 일시적으로 잠시만 사용할 경우

    와 같은 때가 있지요. 아직은 Production에서 사용한 경험은 없습니다.

    장점 1

    결과를 눈으로 빨리 확인할 수 있습니다.

    Powershell의 장점은 Verbose하지 않다는 데에 있습니다. 즉, 일반 쉘 커맨드처럼 간결하게 사용할 수 있다는 것이 장점이고, 거기에 스트립트 언어의 기능을 가지고 있기 때문에 다이나믹하게 구현할 수 있다는 데에 있습니다.

    예를 들면 Hyper-V를 다루는 로직이 대부분 powershell로 구현되어 있다면, 이를 호출하는 REST API의 인터페이스는 Powershell을 System-Call하는 것이 구현을 확인하는데에는 빠를 수 있습니다.

    단위 함수의 동작으로 profiling해보면 물론 퍼포먼스는 WMI를 이용한 것에 비기지 못합니다. 하지만, 동작을 빨리 확인하는 데에 촛점을 맞추어 보면, Operating언어로는 제일 적합하다고 생각합니다. 이를 이용하여 WebServer를 구현해 보면 역시, 눈으로 빨리 확인할 수 있다는 장점을 취할 수 있습니다.

    장점 2

    다른 언어와 조합해서 쓸 때와 비교하면, powershell만으로 이루어진 경우는 불필요한 로직을 생략할 수 있습니다.

    결국 powershell엔진을 사용하는 로직을 구현하는 것이라면, 인터페이스를 제공하는 호스트가 csharp 이든지, python이든지 powershell이 serializing해서 되돌리고, 호스트는 이 값을 deserializing 해서 전달 받아야 할 것입니다.

    그리고 다시 필요한 처리를 하고 나서, serializing해서 json형태로 되돌리는 REST API가 되는 거죠.

    만일 이 부분을 powershell을 사용한다면, 이 serializing하는 process를 생략할 수 있을 것입니다.

    생략할 수 있다는 것은 빠르게 움직이게 할 수 있다는 것입니다. 장점입니다.

    이번에는 PServeREST

    PServeREST 를 이용해 웹서비스를 구현해 보았습니다. 사실 다른 모듈도 여러개 존재하는 것 같습니다만, 하나하나 순서대로 살펴보겠습니다.

    goorm ide를 이용해서 설치

    먼저 pwsh 설치

    다음의 커맨드를 사용해서 설치합니다.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    apt-get install build-essential
    # Download the Microsoft repository GPG keys
    wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
    # Register the Microsoft repository GPG keys
    sudo dpkg -i packages-microsoft-prod.deb
    # Update the list of products
    sudo apt-get update
    # Enable the "universe" repositories
    sudo add-apt-repository universe
    # Install PowerShell
    sudo apt-get install -y powershell
    # Start PowerShell
    pwsh
    

    workspace아래에 module을 clone하고 PSModulePath에 soft link생성하기

    PS /root> $env:PSModulePath
    /root/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/opt/microsoft/powershell/7/Modules
    
    $env:PSModulePath.Split(":")[1]
    mkdir -p /usr/local/share/powershell/Modules
    cd /usr/local/share/powershell/Modules
    
    cd /workspace/pwsh
    git clone https://github.com/Tadas/PServeREST
    
    PS /usr/local/share/powershell/Modules> git clone https://github.com/Tadas/PServeREST
    'PServeREST'에 복제합니다...
    remote: Enumerating objects: 40, done.
    remote: Total 40 (delta 0), reused 0 (delta 0), pack-reused 40
    오브젝트 묶음 푸는 중: 100% (40/40), 완료.
    
    ln -s /workspace/pwsh/PServeREST /usr/local/share/powershell/Modules/PServeREST
    ls -la /usr/local/share/powershell/Modules/PServeREST
    

    Import-Module해서 실행해 봤는데 에러가 났습니다.
    Dependency로 Logging을 필요로 했기 때문에 추가로 설치했습니다.
    실행하기 전에 함수의 동작을 살펴보기 위해 열어서 내용을 살펴보았습니다.

    그리고 실행

    Import-Module PServeREST
    
    PS /usr/local/share/powershell/Modules/PServeREST> import-module PServeREST
    Set-LoggingDefaultLevel: /usr/local/share/powershell/Modules/PServeREST/PServeREST.psm1:8
    Line |
       8 |  Set-LoggingDefaultLevel -Level 'NOTSET'
         |  ~~~~~~~~~~~~~~~~~~~~~~~
         | The term 'Set-LoggingDefaultLevel' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
         | the spelling of the name, or if a path was included, verify that the path is correct and try again.
    
    Import-Module: The module to process 'PServeREST.psm1', listed in field 'ModuleToProcess/RootModule' of module manifest '/usr/local/share/powershell
    /Modules/PServeREST/PServeREST.psd1' was not processed because no valid module was found in any module directory.
    
    Install-Module Logging
    
    Untrusted repository
    You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the
    Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
    PS /usr/local/share/powershell/Modules/PServeREST>
    
    Import-Module PServeREST
    Invoke-PServeREST -ListenURI 'http://+:80/'
    

    실행한 웹 모습은 다음과 같습니다

    PServeREST.png

    동작 테스트

    도메인 pwsh-jhdby.run.goorm.io 은 dashboard에서 확인할 수 있습니다.

    동시 접속 테스트

    /DemoResources/SleepTime.ps1 파일을 만들고 10초 sleep후에 시간을 출력하는 함수를 추가했습니다.

    1
    2
    3
    4
    5
    
    function GET-SleepTime {
    	Param($Context)
    	Start-Sleep -s 10
    	(Get-Date).ToString($Context.Request.QueryString['format'])
    }
    

    결과는

    2020. 7. 27. 오후 2:18:08
    2020. 7. 27. 오후 2:28:08
    

    순처적으로 실행되었습니다.

    결론

    • 손에 powershell 이 익은 경우라면 레퍼런스를 찾아서 봐야하는 다른 언어보다는 적당한 노력으로 결과를 빨리 볼 수 있습니다.
    • 많은 Simple Server가 그렇듯이 동시접속은 고려되어 있지 않았습니다.

    레퍼런스

    공유하기

    tkim
    글쓴이
    tkim
    Software Engineer