How can I run / adapt a .sh file to windows environement - windows

I have the following configuration file for a project.
It does not run on windows per se.
I have powershell installed, the linux subsystem, docker running, etc.
What steps should I follow to make the project run on windows ? I am a bit lost
Can I run it without cygwin ?
#!/usr/bin/env bash
CYAN='\033[0;36m'
BLUE='\033[0;34m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$#" || die "cannot $*"; }
# Path to your hosts file
hostsFile="/etc/hosts"
# Default IP address for host
ip="127.0.0.1"
hostnames="api.foodmeup.local"
removeHost() {
if [ -n "$(grep -p "[[:space:]]$1" /etc/hosts)" ]; then
echo "$1 found in $hostsFile. Removing now...";
try sudo sed -ie "/[[:space:]]$1/d" "$hostsFile";
else
yell "$1 was not found in $hostsFile";
fi
}
addHost() {
if [ -n "$(grep -p "[[:space:]]$1" /etc/hosts)" ]; then
yell "$1, already exists: $(grep $1 $hostsFile)";
else
echo "Adding $1 to $hostsFile...";
try printf "%s\t%s\n" "$ip" "$1" | sudo tee -a "$hostsFile" > /dev/null;
if [ -n "$(grep $1 /etc/hosts)" ]; then
echo "$1 was added succesfully:";
echo "$(grep $1 /etc/hosts)";
else
die "Failed to add $1";
fi
fi
}
addLinuxSSL() {
sudo mkdir -p /usr/local/share/ca-certificates/foodmeup.local
sudo cp ./.docker/nginx/ssl/foodmeup-ca.cert.pem /usr/local/share/ca-certificates/foodmeup.local
sudo update-ca-certificates
}
addMacSSL() {
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ./.docker/nginx/ssl/foodmeup-ca.cert.pem
}
addCygwinSSL() {
echo "Unable to add SSL for CygWin platform";
exit;
}
while true; do
echo "Do you wish to init the FoodMeUp API project?";
read -p "(Y/N) " yn
case $yn in
[Yy]* )
echo "";
echo -e "${BLUE}Setting local host names...${NC}";
IFS=', '; array=($hostnames)
for host in ${array[#]}; do addHost $host; done
echo -e "${GREEN}Host names set!${NC}";
echo "";
echo "";
echo -e "${BLUE}Prepping environment, please wait...${NC}";
aws s3 cp s3://fmu-dev/ssl ./.docker/nginx/ssl/ --recursive --profile fmu
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) addLinuxSSL;;
Darwin*) addMacSSL;;
CYGWIN*) addCygwinSSL;;
*) exit;;
esac
rm ./.docker/nginx/ssl/foodmeup-ca.cert.pem
aws s3 cp s3://fmu-dev/env-api-devel ./.env --profile fmu
mkdir -p ./var/cache ./var/logs ./var/jwt ./var/cloud ./public/uploads
aws s3 cp s3://fmu-dev/fmu-google-cloud.json ./var/cloud/FoodMeUp-dc2389a0a0cd.json --profile fmu
JWT_PASSPHRASE=$(grep JWT_PASSPHRASE .env | cut -d '=' -f 2-)
openssl genrsa -passout pass:${JWT_PASSPHRASE} -out ./var/jwt/private.pem -aes256 4096
openssl rsa -passin pass:${JWT_PASSPHRASE} -pubout -in ./var/jwt/private.pem -out ./var/jwt/public.pem
echo -e "${GREEN}Environment all set!${NC}";
echo "";
echo -e "${BLUE}Building Docker containers, please wait...${NC}";
docker-compose up -d nginx;
docker-compose up -d postgres;
docker-compose up -d rabbitmq;
echo -e "${GREEN}Docker containers built!${NC}";
echo "";
echo -e "${BLUE}Installing Composer dependencies, please wait...${NC}";
docker exec -ti fmu_backend-php composer install --no-ansi --no-interaction --no-progress --no-suggest --optimize-autoloader;
echo -e "${GREEN}Composer dependencies installed!!${NC}";
echo "";
echo -e "${BLUE}Generating assets, please wait...${NC}";
docker exec -ti fmu_backend-php bin/console assets:install
echo -e "${GREEN}Assets generated!${NC}";
echo "";
echo -e "${BLUE}Initializing application, please wait...${NC}";
docker exec -ti fmu_backend-php /var/www/bin/phing init
echo -e "${GREEN}Application initialized!${NC}";
echo "";
echo -e "${GREEN}[ALL DONE]${NC}";
break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
echo -e "${GREEN}Project successfully installed${NC}";

Well, I took each command one by one and tried to make it work
here is the result
function Green {
process { Write-Host $_ -ForegroundColor Green }
}
function Red {
process { Write-Host $_ -ForegroundColor Red }
}
Write-output "Adding hosts...";
Set-HostsEntry -IPAddress 127.0.0.1 -HostName 'api.foodmeup.local' -Description "FoodMeUp local API"
Write-output "Host added" | Green;
Write-output "Adding certificates...";
aws s3 cp s3://fmu-dev/ssl ./.docker/nginx/ssl/ --recursive --profile fmu
Import-Certificate -FilePath "./.docker/nginx/ssl/foodmeup-ca.cert.pem" -CertStoreLocation cert:\CurrentUser\Root
Remove-Item ./.docker/nginx/ssl/foodmeup-ca.cert.pem
aws s3 cp s3://fmu-dev/env-api-devel ./.env --profile fmu
'./var/cache','./var/logs','./var/jwt','./var/cloud','./public/uploads','./vendor' | % {New-Item -Name "$_" -ItemType 'Directory'}
aws s3 cp s3://fmu-dev/fmu-google-cloud.json ./var/cloud/FoodMeUp-dc2389a0a0cd.json --profile fmu
$file = ".env"
$pattern = "(?<=JWT_PASSPHRASE=).*"
$values=Select-String -Path $file -Pattern $pattern |
Select-Object -Expand matches |
Select-Object value
$JWT_PASSPHRASE=$values.value
openssl genrsa -passout pass:$JWT_PASSPHRASE -out ./var/jwt/private.pem -aes256 4096
openssl rsa -passin pass:$JWT_PASSPHRASE -pubout -in ./var/jwt/private.pem -out ./var/jwt/public.pem
Write-output "Certificates added successfully" | Green
Write-output "Granting rights on all folders...";
takeown /r /d y /f "var"
icacls "./var/" /grant:r Users:F /t
Write-output "Rights granted" | Green;
Write-output "Building and uping all containers..."
Write-output "Make sure to accept prompts asking to share project local drive or share it manually...";
docker-compose up -d
Write-output "All containers up!" | Green
Write-output "Installing dependencies, please wait..."
docker exec -ti fmu_backend-php php composer install
Write-output "Dependencies installed!" | Green
Write-output "Generating assets, please wait..."
docker exec -ti fmu_backend-php bin/console assets:install
Write-output "Assets generated!" | Green
Write-output "Initializing application, please wait..."
docker exec -ti fmu_backend-php /var/www/bin/phing init
Write-output "Application initialized!" | Green
Write-output "All done!" | Green

Related

Bash - While loop exit code 1

