How to read flags on jenkins jobs - python

We have two scripts (one for Katalon and one for Python) that we want to launch from Jenkins.
First we want to launch Katalon and, at a certain point in the script, tell Jenkins that launch the python script. Then finished the python script, Jenkins should tell katalon that can continue.
Current jenkins pipeline code:
"pipeline {
agent any
stages {
stage('Unit Test') {
steps {
echo 'Hello Example'
bat """./katalon -noSplash -runMode=console projectPath="/Users/mypc/project/proyect1/example.prj" -retry=0 -
testSuitePath="Test Suites/IOS/TestSuiteAccount" -executionProfile="default" -
deviceId="example" -browserType="iOS" """
sleep 5
}
}
stage('Unit Test2') {
steps {
echo 'Start second test'
bat """python C:\\Users\\myPC\\Documents\\project\\project-katalon-code\\try_python.py"""
sleep 5
}
}
}
}"
In pseudocode it would be the following:
Katalon script:
my_job()
call_jenkins_to_start_python()
if jenkins.python_flag == True
my_job_continue()
Pipeline Jenkins script:
Katalon.start()
if katalon_sent_signal_to_start_python == True
start_python_job()
if python_finished_job_signal == True
send_katalon_signal_to_continue()
Would be a good solution to read/write an external file? Didn't find anything similar.
Thank you!

AFAIK, jenkins starts a sparate process for the bat () step and waits for it to finish. Also, communication between jenkins and the bat process is not possible: you trigger the script and you read the returned value and, if needed, stdout.
Additionally, I do not know if it is possible what you want, because I do not know Katalon at all. What you want requires Katalon waiting for a result from the python script and then, when this results reach Katalon, it should resume its execution.
I recommend you to first try the process without Jenkins: Create a windows script that does exactly what you want. If you are able to do that, you can then call that new script from jenkins, giving input or reading outputs as needed. Even as you suggested, using files for that.

Related

Minimize Process from powershell/python using Process ID (PID)

I'm writing a python script for an app lock. For this, I'm executing the Get-Process -Name "notepad++" PowerShell command using python subprocess to get the process id.
Now, using psutil I'm able to kill the process. But my objective is to minimize the windows in a while loop either using powershell/python. So, the program becomes unusable until the user enters the password.
With Powershell, you could do it with the use of the UIAutomationClient methods without having to rely on native calls.
Here is a small example to demonstrate how to check the window state and minimize the window if not.
Add-Type -AssemblyName UIAutomationClient
$MyProcess = Get-Process -Name "notepad++"
$ae = [System.Windows.Automation.AutomationElement]::FromHandle($MyProcess.MainWindowHandle)
$wp = $ae.GetCurrentPattern([System.Windows.Automation.WindowPatternIdentifiers]::Pattern)
# Your loop to make sure the window stay minimized would be here
# While...
$IsMinimized = $wp.Current.WindowVisualState -eq 'Minimized'
if (! $IsMinimized) { $wp.SetWindowVisualState('Minimized') }
# End While
Reference
How to switch minimized application to normal state

Make a batch file to run python script for a certain amount of time and then auto-closes after

I need to make a batch file to delete a file created each time a script starts, then, set a timer to stop the python script and exit it to run another command line after a certain amount of time. Here's the code, I can't find any solution. The thing is that the python program is a bit complicated and I can't modify it so all I can do is create a batch or powershell file that does this task indefinitely. Here's the batch file :
cd C:\Users\Admin\Desktop\instabot.py-master
del -f user.session
instabot-py #user
#Here I need a line to stop the instabot-py running on cmd maybe using a condition before launching the script
I feel lost I can't figure out a way to do that, and the must would be a loop that does all this code again and again every 5 minutes for example (runs the python program for 5 minutes then closes it and start again at with cd, del, launches the program and stops it again.
PS : I'm on windows 10 x64.
You can use powershell jobs for that:
$myScript = {
cd c:\some\folder
python .\py_script.py
}
$timeOut = 10
while ($true){
$job = start-job -ScriptBlock $myScript
Start-Sleep $timeOut
Remove-Item -Path .\some.file
stop-job -Job $job
Remove-Job -Job $job
}

How to monitor Python scripts using Sensu?

