Skip to content

Instantly share code, notes, and snippets.

@scottpurdy
Created August 24, 2016 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottpurdy/0f8701a5f2a31000d302f0a405ce1465 to your computer and use it in GitHub Desktop.
Save scottpurdy/0f8701a5f2a31000d302f0a405ce1465 to your computer and use it in GitHub Desktop.
/*
* Numenta Platform for Intelligent Computing (NuPIC)
* Copyright (C) 2016, Numenta, Inc. Unless you have purchased from
* Numenta, Inc. a separate commercial license for this software code, the
* following terms and conditions apply:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero Public License for more details.
*
* You should have received a copy of the GNU Affero Public License
* along with this program. If not, see http://www.gnu.org/licenses.
*
* http://numenta.org/licenses/
*/
package com.numenta.bamboo.tasks;
import com.atlassian.bamboo.collections.ActionParametersMap;
import com.atlassian.bamboo.configuration.AdministrationConfigurationAccessor;
import com.atlassian.bamboo.credentials.ConfigurableSharedCredentialDepender;
import com.atlassian.bamboo.credentials.CredentialsAccessor;
import com.atlassian.bamboo.credentials.CredentialsData;
import com.atlassian.bamboo.task.AbstractTaskConfigurator;
import com.atlassian.bamboo.task.TaskDefinition;
import com.atlassian.bamboo.task.TaskRequirementSupport;
import com.atlassian.bamboo.utils.error.ErrorCollection;
import com.atlassian.bamboo.v2.build.agent.capability.Requirement;
import com.atlassian.bamboo.v2.build.agent.capability.RequirementImpl;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map;
import java.util.Set;
/**
* Bamboo deployment Task for publishing Python wheels.
* Create a Task in the Bamboo plugin that implements DeploymentTaskType (which is for deploy-only tasks) and uses shared credentials to publish a specified list of files from the release.
*/
public class PythonDeployTaskConfigurator extends AbstractTaskConfigurator implements ConfigurableSharedCredentialDepender, TaskRequirementSupport {
@Autowired
private CredentialsAccessor credentialsAccessor;
private static final String CFG_AVAILABLE_CREDENTIALS = "availableCredentials";
@Autowired
private AdministrationConfigurationAccessor administrationConfigurationAccessor;
@NotNull
@Override
public Map<String, String> generateTaskConfigMap(@NotNull ActionParametersMap params, @Nullable TaskDefinition previousTaskDefinition) {
final Map<String, String> config = super.generateTaskConfigMap(params, previousTaskDefinition);
config.put(NumentaConstants.CFG_PYPI_CREDENTIALS_ID, params.getString(NumentaConstants.CFG_PYPI_CREDENTIALS_ID));
config.put(NumentaConstants.CFG_PYTHON_DEPLOY_PACKAGE_PATTERN, params.getString(NumentaConstants.CFG_PYTHON_DEPLOY_PACKAGE_PATTERN));
config.put(NumentaConstants.CFG_PYTHON_DEPLOY_ALLOW_MULTIPLE, String.valueOf(params.getBoolean(NumentaConstants.CFG_PYTHON_DEPLOY_ALLOW_MULTIPLE)));
return config;
}
@Override
public void populateContextForCreate(@NotNull Map<String, Object> context) {
super.populateContextForCreate(context);
populateCredentials(context);
context.put(NumentaConstants.CFG_PYTHON_DEPLOY_PACKAGE_PATTERN, NumentaConstants.PYTHON_DEPLOY_PATTERN_DEFAULT);
context.put(NumentaConstants.CFG_PYTHON_DEPLOY_ALLOW_MULTIPLE, NumentaConstants.PYTHON_DEPLOY_ALLOW_MULTIPLE_DEFAULT);
}
@Override
public void populateContextForEdit(@NotNull Map<String, Object> context, @NotNull TaskDefinition taskDefinition) {
super.populateContextForEdit(context, taskDefinition);
populateCredentials(context);
// Set to default in case where no value has been set.
context.put(NumentaConstants.CFG_PYTHON_DEPLOY_ALLOW_MULTIPLE, NumentaConstants.PYTHON_DEPLOY_ALLOW_MULTIPLE_DEFAULT);
context.putAll(taskDefinition.getConfiguration());
}
@Override
public void validate(@NotNull ActionParametersMap params, @NotNull ErrorCollection errorCollection) {
super.validate(params, errorCollection);
// Validate credential
long credentialsId = params.getLong(NumentaConstants.CFG_PYPI_CREDENTIALS_ID, -1L);
CredentialsData credentialsData = this.credentialsAccessor.getCredentials(credentialsId);
if (credentialsData == null) {
errorCollection.addError(NumentaConstants.CFG_PYPI_CREDENTIALS_ID,
getI18nBean().getText("pypi.credentials.error.notFound", new String[]{Long.toString(credentialsId)}));
}
// Validate artifact file
String packageFilePattern = params.getString(NumentaConstants.CFG_PYTHON_DEPLOY_PACKAGE_PATTERN);
if (StringUtils.isBlank(packageFilePattern)) {
errorCollection.addError(NumentaConstants.CFG_PYTHON_DEPLOY_PACKAGE_PATTERN, getI18nBean().getText("pythonDeploy.task.pattern.error.invalid"));
}
// Nothing to verify with allow-multiple checkbox. If it isn't checked then it won't be present in POST data.
}
@NotNull
@Override
public Iterable<Long> getSharedCredentialIds(@NotNull Map<String, String> config) {
return ImmutableList.of(Long.valueOf(config.get(NumentaConstants.CFG_PYPI_CREDENTIALS_ID)));
}
private void populateCredentials(Map<String, Object> context) {
Iterable availableCredentials = this.credentialsAccessor.getAllCredentials(NumentaConstants.PYPI_CREDENTIALS_TYPE_KEY);
context.put(CFG_AVAILABLE_CREDENTIALS, Lists.newArrayList(availableCredentials));
context.put(NumentaConstants.CFG_BASE_URL, this.administrationConfigurationAccessor.getAdministrationConfiguration().getBaseUrl());
}
@Override
public Set<Requirement> calculateRequirements(@NotNull TaskDefinition taskDefinition) {
// Requires 'twine' capability
return ImmutableSet.of(new RequirementImpl(NumentaConstants.TWINE_CAPABILITY_KEY, true, ".*"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment