Monday, May 27, 2013

Essential programs for my Lenovo Z570 with Ubuntu

Leading off from my last blog post about Optimizing Ubuntu 13.10 for the Lenovo Z570, I thought I would talk about what I consider Essential programs & tools in Ubuntu, especially for my laptops.

1. Psensor

First and foremost is Psensor. This is an excellent program for monitor device temperatures. You can even set it to warn you if any of your devices (CPU, GPU, Motherboard, etc.) exceed a set temperature. This tool has not only been invaluable in monitoring my system, but also diagnosing issues. Click here to install Psensor.

2. Indicator-cpufreq

Next, indicator-cpufreq is fantastic for not only viewing your laptop's current processor load, but also limiting it appropriately. So if you are on the go and would like to conserve some power, you can switch it down to powersave. If you are plugged in and are doing something processor intensive, you can set it to performance, or even "turbo". Click here to install indicator-cpufreq.

3. Ubuntu-tweak

Ubuntu tweak is a great little application that is all about making it easier to tweak the settings of Ubuntu and Unity, as well as install many popular titles, like Chrome, that you would otherwise have to search for and install manually. This is a must have for making Ubuntu run the way you want it to! Installing is as easy as adding a ppa and running an install script.
sudo add-apt-repository ppa:tualatrix/ppa
sudo apt-get update
sudo apt-get install ubuntu-tweak

4. Oracle Java 7

Oracle Java vs OpenJDK is one of the great linux debates. For most people, installing Open Java is as simple as clicking on this link for OpenJDK 7 and this link for IcedTea browser plugin. For Oracle Java, it takes a few extra steps to pull it off. You can either manually download and install Oracle Java, or, take advantage of webupd8team's PPA.
sudo apt-get purge openjdk*
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

5. Other programs



I am confident there are more I am not thinking about at the moment. I'll update this post as I remember them!

Setting up a Postgresql Database Adapter with ZF2

Setting up Database access with Zend Framework 2 can feel a lot more intimidating then it really is. I partially blame the confusing nature of complex documentation along with the lack of good code samples. I am hoping to help with the second part of this equation.

Assuming you are using an MVC structure and placing most of your code in the module structure, you may find that centralizing your database connections is not only essential but also helps in reducing code redundancy. Thankfully, once you understand some of the core features of the ZF2 structure, overcoming obstacles like dependency injection, adapter factories, and global database access can become much less daunting.

So lets dive right in!

The first step to global database domination is setting up the global configurations. In your project's config/autoload directory open or create the file global.php. As with most other ZF2 configuration files, you will be simply returning an array of settings. The first is the "db" configuration array which will look something like this:
'db' => array(
    'driver' => 'Pdo_Pgsql',
    'database' => 'uHackedMe',
    'username'=>'obscureFictitiousName',
    'password'=>'likeIWouldTellYou'
),

The "driver" parameter represents the database driver you plan on using. Official options include: Mysqli, Pgsql, Sqlsrv, Pdo_Mysql, Pdo_Sqlite, and Pdo_Pgsql. The other settings should be pretty self explanatory. For the security conscious among us, you can always implement this logic outside your version control system and include the file. In my case, I simply return include('../../mySecretDbConfigFile.php');

The next step is wiring up the dependency injection that will prevent you from having to manually step through all of the hoops every time you want to run a simple query. That should look something like this:
'service_manager' => array(
    'factories' => array(
        'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
    ),

),

Essentially, this is setting up a database adapter factory that will pull in it's depended resources based on the db configuration settings, and store it all in the service manager for your happy consumption later on. It is amazing how much these few lines of code help simplify the whole process!

Now depending on your desired level of data abstraction,  you can easily create a table model that extends Zend\Db\TableGateway\AbstractTableGateway or simply access the Adapter you created earlier directly in your model. For the first option, you will want to define a second factory to automate the injection of the Adapter into your table model. You can do this by taping into the getServiceConfig() method of your Module.php files like this:
public function getServiceConfig(){
    return array(
        'factories' => array(
            'YourModule\Model\YourTableModel' => function($serviceManager){
                return new YourTableModel($serviceManager->get('Zend\Db\Adapter\Adapter'));
            }
        )
    );
}

At this point, inside your table model file, you now have direct access to the database as easily as this:
public function getRecord($id) {
    $row = $this->select(array('id' => (int) $id))->current();
    if (!$row){
        return false;
    }
    return array(
        'id' => $row->id,
        'name' => $row->name,
        'created' => $row->created,
    );

}

Finally, wherever you are accessing your table model (inside a controller or even another model), just use the service manager to get YourModule\Model\YourTableModel and your data will await you!
$serviceManager = $this->getServiceLocator();
$yourTableModel = $serviceManager->get('YourModule\Model\YourTableModel');
$yourRecord = $yourTableModel->getRecord($recordId);

