Skip to content

Instantly share code, notes, and snippets.

@rtomaszewski
rtomaszewski / cloud_api_limits.py
Created July 29, 2012 17:49
Be aware of the cloud API limitations when creating cloud servers
# example code that creates cloud server and try not to hit the cloud API climits
cs_count=13 # number of cs to build
build_nr=1
delayed_10s=False
while build_nr <= cs_count :
if 0==build_nr % 11 and not delayed_10s:
time.sleep(60+10)
@rtomaszewski
rtomaszewski / naive_cloud_api_limits.py
Created July 29, 2012 17:53
Naive API trying to create too many cloud server
def cs_create(self, count, sample_nr):
name='test' + str(int(time.time()))
image=112
flavor=1
log("[%2d][%2d] creating image: " % (sample_nr, count) + pformat( {'name': name, 'image' : image, 'flavor' : flavor } ) )
sm=self.cs.servers
server=sm.create(name, image, flavor)
@rtomaszewski
rtomaszewski / example_debug.py
Created July 29, 2012 19:41
Primitive logging functions in your python scripts that are buffered before will be printed on the stdout
# Try to execute the script in two different modes to see the difference
#
# $ python log.py | tee log
# <noting for a long time>
#
# $ python -u log.py | tee log
# my log message 0
#
import time
@rtomaszewski
rtomaszewski / gil_threads_test.py
Created July 31, 2012 20:39
An example of multithreaded python code that creates about 500 long running threads
import threading
import datetime
import time
from random import random
class ThreadClass(threading.Thread):
def set_id(self, id):
self.id=id
def dummy_delay(self):
@rtomaszewski
rtomaszewski / object_variables_example.py
Created August 9, 2012 20:38
Differences between class and instance variables in Python
class Test1InstanceVariable:
mylist=[]
def __init__(self, number):
self.number= number
def add(self, i):
self.mylist.append(i)
def show(self):
@rtomaszewski
rtomaszewski / JavaExample.java
Created August 9, 2012 21:56
Simple java code showing that before we can assign value to variable it has to be known to the javac compiler in advance
public class JavaExample {
// with the next line commented the code is erroring out when we try to compile it
//public int number;
public JavaExample (int _number) {
number=_number;
}
public void show() {
System.out.println("[test " + number + "] list=%s");
@rtomaszewski
rtomaszewski / ssh_example.py
Last active July 7, 2019 11:13
Example script written in python that executes remotely commands over ssh
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect('5.79.4.10', username='root', password='7raW6nS6Krttest')
stdin, stdout, stderr = ssh.exec_command('echo text on stdout stream; echo text on stderror stream 1>&2')
print("reading outputs from the remote command")
for l in stdout :
@rtomaszewski
rtomaszewski / check_rackconnect.sh
Created August 13, 2012 21:47
An example bash script checking RackConnect deployment status
#!/bin/bash
iptables=$(iptables -nL)
ret=no
if echo $iptables | grep -q 'Chain INPUT .policy DROP.' ; then
if echo $iptables | grep -q 'Chain FORWARD .policy DROP.' ; then
if echo $iptables | grep -q 'Chain OUTPUT .policy ACCEPT.' ; then
if echo $iptables | grep -q 'Chain RS-RackConnect-INBOUND' ; then
ret=yes
@rtomaszewski
rtomaszewski / example_paramiko_notty.py
Created August 19, 2012 19:05
example paramiko script that tries to run an interactive command that needs a terminal
import paramiko
bastion_ip='ip' # you have to edit and provide valid IP address
bastion_pass='pass' # you have to edit it and provide valid password
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect(bastion_ip, username='root', password=bastion_pass)
chan = ssh.invoke_shell()
@rtomaszewski
rtomaszewski / example_paramiko_with_tty.py
Created August 19, 2012 19:49
example paramiko script with interactive terminal
import paramiko
import time
import re
bastion_ip='ip'
bastion_pass='pass'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect(bastion_ip, username='root', password=bastion_pass)