I have a problem with one of my bash scripts. Here is code.
#!/bin/bash
ENVIRONMENT=$1
STORAGE_ACCOUNT_NAME=$2
TOKEN_AZURE=$3
TOKEN_GITHUB=$4
DATA_GOVERNANCE_NAME=$5
CURRENT_WORKING_DIR=$6
GIT_TIME_PERIOD=${7:-60 days}
if [ -z $2 ]; then echo "Environment name and the storage account name must be passed as a parameter" && exit 1; fi
echo "Filtering the repos to find out which should be deployed to blob containers workspace..."
less $CURRENT_WORKING_DIR/$DATA_GOVERNANCE_NAME/$ENVIRONMENT/data_ingestion_framework/Repos/Deployable_repos.json | jq -r '[.Repos[] | select(.LOCATION[]=="STORAGE")]' >$CURRENT_WORKING_DIR/repos_filtered_blob.json
REPOS_COUNT=$(jq length $CURRENT_WORKING_DIR/repos_filtered_blob.json)
for containers in $(jq -r '[.Repos[] | select(.LOCATION[]=="STORAGE") | .PATH ] | unique | .[]' $CURRENT_WORKING_DIR/$DATA_GOVERNANCE_NAME/$ENVIRONMENT/data_ingestion_framework/Repos/Deployable_repos.json);
do
echo "Clearing content of $containers blob"
STRIPED_CONTAINER=$(echo $containers | sed -r "s|/?$||")
if [ $STRIPED_CONTAINER == 'datagovernance' ];
then
az storage blob delete-batch --auth-mode login --account-name $STORAGE_ACCOUNT_NAME --source $STRIPED_CONTAINER --query '[::-1]' --delete-snapshots include
else
az storage blob delete-batch --auth-mode login --account-name $STORAGE_ACCOUNT_NAME --source $STRIPED_CONTAINER --query '[::-1]'
fi
done
if [ $REPOS_COUNT -eq "0" ]; then echo "No repos meant for blob containers found" && exit 1; fi
echo "Repos meant for blob containers found: $REPOS_COUNT"
echo ""
REGEX="^https://(.*#)?"
declare -i i=0
while [ $REPOS_COUNT -gt 0 ]; do
echo "Getting the repo name to be deployed..."
REPO_NAME=$(jq -r .[$i].Name $CURRENT_WORKING_DIR/repos_filtered_blob.json)
echo "Repository name: $REPO_NAME"
echo "Checking if the repo should be deployed to $ENVIRONMENT environment..."
echo "$ENVIRONMENT environment detected! Extracting the source branch name..."
BRANCH_NAME=$(less $CURRENT_WORKING_DIR/repos_filtered_blob.json | jq -r '.['$i'].BRANCH')
echo "Source branch name: $BRANCH_NAME"
REPO_LINK=$(jq -r .[$i].Link $CURRENT_WORKING_DIR/repos_filtered_blob.json)
echo "Repository LINK: $REPO_LINK"
if $(echo $REPO_LINK | grep -q github.dxc.com);
then
echo "GitHub link detected!"
ADJUSTED_LINK=$(echo $REPO_LINK | sed -r "s|$REGEX|https://$TOKEN_GITHUB#|g")
else
echo "Azure DevOps repository link detected!"
ADJUSTED_LINK=$(echo $REPO_LINK | sed -r "s|$REGEX|https://$TOKEN_AZURE#|g")
fi
echo "Adjusted link: $ADJUSTED_LINK"
REPO_SSH=$(jq -r .[$i].SSH $CURRENT_WORKING_DIR/repos_filtered_blob.json)
echo ""
CONTAINER_NAME=$(jq -r .[$i].PATH $CURRENT_WORKING_DIR/repos_filtered_blob.json | sed -r "s|/?$||")
CLEAN_COPY=$(jq -r .[$i].CLEAN_COPY $CURRENT_WORKING_DIR/repos_filtered_blob.json)
echo "Container name: $CONTAINER_NAME"
echo "Cloning the source branch..."
rm -rf $CURRENT_WORKING_DIR/Repos/$REPO_NAME
git clone --branch $BRANCH_NAME --single-branch $ADJUSTED_LINK $CURRENT_WORKING_DIR/Repos/$REPO_NAME
echo "Checking if the repository had recent commits..."
cd $CURRENT_WORKING_DIR/Repos/$REPO_NAME
if git log --date=relative --since "$GIT_TIME_PERIOD" | grep -q commit;
then
echo "Repository edited in last $GIT_TIME_PERIOD, will proceed with copying..."
cd ..
echo "Saving the list of the contents..."
if [ "$CLEAN_COPY" = "true" ];
then
if [ "$REPO_NAME" = "$DATA_GOVERNANCE_NAME" ];
then
find $REPO_NAME/$ENVIRONMENT/ | sed -r 's,^([a-zA-Z0-9_-]*\/){2},,' >contents.txt
else
find $REPO_NAME/ | sed 's,^([a-zA-Z0-9_-]*\/){1},,' >contents.txt
fi
# cat contents.txt
echo "Deleting the pre-existing files from the container..."
az storage blob delete-batch --auth-mode login --account-name $STORAGE_ACCOUNT_NAME --source $CONTAINER_NAME --query '[::-1]'
else
echo "Skiping deleting the pre-existing files from the container.. "
fi
if [ "$REPO_NAME" = "$DATA_GOVERNANCE_NAME" ];
then
echo "Copying $ENVIRONMENT to datagovernance"
az storage blob upload-batch --auth-mode login --account-name $STORAGE_ACCOUNT_NAME --destination $CONTAINER_NAME --source "$CURRENT_WORKING_DIR/Repos/$REPO_NAME/$ENVIRONMENT" --overwrite --no-progress
else
echo ""
echo "Copying the files to the container..."
az storage blob upload-batch --auth-mode login --account-name $STORAGE_ACCOUNT_NAME --destination $CONTAINER_NAME --source "$CURRENT_WORKING_DIR/Repos/$REPO_NAME" --overwrite --no-progress
fi
else
echo "Repository not edited recently, will not copy..."
echo ""
cd ..
fi
((i = i + 1))
((REPOS_COUNT = REPOS_COUNT - 1))
done
The problem with this script is that whenever I run whole run is going smoothly and perform everything that is required. However at the end it always throws error bellow.
##[error]The process '/bin/bash' failed with exit code 1
##[error]Bash failed with error: The process '/bin/bash' failed with exit code 1
I have spent quite few hours trying to debug this but i don't know what is cause of that error. Can error be caused by while loop itself?

