Action "lxc.file.exists"
Check if the file exists in a container.
Example
const {$status} = await nikita.lxc.file.exists({
container: 'my_container',
target: '/root/a_file'
})
console.info(`File exists: ${$status}`)
Todo
- Push recursive directories
- Handle unmatched target permissions
- Handle unmatched target ownerships
- Detect name from lxd_target
Implementation change
Previous implementation used lxc.query
action to retrieve the content of the
file and then determine if it exists or not:
lxc query --request GET /1.0/instances/container_name/files?path=file_path
It presents two problems:
- The file is fetch which introduce delay and be unacceptable for large file.
- The current LXD version throw an error when the file is empty, see LXD issue #11388.
The LXD API exposes a REST endpoint to obtain file metadata. However, lxc query don't support the HEAD HTTP method, see LXD issue #11383.
This implementation uses the lxc.exec
action to run the existence file test directly
inside the container.
Schema definitions
definitions =
config:
type: 'object'
properties:
'container':
$ref: 'module://@nikitajs/lxd/src/init#/definitions/config/properties/container'
'target':
type: 'string'
description: '''
File location in the form of "<path>".
'''
required: ['container']
Handler
handler = ({config}) ->
{$status} = await @lxc.exec
$header: "Check if file exists in container #{config.container}"
container: config.container
command: "test -f #{config.target}"
code: [0, 1]
exists: $status
Exports
module.exports =
handler: handler
metadata:
definitions: definitions
shy: true