function getHTML($globalopts) { global $user; $data = getVariablesRegex("^{$this->key}.*"); $h = "<div class=\"configwidget\" style=\"width: 100%;\">\n"; $h .= "<h3>{$this->name}</h3>\n"; $h .= "<span class=\"siteconfigdesc\">\n"; $h .= $this->desc; $h .= "<br><br></span>\n"; $origvals = array(); $affils = getAffiliations(); if ($globalopts) { $key = $this->key; $prekey = $key; if (array_key_exists($key, $data)) { $val = $data[$key]; $dispval = $data[$key]; if ($this->scale60) { $dispval = (int) ($dispval / 60); } } else { setVariable($key, $this->defaultval, 'none'); $val = $this->defaultval; $dispval = $this->defaultval; if ($this->scale60) { $dispval = (int) ($dispval / 60); } } $label = i('Global'); unset_by_val(i('Global'), $affils); } else { $key = "{$this->key}|{$user['affiliation']}"; if (array_key_exists($key, $data)) { $val = $data[$key]; $dispval = $data[$key]; if ($this->scale60) { $dispval = (int) ($dispval / 60); } } elseif (array_key_exists($this->key, $data)) { $val = 0; # have to set it to something different so we recognize it changed when saving $dispval = $data[$this->key]; if ($this->scale60) { $dispval = (int) ($dispval / 60); } } else { setVariable($this->key, $this->defaultval, 'none'); $val = 0; # have to set it to something different so we recognize it changed when saving $dispval = $this->defaultval; if ($this->scale60) { $dispval = (int) ($dispval / 60); } } $label = $user['affiliation']; $prekey = $key; $key = str_replace('|', '_', $key); } $extra = array('smallDelta' => 1, 'largeDelta' => 10); $h .= labeledFormItem($key, $label, 'spinner', "{min:{$this->minval}, max:{$this->maxval}}", 1, $dispval, '', '', $extra, '', '', 0); $h .= "<br>\n"; $origvals[$key] = array('key' => $prekey, 'val' => $val); if ($globalopts) { $h .= "<div id=\"{$this->domidbase}affildiv\">\n"; foreach ($data as $prekey => $val) { if ($prekey == $this->key) { continue; } $key = str_replace('|', '_', $prekey); $tmp = explode('|', $prekey); $label = $tmp[1]; $dispval = $val; if ($this->scale60) { $dispval = (int) ($dispval / 60); } $h .= "<span id=\"{$key}span\">\n"; $h .= labeledFormItem($key, $label, 'spinner', "{min:{$this->minval}, max:{$this->maxval}}", 1, $dispval, '', '', $extra, '', '', 0); $h .= dijitButton("{$key}delbtn", i("Delete"), "{$this->jsname}.deleteAffiliationSetting('{$key}', '{$this->domidbase}');") . "<br>\n"; $h .= "</span>\n"; $origvals[$key] = array('key' => $prekey, 'val' => $val); unset_by_val($label, $affils); } $h .= "</div>\n"; $h .= "<div id=\"{$this->domidbase}adddiv\""; if (!count($affils)) { $h .= " class=\"hidden\""; } $h .= ">\n"; $newval = $this->defaultval; if ($this->scale60) { $newval = (int) ($newval / 60); } $h .= selectInputHTML('', $affils, "{$this->domidbase}newaffilid", "dojoType=\"dijit.form.Select\" maxHeight=\"250\""); $h .= "<input dojoType=\"dijit.form.NumberSpinner\" "; $h .= "required=\"1\" "; $h .= "style=\"width: 70px;\" "; $h .= "value=\"{$newval}\" "; $h .= "constraints=\"{min:{$this->minval}, max:{$this->maxval}}\" "; $h .= "smallDelta=\"1\" "; $h .= "largeDelta=\"10\" "; $h .= "id=\"{$this->domidbase}newval\">\n"; $h .= dijitButton("{$this->domidbase}addbtn", i('Add'), "{$this->jsname}.addAffiliationSetting();"); $cont = addContinuationsEntry('AJaddAffiliationSetting', $this->basecdata); $h .= "<input type=\"hidden\" id=\"{$this->domidbase}addcont\" value=\"{$cont}\">\n"; $h .= "</div>\n"; $cdata = $this->basecdata; $cdata['origvals'] = $origvals; $cont = addContinuationsEntry('AJdeleteAffiliationSetting', $cdata); $h .= "<input type=\"hidden\" id=\"delete{$this->domidbase}cont\" value=\"{$cont}\">\n"; } $tmp = array_keys($origvals); $keys = implode(',', $tmp); $h .= "<div id=\"{$this->domidbase}msg\"></div>\n"; $h .= "<input type=\"hidden\" id=\"{$this->domidbase}savekeys\" value=\"{$keys}\">\n"; $h .= dijitButton("{$this->domidbase}btn", i('Submit Changes'), "{$this->jsname}.saveSettings();", 1); $cdata = $this->basecdata; $cdata['origvals'] = $origvals; $cont = addContinuationsEntry('AJupdateAllSettings', $cdata); $h .= "<input type=\"hidden\" id=\"{$this->domidbase}cont\" value=\"{$cont}\">\n"; $h .= "</div>\n"; return $h; }
function AJvmToHost() { $hostid = processInputVar('hostid', ARG_NUMERIC); $hostdata = getVMHostData($hostid); $resources = getUserResources(array("computerAdmin"), array("administer")); if (!array_key_exists($hostdata[$hostid]['computerid'], $resources['computer'])) { $arr = array('failed' => 'nohostaccess'); header('Content-Type: text/json-comment-filtered; charset=utf-8'); print '/*{"items":' . json_encode($arr) . '}*/'; return; } # find out how many vms are currently on the host $query = "SELECT COUNT(id) " . "FROM computer " . "WHERE vmhostid = {$hostid}"; $qh = doQuery($query, 101); $row = mysql_fetch_row($qh); if ($row[0] >= $hostdata[$hostid]['vmlimit']) { $arr = array('failed' => 'vmlimit'); header('Content-Type: text/json-comment-filtered; charset=utf-8'); print '/*{"items":' . json_encode($arr) . '}*/'; return; } $adds = array(); $fails = array(); $vmlistids = processInputVar('listids', ARG_STRING); $vmids = explode(',', $vmlistids); # get data about submitted vms to add $query = "SELECT id, hostname, vmhostid " . "FROM computer " . "WHERE id in ({$vmlistids})"; $qh = doQuery($query, 101); $vmdata = array(); while ($row = mysql_fetch_assoc($qh)) { if (!array_key_exists($row['id'], $resources['computer'])) { $fails[] = array('id' => $row['id'], 'name' => $row['hostname'], 'reason' => 'noaccess'); unset_by_val($row['id'], $vmids); continue; } $vmdata[$row['id']] = $row; } # build list of vm hosts $query = "SELECT id FROM vmhost"; $vmhosts = array(); $qh = doQuery($query, 101); while ($row = mysql_fetch_assoc($qh)) { $vmhosts[$row['id']] = 1; } # check to see if there any submitted vms have a hostid of an existing vm host foreach ($vmids as $compid) { if (!array_key_exists($vmdata[$compid]['vmhostid'], $vmhosts)) { $query = "UPDATE computer " . "SET vmhostid = {$hostid}, " . "stateid = 2 " . "WHERE id = {$compid}"; doQuery($query, 101); $adds[] = array('id' => $compid, 'name' => $vmdata[$compid]['hostname'], 'state' => 'available'); } else { $fails[] = array('id' => $compid, 'name' => $vmdata[$compid]['hostname'], 'reason' => 'otherhost'); } } header('Content-Type: text/json-comment-filtered; charset=utf-8'); $arr = array('vms' => $adds, 'fails' => $fails, 'addrem' => 1); print '/*{"items":' . json_encode($arr) . '}*/'; }
function submitCompStateChange() { global $user; print "<H2>Change State of Computers</H2>\n"; $data = getContinuationVar(); $computers = getComputers(1); # switching to available if ($data['stateid'] == 2) { $compids = implode(',', $data['computerids']); $query = "UPDATE computer " . "SET stateid = 2, " . "notes = '' " . "WHERE id IN ({$compids})"; doQuery($query, 101); print "The following computers were changed to the available state:\n"; print "<TABLE>\n"; foreach ($data['computerids'] as $compid) { print " <TR>\n"; print " <TD>{$computers[$compid]['hostname']}</TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; } elseif ($data['stateid'] == 10) { $data['notes'] = processInputVar('notes', ARG_STRING); if (get_magic_quotes_gpc()) { $data['notes'] = stripslashes($data['notes']); } $data['notes'] = mysql_escape_string($data['notes']); $data["notes"] = $user["unityid"] . " " . unixToDatetime(time()) . "@" . $data["notes"]; $vclreloadid = getUserlistID('vclreload@Local'); // get semaphore lock if (!semLock()) { abort(3); } $noaction = array(); $changenow = array(); $changeasap = array(); $changetimes = array(); foreach ($data['computerids'] as $compid) { if ($computers[$compid]['state'] == 'maintenance') { array_push($noaction, $compid); } else { array_push($changeasap, $compid); } } $passes = array(); $fails = array(); foreach ($changeasap as $compid) { # TODO what about blockComputers? # try to move future reservations off of computer moveReservationsOffComputer($compid); # get end time of last reservation $query = "SELECT rq.end " . "FROM request rq, " . "reservation rs " . "WHERE rs.requestid = rq.id AND " . "rs.computerid = {$compid} AND " . "rq.stateid NOT IN (1,5,12) " . "ORDER BY end DESC " . "LIMIT 1"; $qh = doQuery($query, 101); # create a really long reservation starting at that time in state tomaintenance if ($row = mysql_fetch_assoc($qh)) { $start = $row['end']; $changetimes[$compid] = $start; $end = datetimeToUnix($start) + SECINWEEK; // hopefully keep future reservations off of it $end = unixToDatetime($end); if (simpleAddRequest($compid, 4, 3, $start, $end, 18, $vclreloadid)) { $passes[] = $compid; } else { $fails[] = $compid; } } else { $query = "UPDATE computer " . "SET stateid = 10, " . "notes = '{$data['notes']}' " . "WHERE id = {$compid}"; doQuery($query, 101); unset_by_val($compid, $changeasap); array_push($changenow, $compid); } } // release semaphore lock semUnlock(); if (count($noaction) || count($changeasap)) { $comparr = array_merge($noaction, $changeasap); $compids = implode(',', $comparr); $query = "UPDATE computer " . "SET notes = '{$data['notes']}' " . "WHERE id IN ({$compids})"; doQuery($query, 101); } if (count($changenow)) { print "The following computers were immediately placed into the "; print "maintenance state:\n"; print "<TABLE>\n"; foreach ($changenow as $compid) { print " <TR>\n"; print " <TD><font color=\"#008000\">{$computers[$compid]['hostname']}</font></TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; print "<br>\n"; } if (count($passes)) { print "The following computers currently have reservations on them "; print "and will be placed in the maintenance state at the time listed "; print "for each computer:\n"; print "<TABLE>\n"; print " <TR>\n"; print " <TH>Computer</TH>\n"; print " <TH>Maintenance time</TH>\n"; print " </TR>\n"; foreach ($passes as $compid) { print " <TR>\n"; print " <TD align=center><font color=\"ff8c00\">{$computers[$compid]['hostname']}</font></TD>\n"; $time = date('n/j/y g:i a', datetimeToUnix($changetimes[$compid])); print " <TD align=center>{$time}</TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; print "<br>\n"; } if (count($fails)) { print "The following computers currently have reservations on them "; print "but no functional management node was found for them. Nothing "; print "be done with them at this time:\n"; print "<TABLE>\n"; print " <TR>\n"; print " <TH>Computer</TH>\n"; print " </TR>\n"; foreach ($passes as $compid) { print " <TR>\n"; print " <TD align=center><font color=\"ff0000\">{$computers[$compid]['hostname']}</font></TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; print "<br>\n"; } if (count($noaction)) { print "The following computers were already in the maintenance "; print "state and had their notes on being in the maintenance state "; print "updated:\n"; print "<TABLE>\n"; foreach ($noaction as $compid) { print " <TR>\n"; print " <TD>{$computers[$compid]['hostname']}</TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; print "<br>\n"; } } elseif ($data['stateid'] == 20) { $profileid = processInputVar('profileid', ARG_NUMERIC); if (!array_key_exists($profileid, $data['profiles'])) { $keys = array_keys($data['profiles']); $profileid = $keys[0]; } $noaction = array(); $changenow = array(); $changeasap = array(); $changetimes = array(); foreach ($data['computerids'] as $compid) { if ($computers[$compid]['state'] == 'vmhostinuse') { array_push($noaction, $compid); } else { array_push($changeasap, $compid); } } if (!semLock()) { abort(3); } foreach ($changeasap as $compid) { # TODO what about blockComputers? moveReservationsOffComputer($compid); # get end time of last reservation $query = "SELECT rq.end " . "FROM request rq, " . "reservation rs " . "WHERE rs.requestid = rq.id AND " . "rs.computerid = {$compid} AND " . "rq.stateid NOT IN (1,5,12) " . "ORDER BY end DESC " . "LIMIT 1"; $qh = doQuery($query, 101); if ($row = mysql_fetch_assoc($qh)) { // if there is a reservation, leave in $changeasap so we can # notify that we can't change this one } else { # create a reload reservation to load machine with image # corresponding to selected vm profile $start = getReloadStartTime(); $end = $start + SECINYEAR; # don't want anyone making a future reservation for this machine $start = unixToDatetime($start); $end = unixToDatetime($end); $imagerevisionid = getProductionRevisionid($data['profiles'][$profileid]['imageid']); $vclreloadid = getUserlistID('vclreload@Local'); simpleAddRequest($compid, $data['profiles'][$profileid]['imageid'], $imagerevisionid, $start, $end, 21, $vclreloadid); unset_by_val($compid, $changeasap); array_push($changenow, $compid); # check for existing vmhost entry $query = "SELECT id, " . "vmprofileid " . "FROM vmhost " . "WHERE computerid = {$compid}"; $qh = doQuery($query, 101); if ($row = mysql_fetch_assoc($qh)) { if ($row['vmprofileid'] != $profileid) { # update vmprofile $query = "UPDATE vmhost " . "SET vmprofileid = {$profileid} " . "WHERE id = {$row['id']}"; doQuery($query, 101); } } else { # create vmhost entry $query = "INSERT INTO vmhost " . "(computerid, " . "vmlimit, " . "vmprofileid) " . "VALUES ({$compid}, " . "2, " . "{$profileid})"; doQuery($query, 101); } } } // release semaphore lock semUnlock(); if (count($changenow)) { print "The following computers were placed into the "; print "vmhostinuse state:\n"; print "<TABLE>\n"; foreach ($changenow as $compid) { print " <TR>\n"; print " <TD><font color=\"#008000\">{$computers[$compid]['hostname']}</font></TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; print "<br>\n"; } if (count($changeasap)) { print "The following computers currently have reservations on them "; print "and cannot be placed in the vmhostinuse state at this time:\n"; print "<TABLE>\n"; foreach ($changeasap as $compid) { print " <TR>\n"; print " <TD><font color=\"ff0000\">{$computers[$compid]['hostname']}</font></TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; print "<br>\n"; } if (count($noaction)) { print "The following computers were already in the vmhostinuse "; print "state:\n"; print "<TABLE>\n"; foreach ($noaction as $compid) { print " <TR>\n"; print " <TD>{$computers[$compid]['hostname']}</TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; print "<br>\n"; } } elseif ($data['stateid'] == 23) { $noaction = array(); $changenow = array(); $changeasap = array(); $changetimes = array(); foreach ($data['computerids'] as $compid) { if ($computers[$compid]['state'] == 'hpc') { array_push($noaction, $compid); } else { array_push($changeasap, $compid); } } if (!semLock()) { abort(3); } foreach ($changeasap as $compid) { # TODO what about blockComputers? moveReservationsOffComputer($compid); # get end time of last reservation $query = "SELECT rq.end " . "FROM request rq, " . "reservation rs " . "WHERE rs.requestid = rq.id AND " . "rs.computerid = {$compid} AND " . "rq.stateid NOT IN (1,5,12) " . "ORDER BY end DESC " . "LIMIT 1"; $qh = doQuery($query, 101); if ($row = mysql_fetch_assoc($qh)) { // if there is a reservation, leave in $changeasap so we can # notify that we can't change this one } else { $query = "UPDATE computer " . "SET stateid = 23 " . "WHERE id = {$compid}"; doQuery($query, 101); unset_by_val($compid, $changeasap); array_push($changenow, $compid); } } // release semaphore lock semUnlock(); if (count($changenow)) { print "The following computers were placed into the "; print "hpc state:\n"; print "<TABLE>\n"; foreach ($changenow as $compid) { print " <TR>\n"; print " <TD><font color=\"#008000\">{$computers[$compid]['hostname']}</font></TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; print "<br>\n"; } if (count($changeasap)) { print "The following computers currently have reservations on them "; print "and cannot be placed in the hpc state at this time:\n"; print "<TABLE>\n"; foreach ($changeasap as $compid) { print " <TR>\n"; print " <TD><font color=\"ff0000\">{$computers[$compid]['hostname']}</font></TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; print "<br>\n"; } if (count($noaction)) { print "The following computers were already in the hpc "; print "state:\n"; print "<TABLE>\n"; foreach ($noaction as $compid) { print " <TR>\n"; print " <TD>{$computers[$compid]['hostname']}</TD>\n"; print " </TR>\n"; } print "</TABLE>\n"; print "<br>\n"; } } else { abort(50); } }