Skip to content

Instantly share code, notes, and snippets.

View akostadinov's full-sized avatar

Aleksandar N. Kostadinov akostadinov

View GitHub Profile
@akostadinov
akostadinov / amicable.rb
Created March 1, 2021 22:05
amicable numbers
# https://www.jdoodle.com/execute-ruby-online/
def amicable?(int1, int2)
sum_proper_divisors(int1) == int2 && sum_proper_divisors(int2) == int1
end
def sum_proper_divisors(num)
(1..num/2).select { |i|
num%i == 0
}.sum
@akostadinov
akostadinov / pretty_print_json.rb
Created January 26, 2021 11:59
Pretty print JSON-like exercise
INDENT_SIZE = 2
def pretty_jsonify(struct, indent=0)
case struct
when Array
"[\n" + indent_str("", indent+INDENT_SIZE) +
struct.map { |value|
pretty_jsonify(value, indent+INDENT_SIZE)
}.join(",\n#{indent_str('', indent+INDENT_SIZE)}") +
@akostadinov
akostadinov / jenkins cancel running builds
Created December 22, 2020 19:56
Cancel all running Jenkins builds with system groovy script
import jenkins.model.Jenkins
def numCancels = 0;
Jenkins.instance.getAllItems(Job.class).each{
def job = it
for (build in job.builds) {
if (build.isBuilding()) { build.doStop(); numCancels++; }
}
}
@akostadinov
akostadinov / CSV-from-facebook-friends-export.rb
Last active January 22, 2021 18:11
Fix facebook broken exported UTF
require 'json'
require 'uri'
require 'csv'
# too bad we lack variable size lookbehind
bytes_re = /((?:\\\\)+|[^\\])(?:\\u[0-9a-f]{4})+/
friends_txt = File.read('friends.json').gsub(bytes_re) do |bad_unicode|
$1 + eval(%Q{"#{bad_unicode[$1.size..-1].gsub('\u00', '\x')}"}).to_json[1...-1]
end
@akostadinov
akostadinov / jenkins-job-param.groovy
Last active October 16, 2020 21:41
Jenkins Job deafault value of parameter of a job
job = Jenkins.getInstance().getItem("Flexy-install")
println job.getProperty(ParametersDefinitionProperty.class)?.getParameterDefinition("REPOSITORIES")?.getDefaultParameterValue()
/*
License: https://en.wikipedia.org/wiki/WTFPL
Some readers:
https://support.cloudbees.com/hc/en-us/articles/226941767-Groovy-to-list-all-jobs
https://medium.com/@mukeshsingal/update-default-values-of-jenkins-job-parameters-416de5ff9f96
https://javadoc.jenkins.io/hudson/model/Job.html
@akostadinov
akostadinov / touchpad_toggle.sh
Created April 19, 2020 15:20
Turn on and off a libinput device.
# the above works with synaps driver
DEV="SynPS/2 Synaptics TouchPad"
# DEV="Synaptics TM3053-006"
toggle_touchpad () {
local enabled=`xinput list-props "$DEV" | awk '/Device Enabled/{ print $4 }'`
case "$enabled" in
0) xinput enable "$DEV"
;;
1) xinput disable "$DEV"
;;
@akostadinov
akostadinov / jenkins_creds_script.groovy
Last active June 19, 2024 08:50
jenkins credentials obtain on script console
com.cloudbees.plugins.credentials.SystemCredentialsProvider.getInstance().getCredentials().forEach{
it.properties.each { prop, val ->
if (prop == "secretBytes") {
println(prop + "=>\n" + new String(com.cloudbees.plugins.credentials.SecretBytes.fromString("${val}").getPlainData()) + "\n")
} else {
println(prop + ' = "' + val + '"')
}
}
println("-----------------------")
}
@akostadinov
akostadinov / fahclient.service
Last active March 18, 2020 17:31
fahclient.service
[Unit]
Description=FAHClient cruncher
After=syslog.target network.target
[Service]
Type=simple
User=fahclient
WorkingDirectory=/var/lib/fahclient
EnvironmentFile=-/etc/sysconfig/fahclient
# log will go fahclient home dir
@akostadinov
akostadinov / udp.rb
Created February 18, 2019 19:07
Ruby UDP echo server and client
# echo server
Socket.udp_server_loop(4444) do |data, src|
src.reply data
end
# client
addr = Socket.sockaddr_in(4444, "localhost")
socket = Socket.new(:INET, :DGRAM)
begin
socket.send("hello\n", 0)
@akostadinov
akostadinov / monty.rb
Created January 2, 2019 14:46
Monty Hall empirical proof
require 'test/unit'
# Read about the problem and mathematical proof here:
# http://www.greenteapress.com/thinkbayes/html/thinkbayes002.html#sec15
class MontyHall
include Test::Unit::Assertions
NUM_DOORS = 3