Commits

mwent  committed 75180ed

BDEV-1572 scp task recursive copy not working properly

  • Participants
  • Parent commits 2a272d4

Comments (0)

Files changed (8)

File src/main/java/com/atlassian/bamboo/plugins/scp/BaseSshTaskConfigurator.java

     private static final EnumSet<AuthType> SUPPORTED_AUTH_TYPES = EnumSet.of(AuthType.PASSWORD, AuthType.KEY, AuthType.KEY_WITH_PASSPHRASE);
     public static final int DEFAULT_SSH_PORT_NUMBER = 22;
 
+    public static final String SCP_PLUGIN = "com.atlassian.bamboo.plugins.bamboo-scp-plugin";
+    public static final String SCP_TASK = SCP_PLUGIN + ":scptask";
+    public static final String SSH_TASK = SCP_PLUGIN + ":sshtask";
+
     public static final String HOST = "host";
     public static final String USERNAME = "username";
     public static final String AUTH_TYPE = "authType";

File src/main/java/com/atlassian/bamboo/plugins/scp/ScpTask.java

 import com.atlassian.bamboo.task.TaskState;
 import com.atlassian.bamboo.task.TaskType;
 import com.atlassian.bamboo.utils.FileVisitor;
+import com.google.common.base.Function;
+import com.google.common.base.Joiner;
+import com.google.common.base.Splitter;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
 import net.schmizz.sshj.SSHClient;
 import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
 import org.apache.commons.lang.math.NumberUtils;
 import org.jetbrains.annotations.NotNull;
 
