Skip to content

Commit

Permalink
feat(image-builder): add support for set USER on image build process
Browse files Browse the repository at this point in the history
  • Loading branch information
yunielrc committed May 11, 2023
1 parent 08ae2c9 commit ee6f24b
Show file tree
Hide file tree
Showing 26 changed files with 1,301 additions and 90 deletions.
4 changes: 3 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@
"program": "${workspaceFolder}/dist/usr/bin/vedv",
"args": [
"image",
"build"
"build",
"-t",
"image123"
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion cac/configure-image.alpine-linux
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ cat <<'EOF' >/usr/local/bin/vedv-adduser
set -eu
if [[ "$#" -ne 2 ]]; then
echo "usage: ved-adduser <username> <password>" >&2
echo "usage: vedv-adduser <username> <password>" >&2
exit 1
fi
Expand Down
59 changes: 54 additions & 5 deletions dist/lib/vedv/components/__base/vmobj-entity.bash
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,9 @@ vedv::vmobj_entity::__set_attribute() {
# Set ssh_port value
#
# Arguments:
# type string type (e.g. 'container|image')
# vmobj_id string vmobj id
# ssh_port int ssh port
# type string type (e.g. 'container|image')
# vmobj_id string vmobj id
# ssh_port int ssh port
#
# Returns:
# 0 on success, non-zero on error.
Expand All @@ -592,8 +592,11 @@ vedv::vmobj_entity::set_ssh_port() {
# Get ssh_port value
#
# Arguments:
# type string type (e.g. 'container|image')
# vmobj_id string vmobj id
# type string type (e.g. 'container|image')
# vmobj_id string vmobj id
#
# Output:
# Writes ssh_port (int) to the stdout.
#
# Returns:
# 0 on success, non-zero on error.
Expand All @@ -607,3 +610,49 @@ vedv::vmobj_entity::get_ssh_port() {
"$vmobj_id" \
'ssh_port'
}

#
# Set user name
#
# Arguments:
# type string type (e.g. 'container|image')
# vmobj_id string vmobj id
# user_name string user name
#
# Returns:
# 0 on success, non-zero on error.
#
vedv::vmobj_entity::set_user_name() {
local -r type="$1"
local -r vmobj_id="$2"
local -r value="$3"

vedv::vmobj_entity::__set_attribute \
"$type" \
"$vmobj_id" \
'user_name' \
"$value"
}

#
# Get user name
#
# Arguments:
# type string type (e.g. 'container|image')
# vmobj_id string vmobj id
#
# Output:
# Writes user_name (string) to the stdout.
#
# Returns:
# 0 on success, non-zero on error.
#
vedv::vmobj_entity::get_user_name() {
local -r type="$1"
local -r vmobj_id="$2"

vedv::vmobj_entity::__get_attribute \
"$type" \
"$vmobj_id" \
'user_name'
}
88 changes: 84 additions & 4 deletions dist/lib/vedv/components/__base/vmobj-service.bash
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ vedv::vmobj_service::constructor() {
readonly __VEDV_VMOBJ_SERVICE_SSH_PASSWORD="$3"
}

#
# Get ssh user
#
# Output:
# Writes ssh_user (string) to the stdout
#
vedv::vmobj_service::get_ssh_user() {
echo "$__VEDV_VMOBJ_SERVICE_SSH_USER"
}

#
# Tell if a vmobj is started
#
Expand Down Expand Up @@ -667,7 +677,8 @@ vedv::vmobj_service::__exec_ssh_func() {
local -r type="$1"
local -r vmobj_id="$2"
local -r exec_func="$3"
local -r user="${4:-"$__VEDV_VMOBJ_SERVICE_SSH_USER"}"
local user="${4:-}"

# validate arguments
vedv::vmobj_entity::validate_type "$type" ||
return "$?"
Expand All @@ -679,6 +690,14 @@ vedv::vmobj_service::__exec_ssh_func() {
err "Invalid argument 'exec_func': it's empty"
return "$ERR_INVAL_ARG"
fi
if [[ -z "$user" ]]; then
user="$(vedv::vmobj_entity::get_user_name "$type" "$vmobj_id")" || {
err "Failed to get default user for ${type}"
return "$ERR_VMOBJ_OPERATION"
}
fi
readonly user

if [[ -z "$user" ]]; then
err "Invalid argument 'user': it's empty"
return "$ERR_INVAL_ARG"
Expand Down Expand Up @@ -714,6 +733,7 @@ vedv::vmobj_service::__exec_ssh_func() {
# type string type (e.g. 'container|image')
# vmobj_id string vmobj id
# cmd string command to execute
# [user] string user name
#
# Output:
# writes command output to the stdout
Expand All @@ -725,6 +745,7 @@ vedv::vmobj_service::execute_cmd_by_id() {
local -r type="$1"
local -r vmobj_id="$2"
local -r cmd="$3"
local -r user="${4:-}"
# validate arguments
vedv::vmobj_entity::validate_type "$type" ||
return "$?"
Expand All @@ -740,7 +761,7 @@ vedv::vmobj_service::execute_cmd_by_id() {

local -r exec_func="vedv::ssh_client::run_cmd \"\$user\" \"\$ip\" \"\$password\" '${cmd}' \"\$port\""

vedv::vmobj_service::__exec_ssh_func "$type" "$vmobj_id" "$exec_func" || {
vedv::vmobj_service::__exec_ssh_func "$type" "$vmobj_id" "$exec_func" "$user" || {
err "Failed to execute command in ${type}: ${vmobj_id}"
return "$ERR_VMOBJ_OPERATION"
}
Expand All @@ -753,6 +774,7 @@ vedv::vmobj_service::execute_cmd_by_id() {
# type string type (e.g. 'container|image')
# vmobj_id_or_name string vmobj id or name
# cmd string command to execute
# [user] string user name
#
# Output:
# writes command output to the stdout
Expand All @@ -764,13 +786,14 @@ vedv::vmobj_service::execute_cmd() {
local -r type="$1"
local -r vmobj_id_or_name="$2"
local -r cmd="$3"
local -r user="${4:-}"

local vmobj_id
vmobj_id="$(vedv::vmobj_service::get_ids_from_vmobj_names_or_ids "$type" "$vmobj_id_or_name")" || {
err "Failed to get ${type} id by name or id: ${vmobj_id_or_name}"
return "$ERR_VMOBJ_OPERATION"
}
vedv::vmobj_service::execute_cmd_by_id "$type" "$vmobj_id" "$cmd"
vedv::vmobj_service::execute_cmd_by_id "$type" "$vmobj_id" "$cmd" "$user"
}

#
Expand Down Expand Up @@ -875,7 +898,7 @@ vedv::vmobj_service::copy_by_id() {
}
readonly vedvfileignore

local -r exec_func="vedv::ssh_client::copy \"\$user\" \"\$ip\" \"\$password\" \"\$port\" '${src}' '${dest}' ${vedvfileignore}"
local -r exec_func="vedv::ssh_client::copy \"\$user\" \"\$ip\" \"\$password\" \"\$port\" '${src}' '${dest}' '${vedvfileignore}'"

vedv::vmobj_service::__exec_ssh_func "$type" "$vmobj_id" "$exec_func" "$user" || {
err "Failed to copy to ${type}: ${vmobj_id}"
Expand Down Expand Up @@ -918,3 +941,60 @@ vedv::vmobj_service::copy() {
"$dest" \
"$user"
}

#
# Create an user if not exits and set its name to
# the vmobj-entity
#
# Arguments:
# type string type (e.g. 'container|image')
# vmobj_id string vmobj id
# user_name string user name
#
# Output:
# writes command output to the stdout
#
# Returns:
# 0 on success, non-zero on error.
#
vedv::vmobj_service::set_user() {
local -r type="$1"
local -r vmobj_id="$2"
local -r user_name="$3"
# validate arguments
vedv::vmobj_entity::validate_type "$type" ||
return "$?"

if [[ -z "$vmobj_id" ]]; then
err "Invalid argument 'vmobj_id': it's empty"
return "$ERR_INVAL_ARG"
fi
if [[ -z "$user_name" ]]; then
err "Invalid argument 'user_name': it's empty"
return "$ERR_INVAL_ARG"
fi

local cur_user_name
cur_user_name="$(vedv::vmobj_entity::get_user_name "$type" "$vmobj_id")" || {
err "Error getting attribute user name from the ${type} '${vmobj_id}'"
return "$ERR_VMOBJ_OPERATION"
}
readonly cur_user_name

if [[ "$cur_user_name" == "$user_name" ]]; then
return 0
fi

# create user if it doesn't exist
local -r cmd="vedv-adduser '${user_name}' '${__VEDV_VMOBJ_SERVICE_SSH_PASSWORD}'"

vedv::vmobj_service::execute_cmd_by_id "$type" "$vmobj_id" "$cmd" 'root' &>/dev/null || {
err "Failed to set user '${user_name}' to ${type}: ${vmobj_id}"
return "$ERR_VMOBJ_OPERATION"
}

vedv::vmobj_entity::set_user_name "$type" "$vmobj_id" "$user_name" || {
err "Error setting attribute user name '${user_name}' to the ${type}: ${vmobj_id}"
return "$ERR_VMOBJ_OPERATION"
}
}
37 changes: 36 additions & 1 deletion dist/lib/vedv/components/container/container-entity.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fi

readonly VEDV_CONTAINER_ENTITY_TYPE='container'
# shellcheck disable=SC2034
readonly VEDV_CONTAINER_ENTITY_VALID_ATTRIBUTES='parent_image_id|ssh_port'
readonly VEDV_CONTAINER_ENTITY_VALID_ATTRIBUTES='parent_image_id|ssh_port|user_name'

# FUNCTIONS

Expand Down Expand Up @@ -196,3 +196,38 @@ vedv::container_entity::get_parent_image_id() {
"$container_id" \
'parent_image_id'
}

#
# Get user_name value
#
# Arguments:
# container_id string container id
#
# Output:
# Writes user_name (string) to the stdout
#
# Returns:
# 0 on success, non-zero on error.
#
vedv::container_entity::get_user_name() {
local -r container_id="$1"

vedv::vmobj_entity::get_user_name "$VEDV_CONTAINER_ENTITY_TYPE" "$container_id"
}

#
# Set user_name value
#
# Arguments:
# container_id string container id
# user_name string user name
#
# Returns:
# 0 on success, non-zero on error.
#
vedv::container_entity::set_user_name() {
local -r container_id="$1"
local -r value="$2"

vedv::vmobj_entity::set_user_name "$VEDV_CONTAINER_ENTITY_TYPE" "$container_id" "$value"
}
17 changes: 17 additions & 0 deletions dist/lib/vedv/components/container/container-service.bash
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ vedv::container_service::create() {
return "$ERR_CONTAINER_OPERATION"
}

local user_name
user_name="$(vedv::image_entity::get_user_name "$image_id")" || {
err "Error getting attribute 'user_name' from the image: ${image_id}"
return "$ERR_IMAGE_OPERATION"
}
readonly user_name

if [[ -z "$user_name" ]]; then
err "Attribute 'user_name' is empty for the image '${image_id}'"
return "$ERR_INVAL_VALUE"
fi

vedv::container_entity::set_user_name "$container_id" "$user_name" || {
err "Failed to set 'user_name' for container: ${container_id}"
return "$ERR_CONTAINER_OPERATION"
}

echo "$container_name"
}

Expand Down
Loading

0 comments on commit ee6f24b

Please sign in to comment.