DockerContainerCreate.js 4.26 KB
import React, { useState, useEffect } from 'react'
import { useHistory, Redirect } from 'react-router-dom'
import {
  Container,
  Heading,
  Button,
  Segment,
  Message,
  Form,
  Input,
  Transition,
  Select,
  Checkbox,
  FormControl,
  FormLabel,
  FormErrorMessage,
  FormHelperText,
  VStack,
  Stack,
  StackDivider,
  Box,
  Text, AlertDialogOverlay, AlertDialogContent, Alert, AlertIcon, AlertDialog,
} from '@chakra-ui/react'
import {
  Divider,
} from 'semantic-ui-react'
import http from '../utils/http'

const DockerContainerCreate = () => {
  const [images, setImages] = useState([])
  const [image, setImage] = useState('')
  const [containerName, setContainerName] = useState('')
  const [ssh, setSsh] = useState('')
  const [port, setPort] = useState(0)
  const [randomPort, setRandomPort] = useState(false)
  const [isSuccess, setSuccess] = useState(false)
  const [isLoading, setLoading] = useState(false)
  const [error, setError] = useState('')

  const history = useHistory()

  const loadImages = () => {
    setLoading(true)
    http.get('/docker/image/list').then(response => {
      const { status, message } = response.data
      const images = response.data.data
      setLoading(false)

      if (status) {
        setImages(images)
      } else {
        setError(message)
        console.log('[ ERROR ]', message)
      }

    }).catch(error => {
      console.error(error)
    })
  }

  const getImageDropdownOptions = () => {
    const defaultImage = {
      key: 999,
      value: 'default',
      text: 'default image',
    }

    return [defaultImage].concat(images.map((image, key) => {
      return {
        key: key,
        value: image.Id,
        text: image.RepoTags.length > 0 ? image.RepoTags[0] : ' - ',
      }
    }))
  }

  const createContainer = () => {
    setLoading(true)
    setError('')
    console.log('[ DEBUG ] image :', image)
    http.post('/docker/container/create', {
      name: containerName,
      image: image,
      port: port,
      random_port: randomPort,
    }).then(response => {
      const { status, message } = response.data
      const { username, password, port } = response.data.data
      setLoading(false)

      if (status) {
        setSuccess(true)
        setTimeout(3000, () => {
          history.push('/docker/container/list')
        })
      } else {
        setError(message)
      }

      // setTimeout(() => setSuccess(false), 10000);
    }).catch(error => {
      console.error('[ ERROR ]', error)
    })
  }

  const handleImage = (e, data) => {
    setImage(data.value)
  }

  const handleContainerName = (e) => {
    setContainerName(e.target.value)
  }

  const handlePort = (e) => {
    setPort(e.target.value)
  }

  const handleRandomPort = () => {
    setRandomPort(!randomPort)
  }

  useEffect(() => {
    loadImages()
  }, [])

  return (
    <Container>
      <Divider hidden/>

      <Stack
        divider={<Divider hidden/>}
        spacing={4}
        align="stretch"
      >
        <Heading>Create Docker Container</Heading>

        <FormControl id="container" isRequired>
          <FormLabel>Container Name</FormLabel>
          <Input
            type="email"
            placeholder='Container Name'
          />
        </FormControl>

        <FormControl id="image">
          <FormLabel>Image</FormLabel>
          <Select placeholder="생성할 컨테이너의 OS를 설정해주세요">
            <option value="ubuntu 18.04">Ubuntu 18.04.5 LTS</option>
            <option value="ubuntu 20.04">Ubuntu 20.04.2 LTS</option>
          </Select>
        </FormControl>

        <Button size="md" colorScheme="teal" mt="24px"
          onClick={() => createContainer()}
        >
          Create Container
        </Button>
      </Stack>

      <AlertDialog
        isOpen={isSuccess}
        onClose={() => setSuccess(false)}
      >
        <AlertDialogOverlay>
          <AlertDialogContent>
            <Alert status="success">
              <AlertIcon />
              Successfully created !
            </Alert>
          </AlertDialogContent>
        </AlertDialogOverlay>
      </AlertDialog>
    </Container>
  )
}

export default DockerContainerCreate