-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NIFI-13008 - CLI command to upgrade all instances of a versioned flow #8611
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...i/src/main/java/org/apache/nifi/toolkit/cli/impl/command/nifi/pg/PGChangeAllVersions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.nifi.toolkit.cli.impl.command.nifi.pg; | ||
|
||
import org.apache.commons.cli.MissingOptionException; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.nifi.toolkit.cli.api.CommandException; | ||
import org.apache.nifi.toolkit.cli.api.Context; | ||
import org.apache.nifi.toolkit.cli.impl.client.nifi.FlowClient; | ||
import org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient; | ||
import org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException; | ||
import org.apache.nifi.toolkit.cli.impl.command.CommandOption; | ||
import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand; | ||
import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsResult; | ||
import org.apache.nifi.toolkit.cli.impl.result.nifi.ProcessGroupsVersionChangeResult; | ||
import org.apache.nifi.toolkit.cli.impl.result.nifi.ChangeVersionResult; | ||
import org.apache.nifi.web.api.dto.ProcessGroupDTO; | ||
import org.apache.nifi.web.api.entity.VersionControlInformationEntity; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Command to change the version of a version controlled process group. | ||
*/ | ||
public class PGChangeAllVersions extends AbstractNiFiCommand<ProcessGroupsVersionChangeResult> { | ||
|
||
public PGChangeAllVersions() { | ||
super("pg-change-all-versions", ProcessGroupsVersionChangeResult.class); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Changes the version for all of the controlled process group instances for a given flow ID. " | ||
+ "This can be used to upgrade all the instances of a versioned flow to a new version, or " | ||
+ "revert to a previous version. If no version is specified, the latest version will be used. " | ||
+ "If no process group ID is provided, the root process group will be used to recursively " | ||
+ "search for all instances of the Flow ID. It is possible to force the recursive operation " | ||
+ "and not stop the operation in case the upgrade of a process group fails."; | ||
} | ||
|
||
@Override | ||
protected void doInitialize(final Context context) { | ||
addOption(CommandOption.FLOW_ID.createOption()); | ||
addOption(CommandOption.FLOW_VERSION.createOption()); | ||
addOption(CommandOption.PG_ID.createOption()); | ||
addOption(CommandOption.FORCE.createOption()); | ||
} | ||
|
||
@Override | ||
public ProcessGroupsVersionChangeResult doExecute(final NiFiClient client, final Properties properties) | ||
throws NiFiClientException, IOException, MissingOptionException, CommandException { | ||
|
||
final FlowClient flowClient = client.getFlowClient(); | ||
final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID); | ||
|
||
// get the optional id of the parent PG, otherwise fallback to the root group | ||
String parentPgId = getArg(properties, CommandOption.PG_ID); | ||
if (StringUtils.isBlank(parentPgId)) { | ||
parentPgId = flowClient.getRootGroupId(); | ||
} | ||
|
||
final PGList doPGList = new PGList(); | ||
final List<ProcessGroupDTO> pgList = new ArrayList<ProcessGroupDTO>(); | ||
recursivePGList(pgList, doPGList, client, properties, parentPgId); | ||
|
||
final PGChangeVersion doPGChangeVersion = new PGChangeVersion(); | ||
|
||
// new version, if specified in the arguments | ||
String newVersion = getArg(properties, CommandOption.FLOW_VERSION); | ||
|
||
// force operation, if specified in the arguments | ||
final boolean forceOperation = properties.containsKey(CommandOption.FORCE.getLongName()); | ||
|
||
final List<ProcessGroupDTO> processGroups = new ArrayList<>(); | ||
final Map<String, ChangeVersionResult> changeVersionResults = new HashMap<String, ChangeVersionResult>(); | ||
|
||
for (final ProcessGroupDTO pgDTO : pgList) { | ||
final VersionControlInformationEntity entity = client.getVersionsClient().getVersionControlInfo(pgDTO.getId()); | ||
|
||
if(entity.getVersionControlInformation() == null || !entity.getVersionControlInformation().getFlowId().equals(flowId)) { | ||
continue; // the process group is not version controlled or does not match the provided Flow ID | ||
} | ||
|
||
if(newVersion == null) { | ||
newVersion = doPGChangeVersion.getLatestVersion(client, entity.getVersionControlInformation()); | ||
} | ||
|
||
processGroups.add(pgDTO); | ||
|
||
final String previousVersion = pgDTO.getVersionControlInformation().getVersion(); | ||
if (previousVersion.equals(newVersion)) { | ||
changeVersionResults.put(pgDTO.getId(), new ChangeVersionResult(newVersion, newVersion, "Process group already at desired version")); | ||
continue; // Process group already at desired version | ||
} | ||
|
||
try { | ||
doPGChangeVersion.changeVersion(client, entity, newVersion, pgDTO.getId(), getContext()); | ||
changeVersionResults.put(pgDTO.getId(), new ChangeVersionResult(previousVersion, newVersion, "SUCCESS")); | ||
} catch (Exception e) { | ||
changeVersionResults.put(pgDTO.getId(), new ChangeVersionResult(previousVersion, null, e.getMessage())); | ||
if (forceOperation) { | ||
continue; | ||
} else { | ||
e.printStackTrace(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return new ProcessGroupsVersionChangeResult(getResultType(properties), processGroups, changeVersionResults); | ||
} | ||
|
||
private void recursivePGList(final List<ProcessGroupDTO> pgList, final PGList doPGList, final NiFiClient client, | ||
final Properties properties, final String pgId) throws NiFiClientException, IOException { | ||
final ProcessGroupsResult result = doPGList.getList(client, properties, pgId); | ||
for(ProcessGroupDTO pgDTO : result.getResult()) { | ||
pgList.add(pgDTO); | ||
recursivePGList(pgList, doPGList, client, properties, pgDTO.getId()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...t-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/result/nifi/ChangeVersionResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.nifi.toolkit.cli.impl.result.nifi; | ||
|
||
/** | ||
* Object to help with the result of a change version operation | ||
*/ | ||
public class ChangeVersionResult { | ||
String previousVersion; | ||
String newVersion; | ||
String message; | ||
|
||
public ChangeVersionResult(final String previousVersion, final String newVersion, final String message) { | ||
this.previousVersion = previousVersion; | ||
this.newVersion = newVersion; | ||
this.message = message; | ||
} | ||
|
||
public String getPreviousVersion() { | ||
return previousVersion; | ||
} | ||
|
||
public String getNewVersion() { | ||
return newVersion; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...n/java/org/apache/nifi/toolkit/cli/impl/result/nifi/ProcessGroupsVersionChangeResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.nifi.toolkit.cli.impl.result.nifi; | ||
|
||
import org.apache.nifi.toolkit.cli.api.Context; | ||
import org.apache.nifi.toolkit.cli.api.ReferenceResolver; | ||
import org.apache.nifi.toolkit.cli.api.Referenceable; | ||
import org.apache.nifi.toolkit.cli.api.ResolvedReference; | ||
import org.apache.nifi.toolkit.cli.api.ResultType; | ||
import org.apache.nifi.toolkit.cli.impl.command.CommandOption; | ||
import org.apache.nifi.toolkit.cli.impl.result.AbstractWritableResult; | ||
import org.apache.nifi.toolkit.cli.impl.result.writer.DynamicTableWriter; | ||
import org.apache.nifi.toolkit.cli.impl.result.writer.Table; | ||
import org.apache.nifi.toolkit.cli.impl.result.writer.TableWriter; | ||
import org.apache.nifi.web.api.dto.ProcessGroupDTO; | ||
|
||
import java.io.PrintStream; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* Result for a list of ProcessGroupEntities. | ||
*/ | ||
public class ProcessGroupsVersionChangeResult extends AbstractWritableResult<List<ProcessGroupDTO>> implements Referenceable { | ||
|
||
private final List<ProcessGroupDTO> processGroups; | ||
private final Map<String, ChangeVersionResult> changeVersionResults; | ||
|
||
public ProcessGroupsVersionChangeResult(final ResultType resultType, final List<ProcessGroupDTO> processGroups, | ||
final Map<String, ChangeVersionResult> changeVersionResults) { | ||
super(resultType); | ||
this.processGroups = Objects.requireNonNull(processGroups); | ||
this.processGroups.sort(Comparator.comparing(ProcessGroupDTO::getName)); | ||
this.changeVersionResults = Objects.requireNonNull(changeVersionResults); | ||
} | ||
|
||
@Override | ||
public List<ProcessGroupDTO> getResult() { | ||
return processGroups; | ||
} | ||
|
||
@Override | ||
protected void writeSimpleResult(final PrintStream output) { | ||
|
||
final Table table = new Table.Builder() | ||
.column("#", 3, 3, false) | ||
.column("Name", 20, 36, true) | ||
.column("Id", 36, 36, false) | ||
.column("Prev Version", 15, 15, false) | ||
.column("New Version", 15, 15, false) | ||
.column("Message", 100, 100, false) | ||
.build(); | ||
|
||
for (int i=0; i < processGroups.size(); i++) { | ||
final ProcessGroupDTO dto = processGroups.get(i); | ||
table.addRow( | ||
String.valueOf(i+1), | ||
dto.getName(), | ||
dto.getId(), | ||
String.valueOf(changeVersionResults.get(dto.getId()).getPreviousVersion()), | ||
String.valueOf(changeVersionResults.get(dto.getId()).getNewVersion()), | ||
String.valueOf(changeVersionResults.get(dto.getId()).getMessage()) | ||
); | ||
} | ||
|
||
final TableWriter tableWriter = new DynamicTableWriter(); | ||
tableWriter.write(table, output); | ||
} | ||
|
||
@Override | ||
public ReferenceResolver createReferenceResolver(final Context context) { | ||
final Map<Integer, ProcessGroupDTO> backRefs = new HashMap<>(); | ||
final AtomicInteger position = new AtomicInteger(0); | ||
processGroups.forEach(p -> backRefs.put(position.incrementAndGet(), p)); | ||
|
||
return new ReferenceResolver() { | ||
@Override | ||
public ResolvedReference resolve(final CommandOption option, final Integer position) { | ||
final ProcessGroupDTO pg = backRefs.get(position); | ||
if (pg != null) { | ||
return new ResolvedReference(option, position, pg.getName(), pg.getId()); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return backRefs.isEmpty(); | ||
} | ||
}; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new command should be added to the toolkit guide as well