Import cassandra schema script doesn't run in docker when using volumes - cassandra

I have a problem on using cassandra in Docker
I've created a Dockerfile this like
----------------------Dockerfile----------------------------
FROM spotify/cassandra:base
COPY cassandra-schema.cql /tmp/
COPY cassandra-init.sh /usr/local/bin/
COPY cassandra-singlenode.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/cassandra-init.sh
RUN chmod +x /usr/local/bin/cassandra-singlenode.sh
#import schema
RUN /usr/local/bin/cassandra-init.sh
EXPOSE 9160
ENTRYPOINT ["/usr/local/bin/cassandra-singlenode.sh"]
If i have use
docker run --name cassandradb cassandra
everything works properly but if i use
docker run --name cassandradb -v /opt/argus/cassandra/data/:/var/lib/cassandra/data -v /opt/argus/cassandra/commitlog:/var/lib/cassandra/commitlog cassandra
the cassandra starts but the /usr/local/bin/cassandra-init.sh doesn't import my scheam
Any idea?
These are my files contents
------------------cassandra-init.sh-----------------------
echo "===================================================="
echo "starting running cqlsh"
echo "===================================================="
cassandra &
while : ;do
# Get the status of this machine from the Cassandra nodetool
STATUS=`nodetool status | grep 'UN' | awk '{print $1}'`
echo $STATUS
# Once the status is Up and Normal, then we are ready
if [ $STATUS = "UN" ]; then
cqlsh -f /tmp/cassandra-schema.cql
break
fi
sleep 1;
done
----------------------------cassandra-singlenode.sh--------------------------------
echo "=============================================== Change configuration ====================================================="
IP=`hostname --ip-address`
SEEDS=`env | grep CASS[0-9]_PORT_9042_TCP_ADDR | sed 's/CASS[0-9]_PORT_9042_TCP_ADDR=//g' | sed -e :a -e N -e 's/\n/,/' -e ta`
if [ -z "$SEEDS" ]; then
SEEDS=$IP
fi
echo "Listening on: "$IP
echo "Found seeds: "$SEEDS
# Setup Cassandra
CONFIG=/etc/cassandra/
sed -i -e "s/^listen_address.*/listen_address: $IP/" $CONFIG/cassandra.yaml
sed -i -e "s/^rpc_address.*/rpc_address: 0.0.0.0/" $CONFIG/cassandra.yaml
sed -i -e "s/- seeds: \"127.0.0.1\"/- seeds: \"$SEEDS\"/" $CONFIG/cassandra.yaml
sed -i -e "s/# JVM_OPTS=\"$JVM_OPTS -Djava.rmi.server.hostname=<public name>\"/ JVM_OPTS=\"$JVM_OPTS -Djava.rmi.server.hostname=$IP\"/" $CONFIG/cassandra-env.sh
echo "=========================================================================================================================="
echo "starting running cassandra server"
echo "=========================================================================================================================="
cassandra &
while :
do
echo "Cassandra running, Press [CTRL+C] to stop.."
sleep 1
done

Related

Terraform error Bad substitution while running shell script

