Skip to content

Instantly share code, notes, and snippets.

@alces
alces / build.xml
Created April 9, 2017 21:53
How to run a command against each file from a set using Apache Ant
<!-- See http://ant.apache.org/manual/index.html for the complete list of options supported by <apply> task -->
<project name="cycle-test" default="stat-cycle">
<target name="stat-cycle">
<!-- set parall="true" in order to run the command only once against all files from the set -->
<apply executable="stat">
<fileset dir="/etc">
<patternset>
<include name="*.conf" />
</patternset>
</fileset>
@alces
alces / ansible_check_syntax.sh
Created April 9, 2017 20:25
How to check whether a given Ansible playbook is syntaxically correct
ansible-playbook -i localhost, --syntax-check my-playbook.yml
@alces
alces / etcd_setup.sh
Created January 30, 2017 22:16
Extract only necessary files from etcd distribution and start one-node cluster
#!/bin/bash
# tested on CentOS 6.8
tar xvzf etcd-v3.1.0-linux-amd64.tar.gz --strip-components 1 --exclude Documentation --exclude '*.md'
myip=$(hostname -I | awk '{print $1}')
nohup ./etcd --data-dir etcd_data --listen-client-urls http://${myip}:2379 --advertise-client-urls http://${myip}:2379 2>&1 > etcd.log &
echo $! > etcd.pid
@alces
alces / ansible_cp_r.yml
Created December 22, 2016 11:35
Ansible playbook copying content of a remote directory to another recursively
---
- name: Recursive copy
hosts: all
vars:
my_src: /path/to/src/dir
my_dst: /path/to/dst/dir
tasks:
- name: find dirs
find:
paths: "{{ my_src }}"
@alces
alces / GetJobNameFromDSL.groovy
Created December 13, 2016 12:03
Getting a name of the currently generated Jenkins job from inside DSL script
// from top-level of the job block:
delegate.name
// from a nested block (such as parameters, steps, etc.):
delegate.item.name
@alces
alces / SetBuildEnvVars.groovy
Created December 1, 2016 07:48
Set environment variables during a Jenkins build using Groovy
// should be run as Groovy System Script
import hudson.EnvVars
import hudson.model.Environment
def build = Thread.currentThread().executable
def vars = [ENV_VAR1: 'value1', ENV_VAR2: 'value2']
build.environments.add(0, Environment.create(new EnvVars(vars)))
@alces
alces / ShellCommand.groovy
Last active August 23, 2016 07:20
Execute a shell command from Groovy, print its output, and process non-zero exit status
def cmdLine = args.join(' ')
def ps = cmdLine.execute()
ps.in.eachLine {
println it
}
def status = ps.exitValue()
if (status) {
println ps.err.text
throw new IOException("$cmdLine returns $status")
@alces
alces / ant_get.groovy
Created July 5, 2016 07:16
How to download a file via HTTP/HTTPS in Groovy script using "get" task from Ant. The script requires no external dependencies.
def ant = new AntBuilder()
ant.get(src: 'http://central.maven.org/maven2/jsch/jsch/0.1.29/jsch-0.1.29.jar',
dest: 'jsch.jar')
@alces
alces / ant_ssh_n_scp.groovy
Created July 5, 2016 06:12
The script demonstrates using Ant scp and sshexec tasks from Groovy
@GrabConfig(systemClassLoader = true)
@Grab('jsch:jsch')
@Grab('ant:ant-jsch')
def sshHost = '127.0.0.1'
def sshPort = 2200
def sshUser = 'vagrant'
def sshPrivKey = "${System.getProperty('user.home')}/.vagrant.d/insecure_private_key"
def ant = new AntBuilder()
@alces
alces / sudo_env_vars.md
Last active July 21, 2023 09:21
How to change environment passed to sudo command

By default, sudo on the Linux systems executes commands into a minimal environment. So, if you for example has some non-standard directories (such as /opt/groovy/bin) added to your PATH, a command running via sudo won't see any executable in these directories.

Normally, this strict sudo behavior can be changed by removing env_reset (or changing env_keep) variables in /etc/sudoers. But what can you do in case when you have only restricted sudo without write access to this file?

The following command passes a current value of PATH under a command run with sudo privileges:

sudo bash -c "export $PATH; which groovy"