Class: Warg::HostCollection
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #add(host_data) ⇒ Object
- #create_file_from(content, path:, mode: 0644) ⇒ Object
- #download(path) ⇒ Object
- #each ⇒ Object
-
#initialize ⇒ HostCollection
constructor
A new instance of HostCollection.
- #length ⇒ Object
- #one ⇒ Object
- #run(order:, &block) ⇒ Object
- #run_command(command, order: :parallel, &setup) ⇒ Object
- #run_script(script, order: :parallel, &setup) ⇒ Object
- #to_a ⇒ Object
- #upload(file, to:) ⇒ Object
- #with(**filters) ⇒ Object
Constructor Details
#initialize ⇒ HostCollection
Returns a new instance of HostCollection.
1126 1127 1128 |
# File 'lib/warg.rb', line 1126 def initialize @hosts = [] end |
Class Method Details
.from(value) ⇒ Object
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 |
# File 'lib/warg.rb', line 1085 def self.from(value) case value when String, Host new.add(value) when HostCollection value when Array is_array_host_specification = value.any? do |item| # Check key=value items by looking for `=` missing `?`. If it has `?`, then we # assume it is in the form `host?key=value` String === item and item.index("=") and not item.index("?") end if is_array_host_specification new.add(value) else value.inject(new) { |collection, host_data| collection.add(host_data) } end when Hash value.inject(new) do |collection, (property, hosts_data)| name, value = property.to_s.split(":", 2) if value.nil? value = name name = "stage" end from(hosts_data).each do |host| host[name] = value collection.add(host) end collection end when nil new else raise InvalidHostDataError.new(value) end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
1148 1149 1150 1151 1152 1153 1154 |
# File 'lib/warg.rb', line 1148 def ==(other) self.class == other.class && length == other.length && # both are the same length and their intersection is the same length (all the same # elements in common) length == @hosts.&(other.hosts).length end |
#add(host_data) ⇒ Object
1138 1139 1140 1141 1142 |
# File 'lib/warg.rb', line 1138 def add(host_data) @hosts << Host.from(host_data) self end |
#create_file_from(content, path:, mode: 0644) ⇒ Object
1162 1163 1164 1165 1166 |
# File 'lib/warg.rb', line 1162 def create_file_from(content, path:, mode: 0644) each do |host| host.create_file_from(content, path: path, mode: mode) end end |
#download(path) ⇒ Object
1174 1175 1176 1177 1178 |
# File 'lib/warg.rb', line 1174 def download(path) map do |host| host.download(path) end end |
#each ⇒ Object
1180 1181 1182 1183 1184 1185 1186 1187 1188 |
# File 'lib/warg.rb', line 1180 def each if block_given? @hosts.each { |host| yield host } else enum_for(:each) end self end |
#length ⇒ Object
1158 1159 1160 |
# File 'lib/warg.rb', line 1158 def length @hosts.length end |
#one ⇒ Object
1130 1131 1132 1133 1134 1135 1136 |
# File 'lib/warg.rb', line 1130 def one if @hosts.empty? raise "cannot pick a host from `#{inspect}'; collection is empty" end HostCollection.from @hosts.sample end |
#run(order:, &block) ⇒ Object
1202 1203 1204 1205 1206 |
# File 'lib/warg.rb', line 1202 def run(order:, &block) strategy = Executor.for(order) executor = strategy.new(self) executor.run(&block) end |
#run_command(command, order: :parallel, &setup) ⇒ Object
1196 1197 1198 1199 1200 |
# File 'lib/warg.rb', line 1196 def run_command(command, order: :parallel, &setup) run(order: order) do |host, result| result.update host.run_command(command, &setup) end end |
#run_script(script, order: :parallel, &setup) ⇒ Object
1190 1191 1192 1193 1194 |
# File 'lib/warg.rb', line 1190 def run_script(script, order: :parallel, &setup) run(order: order) do |host, result| result.update host.run_script(script, &setup) end end |
#to_a ⇒ Object
1208 1209 1210 |
# File 'lib/warg.rb', line 1208 def to_a @hosts.dup end |
#upload(file, to:) ⇒ Object
1168 1169 1170 1171 1172 |
# File 'lib/warg.rb', line 1168 def upload(file, to:) each do |host| host.upload(file, to: to) end end |
#with(**filters) ⇒ Object
1144 1145 1146 |
# File 'lib/warg.rb', line 1144 def with(**filters) HostCollection.from(select { |host| host.matches?(**filters) }) end |