|
| 1 | +package com.rundeck.plugin |
| 2 | + |
| 3 | +import com.dtolabs.rundeck.core.execution.workflow.steps.StepException |
| 4 | +import com.dtolabs.rundeck.core.plugins.Plugin |
| 5 | +import com.dtolabs.rundeck.core.plugins.configuration.Describable |
| 6 | +import com.dtolabs.rundeck.core.plugins.configuration.Description |
| 7 | +import com.dtolabs.rundeck.core.plugins.configuration.PropertyUtil |
| 8 | +import com.dtolabs.rundeck.plugins.PluginLogger |
| 9 | +import com.dtolabs.rundeck.plugins.ServiceNameConstants |
| 10 | +import com.dtolabs.rundeck.plugins.descriptions.PluginDescription |
| 11 | +import com.dtolabs.rundeck.plugins.step.PluginStepContext |
| 12 | +import com.dtolabs.rundeck.plugins.step.StepPlugin |
| 13 | +import com.dtolabs.rundeck.plugins.util.DescriptionBuilder |
| 14 | +import com.rundeck.plugin.util.GitPluginUtil |
| 15 | +import groovy.json.JsonOutput |
| 16 | + |
| 17 | + |
| 18 | +@Plugin(name = GitCloneWorkflowStep.PROVIDER_NAME, service = ServiceNameConstants.WorkflowStep) |
| 19 | +@PluginDescription(title = GitCloneWorkflowStep.PROVIDER_TITLE, description = GitCloneWorkflowStep.PROVIDER_DESCRIPTION) |
| 20 | +class GitCloneWorkflowStep implements StepPlugin, Describable{ |
| 21 | + public static final String PROVIDER_NAME = "git-clone-step"; |
| 22 | + public static final String PROVIDER_TITLE = "Git / Clone" |
| 23 | + public static final String PROVIDER_DESCRIPTION ="Clone a Git repository on Rundeck server" |
| 24 | + |
| 25 | + public final static String GIT_URL="gitUrl" |
| 26 | + public final static String GIT_BASE_DIRECTORY="gitBaseDirectory" |
| 27 | + public final static String GIT_BRANCH="gitBranch" |
| 28 | + public final static String GIT_HOSTKEY_CHECKING="strictHostKeyChecking" |
| 29 | + public final static String GIT_KEY_STORAGE="gitKeyPath" |
| 30 | + public final static String GIT_PASSWORD_STORAGE="gitPasswordPath" |
| 31 | + |
| 32 | + |
| 33 | + final static Map<String, Object> renderingOptionsAuthentication = GitPluginUtil.getRenderOpt("Authentication", false) |
| 34 | + final static Map<String, Object> renderingOptionsAuthenticationPassword = GitPluginUtil.getRenderOpt("Authentication",false, false, true) |
| 35 | + final static Map<String, Object> renderingOptionsAuthenticationKey = GitPluginUtil.getRenderOpt("Authentication",false, false, false, true) |
| 36 | + |
| 37 | + final static Map<String, Object> renderingOptionsConfig = GitPluginUtil.getRenderOpt("Configuration",false) |
| 38 | + |
| 39 | + GitManager gitManager |
| 40 | + |
| 41 | + static Description DESCRIPTION = DescriptionBuilder.builder() |
| 42 | + .name(PROVIDER_NAME) |
| 43 | + .title(PROVIDER_TITLE) |
| 44 | + .description(PROVIDER_DESCRIPTION) |
| 45 | + .property( |
| 46 | + PropertyUtil.string(GIT_BASE_DIRECTORY, "Base Directory", "Directory for checkout.", true, |
| 47 | + null, null, null, renderingOptionsConfig)) |
| 48 | + .property(PropertyUtil.string(GIT_URL, "Git URL", '''Checkout url. |
| 49 | + See [git-clone](https://www.kernel.org/pub/software/scm/git/docs/git-clone.html) |
| 50 | + specifically the [GIT URLS](https://www.kernel.org/pub/software/scm/git/docs/git-clone.html#URLS) section. |
| 51 | + Some examples: |
| 52 | + * `ssh://[user@]host.xz[:port]/path/to/repo.git/` |
| 53 | + * `git://host.xz[:port]/path/to/repo.git/` |
| 54 | + * `http[s]://host.xz[:port]/path/to/repo.git/` |
| 55 | + * `ftp[s]://host.xz[:port]/path/to/repo.git/` |
| 56 | + * `rsync://host.xz/path/to/repo.git/`''', true, |
| 57 | + null,null,null, renderingOptionsConfig)) |
| 58 | + .property(PropertyUtil.string(GIT_BRANCH, "Branch", "Checkout branch.", true, |
| 59 | + "master",null,null, renderingOptionsConfig)) |
| 60 | + .property(PropertyUtil.string(GIT_PASSWORD_STORAGE, "Git Password", 'Password to authenticate remotely', false, |
| 61 | + null,null,null, renderingOptionsAuthenticationPassword)) |
| 62 | + .property(PropertyUtil.select(GIT_HOSTKEY_CHECKING, "SSH: Strict Host Key Checking", '''Use strict host key checking. |
| 63 | +If `yes`, require remote host SSH key is defined in the `~/.ssh/known_hosts` file, otherwise do not verify.''', false, |
| 64 | + "yes",GitResourceModelFactory.LIST_HOSTKEY_CHECKING,null, renderingOptionsAuthentication)) |
| 65 | + .property(PropertyUtil.string(GIT_KEY_STORAGE, "SSH Key Path", 'SSH Key Path', false, |
| 66 | + null,null,null, renderingOptionsAuthenticationKey)) |
| 67 | + .build() |
| 68 | + |
| 69 | + GitCloneWorkflowStep() { |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + Description getDescription() { |
| 74 | + return DESCRIPTION |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + void executeStep(final PluginStepContext context, final Map<String, Object> configuration) throws StepException { |
| 79 | + Properties proConfiguration = new Properties() |
| 80 | + proConfiguration.putAll(configuration) |
| 81 | + |
| 82 | + if(gitManager==null){ |
| 83 | + gitManager = new GitManager(proConfiguration) |
| 84 | + } |
| 85 | + |
| 86 | + def localPath=configuration.get(GIT_BASE_DIRECTORY) |
| 87 | + |
| 88 | + if(configuration.get(GIT_PASSWORD_STORAGE)){ |
| 89 | + def password = GitPluginUtil.getFromKeyStorage(configuration.get(GIT_PASSWORD_STORAGE), context) |
| 90 | + gitManager.setGitPassword(password) |
| 91 | + } |
| 92 | + |
| 93 | + if(configuration.get(GIT_KEY_STORAGE)){ |
| 94 | + def key = GitPluginUtil.getFromKeyStorage(configuration.get(GIT_KEY_STORAGE), context) |
| 95 | + gitManager.setSshPrivateKey(key) |
| 96 | + } |
| 97 | + |
| 98 | + PluginLogger logger = context.getLogger() |
| 99 | + logger.log(3, "Cloning Repo ${gitManager.gitURL} to local path ${localPath}") |
| 100 | + |
| 101 | + File base = new File(localPath) |
| 102 | + |
| 103 | + if(!base){ |
| 104 | + base.mkdir() |
| 105 | + } |
| 106 | + |
| 107 | + try{ |
| 108 | + gitManager.cloneOrCreate(base) |
| 109 | + |
| 110 | + def jsonMap = base.listFiles().collect {file -> |
| 111 | + return [name: file.name, directory: file.directory, file: file.file, path: file.absolutePath] |
| 112 | + } |
| 113 | + def json = JsonOutput.toJson(jsonMap) |
| 114 | + logger.log(2, json) |
| 115 | + |
| 116 | + }catch(Exception e){ |
| 117 | + logger.log(0, e.getMessage()) |
| 118 | + throw new StepException("Error ${op} VM.", GitFailureReason.AuthenticationError) |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + } |
| 125 | +} |
0 commit comments