asdf and terraform (or vault or packer)

Using the asdf version manager to manage versions of various binaries like terraform, vault or packer is easily done by following the instructions below:

  1. Download and install asdf if you haven’t got it already (https://asdf-vm.com/#/core-manage-asdf)
  2. If you want to install the latest version of terraform for example do the following:
asdf plugin add terraform
asdf install terraform latest
asdf global terraform 0.14.9
asdf list
terraform
  0.14.9
terraform --version
Terraform v0.14.9

Automatic emails using bash and sendgrid

Problem

You would like to be able to automatically send some emails (ie reports) from a linux server, using a bash script that runs in cronjob, without installing an email server on the linux server.

Solution

  • Create an account in Sendgrid and follow the directions for using WebAPI with curl
    here
  • Set up your bash script to use the API key and have a script like the following. If you want to use big or multiple files you will need to use a temporary file for the base64 encoding, as in the example below, as there is a limit in curl.
    #!/bin/bash
    
    # Email setup
    SENDGRID_API_KEY="your_sendgrid_api_key"
    FILENAME_ATTACH="title_of_your_attachment"
    FILENAME_ZIP="the_path_to_your_zip_file"
    FILENAME_BASE64_TMP="the_path_to_temporary_base64_encoding"
    EMAIL_TO="email_to_address"
    EMAIL_SUBJECT="email_subject"
    EMAIL_FROM="email_from_address"
    EMAIL_MESSAGE="your_email_message"
    
    function email_exports()
    {
      FILENAME_BASE64=$(base64 -w0 $FILENAME_ZIP);
    
      REQUEST_DATA='{"personalizations": [{
                            "to": [{ "email": "'"$EMAIL_TO"'" }],
                            "subject": "'"$EMAIL_SUBJECT"'"
                    }],
                    "from": {
                            "email": "'"$EMAIL_FROM"'"
                            },
                    "content": [{
                            "type": "text/plain",
                            "value": "'"$EMAIL_MESSAGE"'"
                            }],
                    "attachments": [{
                            "content": "'"$FILENAME_BASE64"'",
                            "filename": "'"$FILENAME_ATTACH"'"
                            }]
      }';
    
      # We need to store the base64 locally as the text
      # is too big for sending directly with curl
       echo $REQUEST_DATA > $FILENAME_BASE64_TMP
    
       curl -X "POST" "https://api.sendgrid.com/v3/mail/send" \
            -H "Authorization: Bearer $SENDGRID_API_KEY" \
            -H "Content-Type: application/json" \
            -d "@$FILENAME_BASE64_TMP";
    
       rm $FILENAME_BASE64_TMP
    }
    
    email_exports
  • Add your script to crontab
  • Bash getting the parent folder name from a bash defined variable

    Problem

    You have defined a variable in a bash script (ie MY_FOLDER) but you also want to dynamically get the parent folder (ie MY_FOLDER can have multiple values).

    Solution

    Use the dirname as in the following

    MY_FOLDER=/home/user/first_folder/second_folder
    MY_PARENT_FOLDER=$(dirname $MY_FOLDER)
    
    echo $MY_FOLDER
    echo $MY_PARENT_FOLDER
    
    .......
    /home/user/first_folder/second_folder
    /home/user/first_folder

    Removing a range of history lines from bash history

    Problem

    You would like to remove some lines from the bash history (ie using cut and paste with wrong values).

    Solution

    You can use the following, taken from the answer here

    for i in {1..no_of_lines}; do history -d start_line; done
    

    where start_line is the start of the lines you want to remove and no_of_lines is the number of lines you want to be removed.