Class: ServiceCatalog

Inherits:
Hash
  • Object
show all
Includes:
Singleton
Defined in:
lib/yasysdui/service_catalog.rb

Constant Summary collapse

@@cmd_know_units =
"sudo systemctl list-units -a -l -t service --no-pager --no-legend"
@@initd =
"/etc/init.d"
@@initd_ignore =
/README|.+~/
@@rgx_ku =

UNIT LOAD ACTIVE SUB DESCRIPTION

Regexp.new('\A([[:graph:]]+)\.service\s+' + 
'([[:graph:]]+)\s+'+
'([[:alnum:]]+)\s+'+
'([[:alnum:]]+)\s+'+
'([[:print:]]+)\Z')
@@cmd_list_unit_files =
"sudo systemctl list-unit-files -a -l -t service --no-pager --no-legend"
@@rgx_li =

UNIT STATE

Regexp.new('\A([[:graph:]]+)\.service\s+' + 
'([[:graph:]]+)\Z')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServiceCatalog

Returns a new instance of ServiceCatalog.



8
9
10
11
12
13
14
15
# File 'lib/yasysdui/service_catalog.rb', line 8

def initialize
    super
    @idx = SimpleIndex.new
    @config = YSConfig.instance
    @config.load 
    self.get_requested_services
    self.build_index
end

Instance Attribute Details

#idxObject

Returns the value of attribute idx.



7
8
9
# File 'lib/yasysdui/service_catalog.rb', line 7

def idx
  @idx
end

Instance Method Details

#add_init_d_scriptsObject

get all legacy init.d scripts



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/yasysdui/service_catalog.rb', line 95

def add_init_d_scripts
    flist = Dir.glob("#@@initd/*").reject{|f| @@initd_ignore.match(f) }.map{|f| File.basename(f)}
    flist.each{|f| 
# only try to add an init script if it does not exist as an native systemd unit,
# as these have precedence
# these assumes we have to read native service before
        if self.has_key?(f)
            srv = self[f]
            srv.overrides_legacy = true
            srv.read_state
        else
            srv = SystemServiceLegacy.new( f )
            self[srv] = srv if srv
        end
    }
end

#add_units_from_file_listObject

add all know systemd units files from systemctl list-unit-files



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
# File 'lib/yasysdui/service_catalog.rb', line 67

def add_units_from_file_list
 
    match = nil; srv = nil
		stdout_str, status = Open3.capture2( @@cmd_list_unit_files )
		if status.success?
			stdout_str.each_line{|l|
match = @@rgx_li.match(l.strip)
if match
                f = match[1]
                unless f.end_with?('@')    # ignore template units for now...
		unless self.has_key?(f)
			srv = SystemService.new( f )
			srv.state = match[2]
			srv.read_status_info 
			self[srv] = srv if srv
		else
			self[f].state = match[2]
		end
                end
            else
	puts l
	raise "Failed parsing line printed above"
end
			}
			
		end
end

#build_indexObject



21
22
23
24
25
26
27
28
# File 'lib/yasysdui/service_catalog.rb', line 21

def build_index
		print "Building fast search index... "
		self.each_value{|srv|
			@idx.compile(srv)
		}
		print "done\n"
		
end

#get_all_known_unitsObject

get all know systemd native units from systemctl list-units



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/yasysdui/service_catalog.rb', line 44

def get_all_known_units
 
    match = nil; srv = nil
		stdout_str, status = Open3.capture2( @@cmd_know_units )
		if status.success?
			stdout_str.each_line{|l|
match = @@rgx_ku.match(l.strip)
if match
                srv = SystemService.new(match[1] )
                srv.load = match[2]
                srv.active = match[3]
                srv.sub = match[4]
                srv.descr = match[5]
	self[srv] = srv
else
	puts l
	raise "Failed parsing line printed above"
end
			}
			
		end
end

#get_managed_servicesObject

get all maanged services as sepcified in config file



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/yasysdui/service_catalog.rb', line 112

def get_managed_services
    @config.managed_services.each{|f| 
# only try to add an init script if it does not exist already in the catlog
        unless self.has_key?(f)
            srv = SystemService.new( f )
            srv.read_status_info 
            srv.read_state
 			self[srv] = srv if srv
        end
    }
end

#get_requested_servicesObject

the anaged services only (as specified in the config file)



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/yasysdui/service_catalog.rb', line 126

def get_requested_services
	if @config.managed_services
		pp :getting_managed_services
		get_managed_services
	else
		pp :getting_all_services
		get_all_known_units; pp :got_all_known_units
		add_units_from_file_list; pp :got_all_units_from_file
		add_init_d_scripts; pp :got_all_init_scripts
		puts "Built service catalog with #{self.size} item(s)" 
	end
	self
end

#rebuildObject



16
17
18
19
20
# File 'lib/yasysdui/service_catalog.rb', line 16

def rebuild
  clear
  self.get_requested_services
  self.build_index
end