Jenkins doesn't invoke psexec command, because "Access is denied" - jenkins

On our project we want to deploy our .Net application to remote machine. For that purpose we have chosen PsExec tool. The propblem is that the commands that work fine in cmd don't work in Jenkins. They look the similar way in Jenkins
bat '%windir%\\sysnative\\PsExec.exe \\\\ipaddress -u user -p password -accepteula -h cmd /c "command" /q"'
Jenkins prints that Access is denied, although it works well in cmd. Why should I do? How it works differently in Jenkins and cmd? Maybe I'm doing something wrong?

Your Jenkins service must be launched by an admin user. Then you'll have access to these commands.

Related

Docker Windows how to keep container running without login?

I have Docker installed inside a Virtual Machine with Windows Server 2016.
I have a Linux Container from Python3 with NGINX server using --restart=always param, it runs fine while I am logged in, if I restart the VM, the container is no longer active, and it starts only if I log in.
Also if I logout, the container stops.
How can I make a container run as a service without login and keep it running on logout?
I've got a better answer from HERE
The summary is to build a Task and assign it to Task Scheduler to run on Windows start.
All the scripts should be run on powershell
Logon to the windows server/machine where you want the Docker services to start automatically.
Create a file called startDocker.ps1 at your location of choice and save the following script inside it:
start-service -Name com.docker.service
start C:\'Program Files'\Docker\Docker\'Docker Desktop.exe'
Verify that the location of Docker.exe is correct on your machine otherwise modify it in the script accordingly.
Create a file called registerTask.ps1 and save the following script inside it.
$trigger = New-ScheduledTaskTrigger -AtStartup
$action = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-File C:\PowershellScripts\startDocker.ps1"
$settings = New-ScheduledTaskSettingsSet -Compatibility Win8 -AllowStartIfOnBatteries
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "Start Docker on Start up" -Settings $settings -User "Your user" -Password "Your user password" -RunLevel Highest
This is needed so that this user has access to docker services
try
{
Add-LocalGroupMember -Group docker-users -Member "Your user" -ErrorAction Stop
}
catch [Microsoft.PowerShell.Commands.MemberExistsException] { }
Modify the script: You will have to change a couple of things in the scripts above according to your computer/server.
In the $action line, change the location of startdocker.ps1 script file to where you have placed this file.
In the Register-ScheduledTask line change the account user and password to an account user that needs Docker services to be started at the Windows start-up.
Execute registerTask.ps1
Open Windows Powershell as Administrator and set the current directory to where you have placed registerTask.ps1. For example
cd C:\PewershellScripts\
Next execute this script as follows
.\PowershellScripts\
Since I went through quite a lot of pain in order to make this work, here is a solution that worked for me for running a linux container using docker desktop on a windows 10 VM.
First, read this page to understand a method for running a python script as a windows service.
Then run your container using powershell and give it a name e.g
docker run --name app your_container
In the script you run as a service, e.g the main method of your winservice class, use subprocess.call(['powershell.exe', 'path/to/docker desktop.exe]) to start docker desktop in the service. Then wait for docker to start. I did this by using the docker SDK:
client = docker.from_env()
started = False
while not started:
try:
info = client.info()
started = True
except:
time.sleep(1)
When client has started, run your app with subprocess again
subprocess.call(['powershell.exe', 'docker start -interactive app'])
Finally ssh into your container to keep the service and container alive
subprocess.check_call(['powershell.exe', 'docker exec -ti app /bin/bash'])
Now install the service using python service.py install
Now you need to create a service account on the VM that has local admin rights. Go to Services in windows, and find your service in the list of services. Right click -> properties -> Log On and enter the service account details under "This account". Finally, under general, select automatic(delayed) start and start the service.
Probably not the most 'by the book' method, but it worked for me.
what version of docker did you install exactly / in detail?
The procedure to get docker running on a server is very different than for desktops!
It's purely script based as described in detail in the MS virtualization docs
The executable name of the windows-server docker EE (enterprise) service is by the way indeed dockerd as in linux.

History in docker container

When I use a normal terminal in linux, I can use the up arroy key to navigate between previous command that I executed. I need do the same in a container in docker.
Ex:
Login to the container work space with this command:
/usr/bin/winpty.exe docker-compose exec workspace bash
Then, In the workspace container I run something command like this:
composer self-update
And then I close the current session, The next time that I try to repeat the same steps whenever I'm logged in the container, the prompt history doesn't have any commands saved.
I use laradock in windows.
After that I searched more about this problem, I found this reports in git-hub
https://github.com/moby/moby/issues/13817
https://github.com/Maximus5/ConEmu/issues/183
Finally the problem for me was the client that I used (git-cli). I change to (Powershell) and it works perfectly. Putty it's too an alternative to connect to docker environment.

how to login into unix system and run script through jenkins in windows

I have install jenkins in windows system but I have to run script which is on unix server. to run that script i have to first login into unix box then run the script. Any one ca help in it
You can use Execute shell script on remote host using ssh in Add BUILD.

Docker commands fail in Cygwin -- Docker service is up

I'm trying to login to my AWS account with a docker loginstring from aws ecr get-login --region eu-west-1 -- however, when I do, I get this:
Warning: failed to get default registry endpoint from daemon (error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.26/info: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.). Using system default: https://index.docker.io/v1/
error during connect: Post http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.26/auth: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running
The peculiar thing is this was just working last week. I'm using Cygwin on Windows 10. I've verified that Docker is running in services.msc -- I'm not entirely sure what to check now.
If you want to run docker commands in cygwin on Windows 10, then call the following command from cygwin:
docker-machine env --shell=bash
It will show the environment variables needed by docker:
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.xx.xx:2376"
export DOCKER_CERT_PATH="C:\Users\...\machines\default"
export DOCKER_MACHINE_NAME="default"
export COMPOSE_CONVERT_WINDOWS_PATHS="true"
# Run this command to configure your shell:
# eval $(docker-machine env --shell=bash)
Run the following command to set the environment variables:
eval $(docker-machine env --shell=bash)
It looks like you are trying to open a pipe to the service by passing a windows pipe path to cygwin. It gets interpreted as an http path by cygwin.
This is probably happening because the docker command finds the cygwin python before it finds the windows python. There are several solutions. The simplest is to switch back to a windows context to execute the docker command. If docker is actually docker.bat then start it with cmd.exe /c docker.bat otherwise cmd.exe /c docker.exe. You may also try cmd.exe /c start /w /i docker.bat. Or some variation.
You do not want to execute any docker command directly in cygwin as it will likely not know how to translate between windows paths and the linux paths it expects.

ssh fails when is being called within windows service

I'm calling cmd file that calls ssh to intercommunicate with Linux machine. I use .NET Process class to accomplish this. But when being called within Windows Service call fails with following error:
C:\test>ssh -o StrictHostKeyChecking=no -i private_linux_key user#host "ls"
0 [main] ssh 9496 fhandler_base::dup: dup(some disk file) failed, handle 0, Win32 error 6
dup() in/out/err failed
Everything works when I start application as Console application.
What may be possible reason of this failure and how to fix this?
EDIT All Windows service has to do - somehow kill predefined daemon on Linux machine
Thanks
EDIT
Similar problem described there: http://www.velocityreviews.com/forums/t714254-executing-commands-from-windows-service.html
Maybe this post will save someones time to struggle similar problem. I've finally found solution that works for me. It is ssh -n key
So instead of
ssh -o StrictHostKeyChecking=no -i private_linux_key user#host "ls"
I've used
ssh -n -o StrictHostKeyChecking=no -i private_linux_key user#host "ls"
It still looks like a magic, but it works!
isn't it a problem of access credentials ?
when running your program as a console applicaiton, you are using the access rights of the currently logged on user. however, a Windows Service executes in a special user account (generally "SYSTEM"), and as such is not granted the same access rights.

Resources