Installer import into contrib (real import this time)
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / backend / lua / pit / 800_configure_network.lua
1 -- $Id: 800_configure_network.lua,v 1.7 2005/03/27 01:20:40 cpressey Exp $
2
3 require "gettext"
4 require "network"
5 require "configvars"
6
7 -- XXX Several of the functions in this script should be in
8 -- XXX scripts/demo/lib/network_ui.lua (or something) instead.
9
10 local execute_and_wait_for = function(ni, cmds)
11         if cmds:execute() then
12                 if App.wait_for{
13                     predicate = function()
14                         ni:probe()
15                         return ni:is_up()
16                     end,
17                     timeout = 30,
18                     frequency = 2,
19                     short_desc = _(
20                         "Waiting for interface %s to come up...",
21                         ni:get_name()
22                     )
23                 } then
24                         -- show_ifconfig(a->c, interface);
25                         App.ui:inform(_(
26                            "Interface\n\n%s\n\nis now up, with IP address %s.",
27                            ni:get_desc(), ni:get_inet_addr()
28                         ))
29                         return true
30                 else
31                         -- show_ifconfig(a->c, interface);
32                         App.ui:inform(_(
33                            "Interface\n\n%s\n\nfailed to come up.",
34                            ni:get_desc()
35                         ))
36                         return false
37                 end
38         else
39                 return false
40         end
41 end
42
43 local dhcp_configure = function(ni)
44         local cmds = CmdChain.new()
45
46         ni:cmds_dhcp_configure(cmds)
47         return execute_and_wait_for(ni, cmds)
48 end
49
50 local static_configure = function(ni)
51         local response = App.ui:present{
52             id = "assign_ip",
53             name = _("Assign IP Address"),
54             short_desc = _("Configuring Interface:"),
55             fields = {
56                 {
57                     id = "ip",
58                     name = _("IP Address"),
59                     short_desc = _("Enter the IP Address you would like to use")
60                 },
61                 {
62                     id = "netmask",
63                     name = _("Netmask"),
64                     short_desc = _("Enter the netmask of the IP address")
65                 },
66                 {
67                     id = "default_router",
68                     name = _("Default Router"),
69                     short_desc = _("Enter the IP address of the default router")
70                 },
71                 {
72                     id = "primary_dns",
73                     name = _("Primary DNS Server"),
74                     short_desc = _("Enter the IP address of primary DNS Server")
75                 },
76                 {
77                     id = "host",
78                     name = _("Hostname")
79                 },
80                 {
81                     id = "domain",
82                     name = _("Domain Name")
83                 }
84             },
85             actions = {
86                 {
87                     id = "ok",
88                     name = _("Configure Interface")
89                 },
90                 {
91                     id = "cancel",
92                     name = _("Return to Utilities Menu")
93                 }
94             },
95             datasets = {
96                 {
97                     ip = "",
98                     netmask = "",
99                     default_router = "",
100                     primary_dns = "",
101                     host = "",
102                     domain = ""
103                 }
104             }
105         }
106
107         if response.action_id == "ok" then
108                 local ip = response.datasets[1].ip
109                 local netmask = response.datasets[1].netmask
110                 local default_router = response.datasets[1].default_router
111                 local primary_dns = response.datasets[1].primary_dns
112                 local host = response.datasets[1].host
113                 local domain = response.datasets[1].domain
114         
115                 local cmds = CmdChain.new()
116                 -- XXX check ip for wellformedness first
117                 ni:cmds_assign_inet_addr(cmds, ip)
118                 ni:cmds_assign_netmask(cmds, netmask)
119
120                 cmds:add({
121                     cmdline = "${root}${ROUTE} add default ${default_router}",
122                     replacements = {
123                         default_router = default_router
124                     }
125                 })
126
127                 local success = execute_and_wait_for(ni, cmds)
128
129                 -- XXX this isn't quite right, yet...
130                 -- XXX nowhere to write these yet! :)
131                 local rc_conf = ConfigVars.new()
132
133                 rc_conf:set(
134                     string.format("ifconfig_%s", ni:get_name()),
135                     string.format("inet %s netmask %s", ip, netmask)
136                 )
137                 rc_conf:set("defaultrouter", default_router)
138
139                 rc_conf:set("hostname", string.format("%s.%s", host, domain))
140
141                 local resolv_conf = ConfigVars.new()
142
143                 resolv_conf:set("search", domain)
144                 resolv_conf:set("nameserver", primary_dns)
145         end
146 end
147
148 return {
149     name = "network",
150     title = "Configure your Network",
151     action = function(fsm)
152         local actions, ifname, ni, result
153
154         if not App.state.net_if then
155                 App.state.net_if = NetworkInterfaces.new()
156                 App.state.net_if:probe()
157         end
158
159         if App.state.net_if:ip_addr_count() > 0 then
160                 return fsm:next()
161         end
162
163         if not App.ui:confirm(_(
164             "You have not yet configured your network settings. "       ..
165             "Would you like to do so now? (Having an operational "      ..
166             "network connection will enhance the ability of "           ..
167             "subsequent tasks, such as installing."
168         )) then
169                 return fsm:next()
170         end
171
172         ni = App.state.net_if:ui_select_interface()
173         if not ni then
174                 return fsm:next()
175         end
176
177         if App.ui:confirm(_(
178             "Would you like to try dynamically configuring this interface? " ..
179             "(This requires that you have a DHCP server operating on " ..
180             "the network that this interface is attached to.)"
181         )) then
182                 dhcp_configure(ni)
183         else
184                 static_configure(ni)
185         end
186
187         return fsm:next()
188     end
189 }