Add OwnerState and PermissionState

This commit is contained in:
garrettmills
2020-03-03 16:47:51 -06:00
parent 2725f9eac2
commit 6f1de65602
5 changed files with 142 additions and 1 deletions

View File

@@ -24,6 +24,12 @@ class Host extends Injectable {
_resolve_path_command = `readlink -f "%%PATH%%"`
_reboot_command = `reboot`
_change_directory_command = `cd "%%PATH%%"`
_file_directory_permission_fetch_command = `stat -c %a "%%PATH%%"`
_file_directory_permission_set_command_flat = `chmod %%LEVEL%% "%%PATH%%"`
_file_directory_permission_set_command_recursive = `chmod -R %%LEVEL%% "%%PATH%%"`
_file_directory_ownership_fetch_command = `stat -c "%U:%G" "%%PATH%%"`
_file_directory_ownership_set_command_flat = `chown %%OWNERS%% "%%PATH%%"`
_file_directory_ownership_set_command_recursive = `chown -R %%OWNERS%% "%%PATH%%"`
constructor(config) {
super()
@@ -117,6 +123,39 @@ class Host extends Injectable {
return this.run_line_result(this._resolve_path_command.replace('%%PATH%%', resource_path))
}
async get_permissions_for_path(resource_path) {
resource_path = typeof resource_path === 'string' ? resource_path : resource_path.path
return this.run_line_result(this._file_directory_permission_fetch_command.replace('%%PATH%%', resource_path))
}
async set_permissions_for_path(resource_path, level, recursive = false) {
resource_path = typeof resource_path === 'string' ? resource_path : resource_path.path
let cmd
if ( recursive ) cmd = this._file_directory_permission_set_command_recursive
else cmd = this._file_directory_permission_set_command_flat
cmd = cmd.replace('%%PATH%%', resource_path)
cmd = cmd.replace('%%LEVEL%%', level)
await this.run(cmd)
}
async get_ownership_for_path(resource_path) {
resource_path = typeof resource_path === 'string' ? resource_path : resource_path.path
const cmd = this._file_directory_ownership_fetch_command.replace('%%PATH%%', resource_path)
const result = await this.run_line_result(cmd)
const parts = result.split(':')
return {user: parts[0], group: parts[1]}
}
async set_ownership_for_path(resource_path, { user, group }, recursive = false) {
resource_path = typeof resource_path === 'string' ? resource_path : resource_path.path
let cmd
if ( recursive ) cmd = this._file_directory_ownership_set_command_recursive
else cmd = this._file_directory_ownership_set_command_flat
cmd = cmd.replace('%%PATH%%', resource_path)
cmd = cmd.replace('%%OWNERS%%', `${user}:${group}`)
await this.run(cmd)
}
async get_mount_points() {
const result = await this.execute(this._list_mount_points_command)
if ( result.exit_code !== 0 ) {