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