modules/machine/machine.rb in nuri-0.5.3 vs modules/machine/machine.rb in nuri-0.5.4

- old
+ new

@@ -2,11 +2,13 @@ include Sfp::Resource def update_state to_model - load_kernel_modules(['acpiphp']) + if platform.include?('linux') + load_kernel_modules(['acpiphp']) + end @state['sfpAddress'] = @model['sfpAddress'] @state['sfpPort'] = @model['sfpPort'] @state['created'] = true @@ -18,19 +20,27 @@ end @state["cpus"] = (File.exist?('/proc/cpuinfo') ? `cat /proc/cpuinfo | grep processor | wc -l`.strip.to_i : -1) @state['memory'] = (`which free`.strip != '' ? `free`.split("\n")[1].split(" ")[1] : -1) - @state['disk'] = get_disk_state + if platform.include?('linux') + @state['disks'] = get_disks_state + else + @state['disks'] = {} + end end ############################## # # Helper methods # ############################## + def platform + RUBY_PLATFORM.downcase + end + protected def load_kernel_modules(modules=[]) loaded = [] `lsmod`.each_line do |line| @@ -46,16 +56,16 @@ end end # generate the disks' state, try to automatically mount the disk to target directory # - def get_disk_state + def get_disks_state def generate_state - disk = {} + disks = {} # get disks UUID uuids = {} - `blkid`.each_line do |line| + `/sbin/blkid`.each_line do |line| line.strip! next if line.length <= 0 device, info = line.split(':', 2) info = info.split(' ') uuids[device] = info[0].split('=', 2)[1].gsub(/"/, '') @@ -67,35 +77,35 @@ next if line.length <= 0 data = line.split(' ') if data[0][0..4] == '/dev/' name = 'root' if data[5] != '/' - model = (@model['disk'].is_a?(Hash) ? @model['disk'].select { |k,v| v['mount'] == data[5] if k[0] != '_' } : {}) + model = (@model['disks'].is_a?(Hash) ? @model['disks'].select { |k,v| v['mount'] == data[5] if k[0] != '_' } : {}) name = (model.length > 0 ? model.keys.first : "uuid_#{uuids[data[0]]}") end - disk[name] = { + disks[name] = { 'size' => (data[1].to_f / 1000.0).to_i, 'mount' => data[5], 'uuid' => uuids[data[0]] } end end - disk + disks end - disk = generate_state + disks = generate_state - if @model['disk'].is_a?(Hash) - names = @model['disk'].keys.sort { |x,y| x <=> y } + if @model['disks'].is_a?(Hash) + names = @model['disks'].keys.sort { |x,y| x <=> y } device = "/dev/vdb" # format unformatted disks, mount unmount disks names.each { |name| - next if name[0] == '_' or disk.has_key?(name) - spec = @model['disk'][name] + next if name[0] == '_' or disks.has_key?(name) + spec = @model['disks'][name] status = `file -s #{device}` if not (status =~ /ERROR/) target = spec['mount'].to_s.strip - # format the disk if not yet formatted + # format the disks if not yet formatted system "mkfs.ext4 #{device}" if not (status =~ /.+ filesystem data/) # create target directory if not exist system "mkdir -p #{target}" if !File.exist? target # add fstab record system "sed -i '/^#{device}/d' /etc/fstab"