Overview

It is easy to make mistakes in a JSON file by simply missing a comma or other punctuation mark. A simple command line tool to check the syntax will be helpful to make sure the syntax is correct before even starting to use the file.

The tool

This simple tool is written in ruby and uses the “json” rubygem. Any version should be fine. Make sure the gem is installed before attempting to use this tool.

1
gem install json

Once the gem is installed, place the contents of the following block into a file (example: /usr/local/bin/jsonlint). Once the file is saved, make the file executable.

1
chmod +x /usr/local/bin/jsonlint

Note: If you do not have permission to edit/modify files in this location, execute the commands using sudo.

If you prefer saving the file in a different location, make sure the file is in your PATH or add the location to the PATH.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env ruby

require "rubygems"
require "json"

if ARGV.size < 1
  puts "Please provide a JSON file"
  exit 1
end

file_name = ARGV.shift

begin
  JSON.load(File.read(file_name))
  puts "Syntax OK"
  exit 0
rescue JSON::ParserError
  puts "Syntax Error!"
  exit 1
end

Tool in action

Here is a simple valid JSON file:

1
2
3
4
5
{
    "example": {
        "title": "This is an example"
    }
}

Running the lint tool against this file gives:

1
2
Kannans-MacBook-Pro:~ kannanmanickam$ jsonlint test.json
Syntax OK

Here is a JSON file that misses a “}”

1
2
3
4
{
    "example": {
        "title": "This is an example"
}

Running the list tool against this file gives:

1
2
Kannans-MacBook-Pro:~ kannanmanickam$ jsonlint test.json
Syntax Error!

Background

In Chef, when a resource is defined all its attributes are evaluated during compile time and the execution of the resource takes place in converge phase. So if the value of a particular attribute is changed in converge (and not in compile) the resource will be executed with the old value.

Note: In Chef 11, there is a feature coming up called lazy evaluator which will allow you to evaluate an attribute during converge instead of compile phase.

Update: This feature is implemented and the documentation can be found here.

Example problem:

Lets consider this situation where there are two steps involved in a recipe. Step 1 is a Ruby block that changes a node attribute. Rubyblocks get executed in converge phase. Step 2 is a Chef resource that makes use of the node attribute that was changed in Step 1

Without any modification to the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
node[:test][:content] = "old content"

# Step 1
ruby_block "step1" do
  block do
    node[:test][:content] = "new content"
  end
end

# Step 2
file "/tmp/some_file" do
  owner "root"
  group "root"
  content node[:test][:content]
end

The file resource will still have the old content as it is set in the compile phase.

With hacked code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
node[:test][:content] = "old content"

# Step 1
ruby_block "step1" do
  block do
    node[:test][:content] = "new_content"

    # Dynamically set the file resource attribute
    # Obtain the desired resource from resource_collection
    file_r = run_context.resource_collection.find(:file => "/tmp/some_file")
    # Update the content attribute
    file_r.content node[:test][:content]
  end
end

# Step 2
file "/tmp/some_file" do
  owner "root"
  group "root"
  content node[:test][:content]
end

The file resource will now have the updated content. This may not be a cleaner way to do things. There might be other workarounds which will avoid hacking the resource collection like this. I had a situation where I had to dig in and find a way to dynamically change the resource attribute. The lazy evaluator will make things simpler. For people who is not ready to adopt Chef 11 yet, this method of hacking will provide a workaround.

Insert the following block of code in .vim/plugin/gitdiff.vim. When you want to see the diff of the edited file and the one on the server, run the command :GITDiff inside from vim.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
if exists("loaded_gitdiff") || &compatible
    finish
endif
let loaded_gitdiff = 1
command! -nargs=? GITDiff :call s:GitDiff(<f-args>)

function! s:GitDiff(...)
    if a:0 == 1
        let rev = a:1
    else
        let rev = 'HEAD'
    endif
    let ftype = &filetype
    let prefix = system("git rev-parse --show-prefix")
    let gitfile = substitute(prefix,'\n$','','') . expand("%")
    " Check out the revision to a temp file
    let tmpfile = tempname()
    let cmd = "git show  " . rev . ":" . gitfile . " > " . tmpfile
    let cmd_output = system(cmd)
    if v:shell_error && cmd_output != ""
        echohl WarningMsg | echon cmd_output
        return
    endif
    " Begin diff
    exe "vert diffsplit" . tmpfile
    exe "set filetype=" . ftype
    set foldmethod=diff
    wincmd l
endfunction

Coming back to blogging after a looong time :) I’ve never blogged much in the past actually, but now trying to share some of the technical stuffs that I come across. I always had a thought when I was a student that scripting languages aren’t powerful and not widely used in professional level programming. Even while I was doing my Masters, we weren’t encouraged to write programs in scripting languages like python, ruby, and perl. We have to strictly pick C, C++, Java and such common languages where we have to write more code as there aren’t some built-in functions compared to scripting languages.

