/
api_tool.go
962 lines (887 loc) · 25.1 KB
/
api_tool.go
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
package controllers
import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/fatih/color"
"github.com/gocarina/gocsv"
"github.com/ssbeatty/oms/internal/config"
"github.com/ssbeatty/oms/internal/models"
"github.com/ssbeatty/oms/internal/ssh"
"github.com/ssbeatty/oms/internal/task"
"github.com/ssbeatty/oms/internal/web/payload"
"github.com/ssbeatty/oms/pkg/utils"
"io"
"io/fs"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
)
const (
MaxPreviewSize = 8 * 1024 * 1024
Utf8Dom = "\xEF\xBB\xBF"
)
var (
red = color.New(color.FgRed).SprintFunc()
blue = color.New(color.FgBlue).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
)
// @BasePath /api/v1
// RunCmd
// @Summary 执行一条命令
// @Description 执行一条命令
// @Param id query integer true "执行者 ID"
// @Param type query string true "执行者类型" example(host,group,tag)
// @Param cmd query string true "命令"
// @Param sudo query bool false "是否sudo执行"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response{data=[]ssh.Result}
// @Failure 400 {object} payload.Response
// @Router /tools/cmd [get]
func (s *Service) RunCmd(c *Context) {
var params payload.CmdParams
err := c.ShouldBind(¶ms)
if err != nil {
c.ResponseError(err.Error())
} else {
hosts, err := models.ParseHostList(params.Type, params.Id)
if err != nil || len(hosts) == 0 {
data := payload.GenerateErrorResponse(HttpStatusError, payload.ErrHostParseEmpty)
c.JSON(http.StatusOK, data)
c.ResponseError("")
return
}
// do cmd
results := s.RunCmdExec(hosts, params.Cmd, params.Sudo)
c.ResponseOk(results)
}
}
// GetPathInfo
// @Summary 目录列表
// @Description 目录列表
// @Param id query string true "远端文件路径"
// @Param host_id query integer true "主机 ID"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response{data=FilePath}
// @Failure 400 {object} payload.Response
// @Router /tools/browse [get]
func (s *Service) GetPathInfo(c *Context) {
var params payload.OptionsFileParams
err := c.ShouldBind(¶ms)
if err != nil {
c.ResponseError(err.Error())
} else {
results, err := s.GetPathInfoExec(params.HostId, params.Id)
if err != nil {
data := payload.GenerateErrorResponse(HttpStatusError, err.Error())
c.JSON(http.StatusOK, data)
return
}
c.ResponseOk(results)
}
}
// DownLoadFile
// @Summary 下载文件
// @Description 下载文件
// @Param id query string true "远端文件路径"
// @Param host_id query integer true "主机 ID"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce application/octet-stream
// @Success 200
// @Failure 400 {object} payload.Response
// @Router /tools/download [get]
func (s *Service) DownLoadFile(c *Context) {
var params payload.OptionsFileParams
err := c.ShouldBind(¶ms)
if err != nil {
c.ResponseError(err.Error())
} else {
file := s.DownloadFile(params.HostId, params.Id)
if file != nil {
defer file.Close()
fh, err := file.Stat()
if err != nil {
s.Logger.Errorf("error when Stat file, err: %v", err)
c.String(http.StatusOK, "file stat error")
return
}
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", fh.Name()))
mode := fh.Mode() & fs.ModeType
// this is a read able file
if mode == 0 {
if fh.Size() != 0 {
http.ServeContent(c.Writer, c.Request, fh.Name(), fh.ModTime(), file)
return
} else {
buf, err := ioutil.ReadAll(file)
if err != nil {
s.Logger.Errorf("read virtual file error: %v", err)
}
http.ServeContent(c.Writer, c.Request, fh.Name(), fh.ModTime(), bytes.NewReader(buf))
return
}
} else {
c.String(http.StatusOK, fmt.Sprintf("read error, file type is [%s]", mode))
return
}
} else {
c.Status(http.StatusNotFound)
return
}
}
}
// DeleteFile
// @Summary 删除文件
// @Description 删除文件
// @Param id formData string true "远端文件路径"
// @Param host_id formData integer true "主机 ID"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response
// @Failure 400 {object} payload.Response
// @Router /tools/delete [post]
func (s *Service) DeleteFile(c *Context) {
var params payload.OptionsFileParams
err := c.ShouldBind(¶ms)
if err != nil {
c.ResponseError(err.Error())
} else {
err = s.DeleteFileOrDir(params.HostId, params.Id)
if err != nil {
c.ResponseError(err.Error())
} else {
c.ResponseOk(nil)
}
}
}
// MakeDirRemote
// @Summary 创建文件夹
// @Description 创建文件夹
// @Param id formData string true "远端文件路径"
// @Param host_id formData integer true "主机 ID"
// @Param dir formData string true "远端目录地址"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response
// @Failure 400 {object} payload.Response
// @Router /tools/mkdir [post]
func (s *Service) MakeDirRemote(c *Context) {
var params payload.MkdirParams
err := c.ShouldBind(¶ms)
if err != nil {
c.ResponseError(err.Error())
} else {
err = s.MakeDir(params.HostId, params.Id, params.Dir)
if err != nil {
c.ResponseError(err.Error())
} else {
c.ResponseOk(nil)
}
}
}
// ExecJob
// @Summary 单次执行任务
// @Description 单次执行任务
// @Param id formData integer true "任务 ID"
// @Tags job
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response{data=models.Job}
// @Failure 400 {object} payload.Response
// @Router /job/exec [post]
func (s *Service) ExecJob(c *Context) {
var form payload.OptionsJobForm
err := c.ShouldBind(&form)
if err != nil {
c.ResponseError(err.Error())
} else {
job, err := models.GetJobById(form.Id)
if err != nil {
s.Logger.Errorf("error when get job, err: %v", err)
c.ResponseError(err.Error())
}
err = s.taskManager.ExecJob(job)
if err != nil {
s.Logger.Errorf("error when start job, err: %v", err)
c.ResponseError(err.Error())
}
c.ResponseOk(job)
}
}
// StartJob
// @Summary 启动任务调度
// @Description 启动任务调度
// @Param id formData integer true "任务 ID"
// @Tags job
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response{data=models.Job}
// @Failure 400 {object} payload.Response
// @Router /job/start [post]
func (s *Service) StartJob(c *Context) {
var form payload.OptionsJobForm
err := c.ShouldBind(&form)
if err != nil {
c.ResponseError(err.Error())
} else {
job, err := models.GetJobById(form.Id)
if err != nil {
s.Logger.Errorf("error when get job, err: %v", err)
c.ResponseError(err.Error())
}
err = s.taskManager.StartJob(job)
if err != nil {
s.Logger.Errorf("error when start job, err: %v", err)
c.ResponseError(err.Error())
}
_ = models.RefreshJob(job)
c.ResponseOk(job)
}
}
// StopJob
// @Summary 停止任务调度
// @Description 停止任务调度
// @Param id formData integer true "任务 ID"
// @Tags job
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response{data=models.Job}
// @Failure 400 {object} payload.Response
// @Router /job/stop [post]
func (s *Service) StopJob(c *Context) {
var form payload.OptionsJobForm
err := c.ShouldBind(&form)
if err != nil {
c.ResponseError(err.Error())
} else {
job, err := models.GetJobById(form.Id)
if err != nil {
s.Logger.Errorf("error when get job, err: %v", err)
c.ResponseError(err.Error())
}
err = s.taskManager.StopJob(job.Id)
if err != nil {
s.Logger.Errorf("error when stop job, err: %v", err)
c.ResponseError(err.Error())
}
_ = models.RefreshJob(job)
c.ResponseOk(job)
}
}
// FilePreview
// @Summary 文件预览
// @Description 文件预览
// @Param id query string true "远端文件路径"
// @Param host_id query integer true "主机 ID"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response
// @Failure 400 {object} payload.Response
// @Router /tools/preview [get]
func (s *Service) FilePreview(c *Context) {
var params payload.OptionsFileParams
err := c.ShouldBind(¶ms)
if err != nil {
c.ResponseError(err.Error())
} else {
file := s.DownloadFile(params.HostId, params.Id)
if file != nil {
defer file.Close()
fh, err := file.Stat()
if err != nil {
s.Logger.Errorf("error when Stat file, err: %v", err)
c.ResponseError("file stat error")
}
if fh.Size() > MaxPreviewSize {
c.ResponseError("file size too large")
return
}
mode := fh.Mode() & fs.ModeType
// this is a read able file
if mode == 0 {
buf, err := ioutil.ReadAll(file)
if err != nil {
s.Logger.Errorf("read virtual file error: %v", err)
}
baseRaw := base64.StdEncoding.EncodeToString(buf)
c.ResponseOk(baseRaw)
} else {
c.ResponseError(fmt.Sprintf("read error, file type is [%s]", mode))
}
} else {
c.ResponseError("file not found")
}
}
}
// ModifyFile
// @Summary 文件修改
// @Description 文件修改
// @Param id query string true "远端文件路径"
// @Param host_id query integer true "主机 ID"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response
// @Failure 400 {object} payload.Response
// @Router /tools/modify [post]
func (s *Service) ModifyFile(c *Context) {
var params payload.ModifyFileParams
err := c.ShouldBind(¶ms)
if err != nil {
c.ResponseError(err.Error())
} else {
file := s.GetRWFile(params.HostId, params.Id)
if file != nil {
defer file.Close()
ModifyContentByte, err := base64.StdEncoding.DecodeString(params.ModifyContent)
if err != nil {
c.ResponseError(fmt.Sprintf("修改内容解码失败%v", err))
return
}
_, err = file.Write(ModifyContentByte)
if err == nil {
c.ResponseOk("文件修改成功")
} else {
c.ResponseError(fmt.Sprintf("文件修改失败%v", err))
}
}
}
}
// FileUploadV2
// @Summary 上传文件到主机
// @Description 上传文件到主机
// @Param X-Files header string true "预处理文件列表" example({"filename base64": "file.size"})
// @Param id formData integer true "执行者 ID"
// @Param type formData string true "执行者类型" example(host,group,tag)
// @Param remote formData string true "远端文件夹"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response
// @Failure 400 {object} payload.Response
// @Router /tools/upload [post]
func (s *Service) FileUploadV2(c *Context) {
var id int
var remoteFile, dType string
files := make(map[string]int)
// map [filename(url encode)] = size(int)
fileHeaders := c.GetHeader("X-Files")
if fileHeaders == "" {
c.Request.URL.Path += "_file"
s.Engine.HandleContext(c.Context)
return
}
err := json.Unmarshal([]byte(fileHeaders), &files)
if err != nil || len(files) == 0 {
c.Request.URL.Path += "_file"
s.Engine.HandleContext(c.Context)
return
}
mediaType, params, err := mime.ParseMediaType(c.Request.Header.Get("Content-Type"))
if err != nil {
data := payload.GenerateErrorResponse(HttpStatusError, "parse content type error")
c.JSON(http.StatusOK, data)
return
}
// 文件必须放到formData的末尾 否则解析会失败
if strings.HasPrefix(mediaType, "multipart/") {
partReader := multipart.NewReader(c.Request.Body, params["boundary"])
for {
part, err := partReader.NextPart()
if errors.Is(err, io.EOF) {
break
}
if part.FileName() == "" {
// 如果不是文件放在form里
all, err := ioutil.ReadAll(part)
if err != nil {
s.Logger.Errorf("file upload v2 error when read_all form part, err: %v", err)
continue
}
switch part.FormName() {
case "type":
dType = string(all)
case "remote":
remote := string(all)
if remote == "" {
remoteFile = remote
} else {
if remote[len(remote)-1] == '/' {
remoteFile = remote
} else {
remoteFile = remote + "/"
}
}
case "id":
id, err = strconv.Atoi(string(all))
if err != nil {
data := payload.GenerateErrorResponse(HttpStatusError, "parse params id error")
c.JSON(http.StatusOK, data)
return
}
}
} else {
// TODO skip repeat file
hosts, err := models.ParseHostList(dType, id)
if err != nil || len(hosts) == 0 {
data := payload.GenerateErrorResponse(HttpStatusError, "hosts parse error")
c.JSON(http.StatusOK, data)
return
}
// 每一个文件对应一个context如果 文件传输一半终止了 其下面所有的传输终止
ctx, cancel := context.WithCancel(context.Background())
fName := part.FileName()
escape := base64.StdEncoding.EncodeToString([]byte(fName))
p := path.Join(path.Join(s.conf.DataPath, config.DefaultTmpPath), fmt.Sprintf("multipart-%d-%s", int(time.Now().Unix()), fName))
tempFile := ssh.TempFile{
Name: fName,
Size: files[escape],
Path: p,
}
tmp, err := os.OpenFile(tempFile.Path, os.O_TRUNC|os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
data := payload.GenerateErrorResponse(HttpStatusError, "create tmp file error")
c.JSON(http.StatusOK, data)
return
}
// 在传输每个文件到tmp的同时就开始复制其到sftp客户端
go s.UploadFileStream(hosts, &tempFile, remoteFile, ctx)
n, err := io.Copy(tmp, part)
if err != nil {
// 这里多半是浏览器取消了请求
if int(n) != files[escape] {
cancel() // cancel all
err = tmp.Close()
if err != nil {
s.Logger.Errorf("close file %s error, err: %v", tmp.Name(), err)
}
os.Remove(tempFile.Path)
}
data := payload.GenerateErrorResponse(HttpStatusError, "io copy error")
c.JSON(http.StatusOK, data)
return
}
// 传输完成这个句柄是一定要关闭的
err = tmp.Close()
if err != nil {
s.Logger.Errorf("close file %s error, err: %v", tmp.Name(), err)
}
}
}
}
data := payload.GenerateMsgResponse(HttpStatusOk, HttpResponseSuccess)
c.JSON(http.StatusOK, data)
}
// FileUploadCancel
// @Summary 取消文件上传任务
// @Description 取消文件上传任务
// @Param addr formData string true "地址和端口"
// @Param file formData string true "文件名"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response
// @Failure 400 {object} payload.Response
// @Router /tools/upload/cancel [post]
func (s *Service) FileUploadCancel(c *Context) {
var form payload.FileTaskCancelForm
err := c.ShouldBind(&form)
if err != nil {
c.ResponseError(err.Error())
} else {
key := fmt.Sprintf("%s/%s", form.Addr, form.File)
s.sshManager.CancelTask(key)
c.ResponseOk(nil)
}
}
// deprecated
//func (s *Service) GetLogStream(c *Context) {
// var offset int64
// idRaw := c.Query("id")
// id, err := strconv.Atoi(idRaw)
// if err != nil {
// s.Logger.Errorf("GetLogStream error when Atoi idStr, idRaw: %s ,err: %v", idRaw, err)
// data := payload.GenerateResponsePayload(HttpStatusError, "can not parse param id", nil)
// c.JSON(http.StatusOK, data)
// return
// }
//
// job, ok := s.taskManager.GetJob(id)
// if !ok {
// c.String(http.StatusOK, "job is stopped")
// return
// }
//
// w := c.Writer
// header := w.Header()
// //http chunked
// header.Set("Transfer-Encoding", "chunked")
// header.Set("Content-Type", "text/plain;charset=utf-8")
// // https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-Content-Type-Options
// // 取消浏览器的MIME嗅探算法
// header.Set("X-Content-Type-Options", "nosniff")
//
// _, _ = fmt.Fprintf(w, "[job]: %s, [cmd]: %s log\n", job.Name(), job.Cmd())
// w.(http.Flusher).Flush()
//
// // todo
// stat, err := os.Stat("")
// if err != nil {
// c.String(http.StatusOK, "log file not existed")
// return
// }
// if stat.Size() > 2000 {
// offset = -2000
// } else {
// offset = -stat.Size()
// }
//
// t, err := tail.TailFile("", tail.Config{
// Follow: true,
// Poll: true,
// Location: &tail.SeekInfo{Offset: offset, Whence: io.SeekEnd},
// })
// if err != nil {
// data := payload.GenerateResponsePayload(HttpStatusError, "tail file error", nil)
// c.JSON(http.StatusOK, data)
// return
// }
//
// defer func() {
// err := t.Stop()
// if err != nil {
// s.Logger.Errorf("error when stop tail, err: %v", err)
// return
// }
// s.Logger.Debug("GetLogStream log file stream exit.")
// }()
//
// for {
// select {
// case line := <-t.Lines:
// if line == nil {
// continue
// }
// _, err := fmt.Fprintln(w, line.Text)
// if err != nil {
// return
// }
// w.(http.Flusher).Flush()
// case <-w.CloseNotify():
// s.Logger.Debug("log stream got done notify.")
// return
// }
// }
//}
func (s *Service) DownloadInstanceLog(c *Context) {
var param payload.GetTaskInstanceLogParam
err := c.ShouldBind(¶m)
if err != nil {
c.ResponseError(err.Error())
} else {
var instance *models.TaskInstance
instance, err = models.GetTaskInstanceById(param.Id)
if err != nil {
s.Logger.Errorf("get instance error: %v", err)
c.ResponseError(err.Error())
return
}
file, err := os.OpenFile(instance.LogPath, os.O_RDONLY, fs.ModePerm)
if file != nil && err == nil {
defer file.Close()
fh, err := file.Stat()
if err != nil {
s.Logger.Errorf("error when Stat file, err: %v", err)
c.String(http.StatusOK, "file stat error")
return
}
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", fh.Name()))
// this is a read able file
http.ServeContent(c.Writer, c.Request, fh.Name(), fh.ModTime(), file)
return
} else {
c.Status(http.StatusNotFound)
return
}
}
}
// GetInstanceLog
// @Summary 获取任务执行日志
// @Description 获取任务执行日志
// @Param id query integer true "执行记录 ID"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response
// @Failure 400 {object} payload.Response
// @Router /task/instance/log/get [get]
func (s *Service) GetInstanceLog(c *Context) {
var (
param payload.GetTaskInstanceLogParam
)
err := c.ShouldBind(¶m)
if err != nil {
c.ResponseError(err.Error())
} else {
var instance *models.TaskInstance
instance, err = models.GetTaskInstanceById(param.Id)
if err != nil {
s.Logger.Errorf("get instance error: %v", err)
c.ResponseError(err.Error())
return
}
file, err := os.OpenFile(instance.LogPath, os.O_RDONLY, fs.ModePerm)
if file != nil && err == nil {
defer file.Close()
var (
buffer bytes.Buffer
host *models.Host
idx int
total, success int
)
buffer.WriteString(blue(strings.Repeat("#", 40)) + "\r\n\r\n")
buffer.WriteString(green("# start run #\r\n"))
buffer.WriteString(fmt.Sprintf("Id : %s\r\n", blue(instance.Id)))
buffer.WriteString(fmt.Sprintf("Job : %s\r\n", blue(instance.Job.Name)))
if instance.Job.CmdType == ssh.CMDTypeShell {
buffer.WriteString(fmt.Sprintf("Cmd : %s\r\n", blue(instance.Job.Cmd)))
} else {
buffer.WriteString(fmt.Sprintf("Player: %s\r\n", blue(instance.Job.CmdId)))
}
buffer.WriteString(fmt.Sprintf("Start : %s\r\n", blue(instance.StartTime.Format(time.RFC3339))))
buffer.WriteString(fmt.Sprintf("End : %s\r\n", blue(instance.EndTime.Format(time.RFC3339))))
buffer.WriteString(fmt.Sprintf("Usage : %s\r\n", blue(instance.EndTime.Sub(instance.StartTime))))
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, task.MarkText) {
idx++
total++
idRaw := regexp.MustCompile("\\d+").FindString(line)
hostId, err := strconv.Atoi(idRaw)
if err != nil {
s.Logger.Errorf("error when parse host_id from log, instance_id: %d, err: %v", instance.Id, err)
continue
}
host, err = models.GetHostById(hostId)
if err != nil {
continue
}
if strings.HasSuffix(line, task.ErrorText) {
buffer.WriteString(red(fmt.Sprintf("## Seq: %d host info ##\r\n", idx)))
} else {
success++
buffer.WriteString(green(fmt.Sprintf("## Seq: %d host info ##\r\n", idx)))
}
buffer.WriteString(fmt.Sprintf("Host: %s\tId: %s\r\n", blue(host.Name), blue(host.Id)))
buffer.WriteString(fmt.Sprintf("Addr: %s\r\n", blue(fmt.Sprintf("%s:%d", host.Addr, host.Port))))
buffer.WriteString(strings.Repeat("-", 40) + "\r\n")
} else if strings.HasPrefix(line, task.DoneMartText) {
buffer.WriteString("\r\n")
buffer.WriteString(blue(fmt.Sprintf("执行完毕, 一共: %d个主机, 成功: %d个\r\n", total, success)))
} else {
buffer.WriteString(line)
buffer.WriteString("\r\n")
}
}
buffer.WriteString(blue(strings.Repeat("#", 40)) + "\r\n")
if err := scanner.Err(); err != nil {
s.Logger.Errorf("error when scanner log file, err: %v", err)
}
c.ResponseOk(buffer.String())
} else {
c.ResponseError("can not found logs")
return
}
}
}
// DataExport
// @Summary 导出资产文件csv
// @Description 导出资产文件csv
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce application/octet-stream
// @Success 200
// @Failure 400 {object} payload.Response
// @Router /tools/export [get]
func (s *Service) DataExport(c *Context) {
var (
b bytes.Buffer
data []models.HostExport
)
defer b.Reset()
hosts, err := models.GetAllHost()
if err != nil {
c.ResponseError(err.Error())
return
}
for _, host := range hosts {
tags := make([]string, 0)
for _, t := range host.Tags {
tags = append(tags, t.Name)
}
data = append(data, models.HostExport{
Name: host.Name,
User: host.User,
Addr: host.Addr,
PassWord: host.PassWord,
Port: host.Port,
VNCPort: host.VNCPort,
Group: host.Group.Name,
GroupParams: host.Group.Params,
Tags: tags,
KeyFile: host.PrivateKey.KeyFile,
KeyName: host.PrivateKey.Name,
KeyPhrase: host.PrivateKey.Passphrase,
})
}
// 写入UTF-8 BOM
b.WriteString(Utf8Dom)
err = gocsv.Marshal(&data, &b)
if err != nil {
c.ResponseError(err.Error())
return
}
c.Writer.Header().Set("Content-type", "application/octet-stream")
c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=%s", "export.csv"))
c.String(http.StatusOK, b.String())
}
// DataImport
// @Summary 导入资产文件csv
// @Description 导入资产文件csv
// @Param files formData file true "csv文件"
// @Tags tool
// @Accept x-www-form-urlencoded
// @Produce json
// @Success 200 {object} payload.Response{data=models.HostExport}
// @Failure 400 {object} payload.Response
// @Router /tools/import [post]
func (s *Service) DataImport(c *Context) {
const (
EmptyFileError = "empty files"
)
var (
data []models.HostExport
resp payload.ImportResponse
)
form, err := c.MultipartForm()
if err != nil {
c.ResponseError(EmptyFileError)
return
}
files := form.File["files"]
if len(files) == 0 {
c.ResponseError(EmptyFileError)
return
}
// 多个文件只取第一个
csvFile := files[0]
fn, err := csvFile.Open()
if err != nil {
c.ResponseError(err.Error())
return
}
content, err := ioutil.ReadAll(fn)
if err != nil {
c.ResponseError(err.Error())
return
}
// issue https://github.com/gocarina/gocsv/issues/191
content = bytes.TrimPrefix(content, []byte(Utf8Dom))
if !utils.IsUtf8(content) {
content, err = utils.GbkToUtf8(content)
if err != nil {
c.ResponseError(err.Error())
return
}
}
err = gocsv.UnmarshalBytes(content, &data)
if err != nil {
c.ResponseError(err.Error())
return
}
for _, row := range data {
var (
groupId, privateKeyID int
tags []int
)
// has grouped
if row.Group != "" {
var (
err error
group *models.Group
)
if !models.ExistedGroup(row.Group) {
if row.GroupParams == "" {
group, err = models.InsertGroup(row.Group, "", models.GroupHostMode)
} else {
group, err = models.InsertGroup(row.Group, row.GroupParams, models.GroupOtherMode)
}
if err == nil {
resp.CreateGroup = append(resp.CreateGroup, group.Name)
}
} else {
group, _ = models.GetGroupByName(row.Group)
}
groupId = group.Id
}
for _, t := range row.Tags {
var (
tag *models.Tag
)
if !models.ExistedTag(t) {
tag, err = models.InsertTag(t)
if err == nil {
resp.CreateTag = append(resp.CreateTag, tag.Name)
}
} else {
tag, _ = models.GetTagByName(t)
}
tags = append(tags, tag.Id)
}
if row.KeyFile != "" {
var (
privateKey *models.PrivateKey
)
if !models.ExistedPrivateKey(row.KeyFile) {
privateKey, err = models.InsertPrivateKey(row.KeyName, row.KeyFile, row.KeyPhrase)
if err == nil {
resp.CreatePrivateKey = append(resp.CreatePrivateKey, privateKey.Name)
}
} else {
privateKey, err = models.GetPrivateKeyByName(row.KeyName)
}
privateKeyID = privateKey.Id
}
if !models.ExistedHost(row.Name, row.Addr) {
h, err := models.InsertHost(
row.Name, row.User, row.Addr, row.Port, row.PassWord, groupId, tags, privateKeyID, row.VNCPort,
)
if err == nil {
resp.CreateHost = append(resp.CreateHost, h.Name)
}
}
}
c.ResponseOk(resp)
}