google-nomulus/java/google/registry/tools/SetNumInstancesCommand.java
shicong 66d98c8d66 Add SetNumInstancesCommand in Nomulus tool to adjust the number of instances
for a given service and version at runtime.

Note that this CL only supports the adjustment for a given service and version. I will add another functionality to let this command be able to detect all non-live versions automatically and apply the adjustment.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=221092001
2018-11-12 14:51:40 -05:00

66 lines
2.3 KiB
Java

// Copyright 2018 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.flogger.FluentLogger;
import google.registry.util.AppEngineServiceUtils;
import javax.inject.Inject;
/** A command to set the number of instances for an App Engine service. */
@Parameters(
separators = " =",
commandDescription =
"Set the number of instances for a given service and version. "
+ "Note that this command only works for manual scaling service.")
final class SetNumInstancesCommand implements CommandWithRemoteApi {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@Parameter(
names = "--service",
description = "Name of the App Engine service, e.g., default.",
required = true)
private String service;
@Parameter(
names = "--version",
description = "Name of the service's version, e.g., canary.",
required = true)
private String version;
@Parameter(
names = "--numInstances",
description = "The new number of instances for the version.",
required = true)
private Long numInstances;
@Inject AppEngineServiceUtils appEngineServiceUtils;
@Override
public void run() throws Exception {
checkArgument(!service.isEmpty(), "Service must be specified");
checkArgument(!version.isEmpty(), "Version must be specified");
checkArgument(numInstances > 0, "Number of instances must be greater than zero");
appEngineServiceUtils.setNumInstances(service, version, numInstances);
logger.atInfo().log(
"Successfully set version %s of service %s to %d instances.",
version, service, numInstances);
}
}