Taskfile, how to run only if binary not exists? - taskfile

How to create precondition that only run if the binary not exists?
tasks:
migrate:
desc: Get golang-migrate
cmds:
- echo running wget bla bla
preconditions:
- sh: "test -z {{.MIGRATE_BIN}}"
msg: "found, ignoring"
vars:
MIGRATE_BIN:
sh: which migrate || true
or what kind of shell commands supported inside sh? it it based on $? return value or?
In normal shell I only need to do this:
MIGRATE_BIN=`which golang-migrate`
if [[ -z "$MIGRATE_BIN" ]]; then
echo "do wget bla bla"
fi

Ah should use status instead of precondition
tasks:
migrate:
desc: Get golang-migrate
cmds:
- echo running wget bla bla
status:
- test -f {{.MIGRATE_BIN}}
vars:
MIGRATE_BIN:
sh: which migrate || true

Related

Problem with Github Actions IF expression syntax

I need to check if steps.get_acc.outputs.acc < 1 but right now the steps.get_acc.outputs.acc is a string how to make it an integer?
cml.yaml
name: MOPS
on: [push]
jobs:
run:
runs-on: [ubuntu-latest]
steps:
- uses: actions/checkout#v2
- uses: iterative/setup-cml#v1
- uses: actions/setup-python#v2
with:
python-version: '3.x'
- name: cml
id: get_acc
shell: pwsh
env:
repo_token: ${{ secrets.GITHUB_TOKEN }}
run: |
pip3 install -r requirements.txt
python train.py
$firstLine = Get-Content -Path 'metrics.txt' -TotalCount 1
$digits = $firstline.Split(':')[-1]
$acc = [int]$digits
Write-Output "::set-output name=acc::$acc"
- name: Check acc
if: ${{steps.get_acc.outputs.acc <1 }}
uses: actions/github-script#v3
with:
script: |
core.setFailed('Accuracy dropped')
The Github Documentation for output explained that:
The value that the output parameter will be mapped to can be set to a string or an expression with context. For example, you can use the steps context to set the value of an output to the output value of a step.
Taking a look at the Literals Github documentation, we can note that:
As part of an expression, we can use boolean, null, number, or string data types. Boolean literals are not case sensitive, so we can use true or True.
Therefore, it all depends on the way you set the output variable.
Example to set a number as output:
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
using: "composite"
steps:
- id: random-number-generator
run: echo "::set-output name=random-id::$(echo $RANDOM)"
shell: bash
EDIT:
Moreover the syntax you used for the if condition isn't correct, you need to remove the ${{ }} to make it work.
Example with if condition:
job:
runs-on: ubuntu-latest
steps:
- name: step-1
id: xyz
run: echo "::set-output name=acc::$(echo $RANDOM)"
- name: step-2
if: steps.xyz.outputs.acc < 1
run: |
echo "Number lower than 1"
echo "${{ steps.xyz.outputs.acc }}"
- name: step-3
if: steps.xyz.outputs.acc > 1
run: |
echo "Number higher than 1"
echo "${{ steps.xyz.outputs.acc }}"
It looks like you have been given an answer on how to handle it as an integer, but given that you are setting the output via code. Why not just evaluate the integer in your code and then set a true/false boolean value or a Y/N string? If all you need to do is use the value as an If condition why bother to set the integer value? You could just evaluate it and set the condition at the source.

How can I get the return value of `wget` from a sh script into an `int` variable

OS: Linux raspberrypi 4.19.58-v7l+ #1245 SMP Fri Jul 12 17:31:45 BST 2019 armv7l GNU/Linux
Board: Raspberry Pi 4
I have a script:
#!/bin/bash
line=$(head -n 1 /var/www/html/configuration.txt)
file=/var/www/html/4panel/url_response.txt
if [ -f "$file" ]; then
wget_output=$(wget -q -i "$line" -O $file --timeout=2)
echo "$?"
else
echo > $file
chown pi:pi $file
fi
which I call from a C++ program using:
int val_system = 0;
val_system = system("/var/www/html/4panel/get_page.sh");
std::cout<<"System return value: "<<val_system<<std::endl;
If there is something wrong with the script, echo "$?" will output the return value of wget, but val_system will always be 0.
Does system() returns the value of echo "$?" ? In which case 0 is correct. And if that is the case how can I put the return value of wget in val_system ?
I have taken a situation in which echo "$?" always returns 8, basically I've entered an incorrect URL and:
I have tried deleting echo "$?" but val_system still returned 0;
With echo "$?" deleted I have changed the wget line to wget -q -i "$line" -O $file --timeout=2 and val_system now returns 2048.
None of my attempts bared any fruit and I have come here to seek guidance. How can I make val_system / system() return what echo "$?" returns ?
How can I get the return value of wget from the script into an int variable that's inside the C++ program that calls the script ?
The integer value system() returned contains extra information about executed command's status along with its exit code, see system() and Status Information. You need to extract exit code using WEXITSTATUS macro, like:
std::cout << "System return value: " << WEXITSTATUS(val_system) << std::endl;
If you want to echo the status and return it, you will need to save the value of $? to a variable, and exit with it explicitly.
wget_output=$(wget -q -i "$line" -O $file --timeout=2)
status=$?
...
echo $status
...
exit $status
If you don't need to execute echo or any other command between the call to wget and the end of the script, you can rely on the script exiting with the last status (i.e the one corresponding to the call to `wget) implicitly.

