Jenkins - Publish Over SSH - EXEC: STDOUT/STDERR - bash: service: command not found - bash

I have issue with Publish Over SSH plugin and already try so many possible solution, but still doesn't work:
using exec in pty
using bash --login
using shebang (#!/usr/bin/env bash)
Exec Script
service monitoring-daemon stop
cd /home/push/monitoring/target
rm -rf Monitoring.jar
ls -la | grep Monitoring | grep -v grep | awk '{print $9}' | xargs -I file mv file Monitoring.jar
service monitoring-daemon start
Console Output
10:26:25 SSH: EXEC: STDOUT/STDERR from command [service monitoring-daemon stop
10:26:25 cd /home/push/monitoring/target
10:26:25 rm -rf Monitoring.jar
10:26:25 ls -la | grep Monitoring | grep -v grep | awk '{print $9}' | xargs -I file mv file Monitoring.jar
10:26:25 service monitoring-daemon start] ...
10:26:25 SSH: EXEC: connected
10:26:25 bash: service: command not found
10:26:25 bash: line 4: service: command not found
10:26:25 SSH: EXEC: completed after 201 ms
10:26:25 SSH: Disconnecting configuration [*********] ...
10:26:25 ERROR: Exception when publishing, exception message [Exec exit status not zero. Status [127]]
10:26:25 Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE
10:26:25 [ANALYSIS-COLLECTOR] Computing warning deltas based on reference build #48
My Jenkins is using bash as default shell, and remote server also bash.
Remote Server .bashrc
# .bashrc
alias service='/sbin/service'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
Remote Server .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
is there any piece of configuration or step that I'm missing?

Try putting the full path for "service", ie.. replace "service" with "/usr/sbin/service"
Adding a first line like below might also help set your path
. /etc/profile

Related

Docker container unable to ignore the EntryPoint bash script failure

Bash script:
clonePath=/data/config/
git branch -r | fgrep -v 'origin/HEAD' | sed 's| origin/|git checkout |' > checkoutAllBranches.sh
chmod +x checkoutAllBranches.sh
echo "Fetch branch: `cat checkoutAllBranches.sh`"
./checkoutAllBranches.sh
git checkout master
git remote rm origin
rm checkoutAllBranches.sh
for config_dir in `ls -a`; do
cp -r $config_dir $clonePath/;
done
echo "API Config update complete..."
Dockerfile which issues this script execution
ENTRYPOINT ["sh","config-update-force.sh","|| true"]
The error below causes the container startup failure despite setting the command status to 0 manually using || true
ERROR:
Error:
cp: cannot create regular file '/data/./.git/objects/pack/pack-27a9d...fb5e368e4cf.pack': Permission denied
cp: cannot create regular file '/data/./.git/objects/pack/pack-27a9d...fbae25e368e4cf.idx': Permission denied
I am looking for 2 options here:
Change these file permissions and then store them in the remote with rwx permissions
Do something to the docker file to ignore this script failure error and start the container.
DOCKERFILE:
FROM docker.hub.com/java11-temurin:latest
USER root
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get install -y rsync telnet vim wget git
RUN mkdir -p /opt/config/clone/data
RUN chown -R 1001:1001 /opt/config
USER 1001
ADD build/libs/my-api-config-server.jar .
ADD config-update-force.sh .
USER root
RUN chmod +x config-update-force.sh
USER 1001
EXPOSE 8080
CMD java $BASE_JAVA_OPTS $JAVA_OPTS -jar my-api-config-server.jar
ENTRYPOINT ["sh","config-update-force.sh","|| true"]
BASH SCRIPT:
#!/bin/bash
set +e
set +x
clonePath=/opt/clone/data/data
#source Optumfile.properties
echo "properties loaded: example ${git_host}"
if [ -d my-api-config ]; then
rm -rf my-api-config;
echo "existing my-api-config dir deleted..."
fi
git_url=https://github.com/my-api-config-server
git clone https://github.com/my-api-config-server
cd my-api-config-server
git branch -r | fgrep -v 'origin/HEAD' | sed 's| origin/|git checkout |' > checkoutAllBranches.sh
chmod +x checkoutAllBranches.sh
echo "Fetch branch: `cat checkoutAllBranches.sh`"
./checkoutAllBranches.sh
git checkout master
git remote rm origin
rm checkoutAllBranches.sh
for config_dir in `ls -a`; do
cp -r $config_dir $clonePath/;
done
echo "My API Config update complete..."
When you do in the script...
chmod +x checkoutAllBranches.sh
...than why not before cp
chmod -R +rwx ${clonePath}
...or if the stderr message 'wont impact anything'...
cp -r $config_dir $clonePath/ 2>/dev/null;
...even cp dont copy -verbosly.
?
When your Dockerfile declares an ENTRYPOINT, that command is the only thing the container does. If it also declares a CMD, the CMD is passed as additional arguments to the ENTRYPOINT; it is not run on its own unless the ENTRYPOINT makes sure to execute it.
Shell errors are not normally fatal, and especially if you explicitly set +e, even if a shell command fails the shell script will keep running. You see this in your output where you get multiple cp errors; the first error does not terminate the script.
You need to do two things here. The first is to set the ENTRYPOINT to actually run the CMD; the simplest and most common way to do this is to end the script with
exec "$#"
The second is to remove the || true from the Dockerfile. As you have it written out currently, this is passed as the first argument to the entrypoint wrapper – it is not run through a shell and it is not interpreted as a "or" operator. If your script begins with a "shebang" line and is marked executable (both of these are correct in the question) the you do not explicitly need the sh interpreter.
# must be a JSON array; no additional "|| true" argument; no sh -c wrapper
ENTRYPOINT ["./config-update-force.sh"]
# any valid CMD will work with `exec "$#"
CMD java $BASE_JAVA_OPTS $JAVA_OPTS -jar my-api-config-server.jar

Why can't I use 'sudo su' within a shell script? How to make a shell script run with sudo automatically

I cannot figure out what's wrong with my bash script. When I run it in terminal, command by command run every command separately in terminal, it works.
#!/bin/bash
sudo su <<EOF
mkdir /home/ubuntu/backup/
cp "$(ls -t /usr/lib/unifi/data/backup/autobackup | head -1)" /home/ubuntu/backup/
curl --insecure --user root:password -T "$(ls -t /home/ubuntu/backup/ | head -1)" sftp://vps2.duckdns.org:/root/backup.unf
EOF
However, when I run the above bash script, give me plenty of erros
bash -v test.sh
#!/bin/bash
sudo su <<EOF
mkdir /home/ubuntu/backup/
cp "$(ls -t /usr/lib/unifi/data/backup/autobackup | head -1)" /home/ubuntu/backup/
curl --insecure --user root:password -T "$(ls -t /home/ubuntu/backup/ | head -1)" sftp://vps2.duckdns.org:/root/backup.unf
EOF
ls: cannot access '/usr/lib/unifi/data/backup/autobackup': Permission denied
ls: cannot access '/home/ubuntu/backup/': No such file or directory
cp: cannot stat '': No such file or directory
curl: (78) Could not open remote file for reading: SFTP server: No such file
Any help will be very much appreciated!
TIA
It's trying to execute the command substitutions in the original shell, which runs with the regular user's permissions. They need to be executed by su. Quote the EOF token to prevent expansions in the here-document.
sudo su <<'EOF'

how does bash do `ssh` autocompletion?

I've got the "bash-completion" package installed.
ssh completion on the command line (in bash) is working: ssh TAB-TAB will complete past used hosts and ssh -TAB-TAB will complete available ssh options.
However when I search for currently defined completions:
$ complete | grep ssh
complete -F _known_hosts ssh-installkeys
complete -F _service /etc/init.d/ssh
... I find that there's no completion registered for ssh ?!
complete -p ssh
bash: complete: ssh: no completion specification
When I check the ssh completions script under /usr/share/bash-completion/completions/ssh then I see that indeed it does register ssh completions:
$ grep complete /usr/share/bash-completion/completions/ssh | grep ssh | grep -v '^#'
shopt -u hostcomplete && complete -F _ssh ssh slogin autossh sidedoor
So why doesn't the ssh completion show up in complete | grep ssh? How does bash complete the ssh options?
If there is no completion defined for a command (or a function or whatever) then the "default" completer kicks in.
That default completer can be seen here:
$ complete -p -D
complete -F _completion_loader -D
When the bash-completion package is installed, then that will source /usr/share/bash-completion/bash_completion via /etc/bash.bashrc and that will assign the _completion_loader function.
_completion_loader will dynamically load specific completers upon completion request for commands the shell's completer doesn't know about yet (see here).
In the case of ssh, the terminals where I was doing the poking around had not yet loaded the ssh completer and so I was not seeing it with complete | grep ssh or complete -p ssh. It's only at the moment when you press TAB-TAB for the first time that the completer for ssh gets loaded.

bash: C:/Program: No such file or directory

I am new to Docker, Debezium, Bash, and Kafka. I am attempting to run the Debezium tutorial/example for MSSQL Server on Windows 10 here:
https://github.com/debezium/debezium-examples/blob/master/tutorial/README.md#using-sql-server
I am able to start the topology, per step one. However, when I go to step two and execute the following command:
cat debezium-sqlserver-init/inventory.sql | docker exec -i tutorial_sqlserver_1 bash -c '/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD'
I get the following error:
bash: C:/Program: No such file or directory
I do not have the foggiest idea why it would even drag C:/Program in to this. I do not see it in the command nor do I see it in the *.sql file. Does anyone know why this is happening and what the fix is?
Note 1: I am already in the current directory where this command should be runnable and there are no spaces in the folder/file path
Note 2: I am running the commands in Git Bash
When using set -x to log how the command is run, there's still no C:/Program anywhere in it, as can be seen by the following log:
$ cat debezium-sqlserver-init/inventory.sql | docker exec -i tutorial_sqlserver_1 bash -c '/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD'
+ cat debezium-sqlserver-init/inventory.sql
+ docker exec -i tutorial_sqlserver_1 bash -c '/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD'
bash: C:/Program: No such file or directory
I had a similar problem yesterday, the solution was adding a backslash before the absolute path, like :
cat debezium-sqlserver-init/inventory.sql | docker exec -i tutorial_sqlserver_1 bash -c '\/opt/mssql-tools/bin/sqlcmd -U sa -P $SA_PASSWORD'
\/opt/mssql-tools/bin/sqlcmd prevents conversion to Windows path.

Injecting a script to Pod with docker returns EOF

my goal is to execute a script once on a permanently running pod in kubernetes. The pod is called busybox-<SOME_ID> and finds itself in the namespace default.Therefore, I wrote this script - called scan-one-pod.sh:
#!/bin/bash
export MASTER_IP=192.168.56.102
export SCRIPT_NAME=script.sh
export POD_NAMESPACE=default
export POD_NAME=busybox
echo "echo HALLO" | ssh ubuntu#$MASTER_IP
export POD_ID=$(kubectl get po | grep busybox | sed -n '1p'|awk '{print $1}')
kubectl cp $SCRIPT_NAME $POD_NAMESPACE/$POD_ID:.
kubectl exec $POD_ID -- chmod +x $SCRIPT_NAME
export CONTAINER_ID=$(kubectl describe pod busybox | grep 'Container ID' | sed -n '1p'|awk '{print $3}')
ssh -t ubuntu#$MASTER_IP "sudo docker exec -u root $CONTAINER_ID -- ./script.sh"
The referred script script.sh has the following content:
$ kubectl exec $POD_ID -- cat script.sh
#!/bin/bash
echo "test" >> test
cp test test-is-working
However, it is not possible to run the script on the pod:
the files test and test-is-working are not created
the script scan-one-pod.sh returns just EOF:
$ ./scan-one-pod.sh
Pseudo-terminal will not be allocated because stdin is not a terminal.
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-87-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
155 Software-Pakete können aktualisiert werden.
72 Aktualisierungen sind Sicherheitsaktualisierungen.
HALLO
[sudo] Passwort für ubuntu:
EOF
Connection to 192.168.56.102 closed.
If I execute the docker-command directly, remote on my kubernetes-controller, I get the same message of EOF:
ubuntu#controller:~$ export CONTAINER_ID=$(kubectl describe pod busybox | grep 'Container ID' | sed -n '1p'|awk '{print $3}')
ubuntu#controller:~$ sudo docker exec -u root $CONTAINER_ID ./script.sh
EOF
If I execute it from my local workstation via kubectl exec I get this error:
$ kubectl exec $POD_ID ./script.sh
rpc error: code = 13 desc = invalid header field value "oci runtime error: exec failed: container_linux.go:247: starting container process caused \"no such file or directory\"\n"
I don't know, which missing file they are referring to, but the script.sh-file is present and the busybox-pod seems to be running:
$ kubectl exec $POD_ID ls script.sh
script.sh
$ kubectl get po busybox-6bdf9b5bbc-4skds
NAME READY STATUS RESTARTS AGE
busybox-6bdf9b5bbc-4skds 1/1 Running 10 12d
Question: As far as I know, EOF means End-Of-File. End of which file would be important for me to know, and why is that a problem?
Thanks in advance, any help is appreciated :)

Resources