forked from rundeck-plugins/git-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitCommitWorkflowStep.groovy
More file actions
183 lines (150 loc) · 10.2 KB
/
Copy pathGitCommitWorkflowStep.groovy
File metadata and controls
183 lines (150 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.rundeck.plugin
import com.dtolabs.rundeck.core.execution.ExecutionListener
import com.dtolabs.rundeck.core.execution.ExecutionContext
import com.dtolabs.rundeck.core.execution.proxy.ProxyRunnerPlugin
import com.dtolabs.rundeck.core.execution.proxy.ProxySecretBundleCreator
import com.dtolabs.rundeck.core.execution.proxy.SecretBundle
import com.dtolabs.rundeck.core.execution.workflow.steps.StepException
import com.dtolabs.rundeck.core.plugins.Plugin
import com.dtolabs.rundeck.core.plugins.configuration.Describable
import com.dtolabs.rundeck.core.plugins.configuration.Description
import com.dtolabs.rundeck.core.plugins.configuration.PropertyUtil
import com.dtolabs.rundeck.plugins.ServiceNameConstants
import com.dtolabs.rundeck.core.plugins.configuration.PropertyScope
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription
import com.dtolabs.rundeck.plugins.step.PluginStepContext
import com.dtolabs.rundeck.plugins.step.StepPlugin
import com.dtolabs.rundeck.plugins.util.DescriptionBuilder
import com.rundeck.plugin.util.GitPluginUtil
import java.nio.file.Path
import java.nio.file.Paths
import groovy.json.JsonOutput
@Plugin(name = GitCommitWorkflowStep.PROVIDER_NAME, service = ServiceNameConstants.WorkflowStep)
@PluginDescription(title = GitCommitWorkflowStep.PROVIDER_TITLE, description = GitCommitWorkflowStep.PROVIDER_DESCRIPTION)
class GitCommitWorkflowStep implements StepPlugin, Describable, ProxyRunnerPlugin, ProxySecretBundleCreator{
public static final String PROVIDER_NAME = "git-commit-step"
public static final String PROVIDER_TITLE = "Git / Commit"
public static final String PROVIDER_DESCRIPTION ="Commit a Git repository on Rundeck server"
public final static String GIT_URL="gitUrl"
public final static String GIT_BASE_DIRECTORY="gitBaseDirectory"
public final static String GIT_LOG_DISABLE ="gitLogDisable"
public final static String GIT_MESSAGE="gitMessage"
public final static String GIT_ADD_ENABLE ="gitAddEnable"
public final static String GIT_PUSH_ENABLE ="gitPushEnable"
public final static String GIT_HOSTKEY_CHECKING="strictHostKeyChecking"
public final static String GIT_KEY_STORAGE="gitKeyPath"
public final static String GIT_PASSWORD_STORAGE="gitPasswordPath"
public final static String GIT_PROJECT_BASED_SUBDIRECTORY="gitUseProjectBasedSubdirectory"
public final static String GIT_REPO_NAME_SUBDIRECTORY="gitUseRepoNameSubdirectory"
final static Map<String, Object> renderingOptionsAuthentication = GitPluginUtil.getRenderOpt("Authentication", false)
final static Map<String, Object> renderingOptionsAuthenticationPassword = GitPluginUtil.getRenderOpt("Authentication",false, false, true)
final static Map<String, Object> renderingOptionsAuthenticationKey = GitPluginUtil.getRenderOpt("Authentication",false, false, false, true)
final static Map<String, Object> renderingOptionsConfig = GitPluginUtil.getRenderOpt("Configuration",false)
GitManager gitManager
static Description DESCRIPTION = DescriptionBuilder.builder()
.name(PROVIDER_NAME)
.title(PROVIDER_TITLE)
.description(PROVIDER_DESCRIPTION)
.property(
PropertyUtil.string(GIT_BASE_DIRECTORY, "Base Directory", "Directory to commit.", true,
null, null, null, renderingOptionsConfig))
.property(PropertyUtil.string(GIT_URL, "Git URL", '''Checkout url.
See [git-commit](https://www.kernel.org/pub/software/scm/git/docs/git-commit.html)
specifically the [GIT URLS](https://www.kernel.org/pub/software/scm/git/docs/git-push.html#URLS) section.
Some examples:
* `ssh://[user@]host.xz[:port]/path/to/repo.git/`
* `git://host.xz[:port]/path/to/repo.git/`
* `http[s]://host.xz[:port]/path/to/repo.git/`
* `ftp[s]://host.xz[:port]/path/to/repo.git/`
* `rsync://host.xz/path/to/repo.git/`''', true,
null,null,null, renderingOptionsConfig))
.property(PropertyUtil.string(GIT_MESSAGE, "Message", "Commit Message.", true,
"Rundeck Commit",null,null, renderingOptionsConfig))
.property(PropertyUtil.bool(GIT_ADD_ENABLE, "Add", "Enabling this flag will add all new files and changes to the commit before commiting", true,
"false",null, renderingOptionsConfig))
.property(PropertyUtil.bool(GIT_PUSH_ENABLE, "Push Commit", "Enabling this flag will push the commit upstream", true,
"false",null, renderingOptionsConfig))
.property(PropertyUtil.bool(GIT_LOG_DISABLE, "Disable log output", "Enabling this flag, the plugin will not show the output log", true,
"false",null, renderingOptionsConfig))
.property(PropertyUtil.string(GIT_PASSWORD_STORAGE, "Git Password", 'Password to authenticate remotely', false,
null,null,null, renderingOptionsAuthenticationPassword))
.property(PropertyUtil.select(GIT_HOSTKEY_CHECKING, "SSH: Strict Host Key Checking", '''Use strict host key checking.
If `yes`, require remote host SSH key is defined in the `~/.ssh/known_hosts` file, otherwise do not verify.''', false,
"yes",GitResourceModelFactory.LIST_HOSTKEY_CHECKING,null, renderingOptionsAuthentication))
.property(PropertyUtil.string(GIT_KEY_STORAGE, "SSH Key Path", 'SSH Key Path', false,
null,null,null, renderingOptionsAuthenticationKey))
.property(PropertyUtil.bool(GIT_PROJECT_BASED_SUBDIRECTORY, "Use per-project subdirectories", "Check repositories out in project-based subdirectories of the Rundeck home directory.",
false, "false", PropertyScope.Project, renderingOptionsConfig))
.property(PropertyUtil.bool(GIT_REPO_NAME_SUBDIRECTORY, "Clone into repo name subdirectory", "Use a subdirectory named after the Git repository under the base directory.",
false, "false", null, renderingOptionsConfig))
.build()
GitCommitWorkflowStep() {
}
@Override
Description getDescription() {
return DESCRIPTION
}
@Override
void executeStep(final PluginStepContext context, final Map<String, Object> configuration) throws StepException {
Properties proConfiguration = new Properties()
proConfiguration.putAll(configuration)
if(gitManager==null){
gitManager = new GitManager(proConfiguration)
}
String localPath
if (Boolean.parseBoolean((String) configuration.get(GIT_PROJECT_BASED_SUBDIRECTORY))) {
String configPath = configuration.get(GIT_BASE_DIRECTORY)
String project = context.getFrameworkProject()
Path localPath_p = Paths.get(project, configPath)
localPath = localPath_p.toString()
} else {
localPath = configuration.get(GIT_BASE_DIRECTORY)
}
if (Boolean.parseBoolean((String) configuration.get(GIT_REPO_NAME_SUBDIRECTORY))) {
String repoName = GitPluginUtil.extractRepoName((String) configuration.get(GIT_URL))
if (repoName) {
localPath = new File(localPath, repoName).getPath()
}
}
if(configuration.get(GIT_PASSWORD_STORAGE)){
def password = GitPluginUtil.getFromKeyStorage(configuration.get(GIT_PASSWORD_STORAGE), context)
gitManager.setGitPassword(password)
}
if(configuration.get(GIT_KEY_STORAGE)){
def key = GitPluginUtil.getFromKeyStorage(configuration.get(GIT_KEY_STORAGE), context)
gitManager.setSshPrivateKey(key)
}
ExecutionListener logger = context.getExecutionContext().getExecutionListener()
logger.log(3, "Committing to repo ${gitManager.gitURL} from local path ${localPath}")
File base = new File(localPath)
Map<String, String> meta = new HashMap<>();
meta.put("content-data-type", "application/json");
try{
if (Boolean.parseBoolean((String) configuration.get(GIT_ADD_ENABLE))) {
gitManager.add(base, ".")
}
gitManager.commit(base, configuration.get(GIT_MESSAGE))
if (Boolean.parseBoolean((String) configuration.get(GIT_PUSH_ENABLE))) {
gitManager.push(base)
}
if (!Boolean.parseBoolean((String) configuration.get(GIT_LOG_DISABLE))) {
def jsonMap = base.listFiles().collect { file ->
return [name: file.name, directory: file.directory, file: file.file, path: file.absolutePath]
}
def json = JsonOutput.toJson(jsonMap)
logger.log(2, json, meta)
}
}catch(Exception e){
logger.log(0, e.getMessage())
throw new StepException("Error ${op} VM.", GitFailureReason.AuthenticationError)
}
}
@Override
List<String> listSecretsPathWorkflowStep(ExecutionContext context, Map<String, Object> configuration) {
return GitPluginUtil.listSecretsPathForStep(context, configuration, GIT_PASSWORD_STORAGE, GIT_KEY_STORAGE)
}
@Override
SecretBundle prepareSecretBundleWorkflowStep(ExecutionContext context, Map<String, Object> configuration) {
return GitPluginUtil.prepareSecretBundleForStep(context, configuration, GIT_PASSWORD_STORAGE, GIT_KEY_STORAGE)
}
}