commandlog.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/sqlchemy"
  21. api "yunion.io/x/onecloud/pkg/apis/webconsole"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. var commandLogManager *SCommandLogManager
  27. type CommandType string
  28. const (
  29. CommandTypeSSH = "ssh"
  30. )
  31. func InitCommandLog() {
  32. commandLogManager = GetCommandLogManager()
  33. }
  34. func GetCommandLogManager() *SCommandLogManager {
  35. if commandLogManager != nil {
  36. return commandLogManager
  37. }
  38. commandLogManager = &SCommandLogManager{
  39. SOpsLogManager: db.NewOpsLogManager(SCommandLog{}, "command_log_tbl", "commandlog", "commandlogs", "start_time", consts.OpsLogWithClickhouse),
  40. }
  41. commandLogManager.SetVirtualObject(commandLogManager)
  42. return commandLogManager
  43. }
  44. type SCommandLogManager struct {
  45. db.SOpsLogManager
  46. }
  47. type SCommandLog struct {
  48. db.SOpsLog
  49. SessionId string `width:"128" charset:"ascii" list:"user"`
  50. AccessedAt time.Time `nullable:"false" list:"user" create:"required"`
  51. Type CommandType `width:"32" charset:"utf8" nullable:"true" list:"user" create:"required"`
  52. LoginUser string `charset:"utf8" list:"user" create:"required"`
  53. StartTime time.Time `list:"user" create:"required"`
  54. Ps1 string `charset:"utf8" list:"user" create:"optional" json:"ps1"`
  55. Command string `charset:"utf8" list:"user" create:"required"`
  56. }
  57. type CommandLogCreateInput struct {
  58. ObjId string
  59. ObjName string
  60. ObjType string
  61. Action string
  62. UserId string
  63. User string
  64. TenantId string
  65. Tenant string
  66. DomainId string
  67. Domain string
  68. ProjectDomainId string
  69. ProjectDomain string
  70. Roles string
  71. SessionId string
  72. AccessedAt time.Time
  73. Type CommandType
  74. LoginUser string
  75. StartTime time.Time
  76. Ps1 string `json:"ps1"`
  77. Command string
  78. Notes jsonutils.JSONObject
  79. }
  80. func (m *SCommandLogManager) Create(ctx context.Context, userCred mcclient.TokenCredential, input *CommandLogCreateInput) (*SCommandLog, error) {
  81. record := &SCommandLog{}
  82. record.ObjId = input.ObjId
  83. record.ObjName = input.ObjName
  84. record.Action = input.Action
  85. record.UserId = input.UserId
  86. record.User = input.User
  87. record.ProjectId = input.TenantId
  88. record.Project = input.Tenant
  89. record.DomainId = input.DomainId
  90. record.Domain = input.Domain
  91. record.ProjectDomainId = input.ProjectDomainId
  92. record.ProjectDomain = input.ProjectDomain
  93. record.Roles = input.Roles
  94. record.SessionId = input.SessionId
  95. record.AccessedAt = input.AccessedAt
  96. record.Type = input.Type
  97. record.LoginUser = input.LoginUser
  98. record.StartTime = input.StartTime
  99. record.Ps1 = input.Ps1
  100. record.Command = input.Command
  101. record.Notes = input.Notes.String()
  102. record.OpsTime = input.StartTime
  103. record.SetModelManager(m, record)
  104. err := m.TableSpec().Insert(ctx, record)
  105. if err != nil {
  106. return nil, errors.Wrap(err, "Insert")
  107. }
  108. return record, nil
  109. }
  110. func (m *SCommandLogManager) ListItemFilter(
  111. ctx context.Context,
  112. q *sqlchemy.SQuery,
  113. userCred mcclient.TokenCredential,
  114. input api.CommandLogListInput,
  115. ) (*sqlchemy.SQuery, error) {
  116. q, err := m.SOpsLogManager.ListItemFilter(ctx, q, userCred, input.OpsLogListInput)
  117. if err != nil {
  118. return nil, errors.Wrap(err, "SOpsLogManager.ListItemFilter")
  119. }
  120. return q, nil
  121. }