Skip to content

Commit

Permalink
feat(core): plugin execute export config to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Feb 26, 2021
1 parent 25ff5fc commit 3022c14
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 103 deletions.
74 changes: 27 additions & 47 deletions packages/core/lib/actions/execute/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/core/lib/metadata/tmpdir.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 41 additions & 6 deletions packages/core/lib/plugins/execute.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 20 additions & 25 deletions packages/core/src/actions/execute/index.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ console.info(stdout)

on_action =
after: [
'@nikitajs/core/src/plugins/execute'
'@nikitajs/core/src/plugins/ssh'
]
before: [
'@nikitajs/core/src/plugins/schema'
'@nikitajs/core/src/metadata/tmpdir'
]
handler: ({config, metadata, ssh, tools: {find, walk}}) ->
sudo = await find ({config: {sudo}}) -> sudo
env = merge {}, ...await walk ({config: {env}}) -> env
config.env ?= process.env unless ssh or Object.keys(env).length
# sudo = await find ({config: {sudo}}) -> sudo
config.env ?= if not ssh and not config.env then process.env else {}
env_export = if config.env_export? then config.env_export else !!ssh
if sudo or config.bash or config.arch_chroot or (Object.keys(env).length and env_export)
if config.sudo or config.bash or config.arch_chroot or (env_export and Object.keys(config.env).length)
metadata.tmpdir = true
config.command = metadata.argument if metadata.argument?
Expand All @@ -96,19 +96,19 @@ console.info(stdout)
type: ['boolean', 'string']
description: """
Run this command inside a root directory with the arc-chroot command
or any provided string, require the "rootdir" option if activated.
or any provided string, require the "arch_chroot_rootdir" option if activated.
"""
'bash':
type: ['boolean', 'string']
description: """
Serialize the command into a file and execute it with bash.
"""
'rootdir':
'arch_chroot_rootdir':
type: 'string'
description: """
Path to the mount point corresponding to the root directory, required
if the "arch_chroot" option is activated.
"""
'bash':
type: ['boolean', 'string']
description: """
Serialize the command into a file and execute it with bash.
"""
'command':
oneOf: [
type: 'string'
Expand Down Expand Up @@ -273,7 +273,7 @@ console.info(stdout)
Unix user id.
"""
dependencies:
arch_chroot: required: ['rootdir']
arch_chroot: required: ['arch_chroot_rootdir']
required: ['command']
## Handler
Expand All @@ -290,14 +290,13 @@ console.info(stdout)
dry = await find ({config: {dry}}) -> dry
# TODO move next 2 lines this to schema or on_action ?
throw Error "Incompatible properties: bash, arch_chroot" if ['bash', 'arch_chroot'].filter((k) -> config[k]).length > 1
# throw Error "Required Option: \"rootdir\" with \"arch_chroot\"" if config.arch_chroot and not config.rootdir
# Environment variables are merged with parent
env = merge {}, ...await walk ({config: {env}}) -> env
# env = merge {}, ...await walk ({config: {env}}) -> env
# Serialize env in a sourced file
env_export = if config.env_export? then config.env_export else !!ssh
if env_export and Object.keys(env).length
if env_export and Object.keys(config.env).length
env_export_content = (
"export #{k}=#{utils.string.escapeshellarg v}\n" for k, v of env
"export #{k}=#{utils.string.escapeshellarg v}\n" for k, v of config.env
).join '\n'
env_export_hash = utils.string.hash env_export_content
# Guess current username
Expand All @@ -317,7 +316,7 @@ console.info(stdout)
{stdout} = await @execute "awk -v val=#{config.uid} -F ":" '$3==val{print $1}' /etc/passwd`", (err, {stdout}) ->
config.uid = stdout.trim()
config.bash = 'bash' unless config.bash or config.arch_chroot
if env_export and Object.keys(env).length
if env_export and Object.keys(config.env).length
env_export_hash = utils.string.hash env_export_content
env_export_target = path.join metadata.tmpdir, env_export_hash
config.command = "source #{env_export_target}\n#{config.command}"
Expand Down Expand Up @@ -346,10 +345,10 @@ console.info(stdout)
command = config.command
config.target = "#{metadata.tmpdir}/#{utils.string.hash config.command}" if typeof config.target isnt 'string'
log message: "Writing arch-chroot script to #{JSON.stringify config.target}", level: 'INFO'
config.command = "#{config.arch_chroot} #{config.rootdir} bash #{config.target}"
config.command += ";code=`echo $?`; rm '#{path.join config.rootdir, config.target}'; exit $code" unless config.dirty
config.command = "#{config.arch_chroot} #{config.arch_chroot_rootdir} bash #{config.target}"
config.command += ";code=`echo $?`; rm '#{path.join config.arch_chroot_rootdir, config.target}'; exit $code" unless config.dirty
await @fs.base.writeFile
target: "#{path.join config.rootdir, config.target}"
target: "#{path.join config.arch_chroot_rootdir, config.target}"
content: "#{command}"
mode: config.mode
sudo: false
Expand All @@ -368,7 +367,7 @@ console.info(stdout)
return resolve result if config.dry
child = exec config,
ssh: ssh
env: env
env: config.env
config.stdin.pipe child.stdin if config.stdin
child.stdout.pipe config.stdout, end: false if config.stdout
child.stderr.pipe config.stderr, end: false if config.stderr
Expand Down Expand Up @@ -413,9 +412,6 @@ console.info(stdout)
result.stdout = result.stdout.trim() if config.trim or config.stdout_trim
result.stderr = result.stderr.map((d) -> d.toString()).join('')
result.stderr = result.stderr.trim() if config.trim or config.stderr_trim
# if config.format
# console.log '>>>', config.command_original
# console.log '!!!', result.stdout
if config.format and config.code.indexOf(code) isnt -1
result.data = switch config.format
when 'json' then JSON.parse result.stdout
Expand Down Expand Up @@ -456,4 +452,3 @@ console.info(stdout)
exec = require 'ssh2-exec'
yaml = require 'js-yaml'
utils = require '../../utils'
{merge} = require 'mixme'
Loading

0 comments on commit 3022c14

Please sign in to comment.