Resources:
  SSMAutomationTestHarnessConfig:
    Type: AWS::SSM::Document
    Properties:
      DocumentType: Automation
      Name: TestHarnessConfiguration
      Content:
        schemaVersion: '0.3'
        description: Setup Test Harness Server.
        assumeRole: !Sub "arn:aws:iam::${AWS::AccountId}:role/SSM-Automation-TestHarness-ConfigRole"
        mainSteps:
          - name: InstallBasePackages
            action: aws:runCommand
            nextStep: DownloadTestScript
            isEnd: false
            inputs:
              DocumentName: AWS-RunShellScript
              Targets:
                - Key: tag:Name
                  Values:
                    - tagmarshal-test-harness-test-instance
              Parameters:
                commands:
                  - |
                    sudo apt-get update
                    sudo npm install -g k6
                    sudo apt-get install -y jq
                    sudo apt-get install -y awscli
                    sudo apt-get install -y rsync

          - name: DownloadTestScript
            action: aws:runCommand
            nextStep: ExtractZipFile
            isEnd: false
            inputs:
              DocumentName: AWS-RunShellScript
              Targets:
                - Key: tag:Name
                  Values:
                    - tagmarshal-test-harness-test-instance
              Parameters:
                commands:
                  - |
                    aws s3 cp s3://k6testharnessrepo/test_harness.zip /tmp/test_harness.zip

          - name: ExtractZipFile
            action: aws:runCommand
            nextStep: CopyToTargetDirectory
            isEnd: false
            inputs:
              DocumentName: AWS-RunShellScript
              Targets:
                - Key: tag:Name
                  Values:
                    - tagmarshal-test-harness-test-instance
              Parameters:
                commands:
                  - |
                    mkdir -p /tmp/test_harness
                    unzip /tmp/test_harness.zip -d /tmp/test_harness

          - name: CopyToTargetDirectory
            action: aws:runCommand
            nextStep: SetPermissions
            isEnd: false
            inputs:
              DocumentName: AWS-RunShellScript
              Targets:
                - Key: tag:Name
                  Values:
                    - tagmarshal-test-harness-test-instance
              Parameters:
                commands:
                  - |
                    rsync -av /tmp/test_harness/ /home/ssm-user/test_harness/

          - name: SetPermissions
            action: aws:runCommand
            nextStep: CleanUp
            isEnd: false
            inputs:
              DocumentName: AWS-RunShellScript
              Targets:
                - Key: tag:Name
                  Values:
                    - tagmarshal-test-harness-test-instance
              Parameters:
                commands:
                  - |
                    sudo chown -R ssm-user:ssm-user /home/ssm-user/test_harness/
                    sudo chmod -R u+X,g+X,o+X /home/ssm-user/test_harness/
                    chmod u+x /home/ssm-user/test_harness/src/newversion/run_k6_wrapper.sh
                    chmod u+x /home/ssm-user/test_harness/src/newversion/start_k6_processes.sh

          - name: CleanUp
            action: aws:runCommand
            isEnd: true
            inputs:
              DocumentName: AWS-RunShellScript
              Targets:
                - Key: tag:Name
                  Values:
                    - tagmarshal-test-harness-test-instance
              Parameters:
                commands:
                  - |
                    rm -rf /tmp/test_harness
                    rm /tmp/test_harness.zip