If creating table gateways is simply not your cup of tea, you can skip the whole AbstractTableGateway step and simply add this into your Model:
$serviceManager = $this->getServiceLocator();
$adapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
$result = $adapter->query('SELECT * FROM `record` WHERE `id` = ?', array($recordId));

When you first start developing like this, it does seem like a whole lot of setup just to get a basic database connection established. But once you understand the process, tools like the serviceManager & pre-baked factory classes sure do make life so much easier!




Sunday, May 26, 2013

Installing & Optimizing Ubuntu 13.04 on the Lenovo Z570

I love my laptop. When purchased, it was a powerhouse of a laptop at a great pricepoint. The trouble with it, back when I first got it, Ubuntu 11.10 did not have good support for this laptop! That prompted me to write my original article: Install Ubuntu 11.10 on Lenovo Z570.

A lot has changed since then, along with 3 versions of Ubuntu. So here is my updated, revised, and even better version of my original installation guide. The great news is that Ubuntu 13.04 has great support for EFI and fully supports the built in WiFi card.

Support for Optimus is still not available out of the box, but tools have made it easier to take advantage of power savings AND 3D graphics.

Also, if you are interested in speed, you MUST replace the old-school HDD with a fast SSD! It was the best $100 upgrade I could have ever spent!

This guide will take you from 0 to GO, optimizing Ubuntu to help you get the most out of your Z570! I cannot take credit for all of the information found here, most of it is information I have found and compiled from many sources over the years. I'll site any sources I still have references for. Finally, as a standard disclaimer, I cannot take responsibility for any problems you might have. These are simply the best options I have found through LOADS of trial and error... More error than trial... LOL.

1. Install Ubuntu 64-bit

I am not going to go into too many details here, as there are countless tutorials for installing Ubuntu on your computer. My only recommendation is that you choose the torrent download, as you will get your .iso SO much faster. Here is a current link for Ubuntu 13.04 64-bit torrent. Here is a page that will have the latest version available. You will also want to check the md5 of your ISO to make sure there are no issues before you turn it into a DVD or USB installer.

2. Install all updates

Launch the "Software Updater" from the unity task bar and install all updates. There will be a lot of updates! On my system, it took about 10 minutes. I recommend that you restart your computer at this point. It may not be required, but, taking only 20 seconds, it's worth the precaution!

3. Get your video card working

If your z570 is like mine, it has Optimus support. Meaning, it has both an integrated Intel video chip, as well as a dedicated nVidia GeForce video card. Out of the box, Ubuntu still does not support the Optimus functionality out of the box. But with the help of a program called Bumblebee, we can get the 3d video support we want via the nvida card, and still keep the battery life we need by using the intel card.

There are two ways to install bumblebee, manually via the terminal, with a bunch of commands, or the easy way, using the Bumblebee Configurator GUI. To install the configurator, simply execute the following in the terminal (Ctrl+Alt+T):
sudo add-apt-repository ppa:alessandrofac93/bumblebee-config-gtk-dev
sudo add-apt-repository ppa:ubuntu-x-swat/x-updates
sudo add-apt-repository ppa:bumblebee/stable
sudo apt-get update
sudo apt-get install bumblebee-config-gui
Then launch the application using sudo:
sudo bumblebee-config
Click the obnoxiously large "Install Bumblebee" button. It will download all of the needed files and install it all for you. If you are ready to configure Bumblebee, simply press the configure button and you'll have all the settings you need. Configuring Bumblebee is outside the scope of this tutorial. I have had very good luck with the default settings.

Restart your computer again. This will ensure your video card drivers are all running correctly.

4. If you have an SSD, optimize it!

Let me reiterate what I said in the introduction. If you want to get the most out of Ubuntu and your Lenovo Z570, or any computer for that matter, invest in a good SSD! It was the best computing choice I've made since switching to Ubuntu in the first place! I now run SSD's on all of my computers. On my desktop, where I need more space, I mount / on a 64GB SSD and /home on a 1TB HDD. You get the best of both worlds!

As far as optimizing your SSD, I found a great guide over at HowToGeek, so I won't try to reinvent the wheel here.

There were a couple things I did notice were missing from the HowToGeek article.

Reduce disk swappiness:
sudo cp /etc/sysctl.conf /etc/sysctl.conf.bak
sudo gedit /etc/sysctl.conf
Then add the following to the last line:
vm.swappiness=10

5. Add Multi-media support

Open the Software Center and search for "Ubuntu restricted extras" and install. Or, just click this link.

Run the following to enable the playback of protected DVD's:
sudo /usr/share/doc/libdvdread4/install-css.sh

6. Make a backup image of your squeaky clean install!

Now that you have gone through all of the trouble to get your clean install setup and running smooth, this would be a great opportunity to take a snapshot of your system. My favoured tool is Clonezilla Live. The process is simple, it took about 20 minutes, and is only consuming 2GB of disk space. The best part is,


Closing thoughts

There are lots of other steps I take to "setup" my Ubuntu install, but these are the ones I feel are most widely applicable. What other tips and tricks are you using on you Lenovo Z570?