Ansible - Replace " and \r\ in a variable

I want to replace the " and \r\ from a variable content using Ansible.
I have the following data in a variable result thatI register the output to the variable from the previous task
curl -s -H \"Authorization: JWT eyJ4NWMiOlsiTUlJQytqQ0NBHuHO96csEQ\r\" https://hub.docker.com/v2/repositories/talasecurityinc/?page_size=10000 | jq -r '.results|.[]|.name'
In the above content I want to replace the \ and \r\ with null.
I have tried the below way but it doesn't work for me.
- set_fact: final_out="{{result | replace('\', "") | replace('\r\', '')}}"
The expected output is
curl -s -H "Authorization: JWT eyJ4NWMiOlsiTUlJQytqQ0NBHuHO96csEQ" https://hub.docker.com/v2/repositories/talasecurityinc/?page_size=10000 | jq -r '.results|.[]|.name'
The example playbook snippet would be helpful for me since I am new to ansible.
Escaping Hell....
I was not able to use replace, probably because I didn't try hard/smart enough. Meanwhile, in your specific case, you can achieve the expected result with a single regex_replace filter call so it was easier (and it worked right away :)).
I used yaml folded blocks (>) with white space control (-) to minimize the escape hassle. If you don't know what that is, have a look at a yaml doc (learn yaml in y minutes is my favorite one)
Note that the remaining backslashes in the last result below are added by ansible to escape the double quotes in the output.
---
- name: Escape chars
hosts: localhost
gather_facts: false
vars:
test: >-
curl -s -H \"Authorization: JWT eyJ4NWMiOlsiTUlJQytqQ0NBHuHO96csEQ\r\"
https://hub.docker.com/v2/repositories/talasecurityinc/?page_size=10000
| jq -r '.results|.[]|.name'
tasks:
- name: Show the untouched var
debug:
var: test
- name: Escape the var as intended
debug:
msg: >-
{{ test | regex_replace('\\r?\\?', '') }}
which results in
PLAY [Escape chars] ********************************************************************
TASK [Show the untouched var] **********************************************************
ok: [localhost] => {
"test": "curl -s -H \\\"Authorization: JWT eyJ4NWMiOlsiTUlJQytqQ0NBHuHO96csEQ\\r\\\" https://hub.docker.com/v2/repositories/talasecurityinc/?page_size=10000 | jq -r '.results|.[]|.name'"
}
TASK [Escape the var as intended] ******************************************************
ok: [localhost] => {
"msg": "curl -s -H \"Authorization: JWT eyJ4NWMiOlsiTUlJQytqQ0NBHuHO96csEQ\" https://hub.docker.com/v2/repositories/talasecurityinc/?page_size=10000 | jq -r '.results|.[]|.name'"
}
PLAY RECAP *****************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0

Script execution using ansible [duplicate]

I am using Ansible to deploy my project and I trying to check if an specified package is installed, but I have a problem with it task, here is the task:
- name: Check if python-apt is installed
command: dpkg -l | grep python-apt
register: python_apt_installed
ignore_errors: True
And here is the problem:
$ ansible-playbook -i hosts idempotent.yml
PLAY [lxc-host] ***************************************************************
GATHERING FACTS ***************************************************************
ok: [10.0.3.240]
TASK: [idempotent | Check if python-apt is installed] *************************
failed: [10.0.3.240] => {"changed": true, "cmd": ["dpkg", "-l", "|", "grep", "python-apt"], "delta": "0:00:00.015524", "end": "2014-07-10 14:41:35.207971", "rc": 2, "start": "2014-07-10 14:41:35.192447"}
stderr: dpkg-query: error: package name in specifier '|' is illegal: must start with an alphanumeric character
...ignoring
PLAY RECAP ********************************************************************
10.0.3.240 : ok=2 changed=1 unreachable=0 failed=0
Why is illegal this character '|' .
From the doc:
command - Executes a command on a remote node
The command module takes the command name followed by a list of
space-delimited arguments. The given command will be executed on all
selected nodes. It will not be processed through the shell, so
variables like $HOME and operations like "<", ">", "|", and "&" will
not work (use the shell module if you need these features).
shell - Executes a commands in nodes
The shell module takes the command name followed by a list of space-delimited arguments.
It is almost exactly like the command module but runs the command
through a shell (/bin/sh) on the remote node.
Therefore you have to use shell: dpkg -l | grep python-apt.
read about the command module in the Ansible documentation:
It will not be processed through the shell, so .. operations like "<", ">", "|", and "&" will not work
As it recommends, use the shell module:
- name: Check if python-apt is installed
shell: dpkg -l | grep python-apt
register: python_apt_installed
ignore_errors: True
For what it's worth, you can check/confirm the installation in a debian environment using the apt command:
- name: ensure python-apt is installed
apt: name=python-apt state=present

