Adrian Andreias

Feb 02, 2014

Feb 08, 2013

make python-rrdtool graph return image bytes

The rrdtool.graph function from the Python library either outputs graph to stdout or saves it to a file. There are many cases when you don't want these, you want to get the returned image in a variable holding the bytes instead. And writing a temporary image to disk and then read it is a hack that makes unnecessarily disk operations which you might not afford in some scenarios.

After spending a few hours on this and asking about it on mailing list and IRC channel I was almost ready to give up and do all sorts of stdout redirecting tricks when I found a note from the author on a Debian bug report. I wish the python rrdtool library actually had some kind of documentation, beyond the command line documentation.

Anyhow: use python functions graphv instead of graph to get the image in the returned variable instead of printing the image.

Feb 05, 2013

vim quick tips

Note: commands are shown exactly as they are typed when you are in normal mode.

  1. . - repeats last action. For instance if you type d/" to delete everything from current cursor position to first double quote (") you can then type . to delete again all text to next double quote.
  2. :%s/oldtext/newtext/gc Search and replace in whole file and confirm each replacement.
  3. ?string while /string will search forward this will search backwards. Use n and N to navigate to next/previous occurance.
  4. :m+2 will move current line two lines downwards
  5. ddfolowed by p will cut the current line (placing it in clipboard) and then p will paste it at the new cursor location
  6. :set nowrap - don't wrap lines
  7. 5645go - go to byte (= \~character) number 5645. This is useful when you have an error that gives you character number.
  8. :set ruler will display cursor's current line and column in the status bar (last line). Use :set noruler to hide it.
  9. :set number / :set nonumber shows/hides the line number at the beginning of each line
  10. cut/paste: CTRL+V to start visual selection mode, then move cursor to select a block of text, d to cut the selected text, move to the new position when you want to paste the text and p to paste after cursor or P before cursor position

Refercences:

Jan 28, 2013

Jan 22, 2013

git your home dir

Having all sorts of .vimrc, /.vim (like .sls files syntax), /.gitconfig settings in my Linux home dir made want to easy port this between servers. So I initialized and pushed to server a git repository and wanted to pull files from git on another server:

cd ~
git clone ssh://<git user>@<git server>/<repo path> .
fatal: destination path '.' already exists and is not an empty directory.

So, we need to get a git repo to a non-empty directory. Stackoverflow comes in handy (as most of the time) and tells us to:

cd ~
git init
git remote add origin ssh://host/repo.git
git pull origin master
posted at 17:53 ·  · git

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixture

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixture

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixture

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixture

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixtures

I will not add trailing comma to lists in django json fixture

I will not add trailing comma to lists in django json fixtures

since I now know that I'll get a meaningful message:

Problem installing fixture '.....json': 
......
File "....\lib\site-packages\django\core\serializers\json.py", line 47, in Deserializer
  raise DeserializationError(e)
DeserializationError: No JSON object could be decoded

Jan 21, 2013

Django model managed=True on testing

I have a bunch of Django models that have managed=False set, but I need Django to create these database tables while running unit testing.

I eventually found an article that describes a method to do this: Simplifying the Testing of Unmanaged Database Models in Django

Place the code in myproject/testrunner.py and specify in your settings.py or test_settings.py file:

TEST_RUNNER = 'myproject.testrunner.ManagedModelTestRunner'

And that's all. Django will now create the tables for the unmanaged models when running automatic unit testing.

Jan 18, 2013

my git reminder

(to be updated)

1. Create the local git repository

Even if you don't plan to use a git server and only store files on your localhost you should create a git repository as early as possible because:

  • you will have a file editing history, provided that you'll commit frequently
  • it only takes a few seconds to create a git repo

(Note that "git commit" means something slightly different than "svn commit". Since svn always assumes having a svn server, git can be used just on your local file system, a git server is not mandatory. Git will keep track of your changes in his internal database stored in directory .git by default. git push would be the equivalent of svn commit, that is publishing changes to a server).

Without further ado:

  1. git init # this will create the git database under directory .git from current path
  2. create file .gitignore. This contains the list of files that will not be added to git database. Here's what mine usually contains: .idea *.pyc settings.py
  3. git add * # to add all files
  4. git commit -a # to commit all files

Read more in the git book.

And GIT for SVN users:

2. Initial server push

Once you have the local git repository you'll want to publish to the git server.

  1. Create the repository on your git server:

    cd /var/repos mkdir myproject.git cd myproject.git git init --bare
    
  2. Add remote server:

    sudo git remote add origin
    ssh://<git user>@<git server>/var/repos/myproject.git
    
  3. and push your local repository to remote server:

    sudo git push -u origin master
    

Inspiration:

posted at 10:44 ·  · git

Jan 16, 2013

saltstack commands cheat sheet

I'll update this list from time to time. My terminology might be wrong, I'm still find my way around Salt Stack.

All commands use the --local flag since I'm using local machine as development environment and updating files in /srv/salt and /srv/pillar.

sudo salt-call --local state.highstate # apply highstate
sudo salt-call --local grains.items # list all grain values
sudo salt-call --local state.sls pdns # apply a substate (/srv/salt/pdns/init.sls or /srv/salt/pdns.sls)
sudo salt-call --local pillar.data # list pillars

Jan 14, 2013

move .git up

After initializing the git repo to /src/salt/.git I discovered I also need to define some pillars in /srv/pillar and needed to move the repo to parent directory (/srv/.git). Fortunately git is smart enough to detect this and automatically rewrite paths in its database. You just need to move the database to parent directory and add original directory:

cp -r /srv/salt ~/salt_backup # backup repo
cd /srv/salt/
mv .git ..
git add salt
git commit -a

(obviously, this was needed because I didn't want to create a new repo and lose the existing commits history)

Thanks go to http://stackoverflow.com/a/3247756.

← Previous Next → Page 2 of 3