ch4n3.yoon

[New] Docker Container Create 화면 구현

1 import React, { useEffect } from 'react' 1 import React, { useEffect } from 'react'
2 import { BrowserRouter, Route, Redirect, Switch, Link } from 'react-router-dom' 2 import { BrowserRouter, Route, Redirect, Switch, Link } from 'react-router-dom'
3 -import { ChakraProvider } from "@chakra-ui/react" 3 +import { ChakraProvider } from '@chakra-ui/react'
4 import Index from './components/Index' 4 import Index from './components/Index'
5 +import DockerContainerCreate from './components/DockerContainerCreate'
5 6
6 const App = () => { 7 const App = () => {
7 return ( 8 return (
...@@ -9,6 +10,7 @@ const App = () => { ...@@ -9,6 +10,7 @@ const App = () => {
9 <BrowserRouter> 10 <BrowserRouter>
10 <Switch> 11 <Switch>
11 <Route exact path="/" component={Index}/> 12 <Route exact path="/" component={Index}/>
13 + <Route exact path="/docker/container/create" component={DockerContainerCreate}/>
12 <Redirect path="*" to="/"/> 14 <Redirect path="*" to="/"/>
13 </Switch> 15 </Switch>
14 </BrowserRouter> 16 </BrowserRouter>
......
1 +import React, { useState, useEffect } from 'react'
2 +import { useHistory, Redirect } from 'react-router-dom'
3 +import {
4 + Container,
5 + Heading,
6 + Button,
7 + Segment,
8 + Message,
9 + Divider,
10 + Form,
11 + Input,
12 + Transition,
13 + Select,
14 + Checkbox,
15 + FormControl,
16 + FormLabel,
17 + FormErrorMessage,
18 + FormHelperText,
19 +} from '@chakra-ui/react'
20 +import http from '../utils/http'
21 +
22 +const DockerContainerCreate = () => {
23 + const [images, setImages] = useState([])
24 + const [image, setImage] = useState('')
25 + const [containerName, setContainerName] = useState('')
26 + const [ssh, setSsh] = useState('')
27 + const [port, setPort] = useState(0)
28 + const [randomPort, setRandomPort] = useState(false)
29 + const [isSuccess, setSuccess] = useState(false)
30 + const [isLoading, setLoading] = useState(false)
31 + const [error, setError] = useState('')
32 +
33 + const history = useHistory()
34 +
35 + const loadImages = () => {
36 + setLoading(true)
37 + http.get('/docker/image/list').then(response => {
38 + const { status, message } = response.data
39 + const images = response.data.data
40 + setLoading(false)
41 +
42 + if (status) {
43 + setImages(images)
44 + } else {
45 + setError(message)
46 + console.log('[ ERROR ]', message)
47 + }
48 +
49 + }).catch(error => {
50 + console.error(error)
51 + })
52 + }
53 +
54 + const getImageDropdownOptions = () => {
55 + const defaultImage = {
56 + key: 999,
57 + value: 'default',
58 + text: 'default image',
59 + }
60 +
61 + return [defaultImage].concat(images.map((image, key) => {
62 + return {
63 + key: key,
64 + value: image.Id,
65 + text: image.RepoTags.length > 0 ? image.RepoTags[0] : ' - ',
66 + }
67 + }))
68 + }
69 +
70 + const createContainer = () => {
71 + setLoading(true)
72 + setError('')
73 + console.log('[ DEBUG ] image :', image)
74 + http.post('/docker/container/create', {
75 + name: containerName,
76 + image: image,
77 + port: port,
78 + random_port: randomPort,
79 + }).then(response => {
80 + const { status, message } = response.data
81 + const { username, password, port } = response.data.data
82 + setLoading(false)
83 +
84 + if (!status) {
85 + setError(message)
86 + } else {
87 + setSuccess(true)
88 + setSsh(
89 + `ssh ${username}@${window.location.hostname} -p ${port} # (password : ${password})`)
90 + }
91 +
92 + // setTimeout(() => setSuccess(false), 10000);
93 + }).catch(error => {
94 + console.error('[ ERROR ]', error)
95 + })
96 + }
97 +
98 + const handleImage = (e, data) => {
99 + setImage(data.value)
100 + }
101 +
102 + const handleContainerName = (e) => {
103 + setContainerName(e.target.value)
104 + }
105 +
106 + const handlePort = (e) => {
107 + setPort(e.target.value)
108 + }
109 +
110 + const handleRandomPort = () => {
111 + setRandomPort(!randomPort)
112 + }
113 +
114 + useEffect(() => {
115 + loadImages()
116 + }, [])
117 +
118 + return (
119 + <Container>
120 + <Divider hidden/>
121 + <Segment raised>
122 + <Heading size='huge'>Create your own docker container</Heading>
123 + <Divider hidden/>
124 +
125 + <FormControl id="container">
126 + <FormLabel>Container Name</FormLabel>
127 + <Input
128 + type="email"
129 + placeholder='Container Name'
130 + />
131 + <FormHelperText>컨테이너 이름을 입력해주세요.</FormHelperText>
132 + </FormControl>
133 +
134 + <div>
135 + <Input
136 + label='Container Name'
137 + placeholder='Container Name'
138 + onChange={handleContainerName}
139 + />
140 +
141 + <Select label='Image'>
142 + <option value="option1">Option 1</option>
143 + <option value="option2">Option 2</option>
144 + <option value="option3">Option 3</option>
145 + </Select>
146 +
147 + {/*<Form.Group>
148 + <Form.Dropdown
149 + label='Image'
150 + selection
151 + options={getImageDropdownOptions()}
152 + defaultValue={getImageDropdownOptions()[0]}
153 + onChange={handleImage}
154 + />
155 + </Form.Group>*/}
156 +
157 + <Input
158 + type='number'
159 + label='SSH Port'
160 + placeholder='22'
161 + disabled={randomPort}
162 + onChange={handlePort}
163 + />
164 +
165 + {/*<Form.Group>
166 + <Form.Input
167 + type='number'
168 + label='SSH Port'
169 + placeholder='22'
170 + disabled={randomPort}
171 + onChange={handlePort}
172 + />
173 + </Form.Group>*/}
174 + <Checkbox
175 + label='Use random port'
176 + checked={randomPort}
177 + onChange={handleRandomPort}
178 + />
179 + <Button
180 + color='violet'
181 + onClick={createContainer}
182 + content='Create'
183 + />
184 + <Button
185 + content='Container List'
186 + onClick={() => history.push('/docker/container/')}
187 + />
188 + </div>
189 + {/*
190 + <Transition.Group
191 + duration={300}
192 + >
193 + {isSuccess && (
194 + <>
195 + <Divider/>
196 + <Message
197 + success
198 + content={ssh}
199 + />
200 + </>
201 + )}
202 + {error && (
203 + <>
204 + <Divider/>
205 + <Message
206 + error
207 + content={error}
208 + />
209 + </>
210 + )}
211 + </Transition.Group>*/}
212 + </Segment>
213 + </Container>
214 + )
215 +}
216 +
217 +export default DockerContainerCreate
1 import React, { useEffect } from 'react' 1 import React, { useEffect } from 'react'
2 import { BrowserRouter, Route, Redirect, Switch, Link } from 'react-router-dom' 2 import { BrowserRouter, Route, Redirect, Switch, Link } from 'react-router-dom'
3 -import { Container } from "@chakra-ui/react" 3 +import { Container } from '@chakra-ui/react'
4 4
5 5
6 const Index = () => { 6 const Index = () => {
......
1 +import axios from 'axios'
2 +
3 +const http = axios.create({
4 + baseURL: 'http://192.168.241.128:3030/',
5 + withCredentials: true,
6 +})
7 +
8 +export default http