Few weeks ago, I got interest in learning some of the scripting languages. Started with Python, I slowly learned some of the concepts in other scripting languages like Ruby and Perl. Scripting languages are really powerful and makes the job easier when it comes to Systems Programming. In Google, most of the system level code is written in Python. The inventor is Python works at Google. Even though all scripting languages are similar, it is sometime hard to remember some functionality across these scripting languages. After I started reading about Ruby, I felt like I forgot some concepts from Python. I googled and came across this awesome cookbook, PLEAC – Programming Lanugage Examples Alike Cookbook. Although it is not 100% complete, it has plenty of information to study scripting languages with comparison.

PLEAC, of sourceforge, started by following the Perl Cookbook (by Tom Christiansen & Nathan Torkington, published by O’Reilly; you can freely browse an excerpt of the book here). The Perl Cookbook showcases some of the common programming problems solved by the Perl programming language. PLEAC implements these problems in all other possible scripting languages.

Access PLEAC here: http://pleac.sourceforge.net

I think the above page has more information that I can give here. Will be back with some other article.

Overview

The performance of computers is affected by all its resources like processor, amount of memory, graphics processor and also hard disk. The upgrade to all other resources except hard disk has already been done. The processor is now i3, i5, i7, etc,. with a speed of more than 2.5 GHz and with at least 4 cores.. The optimum memory for a laptop is around 4 GB and for desktop it’s 8 GB which is more than sufficient. The graphics processors by NVIDIA provide better graphics performance and they rock well in gaming. They also have memory in GBs. But the hard drive is now evolving to a new form.

The technology is simple. They just use flash memory for making the hard drives. It is the same as RAMs. But they are volatile memories. In this SSD technology, they use NAND based flash memory which retains the stored data even after the power is off. It makes them to be non-volatile drives.

What is in an SSD?

In the picture above, you can see the chips mounted in a board. All are light-weighted ICs which makes them more light in weight and the power consumption is very less compared to traditional hard drives.

Why does the hard drive affect the performance of the computer?

All the files are stored in the hard drive. If you try to open a big file, say a pdf document, it will take a lot of time even in a higher-end computer. It’s because of the access time. Even booting rely on the data stored in the hard drive. If the data you want is stored in the main memory that makes your job easier. If you are copying a big file to your hard drive or you are downloading large data, it will affect all other running applications. So, the overall performance is much affected by the performance of the hard drive. The speed of the hard drive was 5400 RPM previously, now its 7200 RPM. Even now the performance these drives can’t defeat the Solid State Drives. Also the traditional hard drives emits lot of heat as the disk spins almost all the time. In case of SSD, no heat at all because there isn’t any moving parts.

So, geeks decided to make the hard drive with flash memories. The set up fix perfectly with a 2.5 inch hard drive case which are used in notebooks. It can also be used in desktop computers.

View of an SSD

In the Image above, you can see that it exactly looks like a traditional hard drive. It has the power connectors and data cables and it is compatible with SATA hard drives.

So, if you have a higher end laptop, you can give your laptop a new life by just upgrading to SSD. But the proce of SSD is much costlier that traditional hard drives. for a 64 GB hard drive, you have to spend around $130 which is almost equal to the cost of my 2TB external hard drive :( So, the better option would be to use the SSD only for the operating system and to store some important and frequently used files. Use a traditional external hard drives to store your all other files. This should work perfectly fine. I saw all video reviews and written reviews about the performance of SSD. Your machine will boot more than 2 times faster than previously. All your application will run faster. Data transfer rate is around 712 Megabytes/sec for reading and 654 Megabytes/sec for writing. It took only 0.8 seconds for transferring a video rip movie of 700 Megabytes.

I know there will be lot of questions after reading this. Let me answer for some questions I know.

  1. I have all my applications installed. I don’t want to re-install my operating system. What can I do? Yes. there is an option for this. You can easily clone your old hard drive and load the new SSD with the data from your old HDD. Most SSD sellers give an application to do this hard drive cloning.

  2. What do I do with my old notebook HDD? You can use your HDD as an external hard drive. Most SSD manufacturers give a kit to use the normal SATA notebook/desktop hard drive to use as an external hard drive. So, nothing is wasted. going for an SSD would be a better choice for a smart upgrade.

Hi all

Welcome to my blog. I am new to blogging. I want to share some of my knowledge, skills, and some of my thoughts with you all. I will try to keep posted of all funniest, interesting stuffs happened in my day-to-day life. I will also post some tricks and tips related to computers. Feel free to comment on the posts. I might have done something wrong. I am not an all-in-all. I am just an ordinary man. I will learn from your comments. It will also help me in making my posts more interesting and helpful.

Arangs.