I would like to use Sensu Core to monitor python scripts and I am confused how to do it.
From Sensu documentation this requires Sensu Checks. In the provided example ruby script checks that chef-client is running:
#!/usr/bin/env ruby
# get the current list of processes
processes = `ps aux`
# determine if the chef-client process is running
running = processes.lines.detect do |process|
process.include?('chef-client')
end
# return appropriate check output and exit status code
if running
puts 'OK - Chef client process is running'
exit 0
else
puts 'WARNING - Chef client process is NOT running'
exit 1
end
How to implement such a check for a specific script and not the application? That is, how would I go about monitoring a specific python script (e.g. test.py) and not python in general?
So, I have been running some python scripts in sensu for my AWS Linux clients successfully , this is a good example of my check definition:
{
"checks": {
"check-rds-limit": {
"interval": 86400,
"command": "/etc/sensu/plugins/rds-limit-check.py",
"contacts": [
"cloud-ops"
],
"occurrences": 1,
"subscribers": [
"cloud-ops-subscription"
],
"handlers": [
"email",
"slack"
]
}
}
}
And your python plugin can start with defining the shebang path:
#!/usr/bin/env python
import sys
...
...
//<code condition for warning>
sys.exit(1)
//<code condition for critical>
sys.exit(2)
//<code condition where everything is fine>
sys.exit(0)
More generally the above script is searching for the string chef-client in the running processes. You could replace that with any other string, like test.py, which would detect if any program running has test.py in its name. (You might need to match the sub-string test.py if you run the program with python test.py, I don't know ruby.)
I would suggest that you use Sensu process check plugin for a more general function which includes more customization. Look at the other sensu plugins too.
Why not monitor the expected result or operation of the script rather than the process itself. Typically, we will setup checks to monitor an end point, in the case of a web application, or an observed behavior, such as messages in a database, to determine if the application is running.
There will be times when the process is technically running but not anything due to an error condition or resource issue. Monitoring the expected result is a much better option than watching a process.

Run a python script in perl

I have two scripts, a python script and a perl script.
How can I make the perl script run the python script and then runs itself?
Something like this should work:
system("python", "/my/script.py") == 0 or die "Python script returned error $?";
If you need to capture the output of the Python script:
open(my $py, "|-", "python2 /my/script.py") or die "Cannot run Python script: $!";
while (<$py>) {
# do something with the input
}
close($py);
This also works similarly if you want to provide input for the subprocess.
The best way is to execute the python script at the system level using IPC::Open3. This will keep things safer and more readable in your code than using system();
You can easily execute system commands, read and write to them with IPC::Open3 like so:
use strict;
use IPC::Open3 ();
use IO::Handle (); #not required but good for portabilty
my $write_handle = IO::Handle->new();
my $read_handle = IO::Handle->new();
my $pid = IPC::Open3::open3($write_handle, $read_handle, '>&STDERR', $python_binary. ' ' . $python_file_path);
if(!$pid){ function_that_records_errors("Error"); }
#read multi-line data from process:
local $/;
my $read_data = readline($read_handle);
#write to python process
print $write_handle 'Something to write to python process';
waitpid($pid, 0); #wait for child process to close before continuing
This will create a forked process to run the python code. This means that should the python code fail, you can recover and continue with your program.
It may be simpler to run both scripts from a shell script, and use pipes (assuming that you're in a Unix environment) if you need to pass the results from one program to the other

Cannot Launch Interactive Program While Piping to Script in Python

I have a python script that needs to call the defined $EDITOR or $VISUAL. When the Python script is called alone, I am able to launch the $EDITOR without a hitch, but the moment I pipe something to the Python script, the $EDITOR is unable to launch. Right now, I am using nano which shows
Received SIGHUP or SIGTERM
every time. It appears to be the same issue described here.
sinister:Programming [1313]$ echo "import os;os.system('nano')" > "sample.py"
sinister:Programming [1314]$ python sample.py
# nano is successfully launched here.
sinister:Programming [1315]$ echo "It dies here." | python sample.py
Received SIGHUP or SIGTERM
Buffer written to nano.save.1
EDIT: Clarification; inside the program, I am not piping to the editor. The code is as follows:
editorprocess = subprocess.Popen([editor or "vi", temppath])
editorreturncode = os.waitpid(editorprocess.pid, 0)[1]
When you pipe something to a process, the pipe is connected to that process's standard input. This means your terminal input won't be connected to the editor. Most editors also check whether their standard input is a terminal (isatty), which a pipe isn't; and if it isn't a terminal, they'll refuse to start. In the case of nano, this appears to cause it to exit with the message you included:
% echo | nano
Received SIGHUP or SIGTERM
You'll need to provide the input to your Python script in another way, such as via a file, if you want to be able to pass its standard input to a terminal-based editor.
Now you've clarified your question, that you don't want the Python process's stdin attached to the editor, you can modify your code as follows:
editorprocess = subprocess.Popen([editor or "vi", temppath],
stdin=open('/dev/tty', 'r'))
The specific case of find -type f | vidir - is handled here:
foreach my $item (#ARGV) {
if ($item eq "-") {
push #dir, map { chomp; $_ } <STDIN>;
close STDIN;
open(STDIN, "/dev/tty") || die "reopen: $!\n";
}
You can re-create this behavior in Python, as well:
#!/usr/bin/python
import os
import sys
sys.stdin.close()
o = os.open("/dev/tty", os.O_RDONLY)
os.dup2(o, 0)
os.system('vim')
Of course, it closes the standard input file descriptor, so if you intend on reading from it again after starting the editor, you should probably duplicate its file descriptor before closing it.

Categories

Resources