Dying battery and suspend

Since some time ago, I could not use the computer on battery for too long, and as time passed, the time was getting smaller. The last time was in an airport, trying to get the last minute work before catching an airplane and within 5 min the laptop was dead and no plugs were to be found. Sigh.

Finally got to the Lenovo agent in a chat, who directed me to run a diagnostic test to check all hardware and a local contact to buy a battery. And after some personal obstacles was able to order the battery and book the service to have it changed.

When the equipment was ready, they showed me the battery… it was one and a half the the normal size. I was told that I was lucky it did not explode on me and that it did not seem to have damaged anything.

Which leads me to… now I need suspend to work correctly as the laptop should not be plugged in continuously but only when it reaches 15% until is fully charged.

The laptop suspends well when closing the lid or when there is 20 minutes of inactivity with a blank screen. However, when trying to use upower (https://upower.freedesktop.org/docs/UPower.html) which attemps to hybernate, the laptop hangs…

Some related blogs:

When trying to figure out what happened, I wanted to check the syslog:

I think the the problem is that upower always wants to hibernate, which uses the disk, and the I might not have a swap space defined. Closing the lid suspends, which uses the ram, but of course needs a minimum of energy supply. The logs were too crowded. I need to attacked those other errors: gitea is not happy, the xserver is complaining, en fin, another kettle of fish.

Rails Migration quirk

It is believed that rails migrations are atomic transactions, that is, if there is an error, the whole thing is rolled back. Well I came to an interesting exception.

I created a migration to add an association to a model:

rails g migration AddModelOneToModelTwo model_one:references

which creates the migration to add model_one_id to model_two.

In this particular project, I am using mysql, and a previous version default of the id was int(11) and most the tables were created like so. The current version’s default for id is int(20). Running the previous migrations creates model_one_id to be int(20) but fails to create the reference to the id in the model_one table as they are not the same type. The migration therefore fails. However, the created column somehow persist, and cannot go forward or backward without manually manipulating the database. The rails console does not load because there are migrations to be run. Trying to run the migration again throws an error with field model_one_id already exists, but this partial change cannot be rollbacked. Stuck!

What to do?

  1. This migration needs to be adjusted so that the model_one_id column is of type int(11) by adding this to the code: type: :integer.
  2. In the mysql CLI, select the database of interest by: use database_name;
  3. Manually delete the already created model_one_id from the model_two table: ALTER TABLE table_name DROP COLUMN column_name;
  4. Check the state of the table with show full columns from TABLE;
  5. Get out of the mysql CLI.
  6. Run the migration.
  7. That’s it.

Carbon X Extreme and Pop!_os

So I was tired of working with in under performing, old laptop which did not like having 20 browser tabs open. Really, who can work with fewer than that! I also have a eye-sight problem so resolution and screen quality are essential for me.

On the quest I went without having any idea of how to start the research. By a combination of relatives’ previous experiences, word of mouth about specs, a sale and gut feeling I ended up buying a Lenovo Carbon X Extreme. A lot of people say that ThinkPad’s are overpriced but I like them. I chose lots of RAM, and cores, and bytes.

But before buying it, the eternal question: Does it run Linux!? Because the graphics card is Nvidia, normal Ubuntu does not run properly as Nvidia drivers are closed source. I found Pop!_os to come to the rescue.

Bought!

I found the documentation for the installation quite nice.

  • First make the bootable disk following these instructions.
  • Then… how do you boot from an external drive. This took me a while to figure out. I had to get to the BIOS, but on boot, could not get at the BIOS menu:
    • The solution after a long search and trial and error: Upon start up, you have about 2 seconds to read the minuscule lettering telling you what to do:
      • Press the enter key to enter the BIOS.
    • I just held the enter key when powering on.
    • Some tips can be found here.
  • Following different posts, I changed different settings, the most important being the OS defaults. This is a safeguard against OS’s different from Windows. The safe boot must be disabled. Basically, anything that has to do with secure booting, has to enable booting differently than the default. In some computers, you have to disallow fast boot to always be presented with the option of entering the BIOS.
  • Once your computer boots from the external device, the installation is straight-forward.

The initial setup of Pop_os! was great. Pretty slick and fast. But soon stumbled on some issues:

  1. The 4k laptop resolution does did not befriend any of the monitors I had around. There does not seem to be an easy way to find, for my set up, a perfect monitor while keeping the quality. I bought a 4k monitor and still the drivers available do not seem to correctly do the transformations to the screen. The search is on-going.
  2. Despite the ample RAM, sometime pictures do not display fluently or at all. Causes? No idea. Not sure even how to start investigating. A reboot seems to fix the glitch momentarily.
  3. There are memory or CPU leaks perhaps inherent to linux, but having so many resources available for the demand presented, I do not understand why once in a while they seem lacking.

These review/summary come a bit late, 9 months after purchase, since I wanted to be using the machine and the OS for a while and get a real feeling of its performance.

Too many ssh authentication failures

Edit August 6, 2016

The solution I gave back in August 1, 2016 does not work. That rule will simply not offer any keys.

I found this post which refers to the pitfalls of ssh-agent, and it decribes that it is a problem having too many keys. It depends on the setting of the server to allow a specific number of keys offered before refusing access.

There is also a ssh argument force using a specific key.

Original post

In  my previous post Managing multiple ssh keys, I describe how to set up different ssh keys to different server and have the ssh command automatically discover which key to user for which (user, server) combination.

However, when I had a new server, if the key was not in the ~/.ssh/config file (se previous post), I would experience the follow:

ssh username@server
Received disconnect from server_ip port 22:2: Too many authentication failures
Connection to server closed by remote host.
Connection to server closed.

Looking around there were many explanations:

  1. That there is a limit on the number of keys offered to the server (true)
  2. That I ( could check what is happening by adding the -v flag to the ssh command (true)
  3. That I should add the line IdentitiesOnly yes to every definition in the ~/.ssh/config file (true but not enough)

I still had the problem.

The final solution was to add the line IdentitiesOnly yes to the /etc/ssh/ssh_config under Host *.

The enlightenment came from this post.

 

 

 

 

Rails: When join tables have no class

I was revisiting an old project of mine, where I had brilliantly created a join table. I had forgotten, though that I did not create a Rails model, because I did not need one. So coming back to the project, I wanted to count the number of rows in that join table in the command line, rails c.

Surprise! I cannot find the the class name, so I cannot access the table directly there…

Instead, I ran psql database_name:

# \dt;
# SELECT * FROM database_name;

to get the list of all the rows in the table.

The join table I created, joins the ids of other two tables: article and category. In was of the models one find the following statement: has_and_belongs_to_many.

To access the information in the table in the rails console, where c is an object of catergory:

c.article_ids

which returns an array of the article ids which are associated with that category. The other direction also works. Given a an object of article:

a.category_ids

returns an array of categories to which the article belongs to.

When in doubt, read the docs:

http://guides.rubyonrails.org/active_record_querying.html

http://guides.rubyonrails.org/active_record_querying.html#joining-tables

Adventures with Ubuntu in a MacBookPro9,2

My daughter came to me one day: “Mum, my Mac is kaput”. When upgrading, the computer just hang. After all the diagnosis possible, we figured it was a disc crash.

A mac techie acquaintance took the computer and after checking it up told us that he could replace the disc and … install Ubuntu! Yes please!

After getting the computer back I started playing with it, connect to the wifi, install this, download that, why is the Ubuntu version only 14.04? Let’s upgrade. After all we are only a few days from the next release 15.10. Hmm, there are errors. We need to reboot, yes, no, ahhh. Kernel panic…

… After some research I had some interesting findings.

There seems to be a tie between the Mac model and the Ubuntu release. This page shows the recommended Ubuntu release to the specific MacBookPro hardware model. They recommend the latest LTS when the user is not sure of the release to install. I was reticent to leave 14.04LTS, but looking at this wikipedia page, I was reassured that this particular version’s support runs until 2019-04! By then this Mac should be history!

To install according to the Mac’s model, first find out the hardware type by typing the following:

sudo dmidecode -s system-product-name

The output in my case:

MacBookPro9,2

And there is where I noticed that 15.04 was not going to work. So I proceeded to reinstall 14.04LTS from a usb stick and that was like a breeze, only after reading how to boot from a usb stick in a Mac:

Insert the Ubuntu LiveCD into your Mac and Shutdown. Restart the Mac and hold the Option Key. When the boot selector screen comes up, choose to boot from the CD.

The full installations instructions can be found here, but I just followed the section “Single-Boot: Ubuntu Only”.

All good except that the wireless card did not seem to be set up. But it was working before so it can be done. I did get scared when I clicked on the MacBookPro9-2/Utopic Unicorn link, and it read that wireless was not supported. But Utopic Unicorn is 14.10. And I have 14.04 Trusty Tahr.

Roughly these are the steps to follow to set up the wireless connection.

Identify the wireless chipset

This can be done in a couple of ways:

  • lspci | grep Network
  • lspci -vvnn | grep -A 9 Network

From the commands I learned that

  • The Chip ID is BCM4331,
  • The PCI-ID is 14e4:4331, and
  • Kernel driver in use is bcma-pci-bridge

Find the drivers for the chipset

This guide contains a full description of specific drivers supporting Broadcom BCM43xx Chipset. And there are a different instructions that one could follow. IN my case the chipset was supported by more that one driver but what worked for me was the section b43 – No Internet access:

  1. Install the b43-fwcutter package.
    cd /media/pool/main/b/b43-fwcutter/
    sudo dpkg -i b43-fwcutter* 
    
  2. Download the firmware file from here unto a computer with internet connection.
  3. Copy the file to your working directory (yes, using a usb stick). In a terminal use b43-fwcutter to extract and install the firmware:
    tar xfvj broadcom-wl-5.100.138.tar.bz2
    sudo b43-fwcutter -w /lib/firmware broadcom-wl-5.100.138/linux/wl_apsta.o
    
  4. Restart the computer or reload the b43 module by switching between drivers. I did the later.
    First unload all conflicting drivers (this includes removing the driver you’re trying to install):

    sudo modprobe -r b43 bcma
    sudo modprobe -r brcmsmac bcma
    

    Then load the driver to use:

    sudo modprobe b43
    

And by magic I now have a wireless connection, and life is good again!

Related links

  1. https://en.wikipedia.org/wiki/List_of_Ubuntu_releases#Ubuntu_15.10_.28Wily_Werewolf.29
  2. https://help.ubuntu.com/community/MacBookPro
  3. https://help.ubuntu.com/community/MactelSupportTeam/AppleIntelInstallation
  4. https://help.ubuntu.com/community/WifiDocs/Driver/bcm43xx
  5. http://www.lwfinger.com/b43-firmware/broadcom-wl-5.100.138.tar.bz2
  6. http://askubuntu.com/questions/338210/broadcom-sta-wireless-driver
  7. https://en.wikipedia.org/wiki/Station_%28networking%29
  8. https://help.ubuntu.com/community/WifiDocs/WirelessCardsSupported

git: comparing with remote branches

I have a Rails app which is deployed in Heroku and its source is in bitbucket. In Heroku I have in fact two instances: staging and production. When I came back after a break on the project, I wanted to compare what was deployed or committed where, as I knew that a JavaScript bug had prevented me to have a full deployment.

git works with branches so the comparison I want to make, takes place between branches, regardless of where they are located.

To lists local and remote branches, run the command:

git branch -a

The output for my app looks something like this:

  ...
  master
  remotes/origin/master
  remotes/heroku/master
  remotes/staging/master
  ...

The first line is the local working branch, the second in the remote master branch in bitbucket, the last two lines are the production and staging branches in heroku.

I can simply run git diff with the name of the two branches to get a detail description, line by line, of the differences:

git diff remotes/heroku/master remotes/staging/master

To get the list of only the files that are different, use the following command:

git diff --stat --color master remotes/heroku/master

Sources:

Dear App, why are you giving me the wrong date?

Localization has always amazed me. But I also giggle when developers get it wrong. I know for experience that it is not obvious. How does the app know what language do you speak if you leave in Montreal, Switzerland or Belgium, where there are more than one official language? And that is when when the user wants to use the app in one of the official languages.

Added to that are the timezones and daylight savings shifts in one place throughout the year. I am originally from Colombia, where even the notion of seasons is summarized as the rain and dry seasons. Don’t ask me when each is supposed to be! Change of time in Summer? Forget it. “Wow, are you really going to sleep now, aunty, here we are going to have lunch!” The internet brings these issues to the fore and forces the need for solutions. The infrastructure is there to be used.

The last time I stumbled upon timezones was when implementing the events section of my Rails app. The first surprise was to find that the time I saved was shifted. The reason made perfect sense. The canonical storage timezone in the database is UTC and the display can be done in a timezone of your choice. Cool. I adjusted the configuration and added to the config/application.rb file the following line:

    config.time_zone = 'Eastern Time (US & Canada)'

That worked just fine in my local development platform: the storage was done in UTC and the display in EST/EDT depending on the time of the year, with the following code:

    event.my_date.to_formatted_s(:long_ordinal)
    event.my_date.zone %>

But when I deployed to heroku, the initial problem came back. I realized that it had something to do with the local timezone of the machine, somehow. As would only make sense, the heroku server is running in UTC time. However, even if the default timezone read EST, the actual saving into the database was done simply by striping the timezone: 17h00 EST became 17h00 UTC.

I could not find a reason, but that does not matter. The point is to make the app work standalone, independently of where it is deployed.

I tried several paths:

1. I added an extra line to the config/application.rb as follows:

    config.active_record.default_timezone = 'Eastern Time (US & Canada)'

However, that line just made the previously saved my_date fields disappear.

2. I then used the use_zone command to create a block, where the default timezone would be defined:

Time.use_zone("Eastern Time (US \& Canada)") {
        time_entered = Time.new(
          params[:date]['year'].to_i,
          params[:date]['month'].to_i,
          params[:date]['day'].to_i,
          params[:date]['hour'].to_i,
          params[:date]['minute'].to_i,
        )
        @event.the_date = time_entered
      }

This was a step forward, the time_entered was produced in the correct timezone but the assignment and then saving in the database, simply stripped off the timezone information.

3. After much trial and error, the solution was to create an object with the UTC offset. This offset had to be dependent on the entered date, as daylight savings for that date is what matters.

The final solution is:

      Time.use_zone("Eastern Time (US \& Canada)") {
        time_entered = Time.new(
          params[:date]['year'].to_i,
          params[:date]['month'].to_i,
          params[:date]['day'].to_i,
          params[:date]['hour'].to_i,
          params[:date]['minute'].to_i,
        )
        the_offset = Time.zone.parse(time_entered.to_s).utc_offset
        new_time = Time.new(
          params[:date]['year'].to_i, 
          params[:date]['month'].to_i,
          params[:date]['day'].to_i,
          params[:date]['hour'].to_i,
          params[:date]['minute'].to_i,
          0,
          the_offset
        )
        @event.my_date = new_time
      }

The code for viewing the date, did not change.

If you have any further comments or explanations, please send them this way.

Sources

Python’s lists: referencing versus copying

I finished the on-line class Introduction to Interactive Programming in Python from Rice University and it was a lot of fun.

asteroids

This is a screen shot of the last game RiceRocks, which is a simplified version of the arcade game Asteroids. My ship has blown up a few asteroids. 😉

Although the course caters for beginners, I learned a great deal of things. For example, I had never programmed an animated game, so working out this kind of interactive programming was great.

In this post I want to put to the fore an issue which comes up very often. In the RiceRocks game, there was a class Sprite, to create rocks and other objects. The sprite image is based on an image object whose center is a list:

[ x, y ]

The sprite instance has an image_center element which corresponds to the image center. It is created as follows:

self.image_center = info.get_center()

and the method in the Image class

    def get_center(self):
        return self.center

is returning the center as list. The result is that the center is a reference to the same list. So the sprite.image_center and image.image_center point to the same list.

This is not a problem for what is called in the game static images. But explosions are dynamic, which means that the image is actually a tile of a sequence of the explosion, and tiles are shown one at a time in consecutive frames as a function of the center of each tile.

In my implementation, I create a sprite for the explosion of a rock, so the rock sprite is replaced by the explosion sprite. As the time advances, I change the center of the explosion sprite to display the new frame. This worked only for the first explosion…

As I was changing the image_center of the explosion sprite, I was also changing the center of the original image. The solution was to create the sprite with a copy of the list:

self.image_center = info.get_center()[0:]