How to set Default for Last Command Line Argument

So I have this code that will tunnel VNC over SSH and start a session.
It has A argument for the hostname but it's the last argument.
I want to put a default Value for the hostname.
I can't just check if the value is blank because it will show a other argument.
This is the script's syntax:
VNC-Over-SSH - Make VNC Connections Over SSH
VNC-Over-SSH [options] [hostname]
hostname: The Hostname of the VNC Server. Defaults to "server1".
options:
-h, --help show brief help
-u, --AuthUser=Username specify an Username. Defaults to System Username.
this is the code:
#!/bin/bash
package="VNC-Over-SSH"
for HOSTNAME; do :; done
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "$package - Make VNC Connections Over SSH"
echo " "
echo "$package [options] [hostname]"
echo " "
echo "hostname: The Hostname of the VNC Server. Defaults to \"server1\"."
echo "options:"
echo "-h, --help show brief help"
echo "-u, --AuthUser=Username specify an Username. Defaults to System Username."
exit 0
;;
-u)
shift
if test $# -gt 0; then
export USERNAME=$1
else
echo "no Username specified"
exit 1
fi
shift
;;
--AuthUser*)
if [[ "$(echo "$1" | sed -e 's/^[^=]*=//g')" != "" ]] && [[ "$1" != "--AuthUser" ]] ; then
USERNAME=$(echo "$1" | sed -e 's/^[^=]*=//g')
else
echo "no Username specified"
exit 1
fi
shift
;;
*)
break
;;
esac
done
if [ "$USERNAME" == "" ]; then
USERNAME=$USER
fi
export USERNAME
ssh -L 9876:localhost:5900 -N -f -l "$USERNAME" "$HOSTNAME"
vncviewer UserName="$USERNAME" localhost:9876 &
wait -n
kill $(ps aux | grep "ssh -L 9876:localhost:5900 -N -f -l $USERNAME $HOSTNAME" | grep -v grep | awk '{print $2}')
exit
After the loop that processes options is done, check whether there's a $1 argument. If there is, use that, otherwise use the default.
HOSTNAME=${1:-server1}

sudo mount -t ntfs complains about not being in fstab only when done from bash

I have no problem with sudo mount -t ntfs4 server/drive /home/larry/folder but when I put this in a bash script it complains about not being in fstab. Can someone please help me here?
Here is the complete script. It allows me to mount or umount the drives. Some of the servers are Debian and some are Ubuntu. My client is Debian 10.
#!/bin/bash
function mountit()
{
verb=$1
from=$2
to=$3
echo "Input is $verb $from $to"
if [ "$verb" == "mount" ]; then
echo "sudo mount -t nfs4 $from $to"
sudo /bin/mount -t nfs4 "$from $to" || true
else
echo "sudo umount $to"
sudo /bin/umount "$to" || true
fi
}
verb="mount"
if [ "$1" == "0" ]; then
verb="umount"
fi
if [ $1 == "umount" ]; then
verb="umount"
fi
echo "$verb drives"
echo "poseidon vhosts - webs"
mountit $verb "192.168.18.2:/var/www/vhosts" "/home/larry/webs"
echo "ramses data - data"
mountit $verb "192.168.18.8:/data" "/home/larry/data"
echo "ramses data1 - data1"
mountit $verb "192.168.18.8:/data1" "/home/larry/data1"
echo "ramses mydata - data2"
mountit $verb "192.168.18.8:/mydata" "/home/larry/data2"
echo "pavilion data1 - data3"
mountit $verb "192.168.18.66:/home/larry/data1" "/home/larry/data3"
echo "ramses data2 - data4"
mountit $verb "192.168.18.8:/data2" "/home/larry/data4"
echo "pavilion data3 - data5"
mountit $verb "192.168.18.66:/home/larry/data3" "/home/larry/data5"
Replace
sudo /bin/mount -t nfs4 "$from $to"
with
sudo /bin/mount -t nfs4 "$from" "$to"

