All files / modules/30-delegates/components/CreateDelegate/DockerDelegate/Step2Script Step2Script.tsx

83.72% Statements 36/43
44.44% Branches 8/18
85.71% Functions 6/7
83.72% Lines 36/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210              3x 3x 3x 3x 3x 3x 3x   3x   3x     3x   3x 3x   3x 8x                                             3x 3x 2x 2x 2x   2x   2x           2x 2x   2x     2x     2x 2x 2x     2x 2x     2x                   2x                           2x                                     1x                   1x                                                                                                                                                   3x  
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useEffect, useState } from 'react'
import { useParams } from 'react-router-dom'
import set from 'lodash-es/set'
import { Button, Layout, StepProps, Heading, Text, Container } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import CopyToClipboard from '@common/components/CopyToClipBoard/CopyToClipBoard'
import { useStrings } from 'framework/strings'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import YamlBuilder from '@common/components/YAMLBuilder/YamlBuilder'
 
import { useGenerateDockerDelegateYAML, DelegateSetupDetails } from 'services/portal'
 
import type { DockerDelegateWizardData } from '../CreateDockerDelegate'
import css from './Step2Script.module.scss'
 
const dockerFileName = 'docker-compose.yml'
const dockerComposeCommand = `docker-compose -f ${dockerFileName} up -d`
 
const getCommandBlock = (label: string, command: string) => (
  <>
    <Text margin={{ top: 'medium' }} color={Color.BLACK}>
      {label}
    </Text>
    <Layout.Horizontal spacing="medium">
      <Container
        intent="primary"
        padding="small"
        font={{
          align: 'center'
        }}
        flex
        className={css.verificationField}
      >
        <Text margin={{ right: 'xxlarge' }} font="small">
          {`$ ${command}`}
        </Text>
        <CopyToClipboard content={command} />
      </Container>
    </Layout.Horizontal>
  </>
)
 
const Step2Script: React.FC<StepProps<DockerDelegateWizardData>> = props => {
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const [delegateDockerYaml, setDelegateDockerYaml] = useState('')
  const { getString } = useStrings()
  const { previousStep, prevStepData } = props
 
  const linkRef = React.useRef<HTMLAnchorElement>(null)
 
  const { mutate: getDockerYaml } = useGenerateDockerDelegateYAML({
    queryParams: {
      accountId
    }
  })
 
  const fetchDockerYaml = async () => {
    const createParams = { ...prevStepData } as DelegateSetupDetails
 
    Iif (projectIdentifier) {
      set(createParams, 'projectIdentifier', projectIdentifier)
    }
    Iif (orgIdentifier) {
      set(createParams, 'orgIdentifier', orgIdentifier)
    }
    set(createParams, 'delegateType', 'DOCKER')
    const dockerYaml = (await getDockerYaml(createParams)) as any
    setDelegateDockerYaml(dockerYaml)
  }
 
  useEffect(() => {
    fetchDockerYaml()
  }, [])
 
  const onDownload = () => {
    const content = new Blob([delegateDockerYaml as BlobPart], { type: 'data:text/plain;charset=utf-8' })
    if (linkRef?.current) {
      linkRef.current.href = window.URL.createObjectURL(content)
      linkRef.current.download = dockerFileName
      linkRef.current.click()
    }
  }
 
  const yamlBuilderComp = (
    <YamlBuilder
      entityType="Delegates"
      fileName={dockerFileName}
      isReadOnlyMode={true}
      isEditModeSupported={false}
      hideErrorMesageOnReadOnlyMode={true}
      existingYaml={delegateDockerYaml as any}
      showSnippetSection={false}
      width="568px"
      height="462px"
      theme="DARK"
    />
  )
 
  return (
    <>
      <Layout.Horizontal padding={{ top: 'small', left: 'xlarge', right: 'xlarge' }}>
        <Layout.Vertical spacing="medium">
          <div>{yamlBuilderComp}</div>
          <Layout.Horizontal padding={{ top: 'medium' }}>
            <Button
              id="stepReviewScriptDownloadButton"
              icon="arrow-down"
              text={getString('delegates.downloadYAMLFile')}
              onClick={onDownload}
              outlined
            />
          </Layout.Horizontal>
          <Layout.Horizontal padding={{ top: 'medium' }} spacing="small">
            <Button
              id="stepReviewScriptBackButton"
              text={getString('back')}
              icon="chevron-left"
              onClick={() => previousStep?.({ ...prevStepData })}
              intent="none"
            />
            <Button
              id="stepReviewScriptContinueButton"
              type="submit"
              intent="primary"
              text={getString('continue')}
              rightIcon="chevron-right"
              onClick={() => {
                props.nextStep?.({ ...prevStepData })
              }}
            />
          </Layout.Horizontal>
        </Layout.Vertical>
        <Layout.Vertical className={css.verticalDivider}>
          <hr />
        </Layout.Vertical>
        <Layout.Vertical>
          <Heading level={2} margin={{ bottom: 'large' }} color={Color.BLACK}>
            {getString('delegates.delegateCreation.docker.verifyTitle')}
          </Heading>
 
          <Text lineClamp={2} font="normal" margin={{ bottom: 'medium' }}>
            {getString('delegates.delegateCreation.docker.verifyDesc1')}
          </Text>
 
          <Text lineClamp={2} margin={{ bottom: 'medium' }}>
            {getString('delegates.delegateCreation.docker.verifyDesc2')}
          </Text>
 
          <Layout.Horizontal spacing="medium">
            <Container
              intent="primary"
              padding="small"
              font={{
                align: 'center'
              }}
              flex
              className={css.verificationField}
            >
              <Text margin={{ right: 'xxlarge' }} font="small">
                {`$ ${dockerComposeCommand}`}
              </Text>
              <CopyToClipboard content={dockerComposeCommand} />
            </Container>
          </Layout.Horizontal>
        </Layout.Vertical>
        <div className={css.dockerDetailsContainer} style={{ display: 'none' }}>
          <Heading level={2} font={{ weight: 'bold' }} color={Color.BLACK} margin={{ bottom: 'large' }}>
            {getString('delegates.delegateCreation.docker.scriptTitle')}
          </Heading>
 
          {getCommandBlock(getString('delegates.delegateCreation.docker.scriptText'), '')}
 
          {getCommandBlock(getString('delegates.delegateCreation.docker.scriptCommandGetIds'), 'docker ps')}
 
          {getCommandBlock(
            getString('delegates.delegateCreation.docker.scriptSeeLogs'),
            'docker logs -f <container-id>'
          )}
 
          {getCommandBlock(
            getString('delegates.delegateCreation.docker.scriptRunInShell'),
            'docker container exec -it <container-id> bash'
          )}
 
          <Text margin={{ top: 'xlarge' }} color={Color.BLACK}>
            {getString('delegates.delegateCreation.docker.docLinkBefore')}
            <a
              rel="noreferrer"
              href="https://ngdocs.harness.io/article/5ww21ewdt8-configure-delegate-proxy-settings"
              target="_blank"
            >
              {getString('delegates.delegateCreation.docker.docLink')}
            </a>
          </Text>
        </div>
      </Layout.Horizontal>
      <a className="hide" ref={linkRef} target={'_blank'} />
    </>
  )
}
 
export default Step2Script