generated from actions/container-action
-
Notifications
You must be signed in to change notification settings - Fork 68
/
main.js
114 lines (99 loc) · 3.94 KB
/
main.js
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
const core = require('@actions/core')
const exec = require('@actions/exec')
const yaml = require('yaml')
const fs = require('fs')
const os = require('os')
async function main() {
try {
const playbook = core.getInput("playbook", { required: true })
const requirements = core.getInput("requirements")
const directory = core.getInput("directory")
const configuration = core.getInput("configuration")
const key = core.getInput("key")
const inventory = core.getInput("inventory")
const vaultPassword = core.getInput("vault_password")
const knownHosts = core.getInput("known_hosts")
const options = core.getInput("options")
const sudo = core.getInput("sudo")
const noColor = core.getInput("no_color")
const fileMode = 0600
let cmd = ["ansible-playbook", playbook]
if (options) {
cmd.push(options.replace(/\n/g, " "))
}
if (directory) {
process.chdir(directory)
core.saveState("directory", directory)
}
if (configuration) {
const ansibleConfigurationFile = "ansible.cfg"
fs.writeFileSync(ansibleConfigurationFile, configuration, { mode: fileMode })
core.saveState("ansibleConfigurationFile", ansibleConfigurationFile)
}
if (requirements) {
const requirementsContent = fs.readFileSync(requirements, 'utf8')
const requirementsObject = yaml.parse(requirementsContent)
if (Array.isArray(requirementsObject)) {
await exec.exec("ansible-galaxy", ["install", "-r", requirements])
} else {
if (requirementsObject.roles)
await exec.exec("ansible-galaxy", ["role", "install", "-r", requirements])
if (requirementsObject.collections)
await exec.exec("ansible-galaxy", ["collection", "install", "-r", requirements])
}
}
if (key) {
const keyFile = ".ansible_key"
fs.writeFileSync(keyFile, key + os.EOL, { mode: fileMode })
core.saveState("keyFile", keyFile)
cmd.push("--key-file")
cmd.push(keyFile)
}
if (inventory) {
const inventoryFile = ".ansible_inventory"
fs.writeFileSync(inventoryFile, inventory, { mode: fileMode })
core.saveState("inventoryFile", inventoryFile)
cmd.push("--inventory-file")
cmd.push(inventoryFile)
}
if (vaultPassword) {
const vaultPasswordFile = ".ansible_vault_password"
fs.writeFileSync(vaultPasswordFile, vaultPassword, { mode: fileMode })
core.saveState("vaultPasswordFile", vaultPasswordFile)
cmd.push("--vault-password-file")
cmd.push(vaultPasswordFile)
}
if (knownHosts) {
const knownHostsFile = ".ansible_known_hosts"
fs.writeFileSync(knownHostsFile, knownHosts, { mode: fileMode })
core.saveState("knownHostsFile", knownHostsFile)
cmd.push(`--ssh-common-args="-o UserKnownHostsFile=${knownHostsFile}"`)
process.env.ANSIBLE_HOST_KEY_CHECKING = "True"
} else {
process.env.ANSIBLE_HOST_KEY_CHECKING = "False"
}
if (sudo) {
cmd.unshift("sudo", "-E", "env", `PATH=${process.env.PATH}`)
}
if (noColor) {
process.env.ANSIBLE_NOCOLOR = "True"
} else {
process.env.ANSIBLE_FORCE_COLOR = "True"
}
let output = ""
await exec.exec(cmd.join(' '), null, {
listeners: {
stdout: function(data) {
output += data.toString()
},
stderr: function(data) {
output += data.toString()
}
}
})
core.setOutput("output", output)
} catch (error) {
core.setFailed(error.message)
}
}
main()