6 require_once 'plc_login.php';
8 // Get session and API handles
9 require_once 'plc_session.php';
13 require_once 'plc_functions.php';
14 require_once 'details.php';
15 require_once 'form.php';
17 // if not a admin, pi, or tech then redirect to node index
18 // xxx does not take site into account
19 $has_privileges=plc_is_admin() || plc_is_pi() || plc_is_tech();
20 if( ! $has_privileges) {
21 drupal_set_error ("Insufficient provileges to add a node");
22 header( "index.php" );
26 //plc_debug('POST',$_POST);
28 // if submitted validate and add
29 // could go in actions.php but OTOH when things fail it's more convenient
30 // to show the current values again
31 if ( $_POST['add-node'] ) {
35 $hostname = trim($_POST['hostname']);
36 $model= trim($_POST['model']);
37 $ip = trim($_POST['ip']);
38 $method = trim($_POST['method']);
39 $netmask = trim($_POST['netmask']);
40 $network = trim($_POST['network']);
41 $broadcast = trim($_POST['broadcast']);
42 $gateway = trim($_POST['gateway']);
43 $dns1 = trim($_POST['dns1']);
44 $dns2 = trim($_POST['dns2']);
45 $bwlimit = trim($_POST['bwlimit']);
47 // used to generate error strings for static fields only
48 $static_fields= array();
49 $static_fields['netmask']= "Netmask address";
50 $static_fields['network']= "Network address";
51 $static_fields['gateway']= "Gateway address";
52 $static_fields['broadcast']= "Broadcast address";
53 $static_fields['dns1']= "Primary DNS address";
55 if( $method == 'static' ) {
56 foreach( $static_fields as $field => $desc ) {
57 if( trim($_POST[$field]) == "" ) {
58 $errors[] = "$desc is required";
59 } elseif( !is_valid_ip(trim($_POST[$field])) ) {
60 $errors[] = "$desc is not a valid address";
64 if( !is_valid_network_addr($network,$netmask) ) {
65 $errors[] = "The network address does not match the netmask";
69 if( $hostname == "" ) {
70 $errors[] = "Hostname is required";
73 $errors[] = "IP is required";
74 } else if ( ! is_valid_ip ($ip)) {
75 $errors []= "Invalid IP $ip";
78 if( !empty($errors) ) {
79 drupal_set_error(plc_itemize($errors));
81 // add new node and its interface
82 $node_fields= array( "hostname"=>$hostname, "model"=>$model );
83 $site_id= plc_my_site_id();
84 $node_id= $api->AddNode( intval( $site_id ), $node_fields );
86 if ( empty($node_id) || ($node_id < 0) ) {
87 drupal_set_error ("AddNode failed - hostname already present, or not valid ?");
89 // now, try to add the network.
90 $interface_fields= array();
91 $interface_fields['is_primary']= true;
92 $interface_fields['ip']= $ip;
93 $interface_fields['type']= $_POST['type'];
94 $interface_fields['method']= $method;
96 $interface_fields['bwlimit']=$bwlimit;
98 if ( $method == 'static' ) {
99 $interface_fields['netmask']= $netmask;
100 $interface_fields['network']= $network;
101 $interface_fields['broadcast']= $broadcast;
102 $interface_fields['gateway']= $gateway;
103 $interface_fields['dns1']= $dns1;
105 $interface_fields['dns2']= $dns2;
108 $interface_id= $api->AddInterface( $node_id, $interface_fields);
109 if ($interface_id > 0) {
110 drupal_set_message ("Node successfully created");
111 drupal_set_message ("Download a boot image in the 'Download' drop down below");
112 plc_redirect (l_node($node_id));
114 // if AddInterface fails, we have the node created,
115 // but no primary interface is present.
116 // The primary interface can be added later,
117 // but take a look at the possible Methods,
118 // if we specify TUN/TAP Method we will have
119 // an error on download of the configuration file
120 drupal_set_message ("Node created");
121 drupal_set_error ("But without an interface");
122 drupal_set_error ("Please review manually");
123 plc_redirect (l_node($node_id));
130 require_once 'plc_drupal.php';
131 include 'plc_header.php';
133 // include javacsript helpers
134 require_once 'prototype.php';
135 drupal_set_html_head ('
136 <script type="text/javascript" src="/planetlab/nodes/interface.js"></script>
139 $sites=$api->GetSites(array(plc_my_site_id()));
141 $sitename=$site['name'];
143 drupal_set_title('Add a new node in site "' . $sitename . '"');
146 $method = $_POST['method'];
147 if( ! $method ) $method= "static";
149 $model = $_POST['model'];
150 if( ! $model ) $model= "Custom";
154 This page lets you declare a new machine in your site.
155 This must be done before the machine is turned on, as it will allow you to download a boot image when complete for this node.
157 You must enter an IP address even if you use DHCP.
161 $details=new PlekitDetails($has_privileges);
163 // xxx hardwire network type for now
164 $form_variables = array('type'=>"ipv4");
165 //$form=$details->form_start(l_actions(),$form_variables);
166 $form=$details->form_start('/db/nodes/node_add.php',$form_variables);
170 $details->th_td("Hostname",$hostname,"hostname");
171 $details->th_td("Model",$model,"model");
172 $method_select = $form->select_html ("method",
173 interface_method_selectors($api,$method,true),
174 array('id'=>'method',
175 'onChange'=>'updateMethodFields()'));
176 $details->th_td("Method",$method_select,"method",
177 array('input_type'=>'select','value'=>$interface['method']));
179 // dont display the 'type' selector as it contains only ipv4
180 //>>> GetNetworkTypes()
183 $details->th_td("IP address",$ip,"ip",array('width'=>15,
184 'onKeyup'=>'networkHelper()',
185 'onChange'=>'networkHelper()'));
186 $details->th_td("Netmask",$netmask,"netmask",array('width'=>15,
187 'onKeyup'=>'networkHelper()',
188 'onChange'=>'networkHelper()'));
189 $details->th_td("Network",$network,"network",array('width'=>15));
190 $details->th_td("Broadcast",$broadcast,"broadcast",array('width'=>15));
191 $details->th_td("Gateway",$gateway,"gateway",array('width'=>15,
192 'onChange'=>'subnetChecker("gateway",false)'));
193 $details->th_td("DNS 1",$dns1,"dns1",array('width'=>15,
194 'onChange'=>'subnetChecker("dns1",false)'));
195 $details->th_td("DNS 2",$dns2,"dns2",array('width'=>15,
196 'onChange'=>'subnetChecker("dns2",true)'));
198 $details->th_td("BW limit (bps)",$bwlimit,"bwlimit",array('width'=>11));
201 $add_button = $form->submit_html ("add-node","Add New Node",
202 array('onSubmit'=>'interfaceSubmit()'));
203 $details->tr($add_button,"right");
209 include 'plc_footer.php';