Bazel run - passing main arguments - bazel

I created an image using java_image but I would like to pass arguments to my main function (i.e. String args[]). How can I do that when I use "bazel run name_of_image" command?

bazel run //your:rule -- arg1 arg2 ... argN
Everything after -- is passed to the binary.

When using java_image, bazel run //my_image will only load the image into the Docker daemon, it won't run it. To run it, you should use:
docker run bazel/my_image:my_image [args]

Related

Is it possible to run a command in a Docker image as a test in Bazel?

I would like to run a command inside a container to test that it works. It should be invoked by bazel test.
Something like this:
container_test(
image = "//:my_image"
test_command = "exit 1"
)
I noticed this: https://github.com/bazelbuild/rules_docker/blob/master/contrib/test.bzl#L125
However it isn't documented.
How should I approach this in Bazel?
Take a look at the sample test rule here
This is a test rule which creates a script (script) that can be invoked in the CLI
The script will then exit with a non-zero error-code to indicate that the test failed (or 0 for success)
The script is then written as an executable output (ctx.actions.write), declares the list of files it needs available at runtime (runfiles)
This python function is then wrapped as a bazel rule (see full guide here)
So, how would you proceed towards creating your container test rule?
The script we want to generate above is probably some usage of docker run --rm IMAGE [COMMAND] [ARG...] to create a container from an image, run a command, and remove the container when done
Don't forget to set the script exit status based on the exit status of the docker command (as done in the example, where they copy the exit status of grep as the exit status for the overall script)
Update the sample above to use the above docker command, and plant the path to the image accordingly
See f.path in the script above showing how they access the path of an individual source file
You will need to make sure docker is available when your bazel rules are evaluated
I haven't done this fully myself since I don't have a computer with both bazel and docker, but this should be enough to get you started :)
Good luck!

Set line-buffering in container output

I use Java S2I image for a container running in Openshift (on premise). My problem is that the output of the image is page-buffered and oc logs ... does not show me the last logs.
I could probably spin up my docker image that would do stdbuf -oL -e0 java ... but I would prefer to stick to the 'official' image (just adding the jar to /deployments). Is there any way to reduce buffering (use line-buffering instead of page-buffering), or flush the output on demand?
EDIT: It seems that I could update deployment config and pass stdbuf in there, but that means that I'd have to compose all the args myself. Ideal solution would be passing --tty do Docker, but I can't see how a custom arguments could be passed that way in Openshift.
In your repo, try creating the file .s2i/bin/run. In it add:
#/bin/bash
exec stdbuf -oL -e0 /usr/local/s2i/run
I always forget where the S2I assemble and run scripts are in the Java S2I image, so you may need to replace /usr/local/s2i with the correct path.
What adding this file does is that it will be run as the startup command instead of the original run script. You can then run the original script with stdbuf. Ensure you use exec so that the sub process replaces the current one, else signals will not be propagated through properly.
Even though this might work, am surprised logging isn't working in an unbuffered mode already. I expect there would be a better way of controlling it through some Java config instead.

string concatenation with variables in Jenkins bat command

I have a simple Jenkins function / procedure:
def StartContainer() {
def SqlPort = BranchToPort[env.BRANCH_NAME]
bat "docker run -e \"ACCEPT_EULA=Y\" -e \"SA_PASSWORD=P#ssword1\" --name SQLLinux${env.BRANCH_NAME} -d -i -p $SqlPort:1433 microsoft/mssql-server-linux"
}
BranchToPort does exactly what I want it to do, the problem I have is plugging the value it returns into the following call to bat, I've tried all sorts of things and this either results in language compile errors with the groovy script or the bat command ending immediately after the -p command. There is obviously something really simple I'm missing here.
My issue is down to scoping the Groovy map is declared outside of the scope of the method that spins up the container, if I move the declaration of the map into the same method as the one that starts the container, it works.

How can I run a bash command in Ruby?

I have a script in Ruby and inside has to run a bash command. This command is an export http_proxy = "" and export https_proxy = "".
The problem is that I run the following, without running errors, but appears that doesn't make any change:
system "export http_proxy=''"
system "export https_proxy=''"
I created a file.sh with this lines and if I run it in a terminal only works when run: source file.sh or . file.sh.
Could you please help me how I can run this commands in the Ruby script? It can be directly with the command lines or executing an .sh file in the script.
When you run a separate process using system, any changes made to the environment of that process affects that process only, and will disappear when the process exits.
That exactly why running file.sh won't change your current shell since it runs it as a sub-shell, and the changes disappear when the process exits.
As you've already discovered, using source or . runs deoes affect the current shell but that's because it runs the script not as a sub-shell, but within the context of the current shell.
If you want to change the environment variables of the current Ruby script, you should look into ENV, something like:
ENV["http_proxy"] = ""
ENV["https_proxy"] = ""
%x( echo 'hi' )
and to capture standard output in a variable
var = %x( echo 'hi' )

Execute script with Ruby on Rails?

I want to start my daemon with my application.
In the command line, I can write something like lib/daemons/mydaemon_ctl start to start up my daemon, but I have to do this manually. I want the daemon to start when I start my server (i.e. when the initializer files are loaded).
Is there a ruby command for executing a command line?
Something like exec "lib/daemons/mydaemon_ctl start"?
Thanks!
Seems you just want to run shell commands in ruby code, well you can use system or backtick(`)
system 'ls' # will return ls output in *nix
`dir` # will return dir output in windows

Resources