Add timestamp to name of created VM's instance in bash script - bash

I deploy some VM's instances in my cloud infrastructure with bash script:
#!/bin/bash
instance_name="vm"
# create instance
yc compute instance create \
--name $instance_name \
--hostname reddit-app \
--memory=2 \
...
I need to add timestamp to instance's name in format vm-DD-MM_YYYY-H-M-S.
For debug I tried to set value instance_name=$(date +%d-%m-%Y_%H-%M-%S) but got the error:
ERROR: rpc error: code = InvalidArgument desc = Request validation error: Name: invalid resource name
Any help would be appreciated.

The Yandex Cloud documentation says:
"The name may contain lowercase Latin letters, numbers, and hyphens. The first character must be a letter. The last character can't be a hyphen. The maximum length of the name is 63 characters".
I changed my script following the recommendations and it works now:
#!/bin/bash
instance_name="vm-$(date +%d-%m-%Y-%H-%M-%S)"
# create instance
yc compute instance create \
--name $instance_name \
--hostname reddit-app \
--memory=2 \
...

Related

How to resolve Unknown host ({snmp_args}) when using SNMPset while trying to set a store and repeat message on a Kapsch RSU

I am SSH'd into the Kapsch RSU with admin credentials and am attempting to manually set a Store and Repeat message on the device.
I am using the SNMP Set command and entered the following parameters. I am unsure what the {snmp_args} portion of the commmand does
admin#QZK00608:/home/kapsch$ snmpset {snmp_args} \
> rsuSRMPsid.1 x 8003 \
> rsuSRMDsrcMsgId.1 i 31 \
> rsuSRMTxMode.1 i 0 \
> rsuSRMTxChannel.1 i 176 \
> rsuSRMTxInterval.1 i 1000\
> rsuSRMDeliveryStart.1 x 07e20b071610 \
> rsuSRMDeliveryStop.1 x 07e80b07161a \
> rsuSRMPayload.1 x 07e40b07161a x 001f4d2010000000000266bccdb082b28e6568c461045380342800002fc25445f0e030800200393205a200ba3174a062df5b290f93d901d05dc036e7ec066877d0c34eba16e3d408364010c189408840 \
> rsuSRMEnable.1 i 0 \
> rsuSRMStatus.1 i 4
I return the error:
Unknown host ({snmp_args})
How do I properly set the host?
It looks like the snmpset command is treating {snmp_args} as a domain name, and trying to use DNS to resolve it into an IP address. You need to replace {snmp_args} with arguments, such as -v 2c (assuming you are using version SNMP2c), and either the community string, or the user credentials (if using SNMPv3), followed by the hostname or IP address of the machine you are trying to communicate with. You can find information about these arguments in the snmpcmd manual page by typing
man snmpcmd

how to load array parameter in another shell file dynamically over ssh connection

