-
Notifications
You must be signed in to change notification settings - Fork 3
/
AnsibleCommandServiceImpl.java
289 lines (265 loc) · 12.8 KB
/
AnsibleCommandServiceImpl.java
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.ansible.southbound;
import static org.opendaylight.ansible.mdsalutils.Datastore.OPERATIONAL;
import ch.vorburger.exec.ManagedProcess;
import ch.vorburger.exec.ManagedProcessBuilder;
import ch.vorburger.exec.ManagedProcessException;
import com.google.common.base.Optional;
import com.google.common.util.concurrent.ListenableFuture;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.opendaylight.ansible.mdsalutils.RetryingManagedNewTransactionRunner;
import org.opendaylight.ansible.mdsalutils.SingleTransactionDataBroker;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.AnsibleCommandService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.Commands;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.RunAnsibleCommandInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.RunAnsibleCommandOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.RunAnsibleCommandOutputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.Status;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.commands.Command;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.commands.CommandBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.commands.CommandKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.run.ansible.command.input.command.type.Playbook;
import org.opendaylight.yang.gen.v1.urn.opendaylight.ansible.command.rev180821.run.ansible.command.input.command.type.Role;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.ops4j.pax.cdi.api.OsgiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class AnsibleCommandServiceImpl implements AnsibleCommandService {
private static final Logger LOG = LoggerFactory.getLogger(AnsibleCommandServiceImpl.class);
private Map<Uuid, ManagedProcess> processMap = new ConcurrentHashMap<>();
private final DataBroker dataBroker;
private static final String ANSIBLEVAR = "ANSIBLEVAR";
private static final String ROLEVAR = "ROLEVAR";
private static final Map<String, String> VAR_MAP = new HashMap<String, String>() {
{
put(ANSIBLEVAR, "--cmdline");
put(ROLEVAR, "--role-vars");
}
};
private final RetryingManagedNewTransactionRunner txRunner;
private static final String DEFAULT_PRIVATE_DIR = "/usr/share/opendaylight-ansible";
@Inject
public AnsibleCommandServiceImpl(@OsgiService final DataBroker dataBroker) {
this.dataBroker = dataBroker;
txRunner = new RetryingManagedNewTransactionRunner(dataBroker, 3);
}
@Override
public ListenableFuture<RpcResult<RunAnsibleCommandOutput>> runAnsibleCommand(RunAnsibleCommandInput input) {
Uuid uuid;
Status status;
String failedEvent;
try {
if (input.getCommandType() instanceof Playbook) {
uuid = runAnsiblePlaybook(input.getHost(), input.getDirectory(),
((Playbook) input.getCommandType()).getFile(),
input.getAnsibleVars());
} else {
// Must be instance of Role
uuid = runAnsibleRole(input.getHost(), input.getDirectory(),
((Role) input.getCommandType()).getRoleName(),
((Role) input.getCommandType()).getRoleVars(),
input.getAnsibleVars());
}
status = Status.InProgress;
failedEvent = null;
initCommandStatus(uuid);
} catch (ManagedProcessException | IOException e) {
status = Status.Failed;
uuid = null;
failedEvent = e.getMessage();
}
return RpcResultBuilder.success(new RunAnsibleCommandOutputBuilder().setStatus(status).setUuid(uuid)
.setFailedEvent(failedEvent).build()).buildFuture();
}
public void initCommandStatus(Uuid uuid) {
updateAnsibleResult(Status.InProgress, null, uuid);
}
public Uuid runAnsibleRole(String host, String dir, String role, List<String> roleVars, List<String> ansibleVars)
throws ManagedProcessException, IOException {
if (dir == null) {
LOG.info("No directory provided, using default: {}", DEFAULT_PRIVATE_DIR);
dir = DEFAULT_PRIVATE_DIR;
}
// Ensure private directory is created
// Ensure blank inventory exists (workaround to avoid ansible warning)
File directory = new File(new File(dir), "inventory");
if (! directory.exists()) {
boolean dirCreated = directory.mkdirs();
if (!dirCreated) {
throw new IOException("Unable to create Ansible private directory and inventory subdir: " + dir);
}
}
File hostFile = new File(directory, "hosts.yaml");
if (!hostFile.exists()) {
if (!hostFile.createNewFile()) {
LOG.warn("Unable to create host inventory file in private directory: {}", hostFile);
}
}
ManagedProcessBuilder ar = new ManagedProcessBuilder("ansible-runner").addArgument("-j")
.addArgument("-r").addArgument(role);
ar = injectVars(ar, ROLEVAR, roleVars, null);
ar = injectVars(ar, ANSIBLEVAR, ansibleVars, host);
ar.addArgument("run").addArgument(dir);
ar.getEnvironment().put("ANSIBLE_CLICONF_PLUGINS", Paths.get(dir,
"project/roles/ansible-network.network-engine/plugins/cliconf").toString());
return runAnsible(ar);
}
private Uuid runAnsiblePlaybook(String host, String dir, String file, List<String> ansibleVars)
throws ManagedProcessException {
ManagedProcessBuilder ar = new ManagedProcessBuilder("ansible-runner").addArgument("-j")
.addArgument("-p").addArgument(file);
ar = injectVars(ar, ANSIBLEVAR, ansibleVars, host);
ar.addArgument("run").addArgument(dir);
ar.getEnvironment().put("ANSIBLE_CLICONF_PLUGINS", Paths.get(dir,
"project/roles/ansible-network.network-engine/plugins/cliconf").toString());
return runAnsible(ar);
}
private Uuid runAnsible(ManagedProcessBuilder builder) {
Uuid uuid = new Uuid(UUID.randomUUID().toString());
LOG.info("Executing Ansible, new uuid for command is: {}", uuid);
builder.setProcessListener(new AnsibleProcessListener(this, uuid));
ManagedProcess mp = builder.build();
processMap.put(uuid, mp);
LOG.info("Starting Ansible process");
try {
mp.start();
LOG.info("Ansible Process is alive: {}", Boolean.toString(mp.isAlive()));
} catch (ManagedProcessException e) {
LOG.warn("Process exited with error code: {}", mp.getProcLongName());
}
return uuid;
}
private ManagedProcessBuilder injectVars(ManagedProcessBuilder builder, String varType, List<String> varList,
String host) {
if (varList != null && ! varList.isEmpty()) {
if (VAR_MAP.get(varType) == null) {
LOG.warn("Unable to determine variable types to add to ansible command {}", varType);
} else {
builder.addArgument(VAR_MAP.get(varType));
if (varType.equals(ANSIBLEVAR)) {
builder.addArgument("'-e" + " " + String.join(" ", varList) + "' -i " + host + ",",
false);
} else {
builder.addArgument(String.join(" ", varList), false);
}
}
}
return builder;
}
public void parseAnsibleResult(Uuid uuid) throws AnsibleCommandException {
ManagedProcess mp = getProcess(uuid);
if (mp == null) {
throw new AnsibleCommandException("Unable to find process for uuid" + uuid.toString());
}
parseAnsibleResult(mp, uuid);
}
public void parseAnsibleResult(ManagedProcess mp, Uuid uuid) {
String output = mp.getConsole();
Status result;
String failedEventOutput = null;
LOG.info("Ansible process complete: {}", output);
try {
LOG.info("Parsing json string into Event List");
AnsibleEventList el = new AnsibleEventList(parseAnsibleOutput(output));
AnsibleEvent lastEvent = el.getLastEvent();
LOG.info("Stdout of last event is {}", lastEvent.getStdout());
if (el.ansiblePassed()) {
LOG.info("Ansible Passed for {}", mp.getProcLongName());
result = Status.Complete;
} else {
result = Status.Failed;
LOG.error("Ansible Failed for " + mp.getProcLongName());
AnsibleEvent failedEvent = el.getFailedEvent();
if (failedEvent != null) {
LOG.error("Failed Event Output: " + failedEvent.getStdout());
failedEventOutput = failedEvent.getStdout();
} else {
LOG.error("Unable to determine failed event");
}
}
} catch (IOException | AnsibleCommandException e) {
LOG.error("Unable to determine Ansible execution result {}", e.getMessage());
result = Status.Failed;
}
updateAnsibleResult(result, failedEventOutput, uuid);
}
private String parseAnsibleOutput(String data) throws AnsibleCommandException {
LOG.info("Parsing result");
if (data.length() == 0) {
throw new AnsibleCommandException("Empty data in ansible output");
}
String[] lines = data.split("\\r?\\n");
StringBuilder jsonStringBuilder = new StringBuilder();
jsonStringBuilder.append("[");
for (String l : lines) {
jsonStringBuilder.append(l).append(",");
}
jsonStringBuilder.deleteCharAt(jsonStringBuilder.length() - 1);
jsonStringBuilder.append("]");
LOG.info("munged json is {}", jsonStringBuilder.toString());
return jsonStringBuilder.toString();
}
private void updateAnsibleResult(Status result, String failedEvent, Uuid uuid) {
CommandKey cmdKey = new CommandKey(uuid);
InstanceIdentifier<Command> cmdPath = InstanceIdentifier.create(Commands.class).child(Command.class, cmdKey);
Command cmd = new CommandBuilder().setStatus(result).setFailedEvent(failedEvent).setUuid(uuid).build();
txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, tx -> {
tx.put(cmdPath, cmd);
});
}
public ManagedProcess getProcess(Uuid uuid) {
if (processMap.containsKey(uuid)) {
return processMap.get(uuid);
}
return null;
}
public Status getAnsibleResult(Uuid uuid) {
Optional<Command> optCmdResult;
CommandKey cmdKey = new CommandKey(uuid);
InstanceIdentifier<Command> cmdPath = InstanceIdentifier.create(Commands.class).child(Command.class, cmdKey);
try {
optCmdResult = SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL,
cmdPath);
if (optCmdResult.isPresent()) {
return optCmdResult.get().getStatus();
}
} catch (ReadFailedException e) {
return null;
}
return null;
}
public boolean isAnsibleComplete(Uuid uuid) {
if (getAnsibleResult(uuid) != null && getAnsibleResult(uuid) != Status.InProgress) {
return true;
}
return false;
}
public boolean ansibleCommandSucceeded(Uuid uuid) {
if (isAnsibleComplete(uuid) && getAnsibleResult(uuid) == Status.Complete) {
return true;
}
return false;
}
}