Module | VirtP2V::UI::Network |
In: |
lib/virt-p2v/ui/network.rb
lib/virt-p2v/ui/network.rb |
DEVCOL_NAME | = | 0 | The indices of Device List colums, taken from glade | |
DEVCOL_MAC | = | 1 | ||
DEVCOL_STATUS | = | 2 | ||
DEVCOL_AVAILABLE | = | 3 | ||
UI_STATE_INVALID | = | 0 | ||
UI_STATE_VALID | = | 1 | ||
UI_STATE_ACTIVATING | = | 2 | ||
UI_STATE_COMPLETE | = | 3 | ||
EV_IP_CONFIG | = | 0 | ||
EV_SELECTION | = | 1 | ||
EV_BUTTON | = | 2 | ||
EV_ACTIVATION | = | 3 | ||
DEVCOL_NAME | = | 0 | The indices of Device List colums, taken from glade | |
DEVCOL_MAC | = | 1 | ||
DEVCOL_STATUS | = | 2 | ||
DEVCOL_AVAILABLE | = | 3 | ||
UI_STATE_INVALID | = | 0 | ||
UI_STATE_VALID | = | 1 | ||
UI_STATE_ACTIVATING | = | 2 | ||
UI_STATE_COMPLETE | = | 3 | ||
EV_IP_CONFIG | = | 0 | ||
EV_SELECTION | = | 1 | ||
EV_BUTTON | = | 2 | ||
EV_ACTIVATION | = | 3 |
# File lib/virt-p2v/ui/network.rb, line 295 295: def self.check_config_valid 296: if !@manual_mode || (!@ip_address.nil? && 297: !@ip_prefix.nil? && 298: !@ip_gateway.nil?) then 299: if @manual_mode then 300: # Check that IPv4/IPv6 is used consistently 301: if @ip_address.ipv4? then 302: event(EV_IP_CONFIG, @ip_gateway.ipv4? && @ip_prefix < 32) 303: else 304: event(EV_IP_CONFIG, @ip_gateway.ipv6? && @ip_prefix < 128) 305: end 306: else 307: event(EV_IP_CONFIG, true) 308: end 309: else 310: event(EV_IP_CONFIG, false) 311: end 312: end
# File lib/virt-p2v/ui/network.rb, line 295 295: def self.check_config_valid 296: if !@manual_mode || (!@ip_address.nil? && 297: !@ip_prefix.nil? && 298: !@ip_gateway.nil?) then 299: if @manual_mode then 300: # Check that IPv4/IPv6 is used consistently 301: if @ip_address.ipv4? then 302: event(EV_IP_CONFIG, @ip_gateway.ipv4? && @ip_prefix < 32) 303: else 304: event(EV_IP_CONFIG, @ip_gateway.ipv6? && @ip_prefix < 128) 305: end 306: else 307: event(EV_IP_CONFIG, true) 308: end 309: else 310: event(EV_IP_CONFIG, false) 311: end 312: end
# File lib/virt-p2v/ui/network.rb, line 38 38: def self.event(event, status) 39: case event 40: when EV_IP_CONFIG 41: @ip_config = status 42: when EV_SELECTION 43: @selected = status 44: when EV_BUTTON, EV_ACTIVATION 45: # Persistent state not required 46: else 47: raise "Unexpected NetworkConfig event: #{event}" 48: end 49: 50: case @state 51: when UI_STATE_INVALID 52: if @ip_config && @selected then 53: set_state(UI_STATE_VALID) 54: end 55: when UI_STATE_VALID 56: if !@ip_config || !@selected then 57: set_state(UI_STATE_INVALID) 58: elsif event == EV_BUTTON 59: set_state(UI_STATE_ACTIVATING) 60: end 61: when UI_STATE_ACTIVATING 62: # UI is disabled and we're waiting for EV_ACTIVATION, but we could 63: # also get events triggered by NetworkManager signals. 64: 65: if event == EV_ACTIVATION then 66: if status then 67: set_state(UI_STATE_COMPLETE) 68: else 69: set_state(UI_STATE_VALID) 70: end 71: elsif !@ip_config || !@selected then 72: set_state(UI_STATE_INVALID) 73: end 74: else 75: raise "Unexpected NetworkConfig UI state: #{@state}" 76: end 77: end
# File lib/virt-p2v/ui/network.rb, line 38 38: def self.event(event, status) 39: case event 40: when EV_IP_CONFIG 41: @ip_config = status 42: when EV_SELECTION 43: @selected = status 44: when EV_BUTTON, EV_ACTIVATION 45: # Persistent state not required 46: else 47: raise "Unexpected NetworkConfig event: #{event}" 48: end 49: 50: case @state 51: when UI_STATE_INVALID 52: if @ip_config && @selected then 53: set_state(UI_STATE_VALID) 54: end 55: when UI_STATE_VALID 56: if !@ip_config || !@selected then 57: set_state(UI_STATE_INVALID) 58: elsif event == EV_BUTTON 59: set_state(UI_STATE_ACTIVATING) 60: end 61: when UI_STATE_ACTIVATING 62: # UI is disabled and we're waiting for EV_ACTIVATION, but we could 63: # also get events triggered by NetworkManager signals. 64: 65: if event == EV_ACTIVATION then 66: if status then 67: set_state(UI_STATE_COMPLETE) 68: else 69: set_state(UI_STATE_VALID) 70: end 71: elsif !@ip_config || !@selected then 72: set_state(UI_STATE_INVALID) 73: end 74: else 75: raise "Unexpected NetworkConfig UI state: #{@state}" 76: end 77: end
# File lib/virt-p2v/ui/network.rb, line 79 79: def self.init(ui) 80: # Configure initial defaults 81: @manual_mode = false 82: @ip_address = nil 83: @ip_prefix = nil 84: @ip_gateway = nil 85: @ip_dns = nil 86: @state = UI_STATE_INVALID 87: @ip_config = false 88: @selected = false 89: 90: @network_button = ui.get_object('network_button') 91: @device_list_frame = ui.get_object('device_list_frame') 92: @ipv4_config_frame = ui.get_object('ipv4_config_frame') 93: @dl_selection = ui.get_object('network_device_list_view'). 94: selection 95: @device_list = ui.get_object('network_device_list') 96: @manual_ui = ui.get_object('ip_manual') 97: @ip_address_ui = ui.get_object('ip_address') 98: @ip_prefix_ui = ui.get_object('ip_prefix') 99: @ip_gateway_ui = ui.get_object('ip_gateway') 100: @ip_dns_ui = ui.get_object('ip_dns') 101: 102: ui.register_handler('network_button_clicked', 103: method(:network_button_clicked)) 104: ui.register_handler('ip_auto_toggled', 105: method(:ip_auto_toggled)) 106: ui.register_handler('ip_address_changed', 107: method(:ip_address_changed)) 108: ui.register_handler('ip_prefix_changed', 109: method(:ip_prefix_changed)) 110: ui.register_handler('ip_gateway_changed', 111: method(:ip_gateway_changed)) 112: ui.register_handler('ip_dns_changed', 113: method(:ip_dns_changed)) 114: 115: check_config_valid() 116: 117: # The user may only select a single device 118: @dl_selection.mode = Gtk::SELECTION_SINGLE 119: 120: @dl_selection.set_select_function { |selection, model, path, current| 121: iter = model.get_iter(path) 122: 123: # This is a toggle event. The new state is the opposite of the 124: # current state 125: new_state = !current 126: 127: # Don't allow the user to select an unavailable device 128: if new_state then 129: # Notify the config UI if we're selecting a device 130: if iter[DEVCOL_AVAILABLE] then 131: event(EV_SELECTION, true) 132: end 133: 134: iter[DEVCOL_AVAILABLE] 135: 136: # Always allow the user to unselect a device 137: else 138: # Notify the UI that we're unselecting the device 139: event(EV_SELECTION, false) 140: true 141: end 142: } 143: 144: # Store a map of device names to row references 145: refs = {} 146: 147: # Populate the device list with all detected network devices 148: VirtP2V::NetworkDevice.all_devices.each { |device| 149: iter = @device_list.append() 150: 151: iter[DEVCOL_NAME] = device.name 152: iter[DEVCOL_MAC] = device.mac 153: iter[DEVCOL_STATUS] = device.state 154: iter[DEVCOL_AVAILABLE] = device.connected 155: 156: # Store a stable reference to this row in the TreeModel 157: refs[device.name] = 158: Gtk::TreeRowReference.new(@device_list, iter.path) 159: } 160: 161: # Listen for updates to device states 162: VirtP2V::NetworkDevice.add_listener( lambda { |device| 163: path = refs[device.name].path 164: 165: iter = @device_list.get_iter(path) 166: iter[DEVCOL_STATUS] = device.state 167: iter[DEVCOL_AVAILABLE] = device.connected 168: 169: # Notify the UI that a device was activated 170: event(EV_ACTIVATION, device.activated) \ 171: unless device.activated.nil? 172: 173: # Unselect the path if it was previously selected and is no 174: # longer available 175: if !device.connected && @dl_selection.iter_is_selected?(iter) 176: then 177: @dl_selection.unselect_all() 178: event(EV_SELECTION, false) 179: end 180: } ) 181: 182: @ui = ui 183: end
# File lib/virt-p2v/ui/network.rb, line 79 79: def self.init(ui) 80: # Configure initial defaults 81: @manual_mode = false 82: @ip_address = nil 83: @ip_prefix = nil 84: @ip_gateway = nil 85: @ip_dns = nil 86: @state = UI_STATE_INVALID 87: @ip_config = false 88: @selected = false 89: 90: @network_button = ui.get_object('network_button') 91: @device_list_frame = ui.get_object('device_list_frame') 92: @ipv4_config_frame = ui.get_object('ipv4_config_frame') 93: @dl_selection = ui.get_object('network_device_list_view'). 94: selection 95: @device_list = ui.get_object('network_device_list') 96: @manual_ui = ui.get_object('ip_manual') 97: @ip_address_ui = ui.get_object('ip_address') 98: @ip_prefix_ui = ui.get_object('ip_prefix') 99: @ip_gateway_ui = ui.get_object('ip_gateway') 100: @ip_dns_ui = ui.get_object('ip_dns') 101: 102: ui.register_handler('network_button_clicked', 103: method(:network_button_clicked)) 104: ui.register_handler('ip_auto_toggled', 105: method(:ip_auto_toggled)) 106: ui.register_handler('ip_address_changed', 107: method(:ip_address_changed)) 108: ui.register_handler('ip_prefix_changed', 109: method(:ip_prefix_changed)) 110: ui.register_handler('ip_gateway_changed', 111: method(:ip_gateway_changed)) 112: ui.register_handler('ip_dns_changed', 113: method(:ip_dns_changed)) 114: 115: check_config_valid() 116: 117: # The user may only select a single device 118: @dl_selection.mode = Gtk::SELECTION_SINGLE 119: 120: @dl_selection.set_select_function { |selection, model, path, current| 121: iter = model.get_iter(path) 122: 123: # This is a toggle event. The new state is the opposite of the 124: # current state 125: new_state = !current 126: 127: # Don't allow the user to select an unavailable device 128: if new_state then 129: # Notify the config UI if we're selecting a device 130: if iter[DEVCOL_AVAILABLE] then 131: event(EV_SELECTION, true) 132: end 133: 134: iter[DEVCOL_AVAILABLE] 135: 136: # Always allow the user to unselect a device 137: else 138: # Notify the UI that we're unselecting the device 139: event(EV_SELECTION, false) 140: true 141: end 142: } 143: 144: # Store a map of device names to row references 145: refs = {} 146: 147: # Populate the device list with all detected network devices 148: VirtP2V::NetworkDevice.all_devices.each { |device| 149: iter = @device_list.append() 150: 151: iter[DEVCOL_NAME] = device.name 152: iter[DEVCOL_MAC] = device.mac 153: iter[DEVCOL_STATUS] = device.state 154: iter[DEVCOL_AVAILABLE] = device.connected 155: 156: # Store a stable reference to this row in the TreeModel 157: refs[device.name] = 158: Gtk::TreeRowReference.new(@device_list, iter.path) 159: } 160: 161: # Listen for updates to device states 162: VirtP2V::NetworkDevice.add_listener( lambda { |device| 163: path = refs[device.name].path 164: 165: iter = @device_list.get_iter(path) 166: iter[DEVCOL_STATUS] = device.state 167: iter[DEVCOL_AVAILABLE] = device.connected 168: 169: # Notify the UI that a device was activated 170: event(EV_ACTIVATION, device.activated) \ 171: unless device.activated.nil? 172: 173: # Unselect the path if it was previously selected and is no 174: # longer available 175: if !device.connected && @dl_selection.iter_is_selected?(iter) 176: then 177: @dl_selection.unselect_all() 178: event(EV_SELECTION, false) 179: end 180: } ) 181: 182: @ui = ui 183: end
# File lib/virt-p2v/ui/network.rb, line 237 237: def self.ip_address_changed 238: @ip_address = parse_ip(@ip_address_ui) 239: 240: check_config_valid() 241: end
# File lib/virt-p2v/ui/network.rb, line 237 237: def self.ip_address_changed 238: @ip_address = parse_ip(@ip_address_ui) 239: 240: check_config_valid() 241: end
# File lib/virt-p2v/ui/network.rb, line 230 230: def self.ip_auto_toggled 231: @manual_mode = !@manual_mode 232: @manual_ui.sensitive = @manual_mode 233: 234: check_config_valid() 235: end
# File lib/virt-p2v/ui/network.rb, line 230 230: def self.ip_auto_toggled 231: @manual_mode = !@manual_mode 232: @manual_ui.sensitive = @manual_mode 233: 234: check_config_valid() 235: end
# File lib/virt-p2v/ui/network.rb, line 281 281: def self.ip_dns_changed 282: dns = @ip_dns_ui.text 283: 284: @ip_dns = [] 285: dns.split(/\s*,+\s*/).each { |entry| 286: begin 287: @ip_dns << IPAddr.new(entry) 288: rescue ArgumentError => e 289: @ip_dns = () 290: break 291: end 292: } 293: end
# File lib/virt-p2v/ui/network.rb, line 281 281: def self.ip_dns_changed 282: dns = @ip_dns_ui.text 283: 284: @ip_dns = [] 285: dns.split(/\s*,+\s*/).each { |entry| 286: begin 287: @ip_dns << IPAddr.new(entry) 288: rescue ArgumentError => e 289: @ip_dns = () 290: break 291: end 292: } 293: end
# File lib/virt-p2v/ui/network.rb, line 261 261: def self.ip_gateway_changed 262: @ip_gateway = parse_ip(@ip_gateway_ui) 263: 264: check_config_valid() 265: end
# File lib/virt-p2v/ui/network.rb, line 261 261: def self.ip_gateway_changed 262: @ip_gateway = parse_ip(@ip_gateway_ui) 263: 264: check_config_valid() 265: end
Check IP prefix is a positive integer We check that it‘s appropriate to the address class in use elsewhere
# File lib/virt-p2v/ui/network.rb, line 245 245: def self.ip_prefix_changed 246: begin 247: @ip_prefix = Integer(@ip_prefix_ui.text) 248: rescue ArgumentError => e 249: # Ignore the result if it didn't parse 250: @ip_prefix = nil 251: return 252: end 253: 254: if @ip_prefix < 0 then 255: @ip_prefix = nil 256: end 257: 258: check_config_valid() 259: end
Check IP prefix is a positive integer We check that it‘s appropriate to the address class in use elsewhere
# File lib/virt-p2v/ui/network.rb, line 245 245: def self.ip_prefix_changed 246: begin 247: @ip_prefix = Integer(@ip_prefix_ui.text) 248: rescue ArgumentError => e 249: # Ignore the result if it didn't parse 250: @ip_prefix = nil 251: return 252: end 253: 254: if @ip_prefix < 0 then 255: @ip_prefix = nil 256: end 257: 258: check_config_valid() 259: end
# File lib/virt-p2v/ui/network.rb, line 219 219: def self.network_button_clicked 220: event(EV_BUTTON, true) 221: 222: iter = @dl_selection.selected 223: return if iter.nil? # Shouldn't be possible 224: name = iter[DEVCOL_NAME] 225: 226: VirtP2V::NetworkDevice[name].activate(!@manual_mode, @ip_address, 227: @ip_prefix, @ip_gateway, @ip_dns) 228: end
# File lib/virt-p2v/ui/network.rb, line 219 219: def self.network_button_clicked 220: event(EV_BUTTON, true) 221: 222: iter = @dl_selection.selected 223: return if iter.nil? # Shouldn't be possible 224: name = iter[DEVCOL_NAME] 225: 226: VirtP2V::NetworkDevice[name].activate(!@manual_mode, @ip_address, 227: @ip_prefix, @ip_gateway, @ip_dns) 228: end
Parse an IP address understood by IPAddr
# File lib/virt-p2v/ui/network.rb, line 268 268: def self.parse_ip(entry) 269: a = entry.text.strip 270: 271: begin 272: ip = IPAddr.new(a) 273: rescue ArgumentError => e 274: # Ignore the result if it didn't parse 275: ip = nil 276: end 277: 278: return ip 279: end
Parse an IP address understood by IPAddr
# File lib/virt-p2v/ui/network.rb, line 268 268: def self.parse_ip(entry) 269: a = entry.text.strip 270: 271: begin 272: ip = IPAddr.new(a) 273: rescue ArgumentError => e 274: # Ignore the result if it didn't parse 275: ip = nil 276: end 277: 278: return ip 279: end
# File lib/virt-p2v/ui/network.rb, line 185 185: def self.set_state(state) 186: # Don't do anything if state hasn't changed 187: return if state == @state 188: 189: case state 190: when UI_STATE_INVALID 191: @network_button.sensitive = false 192: @device_list_frame.sensitive = true 193: @ipv4_config_frame.sensitive = true 194: 195: @state = UI_STATE_INVALID 196: when UI_STATE_VALID 197: @network_button.sensitive = true 198: @device_list_frame.sensitive = true 199: @ipv4_config_frame.sensitive = true 200: 201: @state = UI_STATE_VALID 202: when UI_STATE_ACTIVATING 203: @network_button.sensitive = false 204: @device_list_frame.sensitive = false 205: @ipv4_config_frame.sensitive = false 206: 207: @state = UI_STATE_ACTIVATING 208: when UI_STATE_COMPLETE 209: # Activate the next page 210: @ui.active_page = 'server_win' 211: 212: # ... then leave this one as we hope to find it if we come back here 213: set_state(UI_STATE_VALID) 214: else 215: raise "Attempt to set unexected NetworkConfig UI state: #{@state}" 216: end 217: end
# File lib/virt-p2v/ui/network.rb, line 185 185: def self.set_state(state) 186: # Don't do anything if state hasn't changed 187: return if state == @state 188: 189: case state 190: when UI_STATE_INVALID 191: @network_button.sensitive = false 192: @device_list_frame.sensitive = true 193: @ipv4_config_frame.sensitive = true 194: 195: @state = UI_STATE_INVALID 196: when UI_STATE_VALID 197: @network_button.sensitive = true 198: @device_list_frame.sensitive = true 199: @ipv4_config_frame.sensitive = true 200: 201: @state = UI_STATE_VALID 202: when UI_STATE_ACTIVATING 203: @network_button.sensitive = false 204: @device_list_frame.sensitive = false 205: @ipv4_config_frame.sensitive = false 206: 207: @state = UI_STATE_ACTIVATING 208: when UI_STATE_COMPLETE 209: # Activate the next page 210: @ui.active_page = 'server_win' 211: 212: # ... then leave this one as we hope to find it if we come back here 213: set_state(UI_STATE_VALID) 214: else 215: raise "Attempt to set unexected NetworkConfig UI state: #{@state}" 216: end 217: end