VMware {code} Community
eholtz
Contributor
Contributor
Jump to solution

Problems with applying ntp service configuration

Hi folks,

I'm stuck at configuring the ntp service to get started by default on an esxi server via perl.

This is what I'm trying to do:

my $hostServiceConfig = new HostServiceConfig(serviceId => 'ntpd', startupPolicy => 'on');

my $hsca=[];

push (@$hsca,$hostServiceConfig);

my $hostConfigSpec = new HostConfigSpec(service => $hsca );

my $taskRef=$hostProfileManager->ApplyHostConfig_Task(host => $host, configSpec => $hostConfigSpec);

When I ask for the status the command fails with:

SOAP Fault:

-----------

Fault string: The operation is not allowed in the current state.

Fault detail: InvalidState

I assume I'm just missing some little detail. Maybe someone sees my mistake right away 🙂

Reply
0 Kudos
1 Solution

Accepted Solutions
dermoth
Contributor
Contributor
Jump to solution

Hi eholtz,

Here's the code I added to hostServiceManagement.pl (IIRC!):

1. Check startup policy:

sub checkServiceEnabled {
        my ($serviceSystem,$service) = @_;

        my $services = $serviceSystem->serviceInfo->service;

        foreach(@$services) {
                if($_->key eq $service) {
                        return $_->policy;
                }
        }
        return 0;
}

2. Then to disable the service:

my $policy = &checkServiceEnabled($serviceSystem,$service);
if($policy eq "on") {
        eval {
                $serviceSystem->UpdateServicePolicy(id => $service, policy => "off");
                print "\t" . color("cyan") . "Successfully disabled $service\n" . color("reset");
        };
        if($@) {
                print "\t" . color("red") . "Error: Unable to change startup policy on service \"$service\" due to: " . $@ . color("reset") . "\n";
        }
}elsif($policy eq "off") {
        print "\t" . color("yellow") . "$service is already disabled" . color("reset") . "\n";
} else {
        print "\t" . color("red") . "Error: Unknown startup policy \"$policy\"" . color("reset") . "\n";
}

And to enable:

my $policy = &checkServiceEnabled($serviceSystem,$service);
if($policy eq "off") {
        eval {
                $serviceSystem->UpdateServicePolicy(id => $service, policy => "on");
                print "\t" . color("cyan") . "Successfully enabled $service\n" . color("reset");
        };
        if($@) {
                print "\t" . color("red") . "Error: Unable to change startup policy on service \"$service\" due to: " . $@ . color("reset") . "\n";                     }

}elsif($policy eq "on") {
        print "\t" . color("yellow") . "$service is already enabled" . color("reset") . "\n";
} else {
        print "\t" . color("red") . "Error: Unknown startup policy \"$policy\"" . color("reset") . "\n";
}

P.s.: based on your current score you should be able to PM me now...

Regards,

--

Thomas

View solution in original post

Reply
0 Kudos
5 Replies
lamw
Community Manager
Community Manager
Jump to solution

Not sure if you've seen this sample here, but it may help http://www.virtuallyghetto.com/2010/07/new-way-of-enabling-and-disabling.html

eholtz
Contributor
Contributor
Jump to solution

Thanks for that link, I did not find that.

The code itself does not help, because it only starts and stops the service, but doesn't set the startup policy when the ESX host restarts. The guy in the answers seems to have solved the problem - maybe he'll help me out 🙂

Reply
0 Kudos
dermoth
Contributor
Contributor
Jump to solution

Hi eholtz,

Here's the code I added to hostServiceManagement.pl (IIRC!):

1. Check startup policy:

sub checkServiceEnabled {
        my ($serviceSystem,$service) = @_;

        my $services = $serviceSystem->serviceInfo->service;

        foreach(@$services) {
                if($_->key eq $service) {
                        return $_->policy;
                }
        }
        return 0;
}

2. Then to disable the service:

my $policy = &checkServiceEnabled($serviceSystem,$service);
if($policy eq "on") {
        eval {
                $serviceSystem->UpdateServicePolicy(id => $service, policy => "off");
                print "\t" . color("cyan") . "Successfully disabled $service\n" . color("reset");
        };
        if($@) {
                print "\t" . color("red") . "Error: Unable to change startup policy on service \"$service\" due to: " . $@ . color("reset") . "\n";
        }
}elsif($policy eq "off") {
        print "\t" . color("yellow") . "$service is already disabled" . color("reset") . "\n";
} else {
        print "\t" . color("red") . "Error: Unknown startup policy \"$policy\"" . color("reset") . "\n";
}

And to enable:

my $policy = &checkServiceEnabled($serviceSystem,$service);
if($policy eq "off") {
        eval {
                $serviceSystem->UpdateServicePolicy(id => $service, policy => "on");
                print "\t" . color("cyan") . "Successfully enabled $service\n" . color("reset");
        };
        if($@) {
                print "\t" . color("red") . "Error: Unable to change startup policy on service \"$service\" due to: " . $@ . color("reset") . "\n";                     }

}elsif($policy eq "on") {
        print "\t" . color("yellow") . "$service is already enabled" . color("reset") . "\n";
} else {
        print "\t" . color("red") . "Error: Unknown startup policy \"$policy\"" . color("reset") . "\n";
}

P.s.: based on your current score you should be able to PM me now...

Regards,

--

Thomas

Reply
0 Kudos
dermoth
Contributor
Contributor
Jump to solution

Hi lamw,

This reply should really belong to the other post - https://communities.vmware.com/docs/DOC-11656 - unfortunately I cannot attach files to replies there (?!) and I don't have the score required to send PM's (so sorry for hijacking another thread...)

I have updated your script to enable/disable services in addition to start/stop them. I attached the full script, as well as a patch from the initial copy I downloaded.

$ md5sum hostServiceManagement.pl*
66b97f2c4230d7717813b42821b57b3d *hostServiceManagement.pl
4047cd45b8cf52b846cdd846dad6bc4b *hostServiceManagement.pl.new
98c27856e1d13ed33810d8577bd49401 *hostServiceManagement.pl.patch

Regards,

--

Thomas

Reply
0 Kudos
eholtz
Contributor
Contributor
Jump to solution

Hi Thomas,

many thanks - that worked for me 🙂

Best regards,

eholtz

Reply
0 Kudos