Capistrano 3 Sudo Password Hangs - capistrano3

I have the following capistrano 3 task:
desc 'Add root scheduled tasks'
task :root_schedules do
on roles(:all) do
within release_path do
execute :sudo, :bundle, 'exec whenever -w config/root_schedule.rb -u root'
end
end
end
I need this task to run with sudo privileges because I need the scheduled tasks to run with higher privileges. These are backend scheduled server tasks. Problem is that everytime I run this command I get following:
DEBUG [46d3800c] [sudo] password for [user]
After I type my password and hit enter, it never completes anything. No response at all. Any answers out there?

I just created a gem for this purpose: sshkit-sudo
Add gem 'sshkit-sudo' to Gemfile
Add require 'sshkit/sudo' to Capfile
Now you can use sudo command in your Capistrano 3 task.

Capistrano kinda expects you to have this handled with a sudoers file, link so that password input is not required.
The direct answer to the question is: capistrano probably uses the default non-login, non-interactive shell. Since the shell is non-interactive, that probably explains why user input is ignored.
Read more about this here.

Related

How to enable that terminal reads from stdin [duplicate]

I have written scripts for Windows and Linux to essentially set up a new users workspace with all the git repositories from our server.
I would like the user to enter the password for our server once, store it in a local variable, pass that variable to each git pull command, then erase the password variable and exit.
How can I input the password when the git pull command requests it? Both for Windows batch file and a Linux shell script.
Here is code from the Linux script:
#!/bin/bash
echo "Enter password: "
read pswd
clear #No screen peaking
#This is repeated for each repo
location=folderName
mkdir $location
cd $location
git init
git remote add origin git#<server>:$location.git
git pull origin master
#Above prompts for password & is where I want to automatically input $pswd
I've tried various things recommended on SO and elsewhere, such as piping, reading from .txt file, etc. I would prefer to not need anything more than plain old windows cmd and Linux terminal commands. And as this script is just for set up purposes, I do not need to securely store the password permanently with something like ssh agent.
I'm running Windows 7 and Ubuntu 12.10, but this script is meant for setting up new users, so it should ideally work on most distributions.
Synopsis:
git pull "https://<username>:<password>#github.com/<github_account>/<repository_name>.git" <branch_name>
Example:
git pull "https://admin:12345#github.com/Jet/myProject.git" master
Note: This works for me on a bash script
I would really recommend to not try and manage that password step, and delegate that (both on Linux and Windows) to git credential helper.
See:
"Git http - securely remember credentials"
"How to use git with gnome-keyring integration"
The user will enter the password only once per session.
Read the remote url from git and then insert the ID and password (PW) to the url might work.
For example try the following:
cd ${REPOSITORY_DIR}
origin=$(git remote get-url origin)
origin_with_pass=${origin/"//"/"//${USER_ID}:${USER_PW}#"}
git pull ${origin_with_pass} master

SQL Server Agent Job and gsutil

