miq_utilities 0.2.7 → 0.2.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/miq_utilities/chef.rb +7 -1
- data/lib/miq_utilities/general.rb +2 -3
- data/lib/miq_utilities/infoblox.rb +6 -32
- data/lib/miq_utilities/logging.rb +1 -1
- data/lib/miq_utilities/notification.rb +45 -73
- data/lib/miq_utilities/sql.rb +1 -11
- data/lib/miq_utilities/ssh.rb +12 -13
- data/lib/miq_utilities/version.rb +1 -1
- data/lib/miq_utilities/vmware.rb +4 -14
- data/lib/miq_utilities/winrm.rb +2 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f69b2aecab6ae7d25b391d9244cd67fd62612b7be714dfe27c2e20358c50f483
|
4
|
+
data.tar.gz: ff8ce2ff4a17f63855f97c6d9571ff0b21bf7f87a298294df866d82237dd2df0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86eeb522972b75989b5a3a3ea5bb17335901aa9ef29c4335e91b96b6645e49fc953aacbdc5604e4dc801cff5fcbf2a2cf7b30f0e18c3a6dbeab1d012fd38828f
|
7
|
+
data.tar.gz: 0cbb9dfacd7b597e3b407aa1f38fcc7de44a05328fb5f0212fa49d7083829c820c42bbb841466da9e9bd3d10245882a9b6b845a2464a37ada428858d76fb78cf
|
data/lib/miq_utilities/chef.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# .SYNOPSIS
|
4
|
+
# This Class is used as a wrapper class for the chef interactions
|
5
|
+
#
|
6
|
+
# Methods Summary:
|
7
|
+
# > Name > Description/Params
|
3
8
|
#
|
4
|
-
# This Class is used wrap the chef methods used
|
5
9
|
#
|
6
10
|
|
7
11
|
require_relative 'logging.rb'
|
@@ -33,6 +37,8 @@ class ChefClass
|
|
33
37
|
rescue => timeout
|
34
38
|
@logger.log(level: 'error', message: "Error executing chef: #{timeout.class} #{timeout} #{timeout.backtrace.join("\n")}")
|
35
39
|
return false
|
40
|
+
rescue => err
|
41
|
+
raise("\n Class:<#{self.class}>\n Method:<#{__method__}>\n Error:<#{err} - #{err.backtrace.join("\n")}>")
|
36
42
|
end
|
37
43
|
end
|
38
44
|
|
@@ -1,12 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
3
|
+
# .SYNOPSIS
|
4
4
|
# This Class is used to hold general methods used within this GEM
|
5
5
|
#
|
6
6
|
# Methods Summary:
|
7
|
-
# > Name
|
7
|
+
# > Name > Description/Params
|
8
8
|
#
|
9
|
-
|
10
9
|
require_relative 'logging.rb'
|
11
10
|
|
12
11
|
# Wrapper class to interact with linux machines via ssh
|
@@ -4,37 +4,11 @@
|
|
4
4
|
# This Class utilises the rest-client (version => 1.8.0 x64-mingw32) & json (version => 2.0.4) Ruby Gems
|
5
5
|
#
|
6
6
|
# Methods Summary:
|
7
|
-
# > Name
|
8
|
-
# process_args
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
# process_extattrs - Process extra attributes
|
13
|
-
# inargextattrs - Hash : Hash table of key value pairs of the extra attributes to process i.e. { 'Country' => 'UK' }
|
14
|
-
# inurl - String : The url to append to
|
15
|
-
# args - Boolean : Identifies if arguments were processed, if so an ampersand needs to be appended
|
16
|
-
#
|
17
|
-
# process_returnfields - Process return fields
|
18
|
-
# inurl - String : The url to append to
|
19
|
-
# returnfields - String : A string listing the return fields, delimited by commas (CSV)
|
20
|
-
# args - Boolean (Default:Flase): Identifies if arguments were processed, if so an ampersand needs to be appended
|
21
|
-
#
|
22
|
-
# call_infoblox - Process the inputs and make the API call to infoblox
|
23
|
-
# action - String : The Action being preformed i.e. GET, DELETE
|
24
|
-
# resource - String : The resource being used i.e. record:host
|
25
|
-
# ibobj - Object : Custom object created to hold the extra attributes, arguments and return fields.
|
26
|
-
# Examples: * vars are suffixed with @ *
|
27
|
-
# infobloxobj = { 'args' => nil, 'returnfields' => nil, 'argextattrs' => { 'Country' => @ib_cmdb_country, '*VLAN Number' => @ib_cmdb_vlan, '*Site' => @ib_site } } or;
|
28
|
-
# infobloxobj = { 'args' => { 'name' => @ib_fdqn }, 'returnfields' => nil, 'argextattrs' => nil } or;
|
29
|
-
# infobloxobj = { 'args' => { 'name' => @ib_fdqn }, 'returnfields' => 'ipv4addrs', 'argextattrs' => nil }......
|
30
|
-
#
|
31
|
-
# body_type - String (Default json) : The body type
|
32
|
-
# body - String : The body to use in the call, if there is one
|
33
|
-
# servername - String : The server name to connect to
|
34
|
-
# username - String : The username to connect with
|
35
|
-
# password - String : The password for the user
|
36
|
-
# baseurl - String (Default: https://[SERVERNAME]/wapi/v2.3.1/): the base URL to use
|
37
|
-
#
|
7
|
+
# > Name > Description/Params
|
8
|
+
# process_args - Process Arguments
|
9
|
+
# process_extattrs - Process extra attributes
|
10
|
+
# process_returnfields - Process return fields
|
11
|
+
# call_infoblox - Process the inputs and make the API call to infoblox
|
38
12
|
#
|
39
13
|
require 'rest-client'
|
40
14
|
require 'json'
|
@@ -78,7 +52,7 @@ class InfobloxClass
|
|
78
52
|
end
|
79
53
|
|
80
54
|
def call_infoblox(action:, resource:, ibobj:, body_type: :json, body: nil, servername: nil, username: nil, password: nil, baseurl: 'https://[SERVERNAME]/wapi/v2.3.1/')
|
81
|
-
@logger = LoggingClass.new(
|
55
|
+
@logger = LoggingClass.new("#{self.class}:(#{__method__})")
|
82
56
|
|
83
57
|
servername ||= $evm.object['InfobloxServername']
|
84
58
|
username ||= $evm.object['InfobloxUsername']
|
@@ -39,6 +39,6 @@ class LoggingClass
|
|
39
39
|
# Notify - Only if flag (notify) is set to TRUE
|
40
40
|
@notification.notify(event_level: level, event_message: message, email_to: email_to, email_from: email_from, signature: signature, webhookurl: webhookurl, subjecttype: subjecttype) if notify
|
41
41
|
rescue => err
|
42
|
-
raise("Oopps! Something went wrong.\
|
42
|
+
raise("Oopps! Something went wrong.\nDetails:<#{err}>")
|
43
43
|
end
|
44
44
|
end
|
@@ -4,100 +4,72 @@
|
|
4
4
|
# This Class utilises the slack-notifier Ruby Gem (version => 2.3.2).
|
5
5
|
#
|
6
6
|
# Methods Summary:
|
7
|
-
#
|
7
|
+
# > Name > Description/Params
|
8
8
|
# notify - based on the params passed, process through and notify in the appropriate ways
|
9
|
-
# event_level - String : info, warn, error, debug, note
|
10
|
-
# event_message - String : The message to be used
|
11
|
-
# email_to - String : Who to send an email to
|
12
|
-
# email_from - String : Sent from which email address
|
13
|
-
# signature - String : Who the emails from
|
14
|
-
# webhookurl - String : The slcak channel to send the message to
|
15
|
-
#
|
16
9
|
# send_email - Sends an email to the specified user
|
17
|
-
# message - String : The message to be used
|
18
|
-
# subject - String : The Subject to be used
|
19
|
-
# from_email - String : Sender email
|
20
|
-
# signature - String : Who the emails from
|
21
|
-
# to_email - String : Recipient email
|
22
|
-
#
|
23
10
|
# send_slack_message - Sends a Slcak message
|
24
|
-
# message - String : The message to be used
|
25
|
-
# webhookurl - String : The webhook to be used
|
26
|
-
#
|
27
11
|
# on_screen_message - Sends an on-screen notification
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
# WIP
|
12
|
+
#
|
13
|
+
#
|
14
|
+
#
|
32
15
|
require 'slack-notifier'
|
33
16
|
|
34
17
|
# Wrapper class to notify stakeholders
|
35
18
|
class NotificationClass
|
36
19
|
def notify(event_level:, event_message:, email_to: nil, email_from: nil, signature: nil, webhookurl: nil, subjecttype: nil)
|
37
|
-
@logger = LoggingClass.new(
|
20
|
+
@logger = LoggingClass.new(self.class.to_s)
|
38
21
|
signature ||= 'ManageIQ'
|
39
|
-
@logger.log(level: 'info', message: 'Notify Class started.....')
|
40
|
-
@logger.log(level: 'info', message: "case = #{event_level.upcase}")
|
41
22
|
case event_level.upcase
|
42
23
|
when 'WARN', 'WARNING'
|
43
|
-
@logger.log(level: 'info', message: 'Hit Case WARNING')
|
44
24
|
subject = 'ManageIQ Warning'
|
45
|
-
send_email(message: event_message, subject: subject, from_email: email_from, signature: signature, to_email: email_to)
|
46
25
|
send_slack_message(message: event_message, webhookurl: webhookurl)
|
26
|
+
if $evm.nil?
|
27
|
+
@logger.log(level: 'warn', message: 'The $evm object is null. Unable to send email notification.')
|
28
|
+
else
|
29
|
+
send_email(message: event_message, subject: subject, from_email: email_from, signature: signature, to_email: email_to)
|
30
|
+
end
|
47
31
|
when 'ERR', 'ERROR'
|
48
|
-
@logger.log(level: 'info', message: 'Hit Case ERROR')
|
49
32
|
subject = 'ManageIQ Error'
|
50
|
-
send_email(message: event_message, subject: subject, from_email: email_from, signature: signature, to_email: email_to)
|
51
33
|
send_slack_message(message: event_message, webhookurl: webhookurl)
|
52
|
-
|
34
|
+
if $evm.nil?
|
35
|
+
@logger.log(level: 'warn', message: 'The $evm object is null. Unable to send email or on-screen notifications.')
|
36
|
+
else
|
37
|
+
on_screen_message(level: event_level, message: event_message, type: subjecttype)
|
38
|
+
send_email(message: event_message, subject: subject, from_email: email_from, signature: signature, to_email: email_to)
|
39
|
+
end
|
53
40
|
when 'NOTE'
|
54
|
-
|
55
|
-
|
41
|
+
if $evm.nil?
|
42
|
+
@logger.log(level: 'warn', message: 'The $evm object is null. Unable to on-screen notification.')
|
43
|
+
else
|
44
|
+
on_screen_message(level: event_level, message: event_message, type: subjecttype)
|
45
|
+
end
|
56
46
|
end
|
57
47
|
rescue => err
|
58
|
-
raise("
|
48
|
+
raise("<#{err}>")
|
59
49
|
end
|
60
50
|
|
61
51
|
def send_email(message:, subject:, from_email:, signature:, to_email: nil)
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
if
|
74
|
-
|
75
|
-
|
76
|
-
|
52
|
+
to = nil
|
53
|
+
if to_email.nil?
|
54
|
+
vm = nil
|
55
|
+
owner = nil
|
56
|
+
# Look in the Root Object for a Provision/Request
|
57
|
+
prov = $evm.root['miq_provision_request'] || $evm.root['miq_provision']
|
58
|
+
vm = prov.vm if prov && vm.nil?
|
59
|
+
# Get VM Owner ID and then Email
|
60
|
+
evm_owner_id = vm.attributes['evm_owner_id']
|
61
|
+
owner = $evm.vmdb('user', evm_owner_id) unless evm_owner_id.nil?
|
62
|
+
# to_email_address from owner.email then from model if nil
|
63
|
+
to = if owner
|
64
|
+
owner.email.to_s.strip
|
65
|
+
else
|
66
|
+
$evm.object['to_email_address'].to_s.strip unless $evm.nil?
|
67
|
+
end
|
68
|
+
# Look at the Event Type in the Current Object or in the Root Object
|
69
|
+
event_type = $evm.object['event'] || $evm.root['event_type']
|
77
70
|
end
|
78
|
-
|
79
|
-
|
80
|
-
prov = $evm.root['miq_provision_request'] || $evm.root['miq_provision']
|
81
|
-
vm = prov.vm if prov && vm.nil?
|
82
|
-
|
83
|
-
raise 'VM details not found!' if vm.nil?
|
84
|
-
|
85
|
-
# Look at the Event Type in the Current Object or in the Root Object
|
86
|
-
event_type = $evm.object['event'] || $evm.root['event_type']
|
87
|
-
|
88
|
-
# Get VM Owner Name and Email
|
89
|
-
evm_owner_id = vm.attributes['evm_owner_id']
|
90
|
-
owner = nil
|
91
|
-
owner = $evm.vmdb('user', evm_owner_id) unless evm_owner_id.nil?
|
92
|
-
|
93
|
-
# to_email_address from owner.email then from model if nil
|
94
|
-
to = if owner
|
95
|
-
owner.email.to_s.strip
|
96
|
-
else
|
97
|
-
$evm.object['to_email_address'].to_s.strip
|
98
|
-
end
|
99
|
-
|
100
|
-
to ||= to_email unless to_email.nil?
|
71
|
+
event_type ||= 'MIQ Event'
|
72
|
+
to ||= to_email
|
101
73
|
|
102
74
|
subject += " - #{event_type}"
|
103
75
|
body = 'Hello, '
|
@@ -107,14 +79,14 @@ class NotificationClass
|
|
107
79
|
|
108
80
|
$evm.execute(:send_email, to, from_email, subject, body)
|
109
81
|
rescue => err
|
110
|
-
raise("
|
82
|
+
raise("\n Class:<#{self.class}>\n Method:<#{__method__}>\n Error:<#{err} - #{err.backtrace.join("\n")}>")
|
111
83
|
end
|
112
84
|
|
113
85
|
def send_slack_message(message:, webhookurl:)
|
114
86
|
notifier = Slack::Notifier.new webhookurl
|
115
87
|
notifier.ping message
|
116
88
|
rescue => err
|
117
|
-
raise("
|
89
|
+
raise("\n Class:<#{self.class}>\n Method:<#{__method__}>\n Error:<#{err} - #{err.backtrace.join("\n")}>")
|
118
90
|
end
|
119
91
|
|
120
92
|
def on_screen_message(level:, message:, subject: nil, type: nil)
|
@@ -136,7 +108,7 @@ class NotificationClass
|
|
136
108
|
$evm.create_notification(type: type, subject: subject)
|
137
109
|
end
|
138
110
|
rescue => err
|
139
|
-
raise("
|
111
|
+
raise("\n Class:<#{self.class}>\n Method:<#{__method__}>\n Error:<#{err} - #{err.backtrace.join("\n")}>")
|
140
112
|
end
|
141
113
|
end
|
142
114
|
end
|
data/lib/miq_utilities/sql.rb
CHANGED
@@ -6,18 +6,8 @@
|
|
6
6
|
# Methods Summary:
|
7
7
|
# > Name > Description/Params
|
8
8
|
# initialize - Create the connection to the client
|
9
|
-
# user - String : Username
|
10
|
-
# password - String : Password
|
11
|
-
# host - String : The host name to connect to
|
12
|
-
# database - String : The database name to connect to
|
13
|
-
# azure - Boolean : Set to true when connecting to Azure
|
14
|
-
# port - String : The port to use, default (nil) 1433
|
15
|
-
#
|
16
9
|
# get_client - returns the client to utilise the tiny_tds functionality
|
17
|
-
#
|
18
10
|
# run_sql_query - runs the specified sql query against the client when the class was instantiated
|
19
|
-
# sql - String : Sql query to be run
|
20
|
-
#
|
21
11
|
# close_connection - closes the connection to the client
|
22
12
|
#
|
23
13
|
require 'tiny_tds'
|
@@ -28,7 +18,7 @@ class SqlClass
|
|
28
18
|
# Initialize Class
|
29
19
|
def initialize(user:, password:, host:, database:, azure:, port: nil)
|
30
20
|
# Logging
|
31
|
-
@logger = LoggingClass.new(
|
21
|
+
@logger = LoggingClass.new(self.class.to_s)
|
32
22
|
|
33
23
|
# Default port
|
34
24
|
unless port.nil?
|
data/lib/miq_utilities/ssh.rb
CHANGED
@@ -1,15 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
#
|
4
|
-
# This Class is used to handle the
|
4
|
+
# This Class is used to handle the running ssh scripts and utilises the net-ssh Ruby Gem (version => 4.2.0)
|
5
5
|
#
|
6
6
|
# Methods Summary:
|
7
7
|
# > Name > Description/Params
|
8
8
|
# ssh_run - Run script via ssh
|
9
|
-
# script - String : The script to run
|
10
|
-
# ipaddress - String : The message
|
11
|
-
# linux_user - String : User to login as
|
12
|
-
# linux_password - String : password to use
|
13
9
|
#
|
14
10
|
|
15
11
|
require 'net/ssh'
|
@@ -18,16 +14,19 @@ require_relative 'logging.rb'
|
|
18
14
|
# This wrapper class is used to run ssh commands on linux machines
|
19
15
|
class SshClass
|
20
16
|
def ssh_run(ipaddr, script, linux_user = nil, linux_password = nil)
|
21
|
-
@logger = LoggingClass.new(
|
17
|
+
@logger = LoggingClass.new("#{self.class}:(#{__method__})")
|
22
18
|
linux_password ||= $evm.object.decrypt('linux_password').to_s
|
23
19
|
linux_user ||= 'root'
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
20
|
+
begin
|
21
|
+
Net::SSH.start(ipaddr, linux_user, password: linux_password, paranoid: Net::SSH::Verifiers::Null.new) do |ssh|
|
22
|
+
command = script
|
23
|
+
output = ssh.exec!(command)
|
24
|
+
@logger.log(level: 'info', message: "ssh command being run on #{ipaddr}")
|
25
|
+
@logger.log(level: 'info', message: "ssh script being run is #{script}")
|
26
|
+
@logger.log(level: 'info', message: "SSH output = #{output}")
|
27
|
+
end
|
28
|
+
rescue
|
29
|
+
raise("\n Class:<#{self.class}>\n Method:<#{__method__}>\n Error:<#{err} - #{err.backtrace.join("\n")}>")
|
31
30
|
end
|
32
31
|
end
|
33
32
|
end
|
data/lib/miq_utilities/vmware.rb
CHANGED
@@ -4,23 +4,13 @@
|
|
4
4
|
# This Class is used to handle some of the heavily used VMWare utilities
|
5
5
|
#
|
6
6
|
# Methods Summary:
|
7
|
-
# > Name
|
8
|
-
# initialize
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# get_vsphere_connection - returns the client connection
|
12
|
-
# user - String : The user to connect with
|
13
|
-
# password - String : The users password
|
14
|
-
#
|
7
|
+
# > Name > Description/Params
|
8
|
+
# initialize - Assign the host name
|
9
|
+
# get_vsphere_connection - returns the client connection
|
15
10
|
# retrieve_data_center - returns the datacenter based on the connection and datacenter
|
16
|
-
#
|
17
|
-
#
|
18
11
|
# find_vm - Get the VM details from vmware
|
19
|
-
# datacenter - String : retrieve_data_center return value
|
20
|
-
# vmname - String : The vmname
|
21
|
-
#
|
22
12
|
# upg_tools - Get the VM details from vmware
|
23
|
-
#
|
13
|
+
#
|
24
14
|
require 'rbvmomi'
|
25
15
|
|
26
16
|
# Wrapper class to interact with vmware
|
data/lib/miq_utilities/winrm.rb
CHANGED
@@ -4,12 +4,8 @@
|
|
4
4
|
# This Class utilises the winrm Ruby Gem (version => 2.2.3) and is used to handle the running of powershell scripts using winrm
|
5
5
|
#
|
6
6
|
# Methods Summary:
|
7
|
-
# > Name
|
8
|
-
# winrm_run
|
9
|
-
# ps_script - String : The name of the script to run (including path to the file)
|
10
|
-
# host - String : The name of the host to connect to
|
11
|
-
# user - String : The user to connect with
|
12
|
-
# password - String : The users password
|
7
|
+
# > Name > Description/Params
|
8
|
+
# winrm_run - Run a specified PowerShell script
|
13
9
|
#
|
14
10
|
require 'winrm'
|
15
11
|
require 'logging.rb'
|