Merge pull request #1039 from pliablepixels/1038-fixing-state-mgmt-1030-isActive-fix

1038 fixing state mgmt 1030 is active fix
This commit is contained in:
Isaac Connor 2015-08-27 09:08:50 -04:00
commit 6093587ff2
5 changed files with 99 additions and 17 deletions

View File

@ -387,8 +387,8 @@ CREATE TABLE `Monitors` (
-- --
-- Table structure for table `States` -- Table structure for table `States`
-- Added IsActive to track custom run states -- PP - Added IsActive to track custom run states
-- -- Also made sure Name is unique
DROP TABLE IF EXISTS `States`; DROP TABLE IF EXISTS `States`;
CREATE TABLE `States` ( CREATE TABLE `States` (
@ -396,8 +396,11 @@ CREATE TABLE `States` (
`Name` varchar(64) NOT NULL default '', `Name` varchar(64) NOT NULL default '',
`Definition` text NOT NULL, `Definition` text NOT NULL,
`IsActive` tinyint(3) unsigned NOT NULL default '0', `IsActive` tinyint(3) unsigned NOT NULL default '0',
PRIMARY KEY (`Id`) PRIMARY KEY (`Id`),
UNIQUE KEY (`Name`)
) ENGINE=@ZM_MYSQL_ENGINE@; ) ENGINE=@ZM_MYSQL_ENGINE@;
INSERT INTO States (Name,Definition,IsActive) VALUES ('default','','1');
-- --
-- Table structure for table `Servers` -- Table structure for table `Servers`

View File

@ -342,7 +342,7 @@ SET @s = (SELECT IF(
PREPARE stmt FROM @s; PREPARE stmt FROM @s;
EXECUTE stmt; EXECUTE stmt;
-- The States table will be updated to have a new column called IsActive -- PP:The States table will be updated to have a new column called IsActive
-- used to keep track of which custom state is active (if any) -- used to keep track of which custom state is active (if any)
SET @s = (SELECT IF( SET @s = (SELECT IF(
(SELECT COUNT(*) (SELECT COUNT(*)
@ -358,6 +358,23 @@ SET @s = (SELECT IF(
PREPARE stmt FROM @s; PREPARE stmt FROM @s;
EXECUTE stmt; EXECUTE stmt;
-- PP:If default state does not exist, create it and set its IsActive to 1
INSERT INTO States (Name,Definition,IsActive)
SELECT * FROM (SELECT 'default', '', '1') AS tmp
WHERE NOT EXISTS (
SELECT Name FROM States WHERE Name = 'default'
) LIMIT 1;
-- PP:Start with a sane isActive state
UPDATE States SET IsActive = '0';
UPDATE States SET IsActive = '1' WHERE Name = 'default';
-- PP:Finally convert States to make sure Names are unique
-- If duplicate states existed while upgrading, that is
-- very likely an error that ZM allowed earlier, so
-- we are picking up the first one and deleting the others
ALTER IGNORE TABLE States ADD UNIQUE (Name);
SET @s = (SELECT IF( SET @s = (SELECT IF(
(SELECT COUNT(*) (SELECT COUNT(*)
FROM INFORMATION_SCHEMA.TABLES FROM INFORMATION_SCHEMA.TABLES

View File

@ -105,6 +105,8 @@ if ( !$command || $command !~ /^(?:start|stop|restart|status|logrot|version)$/ )
} }
} }
$dbh = zmDbConnect() if ! $dbh; $dbh = zmDbConnect() if ! $dbh;
# PP - Sane state check
isActiveSanityCheck();
# Move to the right place # Move to the right place
chdir( $Config{ZM_PATH_WEB} ) chdir( $Config{ZM_PATH_WEB} )
@ -151,19 +153,17 @@ if ( $command eq "state" )
} }
} }
$sth->finish(); $sth->finish();
#PP - lets go ahead and modify States DB
Debug ("Marking $store_state as Enabled");
# PP - Zero out other states being active
resetStates();
# PP - Now mark a specific state as active # PP - Now mark a specific state as active
resetStates();
Info ("Marking $store_state as Enabled");
$sql = "update States set IsActive = '1' where Name = ?"; $sql = "update States set IsActive = '1' where Name = ?";
$sth = $dbh->prepare_cached( $sql ) $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
$res = $sth->execute( $store_state ) $res = $sth->execute( $store_state )
or Fatal( "Can't execute: ".$sth->errstr() ); or Fatal( "Can't execute: ".$sth->errstr() );
# PP - zero out other states isActive
$command = "restart"; $command = "restart";
} }
@ -173,8 +173,6 @@ if ( $command =~ /^(start|stop|restart)$/ )
# We have to detaint to keep perl from complaining # We have to detaint to keep perl from complaining
$command = $1; $command = $1;
# PP - if we are not switching to a custom state, zero out all isActive
resetStates() if (!$store_state);
if ( systemdRunning() && !calledBysystem() ) { if ( systemdRunning() && !calledBysystem() ) {
qx(@BINDIR@/zmsystemctl.pl $command); qx(@BINDIR@/zmsystemctl.pl $command);
@ -307,8 +305,59 @@ if ( $command eq "logrot" )
exit( $retval ); exit( $retval );
# PP - when the system is restarted/started/stopped, it will # PP - Make sure isActive is on and only one
# not be in a custom state, so lets keep the DB consistent sub isActiveSanityCheck
{
Info ("Sanity checking States table...");
$dbh = zmDbConnect() if ! $dbh;
# PP - First, make sure default exists and there is only one
my $sql = "select Name from States where Name = 'default'";
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute()
or Fatal( "Can't execute: ".$sth->errstr() );
if ($sth->rows != 1) # PP - no row, or too many rows. Either case is an error
{
Info( "Fixing States table - either no default state or duplicate default states" );
$sql = "delete from States where Name = 'default'";
$sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
$res = $sth->execute()
or Fatal( "Can't execute: ".$sth->errstr() );
$sql = "insert into States (Name,Definition,IsActive) VALUES ('default','','1');";
$sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
$res = $sth->execute()
or Fatal( "Can't execute: ".$sth->errstr() );
}
# PP - Now make sure no two states have IsActive=1
$sql = "select Name from States where IsActive = '1'";
$sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
$res = $sth->execute()
or Fatal( "Can't execute: ".$sth->errstr() );
if ( $sth->rows != 1 )
{
Info( "Fixing States table so only one run state is active" );
resetStates();
$sql = "update States set IsActive='1' WHERE Name='default'";
$sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
$res = $sth->execute()
or Fatal( "Can't execute: ".$sth->errstr() );
}
}
# PP - zeroes out isActive for all states
sub resetStates sub resetStates
{ {
$dbh = zmDbConnect() if ! $dbh; $dbh = zmDbConnect() if ! $dbh;

View File

@ -1,5 +1,6 @@
function checkState( element ) function checkState( element )
{ {
var form = element.form; var form = element.form;
var minIndex = running?2:1; var minIndex = running?2:1;
@ -13,13 +14,24 @@ function checkState( element )
form.saveBtn.disabled = false; form.saveBtn.disabled = false;
form.deleteBtn.disabled = false; form.deleteBtn.disabled = false;
} }
if ( form.newState.value != '' ) if ( form.newState.value != '' )
form.saveBtn.disabled = false; form.saveBtn.disabled = false;
// PP if we are in 'default' state, disable delete
// you can still save
if (element.value.toLowerCase() == 'default' )
{
form.saveBtn.disabled = false;
form.deleteBtn.disabled = true;
}
} }
function saveState( element ) function saveState( element )
{ {
var form = element.form; var form = element.form;
form.view.value = currentView; form.view.value = currentView;
form.action.value = 'save'; form.action.value = 'save';
form.submit(); form.submit();

View File

@ -26,7 +26,6 @@ if ( !canEdit( 'System' ) )
$running = daemonCheck(); $running = daemonCheck();
$states = dbFetchAll( "select * from States" ); $states = dbFetchAll( "select * from States" );
$focusWindow = true; $focusWindow = true;
xhtmlHeaders(__FILE__, translate('RunState') ); xhtmlHeaders(__FILE__, translate('RunState') );
@ -76,7 +75,9 @@ if ( empty($_REQUEST['apply']) )
<tbody> <tbody>
<tr> <tr>
<th scope="row"><?php echo translate('NewState') ?></th> <th scope="row"><?php echo translate('NewState') ?></th>
<td><input type="text" name="newState" value="" size="16" onchange="checkState( this );"/></td> <!-- PP - added oninput so that changes are detected immediately -->
<!-- PP - retained onchange for older browsers -->
<td><input type="text" name="newState" value="" size="16" oninput="checkState( this );" onchange="checkState(this);"/></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>