I can successfully run a gsutil command with a windows domain account from the command line in Windows (setting up service account key etc.). When I try to run the same command from a SQL Agent Job using a CmdExec task the job hangs and doesn't complete. I can't see any logging so have no clue what it's waiting for. I've setup the job to run with the same Proxy User that i use to run the gsutil command manually.
Any ideas how I can get this to work or how to see more logging?
Are you using standalone gsutil? Or did you get it as part of installing the Cloud SDK (gcloud)?
If the job hangs for a long time, it could be stuck retrying multiple times. To test if this is the case, you can set the num_retries option to be very small, but above 0 (e.g. 1) either in your .boto file or the the command arguments via this option:
gsutil -o 'Boto:num_retries=1' <rest of command here...>
A second thing to note (at least for the version of gsutil that doesn't come with gcloud) is that gsutil looks for your boto config file (which specifies the credentials it should use) in your home directory by default. If you're running gsutil as a different user (maybe your SQL Agent Job runs as its own dedicated user?), it will look for a .boto file in that user's home directory. The same should apply for the gcloud version -- gcloud uses credentials based on the user executing it. You can avoid this by copying your .boto file to somewhere that the job has permission to read from, along with setting the BOTO_CONFIG environment variable to that path before running gsutil. From the cmd shell, this would look something like:
set BOTO_CONFIG=C:\some\path\.boto && gsutil <rest of command here...>
Note: If you're not sure which boto config file you're normally using, you can find out by running gsutil version -l and looking at the line that displays your config path(s).

Update CCRC view via windows command line and integrate it in Jenkins

I've been looking for a way to update my CCRC view via windows command line in a non-interactive way.
I'm using Jenkins and today there isn't a plugin for CCRC yet (https://issues.jenkins-ci.org/browse/JENKINS-5192), so I'm trying, as a first step, to auto update my CCRC view via batch script. I installed Managed Script Plugin (https://wiki.jenkins-ci.org/display/JENKINS/Managed+Script+Plugin) and wrote this simple script:
cd C:\Views\PathToMyViewVOB
rcleartool login -lname MyUser -server MyServer.com -password MyPass
rcleartool update
With this script, in the first execution, the login works correctly, but, I don't know why, Jenkins ignores the update command. In the following executions, it shows this error:
CRCLI2014E You have already logged on the server
"MyServer.com".
Build step 'Execute managed windows batch' marked build as failure
Finished: FAILURE
I've also tried with this other command, thinking that you can pass my credentials with any subcommands of rcleartool:
cd C:\Views\PathToMyViewVOB
rcleartool update -username MyUser -password MyPass -server MyServer.com
And this is the error now:
CRCLI2001E Unrecognized option: "-username".
Use: update [-print] [-ove/rwrite | -nove/rwrite | -ren/ame] [-pti/me][nombrep ...]
update -add/_loadrules [-print] [-ove/rwrite | -nove/rwrite | -ren/ame][-pti/me] nombrep [...]
Build step 'Execute managed windows batch' marked build as failure
Finished: FAILURE
I've thought in a script that checks if I'm already logged, if not, log in and then update, if yes, only update, and then go on with the compilation, but I don't know how to do that.
Is there any way to do what I want, auto update the CCRC? I can always do it manually, but...you know, it's better non-interactive.
Any suggestions, advice or idea will be helpful.
It depends on the version of ClearCase that you are using.
Before CC 8.0.1.9, there is this bug where, in non interactive mode, IBM ClearCase rcleartool waits on prompt for login when previous login failed, instead of reporting an error and not prompt the user.
As mentioned in this thread, you need to make sure CCSHARED & JAVA_HOME are properly in the rcleartool config.ini, as in here:
CCSHARED=C:\Program Files (x86)\IBM\IMShared
JAVA_HOME=C:\Program Files (x86)\Java\jre7
Those paths are example only, and might differ from your actual path.
The point is: they must be valid in the Jenkins job execution context (on the slave)
The OP Alavaros mentions in the comments:
only updating the version fixed the problem, now I do:
rcleartool update -lname MyUser -server MyServer -password MyPass
And works fine.
"rcleartool update" is ignored because rcleartool.bat itself is a batch file so u have to use "call rcleartool -params args" in each line in your batch file.

Using psexec.exe in jenkins, handle is invalid

I am using Jenkins on a Windows7 system. I would like to use it to execute a batch script on a remote Windows system. The batch script will be used to flash a development board and run some tests. I came across psexec.exe. That works well through a command prompt window--I can connect and run the script without any issues, but when I try to have Jenkins do it, I get the following output:
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
The handle is invalid.
Connecting to ABCDEFG...
Couldn't access ABCDEFG:
Connecting to ABCDEFG...
Build step 'Execute Windows batch command' marked build as failure
The command I am using in both cases is:
psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" "C:\test.bat"
The user associated with username has administrator privileges on the remote system (ABCDEFG is not the real name of the system).
Can anyone help me figure out why it is not working through Jenkins? Or, is there an easier/better way to execute a batch script on a remote Windows system through Jenkins?
Thanks to all your help, especially Technext, I have a solution.
I needed run "services.msc", find "Jenkins", right click on it, and go to "Properties". Once the properties windows appeared, I had to click the "Stop" button to stop Jenkins, open the "Log On" tab, enter in my username and password (the username I used when running through command prompt), and start Jenkins again. That got rid of the "handle is invalid" message in Jenkins.
Update:
A better solution was to go onto the system I was using psexec.exe to get onto, go to Control Panel > User Accounts > Give other users access to this computer. Click on "Add..." and type in the username and domain Jenkins uses to run its commands (to find this, open your Jenkins in a browser window, go to Manage Jenkins > System Information and look for USERNAME and USERDOMAIN under Environment Variables). Make sure you give it Administrator rights. Then click ok. Now psexec.exe shouldn't have the "handle is invalid" issue.
Sorry, I don't have enough reputation for comments, but is the single \ a typo? Since
The handle is invalid.
probably means that the computer address is invalid. Try
psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" "C:\test.bat"
Notice the two backslashes to access a locally mapped computer.
otherwise if that does not work i recommend the # tag
psexec.exe #servername.txt -u "DOMAIN\username" -p "password" "C:\test.bat"
where #servername.txt is a textfile containing only the servernames, one per line. The file parameter handles the formatting of \
ex servername.txt
ABCDEFG
COMPUTER2
EDIT: also found after some quick googling that it can be related to windows security.
Check out that a simple restart of the remote machine doesn't solve the problem. Also, adding parameters -h and -accepteula may help. Modified command:
psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" -h -accepteula "C:\test.bat"
I execute below code from Jenkins pipeline groovy script to connect dynamically created VM as a resource on Jenkins master. Below code connect dynamically created VM as resource on Jenkins master with 4 executors. You can change the number of executors based on your requirement.
bat label: 'ConnectResource', script: """
#echo OFF
C:\\apps\\tools\\psexec \\\\${machine_ip} -u ${machine_ip}\\${machine_username} -p ${machine_password} -accepteula -d -h -i 1 cmd.exe /c "cd C:\\apps\\jenkins\\ & java -jar C:\\apps\\jenkins\\swarm.jar -master http://pnlv6s540:8080 -username ${jenkins_user_name} -password ${jenkins_user_password} -name ${machine_ip}_${BUILD_NUMBER} -labels ${machine_ip}_${BUILD_NUMBER} -deleteExistingClients -disableClientsUniqueId -executors 4" & ping 127.0.0.1 -n 60 > nul
"""

Shell script to startup, exit and run other scripts once it completes

I have been trying to automate this process in shell script in a unix box. I am new to shell scripts.
I have not been able to figure out how to detect when the startup for it has finished . Also, how to
create a new terminal and check the domain.log for the new terminal. I would appreciate if anyone
can help me on this .
cd $/home/oracle/12/bin
./lsnrctl start
Login to sqlplus with username sys as sysdba and password: oracle3211
and run the database startup command startup
Once it is started type exit
--now start the dbconsole
cd $/home/oracle/12/bin
./emctl start dbconsole
--open a new terminal and execute this
$/home/oracle/startWeblogic.sh
--wait for domain.log keyword in the log file to confirm the server has started
--if the server is started proceed to the following in a new terminal
$/home/oracle/startManagedWeblogic.sh
--after this access the following urls
https://178:198:29:28:1167/em (username=system1, password=oracle123)
https://178:198:29:28:1176/em (username=system2, password=oracle132)
Sounds like you also need to learn about the GNU screen command. You may already have it installed. Try screen -R -D
If it gives you a shell prompt at the top of the screen then it works. Now if you disconnect your ssh session or close your terminal window (click on the X), the next time you log in and run the exact same screen command, you are reconnected to the same running terminal session.
This allows you to run scripts, etc. which do not stop when you disconnect. And when you reconnect you can see their current output.
Also, you can have many terminal sessions. Ctrl-A c creates another one. Ctrl-A [space] and Ctrl-A [backspace] rotate between sessions in the list. Ctrl-A ? gives you all the other Ctrl-A commands that you can use. For instance, one session could be tail logfile while another one is running a program waiting for input.

Resources