Skip to content

Commit

Permalink
Merge pull request #456 from Poorna-Gottimukkula1/pvsadm-test
Browse files Browse the repository at this point in the history
Script Updates: Latest pvsadm Version, Updated Repository Commit ID, and Enhancements
  • Loading branch information
Power Cloud Robot authored Jul 27, 2023
2 parents 1e4a171 + ecd6bc4 commit 765a21d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions samples/convert-upload-images-powervs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Args:
--cos-instance-name string COS instance name(Mandatory)
--rhel-url url url pointing to the RHEL qcow2 image(optional)
--rhcos-url url url pointing to the RHCOS qcow2 image(optional)
--skip-os-password Skip the root user password(optional)
--help help for upload
```
#### How to download and use script
Expand Down
45 changes: 29 additions & 16 deletions samples/convert-upload-images-powervs/convert-upload-images-powervs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
set -e
#set -x

source <(curl -L https://raw.githubusercontent.com/ocp-power-automation/openshift-install-power/f3ca66402921c00a5637be678f547192b83ac3e2/openshift-install-powervs 2> /dev/null | sed 's/main "$@"//g')
source <(curl -L https://raw.githubusercontent.com/ocp-power-automation/openshift-install-power/92996305e1a8bef69fbe613b912d5561cc753172/openshift-install-powervs 2> /dev/null | sed 's/main "$@"//g')

function help {
cat <<-EOF
Expand All @@ -35,6 +35,7 @@ Args:
--rhcos-url url Url pointing to the RHCOS qcow2 image(optional)
--cos-access-key string Cloud Storage access key(optional)
--cos-secret-key string Cloud Storage secret key(optional)
--skip-os-password Skip the root user password (optional)
--help help for upload
EOF
Expand All @@ -57,7 +58,7 @@ NO_OF_RETRY=${NO_OF_RETRY:-"5"}
SLEEP_TIME=10
REBOOT_TIMEOUT=30

PVSADM_VERSION="v0.1.7"
PVSADM_VERSION="v0.1.11"
IMAGE_SIZE="11"
TARGET_DISK_SIZE="120"

Expand All @@ -82,7 +83,7 @@ function precheck_tools {
#-------------------------------------------------------------------------
function vm_access_info {
if [[ -f ./terraform.tfstate ]] && checkState "module.prepare.null_resource.rhel83_fix[0]"; then
BASTION_PUBLIC_IP=$($TF output "bastion_public_ip" 2>/dev/null)
BASTION_PUBLIC_IP=$($TF output "bastion_public_ip" | sed 's/"//g' 2>/dev/null)
# TODO: Find a way to change the bastion user as per TF variable; default is root
if [ -s "$VAR_PRIVATE_KEY_FILE" ]; then
echo "Login to bastion: '$($TF output bastion_ssh_command | sed 's/,.*//')' and start using the 'oc' command."
Expand Down Expand Up @@ -236,6 +237,17 @@ function apply {
vm_access_info && success "Congratulations! create command completed"
}

#-------------------------------------------------------------------------
# Read the info from the plan file
#-------------------------------------------------------------------------
function plan_info {
BASTION_COUNT=$(grep ibm_pi_instance.bastion tfplan | wc -l)
BOOTSTRAP_COUNT=$(grep ibm_pi_instance.bootstrap tfplan | wc -l)
MASTER_COUNT=$(grep ibm_pi_instance.master tfplan | wc -l)
WORKER_COUNT=$(grep ibm_pi_instance.worker tfplan | wc -l)
TOTAL_RHCOS=$(( BOOTSTRAP_COUNT + MASTER_COUNT + WORKER_COUNT ))
}

#-------------------------------------------------------------------------
# Destroy VM
#-------------------------------------------------------------------------
Expand Down Expand Up @@ -337,6 +349,7 @@ function convert_image_remote {
local image_dist=$1
local image_name=$2
local image_url=$3
local skip_os_password=$4
log "Converting $image_name from qcow2 to ova"
if [[ "$image_dist" == "coreos" ]];then
RHEL_SUBSCRIPTION_USERNAME="dummy"
Expand All @@ -350,9 +363,15 @@ function convert_image_remote {
for i in $(seq 1 "$NO_OF_RETRY"); do
echo "Attempt: $i/$NO_OF_RETRY"
LOG_FILE="logs/convert_image_remote_${i}_${LOGFILE}.log"
ssh -o 'StrictHostKeyChecking no' -o 'ServerAliveInterval 60' -o 'ServerAliveCountMax 100' -i $VAR_PRIVATE_KEY_FILE root@${BASTION_PUBLIC_IP} "cd \"$REMOTE_TEMP_DIR\"; pvsadm image qcow2ova --os-password \"$RHEL_ROOT_PASSWORD\" --rhn-user \"$RHEL_SUBSCRIPTION_USERNAME\" --rhn-password \"$RHEL_SUBSCRIPTION_PASSWORD\" --image-dist \"$image_dist\" --image-name \"$image_name\" --image-url \"$image_url\" --image-size \"$IMAGE_SIZE\" --target-disk-size \"$TARGET_DISK_SIZE\" 2>&1" > $LOG_FILE || true
convert_cmd="cd \"$REMOTE_TEMP_DIR\"; pvsadm image qcow2ova --rhn-user \"$RHEL_SUBSCRIPTION_USERNAME\" --rhn-password \"$RHEL_SUBSCRIPTION_PASSWORD\" --image-dist \"$image_dist\" --image-name \"$image_name\" --image-url \"$image_url\" --image-size \"$IMAGE_SIZE\" --target-disk-size \"$TARGET_DISK_SIZE\" "
if [ -n "${skip_os_password}" ] ; then
convert_cmd+="--skip-os-password "
else
convert_cmd+="--os-password \"$RHEL_ROOT_PASSWORD\" "
fi
ssh -o 'StrictHostKeyChecking no' -o 'ServerAliveInterval 60' -o 'ServerAliveCountMax 100' -i $VAR_PRIVATE_KEY_FILE root@${BASTION_PUBLIC_IP} "$convert_cmd 2>&1" > $LOG_FILE || true
if grep -c "Successfully converted" "$LOG_FILE" >/dev/null;then
if [[ -z "${RHEL_ROOT_PASSWORD}" ]] ; then
if [[ -z "${RHEL_ROOT_PASSWORD}" && -z "${skip_os_password}" ]] ; then
ROOT_PASSWORD=$(grep "OS root password:" "$LOG_FILE"| awk '{print $4}')
log "Autogenerated root password $ROOT_PASSWORD"
fi
Expand Down Expand Up @@ -555,7 +574,7 @@ function standardize_object_name {
object_extension="ova.gz"
object_temp_name=${object_original_name%.*}
os_name=`echo $object_temp_name |sed 's/\.//g'|tr '[:upper:]' '[:lower:]' |grep -o -E '^rhcos-|^rhel-|^centos-' | sed 's/-//'`
os_version_date=`echo $object_temp_name | sed 's/\.//g'| sed 's/ppc64le|qcow2|ova|openstack|kvm|GenericCloud//g' | sed -e 's/---*//g' | sed -e 's/_/-/g'| sed -e 's/[0-9]-GenericCloud-//g' | tr '[:upper:]' '[:lower:]' | grep -o -E '^[a-z]*-[0-9]{2}'|sed -E 's/rhel|rhcos|centos//g'`-$(date +%m%d%Y)
os_version_date=`echo $object_temp_name | sed -E 's/ppc64le|qcow2|ova|openstack|kvm|GenericCloud//g' | sed -e 's/---*//g' | sed -e 's/_/-/g'| sed -e 's/[0-9]-GenericCloud-//g' | tr '[:upper:]' '[:lower:]' | grep -o -E '^[a-z]*-[0-9]+\.[0-9]+'| sed 's/\.//g' | sed -E 's/rhel|rhcos|centos//g'`-$(date +%m%d%Y)
IMAGE_NAME=$os_name$os_version_date
OBJECT_NAME=$os_name$os_version_date.${object_extension}
}
Expand Down Expand Up @@ -680,6 +699,9 @@ function main {
shift
COS_SECRET_KEY="$1"
;;
"--skip-os-password")
SKIP_OS_PASSWORD="--skip-os-password"
;;
"--help")
help
;;
Expand All @@ -703,21 +725,13 @@ function main {

if ! create_cos_bucket "${COS_BUCKET_NAME}" "${COS_BUCKET_REGION}" "${COS_RESOURCE_GROUP}" ; then exit 1 ; fi

# Downloading rhel locally, else the url may expire by the time the VM comes up
if ! [ -z "${RHEL_URL}" ];then
SECONDS=0
MSG="Downloading rhel qcow2 image completed in"
download_image "rhel"
time_taken "$MSG"
fi
create_vm

if ! prepare_remote; then destroy_vm && exit 1 ; fi
if ! setup_pvsadm_remote; then destroy_vm && exit 1 ; fi

if ! [ -z "${RHEL_URL}" ];then
if copy_image_to_remote; then
if convert_image_remote "rhel" "${RHEL_IMAGE_NAME}" "${RHEL_URL}"; then
if convert_image_remote "rhel" "${RHEL_IMAGE_NAME}" "${RHEL_URL}" "${SKIP_OS_PASSWORD}" ; then
RHEL_IMAGE_PATH_REMOTE=${IMAGE_PATH}
if upload_image_remote "${COS_BUCKET_NAME}" "${RHEL_IMAGE_PATH_REMOTE}" "${COS_BUCKET_REGION}"; then
for SERV_NAME in `echo ${SERVICE_NAMES}`; do
Expand All @@ -727,7 +741,6 @@ function main {
done
fi
fi
fi
fi
if ! [ -z "${RHCOS_URL}" ];then
if convert_image_remote "coreos" "${RHCOS_IMAGE_NAME}" "${RHCOS_URL}" ; then
Expand Down

0 comments on commit 765a21d

Please sign in to comment.