Class: SystemService

Inherits:
String
  • Object
show all
Includes:
Indexable
Defined in:
lib/yasysdui/system_service.rb

Direct Known Subclasses

SystemServiceLegacy

Constant Summary collapse

@@cmd_info =
"sudo systemctl status"
@@descr_rgx =
/\A.+\.service\s+-\s+([[:print:]]+)\Z/
@@descr_masked_rgx =
/\A.+\.service\Z/
@@loaded_rgx =
/\ALoaded:\s+([[:graph:]]+)\s+\((.*)\)\Z/
@@active_rgx =
/\AActive:\s+([[:graph:]]+)\s+\(([[:graph:]]+)\).*\Z/
@@warning_rgx =
/\AWarning:/
@@status_read_failed_rgx =
Regexp.new('\AFailed to get properties:.*\Z')
@@status_unit_notfound_rgx =
Regexp.new('\AUnit\s[[:graph:]]+\scould\snot\sbe\sfound.\Z')

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Indexable

#each_word, #each_wordstart

Constructor Details

#initialize(str) ⇒ SystemService

default constructor, takes unit name



117
118
119
120
# File 'lib/yasysdui/system_service.rb', line 117

def initialize(str)
   super(str)
   @overrides_legacy = false
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



9
10
11
# File 'lib/yasysdui/system_service.rb', line 9

def active
  @active
end

#descrObject

Returns the value of attribute descr.



6
7
8
# File 'lib/yasysdui/system_service.rb', line 6

def descr
  @descr
end

#filename_fullObject

Returns the value of attribute filename_full.



12
13
14
# File 'lib/yasysdui/system_service.rb', line 12

def filename_full
  @filename_full
end

#loadObject

Returns the value of attribute load.



8
9
10
# File 'lib/yasysdui/system_service.rb', line 8

def load
  @load
end

#overrides_legacyObject

Returns the value of attribute overrides_legacy.



11
12
13
# File 'lib/yasysdui/system_service.rb', line 11

def overrides_legacy
  @overrides_legacy
end

#stateObject

Returns the value of attribute state.



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

def state
  @state
end

#subObject

Returns the value of attribute sub.



10
11
12
# File 'lib/yasysdui/system_service.rb', line 10

def sub
  @sub
end

Instance Method Details

#each_sentence {|self.to_str| ... } ⇒ Object

This provides the texts to index (module Indexable)

Yields:

  • (self.to_str)


27
28
29
30
# File 'lib/yasysdui/system_service.rb', line 27

def each_sentence
	yield self.to_str
	yield @descr unless  @descr.nil?
end

#handle_masked_infoObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/yasysdui/system_service.rb', line 70

def handle_masked_info
     if self.state == 'masked'
         @load = 'masked'
         @active = 'inactive'
         @sub = 'dead'
         true
     else
false
     end   
end

#native?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/yasysdui/system_service.rb', line 131

def native?
   true
end

#native_infoObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/yasysdui/system_service.rb', line 121

def native_info
# yes+   : native and overrides init script
# yes    : native and no init script
# no     : init script   
  if    self.native?
     @overrides_legacy ? "yes+" : "yes"
  else
		"no"
  end
end

#parse_status_infoObject



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

def parse_status_info
     match = nil
		return if self.handle_masked_info
     info_tab = @status_info_raw.lines.reject{|l| @@warning_rgx.match(l) }
     info_tab.map!{|r| r.strip}
     if @@status_read_failed_rgx.match(info_tab[0])
         return nil
     end    
    if @@status_unit_notfound_rgx.match(info_tab[0])
         return nil
     end                
     if match = @@descr_rgx.match(info_tab[0])
         self.descr = match[1]
     else
# masked script/service without description returned     
         if match = @@descr_masked_rgx.match(info_tab[0])
         else
             puts info_tab[0]
             raise "Failed parsing line printed above"
         end

     end 
# complete data
     if match
         match = @@loaded_rgx.match(info_tab[1])
         if match 
             self.load = match[1]  
             load_info = match[2].split(';')
             self.filename_full = load_info[0]
             self.state = load_info[1]
         end    
         match = @@active_rgx.match(info_tab[2].strip)
         if match
             self.active = match[1]   
             self.sub = match[2]
         end
     end

end

#read_stateObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/yasysdui/system_service.rb', line 92

def read_state
     return nil if self.end_with?("@")
     match = nil
		stdout_str, status = Open3.capture2("#@@cmd_info #{self.to_s}" )
		if stdout_str.lines.size > 0

         info_tab = stdout_str.lines.reject{|l| @@warning_rgx.match(l) }
         if @@status_read_failed_rgx.match(info_tab[0].strip)
             return nil
         end            
         match = @@loaded_rgx.match(info_tab[1].strip)
         if match 
             load_info = match[2].split(';')
             self.filename_full = load_info[0]
             self.state = load_info[1]
         end    
             

     else
          puts "Failed getting service state info for"
          pp self
           
     end
end

#read_status_infoObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/yasysdui/system_service.rb', line 80

def read_status_info
     return nil if self.end_with?("@")
     return if self.handle_masked_info
		@status_info_raw, status = Open3.capture2("#@@cmd_info #{self.to_s}" )
		if @status_info_raw.lines.size > 0
         self.parse_status_info
     else
# TODO: a more decent error handling...        
          puts "Failed getting service status info for"
          pp self
     end
end

#set_status_info_from_output(action_output) ⇒ Object



22
23
24
25
# File 'lib/yasysdui/system_service.rb', line 22

def set_status_info_from_output( action_output )
     @status_info_raw = action_output
     self.parse_status_info
end