I'm executing shell script in terraform and getting Bad Substitution and getting below error:
module.bastion_ubu.aws_instance.ubuntu_bastion (remote-exec): /home/ubuntu/create.sh: 101: /home/ubuntu/create.sh: Bad substitution.
Code:
#!/bin/bash
tools_list="nginx/ldap/mongo/cloveplatformui"
tools_list1=`echo $tools_list | sed 's/ //g'`
tools_list2=`echo $tools_list1 | sed 's/$/\//'`
tools_list3=`echo $tools_list2 | sed 's#/#.yml #g'`
tools_list4=`echo $tools_list2 | sed 's#/#.conf #g'`
#tools_list_array=($tools_list6)
#tools_conf_array=($tools_list8)
#echo $tools_list_array
sudo touch docker-compose.yml
sudo chmod -R 755 docker-compose.yml
sudo chown -R ubuntu:ubuntu docker-compose.yml
sudo echo '---' >> docker-compose.yml
sudo echo 'version: "3.3"' >> docker-compose.yml
sudo echo 'services:' >> docker-compose.yml
# Print array values in one line
echo "Print all elements in a single line"
for val in "${tools_list3[#]}\" ; do
sudo cat $val >> docker-compose.yml
done```

Calling multiple shell scripts within a script on different virtual machines

I am trying to create shell scripts which will setup Zookeeper Server in one VM, and its corresponding Zookeeper Clients in different VM's so i written a shell script as below
#!/bin/bash
ZOOKEEPER_SERVER_IP="1.2.3.4"
while read ipaddress zookeepertype number
do
echo -e "Setting up the Zookeepers \n"
echo $ipaddress
if [ "${zookeepertype}" = 'zookeeperserver' ]; then
echo "Setup Zookeeper Server"
#ZOOKEEPER_SERVER_IP = $ipaddress
#echo $ZOOKEEPER_SERVER_IP
#echo $ipaddress
sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/ZooKeeper_Server_Script.sh ubuntu#$ipaddress:/home/ubuntu/
ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress /home/ubuntu/ZooKeeper_Server_Script.sh
echo "This script is about to run ZooKeeper_Server_Script."
echo "The server script has completed.";
#sleep 30
exit 1
fi
echo -e $ZOOKEEPER_SERVER_IP
if [ $zookeepertype = "zookeeperclient" ] ; then
echo "Setup Zookeeper Client"
echo $ipaddress
sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/ZooKeeper_Client_Script.sh ubuntu#$ipaddress:/home/ubuntu/
ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress
#mkdir /home/ubuntu/keyfiles
#exit
#sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/abc/network/test/keyfiles/* ubuntu#$ipaddress:/home/ubuntu/keyfiles
#sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/abc/test/simple/abc.json ubuntu#$ipaddress:/home/ubuntu/
#ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress
#chmod 777 ZooKeeper_Client_Script.sh
#echo "This script is about to run ZooKeeper_Client_Script."
#sh ./ZooKeeper_Client_Script.sh $ZOOKEEPER_SERVER_IP
echo "The client script has completed."
#exit
fi
#Separating Runhosts File
done < setupZkinput.txt
the input file is
1.2.3.4 zookeeperserver 1
5.6.7.8 zookeeperclient 2
9.10.11.12 zookeeperclient 3
The issue that i am facing is
1) Only the server setup is being done , i.e the script is exiting after the first line
2)Not able to assign the server ip dynamically , in the line ZOOKEEPER_SERVER_IP = $ipaddress
Thanks
What is the default permission you are setting up after copying a file to the server?
remember it required execute permission in order to execute script.

DOCKER_OPTS are reset after system reboot

I am specifying my TLS certs in /etc/default/docker, like this:
DOCKER_OPTS="-H=unix:// --tlsverify --tlscacert=/etc/docker/mynewca.pem
--tlscert=/etc/docker/mynewcert.pem
--tlskey=/etc/docker/mynewkey.pem -H=0.0.0.0:2376"
However, every time my Docker host restarts, my settings are overridden with the defaults:
DOCKER_OPTS="-H=unix:// --tlsverify --tlscacert=/etc/docker/ca.pem
--tlscert=/etc/docker/cert.pem
--tlskey=/etc/docker/key.pem -H=0.0.0.0:2376"
This means that I can not communiate with the Docker daemon remotely until I reconfigure DOCKER_OPTS and run
sudo service restart docker
upstart is starting the Docker daemon, and it looks like the script section of /etc/init/docker.conf is overriding DOCKER_OPTS, although I can't find where it's getting the defaults from.
script
# modify these in /etc/default/$UPSTART_JOB (/etc/default/docker)
DOCKERD=/usr/bin/dockerd
DOCKER_OPTS=
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi
exec "$DOCKERD" $DOCKER_OPTS --raw-logs
end script
# Don't emit "started" event until docker.sock is ready.
# See https://github.com/docker/docker/issues/6647
post-start script
DOCKER_OPTS=
DOCKER_SOCKET=
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi
if ! printf "%s" "$DOCKER_OPTS" | grep -qE -e '-H|--host'; then
DOCKER_SOCKET=/var/run/docker.sock
else
DOCKER_SOCKET=$(printf "%s" "$DOCKER_OPTS" | grep -oP -e '(-H|--host)\W*unix://\K(\S+)' | sed 1q)
fi
if [ -n "$DOCKER_SOCKET" ]; then
while ! [ -e "$DOCKER_SOCKET" ]; do
initctl status $UPSTART_JOB | grep -qE "(stop|respawn)/" && exit 1
echo "Waiting for $DOCKER_SOCKET"
sleep 0.1
done
echo "$DOCKER_SOCKET is up"
fi
end script
Which
You may want to use the docker configuration file that is usually located in /etc/docker/daemon.json. See here for more information on the configuration:
https://docs.docker.com/engine/reference/commandline/dockerd//#daemon-configuration-file
In your case, the "tlscacert" option might be of special interest.
Nevertheless, the location of the configuration file may really depend on the OS and distribution (I remember the famous Gentoo /etc/conf.d/ directory)

CoreOS - get docker container name by PID?

I have a list of PID's and I need to get their docker container name. Going the other direction is easy ... get PID of docker container by image name:
$ docker inspect --format '{{.State.Pid}}' {SOME DOCKER NAME}
Any idea how to get the name by PID?
Something like this?
$ docker ps -q | xargs docker inspect --format '{{.State.Pid}}, {{.ID}}' | grep "^${PID},"
[EDIT]
Disclaimer This is for "normal" linux. I don't know anything useful about CoreOS, so this may or may not work there.
Because #Mitar's comment suggestion deserves to be a full answer:
To get container ID you can use:
cat /proc/<process-pid>/cgroup
Then to convert the container ID to docker container name:
docker inspect --format '{{.Name}}' "${containerId}" | sed 's/^\///'
... as a one-liner as well
PID=20168; sudo docker ps --no-trunc | grep $(cat /proc/$PID/cgroup | grep -oE '[0-9a-f]{64}' | head -1) | sed 's/^.* //'
I use the following script to get the container name for any host PID of a process inside a container:
#!/bin/bash -e
# Prints the name of the container inside which the process with a PID on the host is.
function getName {
local pid="$1"
if [[ -z "$pid" ]]; then
echo "Missing host PID argument."
exit 1
fi
if [ "$pid" -eq "1" ]; then
echo "Unable to resolve host PID to a container name."
exit 2
fi
# ps returns values potentially padded with spaces, so we pass them as they are without quoting.
local parentPid="$(ps -o ppid= -p $pid)"
local containerId="$(ps -o args= -f -p $parentPid | grep docker-containerd-shim | cut -d ' ' -f 2)"
if [[ -n "$containerId" ]]; then
local containerName="$(docker inspect --format '{{.Name}}' "$containerId" | sed 's/^\///')"
if [[ -n "$containerName" ]]; then
echo "$containerName"
else
echo "$containerId"
fi
else
getName "$parentPid"
fi
}
getName "$1"

shell script ssh command not working

I have a small list of servers, and I am trying to add a user on each of these servers. I can ssh individually to each server and run the command.
sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
I wrote a script to loop through the list and run this command but I get some errors.
#!/bin/bash
read -p "Enter server list: " file
if [[ $file == *linux* ]]; then
for i in `cat $file`
do
echo "creating amurug on" $i
ssh $i sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
echo "==============================================="
sleep 5
done
fi
When I run the script it does not execute the command.
creating amurug on svr102
Usage: useradd [options] LOGIN
Options:
What is wrong with my ssh crommand in my script?
Try this script:
#!/bin/bash
read -p "Enter server list: " file
if [[ "$file" == *linux* ]]; then
while read -r server
do
echo "creating amurug on" "$server"
ssh -t -t "$server" "sudo /usr/sbin/useradd -c Arun -d /home/amurug \
-e 2014-12-12 -g users -u 1470 amurug"
echo "==============================================="
sleep 5
done < "$file"
fi
As per man bash:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Resources