How to supply password from shell script without using expect command

I'm writing a bash script to stop start my postgres DB service. Initially I succeeded in creating one, but as soon I enabled SSL certificate it prompts to enter the phrase password.
I know the easiest solution is to use expect , but in my environment i am not authorized to use it.
Can someone help me out in scripting as to how can I supply the PEM PHRASE password without a user intervention.
This is what I have worked so far.
-bash-4.2$ cat start_postgres_db.sh
cd `dirname $0`
. `dirname $0`/parameter.env
${POSTGREBIN}/pg_ctl -D ${POSTGREDATAPATH} start -w
while true
do
sleep 1
loopcnt=0
loopcnt=`expr ${loopcnt} + 1`
PRCCNT=`ps -ef | grep ${DBEXENAME} | grep -v grep|wc -l`
if [ ${PRCCNT} -eq 1 ]
then
echo "PostgreSQL process started sucessfully"
exit
fi
if [ ${loopcnt} -gt 11 ]
then
echo "PostgreSQL process not started successfully"
echo "su to postgres and run ${POSTGREBIN}/pg_ctl -D ${POSTGREDATAPATH} restart"
exit
fi
done
Execution:
bash-4.2$ ./start_postgres_db.sh
waiting for server to start....Enter PEM pass phrase:.........
You can provide a password to pg_ctl as argument on the command line with the option -P. I will assume it is contained in the variable ${POSTGREPASSWORD}.
start_postgres_db.sh
cd `dirname $0`
. `dirname $0`/parameter.env
${POSTGREBIN}/pg_ctl start -w -D ${POSTGREDATAPATH} -P ${POSTGREPASSWORD}
while true; do
sleep 1
(( loopcnt++ ))
PRCCNT=$(ps -ef | grep ${DBEXENAME} | grep -v grep | wc -l)
if [ ${PRCCNT} -eq 1 ]; then
echo "PostgreSQL process started sucessfully"
exit 0
fi
if [ ${loopcnt} -gt 11 ]; then
echo "PostgreSQL process not started successfully"
echo "su to postgres and run ${POSTGREBIN}/pg_ctl -D ${POSTGREDATAPATH} restart"
exit 1
fi
done

Azure stop/start: How to loop through VM list and check status

I am trying to start/shut down VMs in parallel while also checking the powerstate to make sure it is either running/deallocated. Currently, my script waits on each VM to complete its operation before moving to the next VM. I want to be able to start/stop, check powerstate, move to the next one, check powerstate again and keep looping till all conditions are met.
#!/usr/bin/env bash
VM_IDS="$(az resource list --tag Nonessential=Yes --query "[?type=='Microsoft.Compute/virtualMachines'].{VM:name, RG:resourceGroup}" -o tsv >> vm.txt)"
cat vm.txt
if [[ "${OPTIONS}" == "stop" ]]; then
while read VM RG; do
timeout 10s az vm deallocate -g $RG -n $VM --no-wait
#PS=$(az vm show -g $RG -n $VM --show-details --query powerState -o tsv | tr -d '"')
echo "$VM is shutting down & deallocating..."
while [[ `az vm show -g $RG -n $VM --show-details --query powerState` != "VM deallocated" ]]; do
sleep 5
PS=`az vm show -g $RG -n $VM --show-details --query powerState -o tsv | tr -d '"'`
if [[ "${PS}" == "VM deallocated" ]]; then
echo "$VM has deallocated successfully..."
echo "--------------------------------------------------"
echo
break
else
echo "$VM is still deallocating..."
fi
done
done <vm.txt
elif [[ "${OPTIONS}" == "start" ]]; then
while read VM RG; do
timeout 10s az vm start -g $RG -n $VM --no-wait
#PS=$(az vm show -g $RG -n $VM --show-details --query powerState -o tsv | tr -d '"')
echo "$VM is starting..."
while [[ `az vm show -g $RG -n $VM --show-details --query powerState` != "VM running" ]]; do
sleep 5
PS=`az vm show -g $RG -n $VM --show-details --query powerState -o tsv | tr -d '"'`
if [[ "${PS}" == "VM running" ]]; then
echo "$VM has started successfully..."
echo "--------------------------------------------------"
echo
break
else
echo "$VM is still starting..."
fi
done
done <vm.txt
else
echo "Try again..."
exit 1
fi
exit 0

Resources