I need to call my executable which is placed in an on-prem server by using an ssh connection and pass a dynamics parameter.
based on my requirement, users should be able to add or remove parameters as they want to work with the executable on the on-prem server.
I wrote a translator to identify any new parameter added to the console but now when I want to pass it via ssh, I am facing 2 problems.
what if I have a value that contains space?
how to load these values dynamically & use them as arguments on my shell script on the server?
**Also take note that I am sending some additional parameters that are not related to my executable argument but I need them as well.
params=(
"$MASTER"
"$NAME"
"$QUEUE"
service.enabled=true
)
for var_name in "${!conf__#}";
do
key=${var_name#conf__};
key=${key//_/.};
value=${!var_name};
params+=( --conf "$key=$value" );
done
echo "${params[#]}"
ssh -o StrictHostKeyChecking=no myuser#server_ip "/bin/bash -s" < deploy_script.sh "${params[#]}"
My deploy_script.sh file will be something like the below file.
#!/bin/bash
set -e
AR_MASTER=${1}
AR_NAME=${2}
AR_QUEUE=${3}
AR_SER_EN=${4}
# How can I get the other dynamic parameters???
main() {
my-executable \
"--master "$AR_MASTER \
"--name "$AR_NAME \
"--queue "$AR_QUEUE \
"--conf service.enabled="$AR_SER_EN \
??? #how to add the additional configuration dynamically?
}
main "$#"
Would you mind help me in figure it out?

How to set variables using terragrunt before_hook

I need to use some gcloud commands in order to create a Redis instance on GCP as terraform does not support some options that I need.
I'm trying this:
terraform {
# Before apply, run script.
before_hook "create_redis_script" {
commands = ["apply"]
execute = ["REDIS_REGION=${local.module_vars.redis_region}", "REDIS_PROJECT=${local.module_vars.redis_project}", "REDIS_VPC=${local.module_vars.redis_vpc}", "REDIS_PREFIX_LENGHT=${local.module_vars.redis_prefix_lenght}", "REDIS_RESERVED_RANGE_NAME=${local.module_vars.redis_reserved_range_name}", "REDIS_RANGE_DESCRIPTION=${local.module_vars.redis_range_description}", "REDIS_NAME=${local.module_vars.redis_name}", "REDIS_SIZE=${local.module_vars.redis_size}", "REDIS_ZONE=${local.module_vars.redis_zone}", "REDIS_ALT_ZONE=${local.module_vars.redis_alt_zone}", "REDIS_VERSION=${local.module_vars.redis_version}", "bash", "../../../scripts/create-redis-instance.sh"]
}
The script is like this:
echo "[+]Creating IP Allocation Automatically using <$REDIS_VPC-network\/$REDIS_PREFIX_LENGHT>"
gcloud compute addresses create $REDIS_RESERVED_RANGE_NAME \
--global \
--purpose=VPC_PEERING \
--prefix-lenght=$REDIS_PREFIX_LENGHT \
--description=$REDIS_RANGE_DESCRIPTION \
--network=$REDIS_VPC
The error I get is:
terragrunt apply
5b35d0bf15d0a0d61b303ed32556b85417e2317f
5b35d0bf15d0a0d61b303ed32556b85417e2317f
5b35d0bf15d0a0d61b303ed32556b85417e2317f
ERRO[0002] Hit multiple errors:
Hit multiple errors:
exec: "REDIS_REGION=us-east1": executable file not found in $PATH
ERRO[0002] Unable to determine underlying exit code, so Terragrunt will exit with error code 1
I encountered the same issue and resigned myself to pass the values as parameters instead of environment variables.
It involves to modify the script and is a far less clearer declaration, but it works :|

Error while creating AWS DynamoDB Table using CLI

I am trying to create table in DynamoDB using CLI.
I am using below command:
aws dynamodb create-table \ --table-name my_table \--attribute-definitions 'AttributeName=Username, AttributeType=S' 'AttributeName=Timestamp, AttributeType=S' \--key-schema 'AttributeName=Username, KeyType=HASH' 'AttributeName=Timestamp, KeyType=RANGE' \--provisioned-throughput 'ReadCapacityUnits=5, WriteCapacityUnits=5' \--stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES \--region us-east-1
On running above, I am getting below error:
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: the following arguments are required: --attribute-definitions, --key-schema
I am new to AWS, in my command I am declaring the attributes and key-schema, what is the error?
The backlashes on the command you typed are used for telling the cmd, when there is a line break, that the command continues on the next line.
Based on the screenshot and command you typed, you are trying to execute it in a single line.
As a solution you could remove the backslashes from your command or copy the original command (the one from the tutorial) as it is (including line breaks).
Without line breaks:
aws dynamodb create-table --table-name my_table --attribute-definitions 'AttributeName=Username, AttributeType=S' 'AttributeName=Timestamp, AttributeType=S' --key-schema 'AttributeName=Username, KeyType=HASH' 'AttributeName=Timestamp, KeyType=RANGE' --provisioned-throughput 'ReadCapacityUnits=5, WriteCapacityUnits=5' --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES --region us-east-1
With line breaks:
aws dynamodb create-table \
--table-name my_table \
--attribute-definitions AttributeName=Username,AttributeType=S AttributeName=Timestamp,AttributeType=S \
--key-schema AttributeName=Username,KeyType=HASH AttributeName=Timestamp,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES \
--region us-east-1
I would try using a json file for both the key schema and the attribute definitions. See https://docs.aws.amazon.com/cli/latest/reference/dynamodb/create-table.html for the json syntax and examples. You shouldn’t need any other arguments other than the table to get your table running.

Passing a parameter's value to shell function prints only the name of the parameter

I need to pass a parameter to my shell function, which looks like this:
function deploy {
docker create \
--name=$1_temp \
-e test_postgres_database=$2 \
-e test_publicAddress="http://${3}:9696"\
# other irrelevant stuff
I am passing the following parameters:
deploy test_container test_name #1 test_database #2 ip_address #3
So when, I pass those 3 parameters, based on them a new container is created. However, the third parameter is something special. So there is another function, which gets the ip of the container.
function get_container_ip_address {
container_id=($(docker ps --format "{{.ID}} {{.Names}}" | grep $1))
echo $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${container_id[0]})
So, the execution of the deploy function actually looks like this:
ip_address=$(get_container_ip_address test_container)
deploy test_container test_database ip_address
Let's say the IP address of the container is 1.1.1.1, so the ip_address=1.1.1.1.
However, when I execute the script and create the container, its IP address is:
"http://ip_address:9696" and not "http://1.1.1.1:9696".
I also tried the following:
...
-e test_publicAddress="http://$3:9696"\
...
But I still got the same result. Is there a way I can get the value of the passed parameter? By the way, I am sure it contains the needed ip address as I use it elsewhere (not in a function) and I printed it for testing. Thank you in advance!
so run this like that:
ip_address=$(get_container_ip_address test_container)
deploy test_container test_database $ip_address
when you call it without the $ the script leave it alone like a string ip_address

Resources