+import javax.annotation.Nullable;
 import java.io.File;
 import java.io.IOException;
 import java.util.Arrays;
     {
         final String localPath = taskContext.getConfigurationMap().get(ScpTaskConfigurator.LOCAL_PATH);
         final String remotePath = taskContext.getConfigurationMap().get(ScpTaskConfigurator.REMOTE_PATH);
+        final boolean useAntPath = Boolean.valueOf(taskContext.getConfigurationMap().get(ScpTaskConfigurator.REMOTE_PATH));
+
+        String adjustedLocalPath = localPath;
+        if (!useAntPath)
+        {
+            adjustedLocalPath = Joiner.on(",").join(Iterables.transform(Splitter.on(",").omitEmptyStrings().trimResults().split(localPath), new Function<String, String>()
+            {
+                @Override
+                public String apply(@Nullable String s)
+                {
+                    if ("*".equals(s) || ("*" + File.separator).equals(s))
+                    {
+                        return "**";
+                    } else {
+                        return s + "/**";
+                    }
+                }
+            }));
+        }
 
         final String baseDirectory = taskContext.getWorkingDirectory().getAbsolutePath();
 
 
         try
         {
-            namesVisitor.visitFilesThatMatch(localPath);
+            namesVisitor.visitFilesThatMatch(adjustedLocalPath);
         }
         catch (InterruptedException e)
         {

File src/main/java/com/atlassian/bamboo/plugins/scp/ScpTaskConfigurator.java

 {
     public static final String REMOTE_PATH = "remotePath";
     public static final String LOCAL_PATH = "localPath";
+    public static final String USE_ANT_PATTERN = "useAntPattern";
 
     public ScpTaskConfigurator(EncryptionService encryptionService)
     {
         final Map<String, String> config = super.generateTaskConfigMap(params, previousTaskDefinition);
         config.put(LOCAL_PATH, params.getString(LOCAL_PATH));
         config.put(REMOTE_PATH, params.getString(REMOTE_PATH));
+        config.put(USE_ANT_PATTERN, params.getString(USE_ANT_PATTERN));
         return config;
     }
 
         super.populateContextForEdit(context, taskDefinition);
         context.put(LOCAL_PATH, taskDefinition.getConfiguration().get(LOCAL_PATH));
         context.put(REMOTE_PATH, taskDefinition.getConfiguration().get(REMOTE_PATH));
+        context.put(USE_ANT_PATTERN, taskDefinition.getConfiguration().get(USE_ANT_PATTERN));
     }
 
     @Override
         super.populateContextForView(context, taskDefinition);
         context.put(LOCAL_PATH, taskDefinition.getConfiguration().get(LOCAL_PATH));
         context.put(REMOTE_PATH, taskDefinition.getConfiguration().get(REMOTE_PATH));
+        context.put(USE_ANT_PATTERN, taskDefinition.getConfiguration().get(USE_ANT_PATTERN));
     }
 
     @Override

File src/main/java/com/atlassian/bamboo/plugins/scp/upgrade/AntPatternUpgradeTask.java

+package com.atlassian.bamboo.plugins.scp.upgrade;
+
+import com.atlassian.bamboo.build.BuildDefinition;
+import com.atlassian.bamboo.build.BuildDefinitionManager;
+import com.atlassian.bamboo.build.Job;
+import com.atlassian.bamboo.plan.PlanManager;
+import com.atlassian.bamboo.plan.TopLevelPlan;
+import com.atlassian.bamboo.plugins.scp.BaseSshTaskConfigurator;
+import com.atlassian.bamboo.plugins.scp.ScpTaskConfigurator;
+import com.atlassian.bamboo.security.EncryptionService;
+import com.atlassian.bamboo.task.TaskDefinition;
+import com.atlassian.sal.api.message.Message;
+import com.atlassian.sal.api.upgrade.PluginUpgradeTask;
+import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+public class AntPatternUpgradeTask implements PluginUpgradeTask
+{
+    private final PlanManager planManager;
+    private final BuildDefinitionManager buildDefinitionManager;
+    private final EncryptionService encryptionService;
+
+    public AntPatternUpgradeTask(PlanManager planManager,
+                                 BuildDefinitionManager buildDefinitionManager,
+                                 EncryptionService encryptionService)
+    {
+        this.planManager = planManager;
+        this.buildDefinitionManager = buildDefinitionManager;
+        this.encryptionService = encryptionService;
+    }
+
+    @Override
+    public int getBuildNumber()
+    {
+        return 2;
+    }
+
+    @Override
+    public String getShortDescription()
+    {
+        return "Encrypt stored passwords for SCP and SSH tasks";
+    }
+
+    @Override
+    public Collection<Message> doUpgrade() throws Exception
+    {
+        Collection<Message> messages = Lists.newArrayList();
+        // no authentication context available when upgrade task is executed, so unrestricted call
+        List<TopLevelPlan> plans = planManager.getAllPlansUnrestricted();
+        for (TopLevelPlan plan : plans)
+        {
+            for (Job job : plan.getAllJobs())
+            {
+                BuildDefinition buildDefinition = job.getBuildDefinition();
+                List<TaskDefinition> taskDefinitions = buildDefinition.getTaskDefinitions();
+                for (TaskDefinition taskDefinition : taskDefinitions)
+                {
+                    if (taskDefinition.getPluginKey().equals(ScpTaskConfigurator.SCP_TASK))
+                    {
+                        Map<String, String> taskConfig = taskDefinition.getConfiguration();
+                        if (taskConfig.get(ScpTaskConfigurator.USE_ANT_PATTERN) == null)
+                        {
+                            taskConfig.put(ScpTaskConfigurator.USE_ANT_PATTERN, "true");
+                            taskDefinition.setConfiguration(taskConfig);
+                        }
+                    }
+                }
+                buildDefinitionManager.savePlanAndDefinition(job, buildDefinition);
+            }
+        }
+        return messages;
+    }
+
+    @Override
+    public String getPluginKey()
+    {
+        return ScpTaskConfigurator.SCP_PLUGIN;
+    }
+}

File src/main/java/com/atlassian/bamboo/plugins/scp/upgrade/PasswordUpgradeTask.java

 import com.atlassian.bamboo.plan.PlanManager;
 import com.atlassian.bamboo.plan.TopLevelPlan;
 import com.atlassian.bamboo.plugins.scp.BaseSshTaskConfigurator;
+import com.atlassian.bamboo.plugins.scp.ScpTaskConfigurator;
 import com.atlassian.bamboo.security.EncryptionService;
 import com.atlassian.bamboo.task.TaskDefinition;
 import com.atlassian.sal.api.message.Message;
     private final BuildDefinitionManager buildDefinitionManager;
     private final EncryptionService encryptionService;
 
-    public static final String SCP_PLUGIN = "com.atlassian.bamboo.plugins.bamboo-scp-plugin";
-    public static final String SCP_TASK = SCP_PLUGIN + ":scptask";
-    public static final String SSH_TASK = SCP_PLUGIN + ":sshtask";
-
     public PasswordUpgradeTask(PlanManager planManager,
                                BuildDefinitionManager buildDefinitionManager,
                                EncryptionService encryptionService)
                 List<TaskDefinition> taskDefinitions = buildDefinition.getTaskDefinitions();
                 for (TaskDefinition taskDefinition : taskDefinitions)
                 {
-                    if (taskDefinition.getPluginKey().equals(SCP_TASK) || taskDefinition.getPluginKey().equals(SSH_TASK))
+                    if (taskDefinition.getPluginKey().equals(ScpTaskConfigurator.SCP_TASK) || taskDefinition.getPluginKey().equals(ScpTaskConfigurator.SSH_TASK))
                     {
                         Map<String, String> taskConfig = taskDefinition.getConfiguration();
                         encryptPassword(taskConfig);
     @Override
     public String getPluginKey()
     {
-        return SCP_PLUGIN;
+        return ScpTaskConfigurator.SCP_PLUGIN;
     }
 }

File src/main/resources/atlassian-plugin.xml

     <component key="passwordUpgradeTask" name="Password Encryption Upgrade Task" class="com.atlassian.bamboo.plugins.scp.upgrade.PasswordUpgradeTask" public="true">
       <interface>com.atlassian.sal.api.upgrade.PluginUpgradeTask</interface>
     </component>
+    <component key="antPatternUpgradeTask" name="Ant Pattern Upgrade Task" class="com.atlassian.bamboo.plugins.scp.upgrade.AntPatternUpgradeTask" public="true">
+        <interface>com.atlassian.sal.api.upgrade.PluginUpgradeTask</interface>
+    </component>
 </atlassian-plugin>

File src/main/resources/com/atlassian/bamboo/plugins/scp/editScpTask.ftl

 [/@ui.bambooSection]
 
 [@ww.textfield labelKey="scp.task.local.path" name="localPath" required="true"/]
-[@ww.textfield labelKey="scp.task.remote.path" name="remotePath" required="true"/]
+[@ww.checkbox labelKey="scp.task.use.ant.glob.patterns" name="useAntPattern" /]
+[@ww.textfield labelKey="scp.task.remote.path" name="remotePath" required="true"/]
+

File src/main/resources/com/atlassian/bamboo/plugins/scp/i18n.properties

 scp.task.passphrase.change = Change Passphrase
 
 scp.task.local.path = Local Path
-scp.task.local.path.description = Location of files to copy to remote host <br /> This is a comma separated list of files or directories.<br />You can also use ant style patterns such as **/target/*.jar.
+scp.task.local.path.description = Location of files to copy to remote host <br /> This is a comma separated list of files or directories.
 scp.task.remote.path = Remote Path
 scp.task.remote.path.description =
+scp.task.use.ant.glob.patterns = Use Ant glob patterns to select files
+scp.task.use.ant.glob.patterns.description = Use Ant glob patterns to select files. See the <a href="http://ant.apache.org/manual/dirtasks.html" target="_blank">Ant glob pattern</a> reference.
+
 
 ssh.task.command = SSH command
 ssh.task.command.description  = Shell command to execute on the remote host