Class: Kscript::KkKibanaManageUtils
- Defined in:
- lib/kscript/plugins/kk_kibana_manage_utils.rb
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
-
#add_all_index ⇒ Object
Add all relevant indices to Kibana.
-
#add_index(index_name) ⇒ Object
Add a new index to Kibana.
- #basic_auth_header ⇒ Object
-
#client ⇒ Object
Return the HTTP client with authentication.
- #create_role ⇒ Object
-
#create_space ⇒ Object
Create a new space.
- #create_user ⇒ Object
-
#delete_dataviews ⇒ Object
Delete space all index.
-
#indices ⇒ Object
Fetch all indices from Kibana.
-
#initialize(*args, **opts) ⇒ KkKibanaManageUtils
constructor
A new instance of KkKibanaManageUtils.
- #run ⇒ Object
-
#space_exists? ⇒ Boolean
Check if the space exists.
Methods inherited from Base
#human_output?, inherited, #with_error_handling
Constructor Details
#initialize(*args, **opts) ⇒ KkKibanaManageUtils
Returns a new instance of KkKibanaManageUtils.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 12 def initialize(*args, **opts) super project_name, project_env, base_url, username, password = args @base_url = base_url @username = username @password = password @project_name = project_name @project_env = project_env @space_name = "#{project_name}-#{project_env}" # Check and create space if it doesn't exist create_space unless space_exists? end |
Class Method Details
.arguments ⇒ Object
167 168 169 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 167 def self.arguments '[subcommand] [options]' end |
.author ⇒ Object
179 180 181 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 179 def self. 'kk' end |
.description ⇒ Object
183 184 185 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 183 def self.description 'Kibana automation: space, index, user, role management.' end |
.group ⇒ Object
175 176 177 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 175 def self.group 'elastic' end |
.usage ⇒ Object
171 172 173 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 171 def self.usage "kscript kibana_manage export --host=localhost --index=log-*\nkscript kibana_manage import --file=dashboard.json" end |
Instance Method Details
#add_all_index ⇒ Object
Add all relevant indices to Kibana
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 114 def add_all_index delete_dataviews indices.each do |index| unless index =~ /#{@project_name}/i && index =~ /#{@project_env}/i && index =~ /#{Time.now.strftime('%Y.%m.%d')}/ next end add_index(index.gsub(/-\d{4}\.\d{2}\.\d{2}/, '')) end end |
#add_index(index_name) ⇒ Object
Add a new index to Kibana
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 99 def add_index(index_name) uuid = SecureRandom.uuid url = "#{@base_url}/s/#{@space_name}/api/content_management/rpc/create" body = index_body(index_name, uuid) response = client.post(url, json: body, headers: kbn_headers) response = response.first if response.is_a?(Array) if response.status == 200 logger.kinfo("#{index_name} Index creation successful!") else handle_error(response, index_name) end end |
#basic_auth_header ⇒ Object
70 71 72 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 70 def basic_auth_header 'Basic ' + Base64.strict_encode64("#{@username}:#{@password}") end |
#client ⇒ Object
Return the HTTP client with authentication
66 67 68 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 66 def client @client ||= HTTPX.with(headers: { 'Authorization' => basic_auth_header }) end |
#create_role ⇒ Object
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 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 125 def create_role url = "#{@base_url}/api/security/role/#{@project_name}?createOnly=true" request_body = { 'elasticsearch' => { 'cluster' => [], 'indices' => [ { 'names' => ["*#{@project_name}*"], 'privileges' => ['read'] } ], 'run_as' => [] }, 'kibana' => [ { 'spaces' => ["#{@project_name}-prod", "#{@project_name}-uat"], 'base' => [], 'feature' => { 'discover' => ['read'] } } ] }.to_json response = client.put(url, body: request_body, headers: kbn_headers) response.first if response.is_a?(Array) logger.kinfo("Create #{@project_name} user role sucessed!") end |
#create_space ⇒ Object
Create a new space
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 44 def create_space url = "#{@base_url}/api/spaces/space" body = { id: @space_name, name: @space_name, description: "Space for #{@project_name} #{@project_env} environment", color: "##{SecureRandom.hex(3)}", # Random color initials: "#{@project_name[0]}#{@project_env[0]}".upcase } response = client.post(url, json: body, headers: kbn_headers) response = response.first if response.is_a?(Array) if response.status == 200 logger.kinfo("Space '#{@space_name}' created successfully!") else logger.kerror("Failed to create space '#{@space_name}': #{response.body}") end rescue StandardError => e logger.kerror("Error creating space: #{e.message}") end |
#create_user ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 153 def create_user url = "#{@base_url}/internal/security/users/#{@project_name}" request_body = { 'password' => '123456', 'username' => @project_name, 'full_name' => @project_name, 'email' => "#{@project_name}@devops.io", 'roles' => [@project_name] }.to_json response = client.post(url, body: request_body, headers: kbn_headers) response.first if response.is_a?(Array) logger.kinfo("Create #{@project_name} user sucessed!") end |
#delete_dataviews ⇒ Object
Delete space all index
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 82 def delete_dataviews url = "#{@base_url}/s/#{@space_name}/api/content_management/rpc/delete" get_dataviews.each do |index| body = { 'contentTypeId' => 'index-pattern', 'id' => index['id'], 'options' => { 'force' => true }, 'version' => 1 } response = client.post(url, json: body, headers: kbn_headers) response.first if response.is_a?(Array) end end |
#indices ⇒ Object
Fetch all indices from Kibana
75 76 77 78 79 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 75 def indices response = client.get("#{@base_url}/api/index_management/indices", headers: kbn_headers) response = response.first if response.is_a?(Array) handle_response(response) { |body| JSON.parse(body).map { |item| item['name'] } } end |
#run ⇒ Object
26 27 28 29 30 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 26 def run with_error_handling do logger.kinfo('Kibana utils executed.') end end |
#space_exists? ⇒ Boolean
Check if the space exists
33 34 35 36 37 38 39 40 41 |
# File 'lib/kscript/plugins/kk_kibana_manage_utils.rb', line 33 def space_exists? url = "#{@base_url}/api/spaces/space/#{@space_name}" response = client.get(url, headers: kbn_headers) response = response.first if response.is_a?(Array) response.status == 200 rescue StandardError => e logger.kerror("Error checking space existence: #{e.message}") false end |