I get syntax error when i call script from c++ program

I'm trying to write a program that will allow easier management of Arduino projects. So I wrote bash script that creates all the necessary folders and files for me and when I execute it I runs like champ. Because I want to change directory in the working terminal inside the script I run script like this
. ./initialize.sh
This is also working great, but because I am writing C++ program, sourcing this script from program is giving me headache.
So inside a program I run this script like this:
system(". /usr/lib/avrduino/script/initialize.sh");
and then when I run the program I get this error:
sh: 25: /usr/lib/avrduino/script/initialize.sh: Syntax error: "(" unexpected (expecting "}")
Running the script from the program like this:
system("/usr/lib/avrduino/script/initialize.sh");
works without error but it runs in subshell.
Syntax error points to this line in script
options=("uno" "mega" "mega2560" "atmega8" "atmega168" "atmega328" "pro" "pro5v" "pro328" "pro5v328")
How come that when I run this script outside of the program it's working like champ, but run this script from program and you have a problem ?
EDIT:
Script code
#!/bin/bash
BLACK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
BROWN='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LGRAY='\033[0;37m'
DGRAY='\033[1;30m'
LRED='\033[1;31m'
LGREEN='\033[1;32m'
YELLOW='\033[1;33m'
LBLUE='\033[1;34m'
LPURPLE='\033[1;35m'
LCYAN='\033[1;36m'
WHITE='\033[1;37m'
NC='\033[0m'
makefile()
{
BOARD="default"
PS3='Chose your board: '
options=("uno" "mega" "mega2560" "atmega8" "atmega168" "atmega328" "pro" "pro5v" "pro328" "pro5v328")
select opt in "${options[#]}"
do case $opt in
"uno") BOARD="uno"
cp -r /usr/lib/avrduino/data/boards_info/uno/board-info.h .
MCU="atmega328P"
F_CPU="16000000UL"
;;
"mega") BOARD="mega"
cp -r /usr/lib/avrduino/data/boards_info/mega/board-info.h .
F_CPU="16000000UL"
;;
"mega2560") BOARD="mega2560"
cp -r /usr/lib/avrduino/data/boards_info/mega2560/board-info.h .
F_CPU="16000000UL"
;;
"atmega8") BOARD="atmega8"
cp -r /usr/lib/avrduino/data/boards_info/atmega8/board_-nfo.h .
MCU="atmega8"
F_CPU="16000000UL"
;;
"atmega168") BOARD="atmega168"
cp -r /usr/lib/avrduino/data/boards_info/atmega168/board-info.h .
MCU="atmega168"
F_CPU="16000000UL"
;;
"atmega328") BOARD="atmega328"
cp -r /usr/lib/avrduino/data/boards_info/atmega328/board-info.h .
MCU="atmega328P"
F_CPU="16000000UL"
;;
"pro") BOARD="pro"
cp -r /usr/lib/avrduino/data/boards_info/pro/board-info.h .
MCU="unknow"
F_CPU="16000000UL"
;;
"pro5v") BOARD="pro5v"
cp -r /usr/lib/avrduino/data/boards_info/pro5v/board-info.h .
MCU="unknown"
F_CPU="16000000UL"
;;
"pro328") BOARD="pro328"
cp -r /usr/lib/avrduino/data/boards_info/pro328/board-info.h .
MCU="atmega328P"
F_CPU="16000000UL"
;;
"pro5v328") BOARD= "pro5v328"
cp -r /usr/lib/avrduino/data/boards_info/pro5v328/board-info.h .
MCU="atmega328P"
F_CPU="16000000UL"
;;
*)
echo "Error : Input is not valid"
echo "Exiting..."
return 1
;;
esac
break
done
[ -e Makefile ] && rm Makefile
read -p "Do you want to configure your Makefile settings [Y/n]: " CONFIGURE
if { [ "$CONFIGURE" == "Y" ] || [ "$CONFIGURE" == "y" ]; }; then
read -p "Enter your MCU: " MCU
read -p "Enter F_CPU: " F_CPU
fi
read -p "Enter ARDUINO_PORT: " ARDUINO_PORT
echo "ARDUINO_DIR = /usr/share/arduino">>Makefile
echo "BOARD_TAG = $BOARD">>Makefile
echo "ARDUINO_PORT = $ARDUINO_PORT">>Makefile
echo "NO_CORE = 1">>Makefile
echo "AVRDUDE_ARD_PROGRAMMER = arduino">>Makefile
echo "HEX_MAXIMUM_SIZE = 30720">>Makefile
echo "AVRDUDE_ARD_BAUDRATE = 115200">>Makefile
echo "#ISP_LOW_FUSE = 0xFF">>Makefile
echo "#ISP_HIGH_FUSE = 0xDA">>Makefile
echo "#ISP_EXT_FUSE = 0x05">>Makefile
echo "#ISP_LOCK_FUSE_PRE = 0x3F">>Makefile
echo "#ISP_LOCK_FUSE_POST = 0x0F">>Makefile
echo "MCU = $MCU">>Makefile
echo "F_CPU = $F_CPU">>Makefile
echo "VARIANT = standard">>Makefile
echo "ARDUINO_LIBS =">>Makefile
echo "include /usr/share/arduino/Arduino.mk">>Makefile
echo "$BOARD|$MCU|" >> .avrduino.txt
clear
echo -e "${LGREEN}Makefile settings${NC}"
echo -e "${LBLUE}ARDUINO_DIR = ${LRED}/usr/share/arduino ${NC}"
echo -e "${LBLUE}BOARD_TAG = ${LRED}$BOARD${NC}"
echo -e "${LBLUE}ARDUINO_PORT = ${LRED}$ARDUINO_PORT${NC}"
echo -e "${LBLUE}NO_CORE = ${LRED}1${NC}"
echo -e "${LBLUE}AVRDUDE_ARD_PROGRAMMER = ${LRED}arduino${NC}"
echo -e "${LBLUE}HEX_MAXIMUM_SIZE = ${LRED}30720${NC}"
echo -e "${LBLUE}AVRDUDE_ARD_BAUDRATE = ${LRED}115200${NC}"
echo -e "${DGRAY}#ISP_LOW_FUSE = ${RED}0xFF${NC}"
echo -e "${DGRAY}#ISP_HIGH_FUSE = ${RED}0xDA${NC}"
echo -e "${DGRAY}#ISP_EXT_FUSE = ${RED}0x05${NC}"
echo -e "${DGRAY}#ISP_LOCK_FUSE_PRE = ${RED}0x3F${NC}"
echo -e "${DGRAY}#ISP_LOCK_FUSE_POST = ${RED}0x0F${NC}"
echo -e "${LBLUE}MCU = ${LRED}$MCU${NC}"
echo -e "${LBLUE}F_CPU = ${LRED}$F_CPU${NC}"
echo -e "${LBLUE}VARIANT = ${LRED}standard${NC}"
echo -e "${LBLUE}ARDUINO_LIBS =${NC}"
}
initializeProject()
{
read -p "Project name: " PROJECT_NAME
if [ ! -e PROJECT_NAME ]; then
mkdir $PROJECT_NAME
cd $PROJECT_NAME
makefile #Call function that makes makefile
cp -r /usr/lib/avrduino/data/include/ .
echo -e "${LGREEN}Project created successfully ${NC}"
else
echo "AVRduino: Project with name [ $PROJECT_NAME ] already exists. "
echo "AVRduino: Stop project wizard and exit."
fi
}
clear
initializeProject
. doesn't execute the script as a process, it only loads it into your current shell process.
In that context, your "shebang" line, #!/bin/bash, is just a comment.
(You can put #! doodle poodle noodle there and it will run just as well.)
When you use system, it executes in /bin/sh, and thus your bash script has syntax errors.
One way to execute scripts is to make them executable:
chmod +x /usr/lib/avrduino/script/initialize.sh
and then you can just pass it directly to system:
system("/usr/lib/avrduino/script/initialize.sh");
Or, you could explictly execute it in bash:
system("/bin/bash /usr/lib/avrduino/script/initialize.sh");
OK, here's a way one might solve your "changing directory" problem:
Rewrite initialize.sh so it takes the project name as an argument instead of asking for it interactively (that's how normal Unix tools work, so stick with it).
Then add the following to your .bashrc:
make_project()
{
/usr/lib/avrduino/script/initialize.sh "$1" && cd "$1"
}
Then you can say make_project foo and get transported to the directory "foo".
Most likely it is the misplaced shebang causing a default shell to be run - make sure the shebang is at the beginning of the first line
#!/bin/bash
# rest of script
If that does not work change your system call to
system("/bin/bash /usr/lib/avrduino/script/initialize.sh");