.zerohack.

git repo. list all.

I write code. These are some of my thoughts.

C versus C++

[linkstandalone] C v. C++

I recently joined the ACCU and I’ve been thinking / reading a lot about these two languages. They both have very large entrenched user bases with very strong opinions. I’m by no means an expert C++ programmer but I am always learning and trying to do my best to stay up to date with all the new features. Between the two communities there exists a large divide in their philosophy of use. Some members of these communities can be very opinionated about the language of choice. The breakdown goes something like this:

This is of course a generalization. There are very small and minimal C++ projects and large behemoth-like C projects. In any case I think the general attitude is accurate; especially when we look at how the ISO committees treat these languages. C++ tends to grow ever larger at each three year interval, while C grows slowly and sometimes even shrinks. Working with C++ I know I cannot keep all of its features in my head and nor can I use all the features in each program. This makes me think; at what point does a language become too large?

Since I cannot hold the entire C++ standard in my head currently, does it even matter that it grows larger? Is minimalism a worthy virtue to strive for in this area? Maybe it is more work to write C programs since the language does not provide all of the useful abstractions that C++ does. If one isn’t going to write all their own C code by hand and decides to use a library to help them out are they any better served by using the smaller of the languages. In other words wouldn’t you be better served by using a standardized library that was guaranteed by the language to be included?

There was the famous post by Linus Torvalds talking about how much he dislikes C++ and its programmers. I think there is some truth to his comment about using new abstractions and then gettign chained to them. Against his other point; people can write just as terrible C code as they can C++.

Why pick C++ over C?

One of the things I do prefer about C++ is its support for functional programming. I love lambdas. This doesn’t mean that C++ covers this area the best. I really wish there the attitude of immutability by default like there is in Rust. That is a whole other rabbit hole which deserves its own post.

Jack of all trades, master of none

I think looking at the attitude of the Python language here highlights and interesting issue. Guido Van Rossum is of the opinion that there should be the One Way to solve a problem. To be exact he wrote, “There should be one– and preferably only one –obvious way to do it.” I think this stemmed from the fact that Perl was much more popular at the time and it was generally considered a very expressive language where there could be many different solutions to the same problem. Then, a programmer who comes along to read or improve anothers program, must first decipher what problem was being solved by the original writer. This contributed to the reputation of Perl being a write-only language. Is it better to give the programmer expressiveness over readability? How do these ideas transfer over to the world of C++?

OOP

Maybe its just my preference towards the functional paradigm, but I don’t think that Object Oriented Programming is all that it was hyped up to be. I think that in many cases it makes a lot of sense. Other times you get this nightmare of nested class structures. OOP was touted as the programmers saviour during the 90’s. I remember reading this quote from Dijkstra where he says:

“Object-oriented programming is an exceptionally bad idea which could only have originated in California.”

I think at this point it’s obvious that OOP has not solved the woes of the programming world and it does not fit every use case of every problem.

Portability?

It may be a fair statement to say that there are more ISO compliant C compilers than there are C++ compilers. For most people there are really only three C++ compilers; gcc, clang, and msvc. Yes, there are other pay-to-play C++ compilers you can use like Intel, IBM, or PGI; which reminds me that I need to try some of those out. But how many people are using those.. What significance does this hold? Does maximum portability acctually matter for your project? Will you code ever run on a obscure microcontroller? If you are writing a 3D game engine, it really only makes sense to target desktop style machines supporting your Big 3 compilers.

On the other hand the Doom engine was written in portable C, and look how that turned out..

Conclusion

All in all I’m still working on these questions myself. These are mostly philosophical and without straightforward solution. Maybe there is no One True Way. Maybe I should just become a Lisp programmer. That’s where the real minimalism lies.

Thu, 12 Sep 2019 12:48:38 +0000

Summer Update

[linkstandalone] Summer Update

With Defcon coming up very quickly I’ve been preparing my laptop. I had an SSD die on me at the most inopportune time. Luckily I keep install scripts maintained in the event I need a reinstall I can simply start her up and type ‘go’. I’ve been working on getting my WireGuard VPN server set up correctly, as well as setting up OpenVPN. I had the latter set up at one point but my scripts were a bit outdated and needed some attention. I’m pretty excited to see WireGuard become more popular. Looking at the install page they list almost every platform imaginable. That speaks to the portability of the code. Hopefully PIA will start offering it in the near future.

I have been tormenting myself with the possibility of moving my C based projects off vanilla Make. The straightforward choice seems to be CMake. I am not thrilled with the idea of learning a whole new language just to generate makefiles. On the other hand, it would be very nice to have the multi-platform support baked in, and the ability to generate ninja files instead of makefiles. Then you have all the other choices like; Meson, Bazel, build2, and SCons. Trying to narrow down what separates all of these different build tools is quite the task in and of itself. Maybe the best choice is to do nothing and stay with Make. Build tools are to C++ what frameworks are to javascript.

Other than that it has been very busy the past few weeks and I am very much looking forward to some downtime by the pool. Maybe I’ll even get one of those drinks with the little umbrella in it.

Cheers

Tue, 06 Aug 2019 17:34:05 +0000

What's New

[linkstandalone] What's New

I’m feeling pretty good about where I am at with the state of my projects right now. The blog migration is complete. It feels much more maintainable now. I am disappointed that my desktop migration to OpenBSD did not go as planned. Unfortunately, the NFS implementation did not cooperate with me. Maybe I can revisit the OS as desktop replacement again in the future. It’s a shame since everything else was set up just the way I liked it. Linux does the job OK for now. Maybe I’ll give Funtoo a shot.

The ol' TODO list is looking slimmer each day. It was starting to look unweildy near the beginning of the year. I’ve knocked out a few of the projects I wanted to work on this year. One of the more practical ones was pecm. I hope there are some other OpenBSD users out there that can make some use from this tool. I’ll be going into my summer hackathon here this weekend, so I’ll get to give some more attention to blink1d. I hope to make it cleaner and more portable. I will also spend some time working through the classic MIT text ‘Structure and Interpretation of Computer Programs’.

The goal with working through the book is to get together with some friends and help each other out. I’ve programmed some Scheme a few years ago and I had fun with it. Hopefully this will be the in depth time I need with Scheme to really wrap my head around it. I suspect there will be some posts going into more of what I learned from the experience.

Cheers.

Fri, 12 Jul 2019 18:27:53 +0000

ETHDenver 2019

[linkstandalone] ETHDenver 2019

I was able to travel and attend this years ETHDenver again! While I arrived to the party late; I did get to spend most of the weekend at the event, albeit minus a team this year. This mean’t that I wan’t really able to finish a project this time around, but I certainly had a great experience! I would say that they outdid themselves compared to the previous year. There were even more people in attendence than before! With an expanded area for hackers to work and roam about.

This year the keynote speaker of the event was the legendary author of Mastering Bitcoin and Mastering Ethereum; Andreas M. Antonopoulos. Great speaker, and a very interesting guy. Once again, I don’t feel very deeply immeresed in what is going on with the blockchain tech companies. There were many companies present which I had never heard of before. (Set, Blocknative, etc). Still have to do some research to find out what all there products are..

Overall, would still recommend the event for newcomers to attend and see what this tech is all about! Cheers to the Colorado Blockchain community!

(All photos rights reserved to optictopic)

Sun, 07 Jul 2019 18:42:03 +0000

Amp Mods

[linkstandalone] Amp Mods

This is a walk through of the modifications which I have made to my Breeze Audio mini amplifier. I have a Micca OriGain mini amplifier I use for my desktop computer setup, which works great. No noise, slick minimal design, perfect for my use case. I was looking for another mini amp to use in the other room to use with some bookshelf speakers, and there is a plethora of these TPA3116 amps floating around. I was able to purchase mine off eBay for $20! I figured it would be a fun project to see how much we could improve the sound quality of this cheap little amp, as well as personalize it a bit. Some of the mods I’ve done are probably overkill for this unit, but I figured why not, since I’m already in there working on it let’s make it as nice as possible. This is what mine looked like although they can come in a variety of anodized colors.

If you are looking for one of these to purchase they can be found on eBay, Amazon, and AliExpress under various additional brand names such as Nobsound and Fosi Audio. Usually the units do not include a power supply, so keep that in mind. You will need a 19-24v DC power supply (center positive) with around 3 to 5 amps. I would not spend more than $30 on one of these amps without a PSU. If you have an old laptop charger lying around these usually work great! Take a look around your local thrift shop for one.

The initial configuration of this amp has a few problems: * The TPA chip is configured for too much gain, which leads to a Poor noise floor * Inputting 21 volts, the output is rated for 50w at 10% distortion (yikes!) * Cheap Chinese clone Sanyo + Panasonic caps * Loose parts (Heatsink + Output Inductors)

Modding Time

Here is the overview of what we are going to do:

Put some nice fresh thermal compound on the chip to help out with the heat issues of this board since the heatsink is on the smaller side.

Parts Used

After

Thanks to FFcossag, NFM, and Adrian Black of YouTube

Fri, 05 Jul 2019 19:41:45 +0000

Crystal Clear

[linkstandalone] Crystal Clear

Do you want to have the productivity of a scripting language while still having the benefits of a statically typed and compiled language? Well, Crystal may interest you. I spend a fair amount of time programming in Python and while I enjoy the languages productivity and all of the wonderful libraries available, there are some things I miss. First of all is speed. Python isn’t exactly known for its speed. While you can use Pypy or write your important code in C and use the FFI, that kind of defeats the purpose of using the language in the first place. Also, I can’t say I’m very much a fan of the syntax of Python. I have programmed in Ruby some in the past, and its syntax has always held a special place in my heart. Someone (likely some random person on IRC) once wrote about Ruby that, “Ruby is what happened when Lisp got lonely and Perl got drunk”.

Crystal takes the syntax of Ruby, adds types, and compiles it down to efficient machine code. Their motto is “Fast as C, slick as Ruby.”. There is no manual memory management. They use the Boehm garbage collector. This is a whole other interesting subject and while there are some cons to using this tool, I can understand why they chose to use garbage collection. Dealing with memory management is a tedious process and slows down the entire development process. So where does this language fit in? Who is it geared towards?

The obvious candidate is Ruby programmers. I could see maybe some Go programmers using the language, Python devs, PHP devs, maybe even D lang devs. D is another one of the languages I have been looking into most recently and I can see a lot of parallels between the two languages. Crystal seems to be geared towards web services in general. There is a simple HTTP server, and built in concurrency support via Fibers. They are working on implementing parallelism, and the claim is that it will be ready in 2018 [Update: There is parallel.cr, which is parallelism as a shard]. There is a growing pool of libraries available for the language.

Rather than gems, Crystal has shards (heh.).

Features: - rspec testing - reuse of ruby code - shards - community

Example

For a pet project I wanted to write and RPN calculator in Crystal (think dc). If you are curious to check out what it is like you can peek at some of my code..

Speed

So how fast is Crystal? There is a nice GitHub repo where a user did some benchmarking of various algorithm implementations. In these synthetic tests the language overall appears to perform very well for being a garbage collected language.

Maturity

Crystal is still a new language. It is considered alpha level software. This doesn’t mean that you shouldn’t use it though. There a few companies who are using Crystal code in production. Give it a shot. I don’t know that I would use this as my main language of choice, but when I want to write Ruby code I’ll likely write more Crystal code instead!

Mon, 17 Jun 2019 16:14:32 +0000

ETHDenver

[linkstandalone] !DOCTYPE html> ETHDenver

Recently I was able to attend a hackathon. This was not just any hackathon. This was the worlds largest Ethereum hackathon; ETHDenver. Previously the largest was located in\nWaterloo Ontario, which happens to be Vitalik Buterin’s Alma Mater. We had people come from all over the world to make it to this event! While the event opened up Friday morning I was not able to arrive until that evening. I was quite surprised at how huge this event itself was. The venue consisted of 5 stories, 3 of them being spaces for the teams of hackers to work. On the first floor there was an art gallery and the main stage. There was also a chill room:

The second floor was comprised of the vendor booths. Being new to the Ethereum world I had not heard of most of the companies. I’ll put a list down at the bottom of some of the ones I found interesting.

Enough about the venue for now. Leading up to the event I had been studying th whole Ethereum ecosystem. While I had I had used the currency aspect of it, I had not dug into the technical aspect of it. First we (the fresh team I was on) had to come up with an idea for a project. After a couple of hours, our team decided we wanted to try our hand at storing torrent magnet links on the Ethereum blockchain. What we came up with was the Stormburst project. The goal of the project was to have a way that you could share a magnet link in such a way that it was near impossible to remove. There is essentially 3 parts to the project. There is the smart contract itself which sits on the network. There is the web front end which is able to interact with the contract. Then there is the wallet, which is able to interact with the web front end. You end up with a picture like this:

Contract <- Front end <- Wallet

On the contract side of things there are a couple of languages to choose from. We ended up going with Solidity. The code ends up looking pretty much like C++ (nice for syntax familiarity), and it seems to work well. A fun way to learn some Solidity is with CryptoZombies. There is a python-like language available, Serpent, but we didn’t seem to find as much support for it so we decided against it. For the web side of things the framework we chose was called Truffle. It seems to be the most popular the documentation was fairly comprehensive. It really did make working with the Ethereum ecosystem much easier. I was able to get it installed with a simple npm install -g truffle. Lastly for the wallet side of things we used [Metamask](https://metamask.io/] to take in payments to our contract. With Truffle this was simply a plug and play process as it checks to see if there are any web wallets available. Two of us had some issues with the Metamask plugin on Firefox on Linux but Chrome seemed to work fine. If you want to check out our project, take a look at the GitHub. Going back to this after the fact; whatever issues Firefox was having, the Metamask team seemed to have fixed it.

Overall the whole experience was really fun. I had not been to a hackathon prior to this one and I have a hard time imagining any others living up to it. We won a small prize and were able to meet and talk with a bunch of cool people I mean, having all of the food trucks available out front was pretty neat as well. I will be looking forward to the event next year.

Companies in attendance: - ERC dEX - KeepKey - 0x Protocol - bZx - Storj - Polymath

[originally posted 2018]

Thu, 13 Jun 2019 03:20:17 +0000

Clojure Web Development

[linkstandalone] Clojure Web Development

These days there are a plethora of web frameworks to choose from when developing a new website. Name any language and it’s pretty much guaranteed that there is a web framework written for it. Haskell? yep. Erlang? yep. Brainfuck? most likely. I myself, am a fan of Lisps. I’ve spent a bit of time programming in Racket (a Scheme to be exact) Something about them just feels rather elegant to use and program in. I’m by no means an expert, but still I find them fun. In my day to day I am a C++ programmer. It seemed like fun to take a look at Clojure since it appears to be one of the more widely used Lisps. And there is a Clojure implementation for embedded devices called Ferret.

There were a couple different choices out there for web frameworks built around Clojure, but I ended up choosing Luminus. In this post I will discuss my process of learning and getting set up with a development environment, picking my project, and learning a bit of Clojure. In the end I made quite a bit of progress on the project and I hope to continue to work on it over the coming weeks. Also, I should not that in my exploration of Clojure, I have come to discover my love for Emacs (praise gnu). I have a little experience programming in the functional paradigm and I wanted to dive deeper into that world. C++ recently has added support for lambdas in to the language in one of the latest standards. The previous experience that I had was working with Racket, a scheme, mostly used in the academic sense. There t seem to be a whole lot in the way of libraries for Racket when it comes to web programming, and I t have time to write my own (nor do I really want to). Clojure has been praised for having excellent support for functional programming, preferring immutability by default unless specified.

It also has great support for concurrent programming with its software transactional memory system. One of the main reasons I ended up picking Clojure was the plethora of libraries available to it, since it runs on top of the JVM. Well, that and I needed an excuse to read through my copy of Clojure For The Brave And True. With picking Luminus, there were several reasons why I ended up at that choice. One, other frameworks I looked at seemed to be less mature or lacking documentation. Two, the projects seemed to have been abandoned and no longer updated. Luminus appeared to both have a good community and documentation. Additionally, I thought it was interesting that they present it as a microframework [https://en.wikipedia.org/wiki/Microframework]. This was a term that was new to me. After doing some research about microframeworks I figured it would be worth the experience of giving it a shot. The short story of how microframeworks differ is that they sacrifice features for simplicity. Microframeworks usually do not have direct support for things like\nauthentication or other advanced features.

Let’s get started

For this post I wanted to do something a little different. I enjoying browsing the website Hacker News. The site has a lot of interesting content and has a very minimal interface. One of the downsides of the site is that you can only browse by currently popular stories, or new stories. There is no way to view old popular stories.

What I want to do is create a site which will track the current top story from Hacker News and when it changes store the old one in a database. You could have several different options available for users to look through old stories. You could show past top stories by week, month, or even year. This would be helpful for the occasional user of the site, so rather than look through the hundreds of stories that were posted to the site that week, they could narrow it down to the few dozen that were the most popular.

s go ahead and grab one of the first pieces of software ll need to get started; Leiningen. Do note that I am also assuming that you have Java JDK 1.8 installed on a Unix machine already (needed by Clojure at the time of this writing) Leiningen acts as your Clojure build tool. It handles downloading Clojure for you, creating new projects, and running your test suite. ll get very used to running the lein command. s install it. Navigate you browser to https://github.com/technomancy/leiningen. Download the lein script. Now we need to allow the script to be executable; open a terminal, navigate to where you downloaded the lein script and run this command:

$ chmod +x ./lein

We can now run Leiningen from that directory. If we want to be able to run it from any directory we need to make sure that it is in our $PATH. If we move the lein script into the directory /bin this should work on most systems. Run this command to move the script:

$ sudo mv ./lein /bin

Like most other programming languages you could edit your Clojure project files through a basic text editor like nano or gedit, but I would recommend setting up an IDE. Since I have been using s IDEA recently, I ended up using the Cursive plugin. It allows you to turn IDEA in a Clojure IDE. This worked out great for me and since Clojure has a REPL, it was very helpful to have quick access to that. The layout is also very familiar to you if you are familiar with IDEA. If Intellij is not to your liking there are still other options out there as well. The most popular seem to be Emacs.

While some view Emacs as part religion, part text editor, it has stuck around\nthirty years for a reason. I have been using Emacs for a brief period, just trying to get my feet wet. While it took me a while to figure out all of the keyboard shortcuts I needed to know, I will say I have been overall very impressed. Esspecially with the fact that no graphical interface is required, meaning I can develop my Clojure code over ssh just using a terminal. Lastly, I will mention that there is a Eclipse plugin called Counterclockwise for those who prefer that environment. I did not try out that one so I cannot speak to its quality. The project appears abandoned.

We will need to pick a database when we start our new project as Leiningen will configure it for us. I went ahead and chose the h2 database as it is well supported in Java. You could have just as easily picked MySQL, or SQLite, or MongoDB. Let navigate to where we want to have our new project located in the terminal. Now to create a new Luminus project in leiningen we can issue the following command:

$ lein new luminus hndottech +h2

We will need a way to scrape the top stories from hacker news website. There are plenty of mobile apps out there in the app stores for accessing the site, so I figured it t be too difficult to integrate into their site. Luckily while I was searching on GitHub, I did end up finding a Java library for interacting with hacker news. It appears that this library scrapes the data directly from the HTML (rather than interacting with some public API). This saves us a lot of work in trying to implement that ourselves. (There may still be a way to actually talk directly to HN’s API, I’m still looking into it). [Update 2019:] Yes, there is now an official Hacker News API you can call directly!

First, I tested out the library just in a Java project just to make that it worked. I had to make a few changes to the Java code to get it to work right. I had to set up the package name to get my classpath correct. This library was also complained about being used by Java 8, as this was written to be used by Java 6. Now that I have verified that the library will work I need to figure out how to interact with Java code from Clojure. Now that we have our environment and libraries set up, we can get into some Clojure code. Before we start with that though, s go back to that Leiningen command we ran earlier. We have created our skeleton application with lein. s take a look at the layout and get a feel for what we need to edit.

Leiningen gives a a good starting point here with our base project tree. We have things like a basic README.md, .gitignore, dev and production environment files. s go ahead and move our Java source files under the src directory in a new folder named java. Lein will automatically take care of compiling those for us. Next we will edit the project.clj file. We need to add in a couple of dependencies as well as tell Lein where those Java source files will be located. Mine ended up looking like this:

Most of our other edits will take place in the resources directory. This is where we can edit our HTML templates, CSS, and database resources. I ended up changing the color scheme, favicon, navbar, and others to suit the appearance of the site. I wanted it to look like the original hacker news site. Also, in the src folder. We need to edit our routes file to have a particular route make a call to hacker news page get function. It ended up looking like this:

We need to initialize our database. First I edited the SQL migrations file to suit holding Page objects and ran the following command:

$ lein run migrate

Your output should be something similar to this:

We can pack up our site into a JAR for running on a production server by issuing the command:

$ lein uberjar

To host my application I ended using an OpenBSD instance from Vultr. That is my preferred hosting environment. You could choose to host this anywhere that you can install the JDK. Since I wanted to see this project through to completion I went ahead a registered a new domain name for it. I ended up settling on hackernews.tech. Definitely worth it for a couple bucks a year. If you want to see the code for this project, take a peek at the GitHub page(dead). [Update 2019:] While the site is dead for now, I still like the idea and want to revive it. Just need to make it through some of the other items in my TODO list..

[originally posted 2017]

Tue, 11 Jun 2019 13:37:28 +0000

Deploying An IRC Server

[linkstandalone] IRC

IRC stands for Internet Relay Chat. It is a protocol for chat room service, and it is one of the oldest. These days some of the most popular networks are places like, Freenode, Rizon, EFNet, and Snoonet. You connect up to a particular network, and from there can pick through their various different channels.

Why run your own server?

With the rise of Slack, Gitter, etc. why would anyone want to run their own IRC server? From 2003 to 2012 there was a loss of about 600k users from IRC networks source. Maybe you just have a spare computer at home you want to make use of, or maybe you are on a small team that needs their own private chat room. There are some benefits to be had from picking IRC.

  1. No web browser needed. I can chat from a headless server if I need.
  2. Own your data. No ads or bothers to upgrade your service.
  3. It’s free. (Minus whatever hosting/electricity costs).
  4. It’s secure. Most daemons offer connections over SSL.

Which server?

Inspircd, UnrealIRCd, IRCd-Hybrid? One of the benefits of IRC being an older protocol is that there is a wide variety of daemons available out there to suit your needs. The one I ended up going with was ngIRCd, which stands for next generation IRC daemon. I wanted a server which would be easy to maintain without many fancy extra features. Plus it was widely available on most Unix platforms.

Install

Depending on what OS you are running the difficulty of installation may vary. On Ubuntu installation is as simple as $ sudo apt install ngircd. It should be available in your distro’s package manager (or Port system on Free/OpenBSD). Otherwise, you can always compile it from source here

Configure

All of the configuration will take place in /etc/ngircd. There should be two files; ngircd.conf, and ngircd.motd. You can edit the motd file to show whatever message you would like displayed to the users upon their login. For the uninitiated MOTD is an acronym for Message Of The Day. Maybe that could be a warning, maybe a fun banner, maybe you don’t want anything. All the other configuration takes place in the conf file. Here is a link an example ngircd.conf file. Feel free to use it if you’d like. The documentation here I found very helpful as well. Spend a few bucks and get a fancy domain name to go along with your fresh server as well! (or a free subdomain from someone like freedns.afraid.org)

Security

I recommend using a small VPS for you IRC server. If you know what you are doing, you can always host it from your home as well. Be sure to have a proper firewall and other precautions in place. For my configuration I only allow SSL connections on port 6697, the users must know the password to connect, and additional security features are enabled from the ngircd.conf file. The most important part is generating a strong SSL/TLS certificate. Follow the instructions here depending on which protocol your daemon was built with.

Final Thoughts

There are some other alternatives if IRC still doesn’t fit your needs. It can’t do fancy things like voice/video chat, it’s not the best at sharing multimedia. For those types of tasks one might look at some of the current open source Slack-a-likes; Rocket.chat, or Let’s Chat. There is also a very cool new distributed chat platform called Matrix, with its glossy interface being Riot.im. I’ll leave you with that thought and those other options to explore. Happy chatting!

[ originally posted 2018 ]

Fri, 07 Jun 2019 00:37:51 +0000

Singularity

[linkstandalone] Singularity

Singularity Containers

I recently was introduced to a new container technology called Singularity. If you are unfamiliar with containers, the short version is that they are like running a virtual machine without the high cost of running an entire operating system. That is a bit of a gross generalization, but this post isn’t about that. Do a little reading elsewhere to get caught up. It’s a pretty cool project geared towards folks in the HPC realm.

So What is it?

The project was born out of Lawrence Berkeley National Laboratory and has several interesting stated goals:

The main idea is that you can take a container which you have made and move it onto another system, and run it without issue. This tries to avoid the age old problem of “Well, it runs on my system!”. It’s interesting that this has been developed to work well with high performance computing architectures. So if you have a RedHat system with a legacy kernel, you can still run your Singularity container. Also I appreciate their security model as well. If you cannot get root access outside of the container, you cannot get root access inside of the container. Singularity also differs from Docker in other ways. For instance, if you need access to hardware resources such as a GPU, you can easily take advantage of that from within your container.

Setup:

The Ubuntu repositories have an outdated version, so I would recommend installing from source (GitHub). It worked painlessly for me. Run these commands to install:

$ git clone https://github.com/singularityware/singularity.git
$ cd singularity
$ ./autogen.sh
$ ./configure --prefix=/usr/local
$ make
$ sudo make install

Type singularity and you should see the help menu appear in your console.

Creating an image

We will set up a container that will be used to mine Monero. If you are unfamiliar with Monero, it is a cryptocurrency and you can read more about it here. First, we will need to create our recipe. This is the simple file I came up with. The format is a mix of shell script and their own syntax. (Name this file whatever you want. Mine is simply ./Singularity)

BootStrap: docker
From: ubuntu:latest
%runscript
%post
apt update && apt install -y git build-essential cmake libuv1-dev libmicrohttpd-dev
git clone https://github.com/xmrig/xmrig.git
mkdir /xmrig/build
cd /xmrig/build
cmake ..
make

What this is doing is simply going out and grabbing the base Ubuntu OS image from Docker, then installing the extra utilities which I need. To build this recipe, you can issue the command:

$ sudo singularity build ubuntu.simg Singularity

Running

Now you should have your ubuntu.simg file in your current directory. To get into your container, simply issue this command (Note: Your prompt should change if it worked correctly):

$ singularity shell ubuntu.img

Congratulations, you’re in business! You’re now inside of your very own Ubuntu Singularity container! We can now hop over to /xmrig/build and fire up our miner! For more information, head over to the Singularity documentation here.

[originally posted 2017]

Tue, 04 Jun 2019 14:38:59 +0000

Ruby + Bootstrap

[linkstandalone] Ruby + Bootstrap

Ruby + Bootstrap

Introduction

For this post we will be extending our web application from part two. If you haven’t read the earlier posts, check out part one here. There are several things which we will be implementing. First, we will be adding a navigation bar to our index pages to help our customer navigate our site. Secondly, we are going to add in student roles. We will also add in the ability to enroll in classes. There are also a few things we will be changing to our views to see our information in a more clear fashion. I’ll remind you once again that you can check out the GitHub for the code here(lost).

Background

The first task we will tackle will be to add a navigation bar to our application. We want our users to be able to navigate to the other pages of our site without having to type in a URL. We could accomplish this by adding some simple HTML at the top of each index page, but we are going to take a different approach. We will be using a front end component library called Bootstrap to accomplish this task. The benefit of using this is that our site will be responsive; meaning that its appearance will change depending on the particular environment you view the site from. For instance, if you view that site from your phone, it will dynamically change its style to accommodate the smaller screen resolution. It is also a bit more aesthetically pleasing than plain HTML (to some ).

The next task we will be working on to add in our new models. We already have a professor model in our project, but we will need a student model. Once we create that, we will need to create an enrollment model to allow students to sign up for particular classes (sections of those classes to be exact). This enrollment model with belong to both a student and a section. For this project we will just give the student a name. If we wanted to be a bit more realistic we could assign a slew of other fields as well (student number, address, academic standing, etc.), but we will keep it simple for the sake of time.

Once we have added in our new models we want to see these features reflected in our view. We will make some changes to our show method where when you click show on a particular student or professor, you will see more details. For example when we click to show on a professor we will want them to see their sections and the students enrolled in those sections. When we click to see the details of a student we will want them to see which classes they are enrolled in, and the professors teaching those classes.

Get to the code already!

Ok, to set up our navigation bar we will need to install Bootstrap. s go ahead and edit our Gemfile and add the dependencies needed. At the time of this writing the latest version of the Bootstrap gem is the 4.0 beta. Put these lines in your Gemfile.

gem 'bootstrap', '~> 4.0.0.beta2.1'
gem 'jquery-rails' # Bootstrap requires jquery
popper_ # Bootstrap requires popper_js

Note : We also have to make sure our version of sprockets-rails is at least 2.3.2

Now we may run a $ bundle install to download bootstrap. Next we have to add the bootstrap styles into our app/assets/stylesheets/application.scss If this file is instead a .css file, be sure to rename it to be a .scss file. Add this line to that file:

@import \"bootstrap\";

Also, be sure to remove remove all the require and require_tree statements from the Sass file. Lastly, we need to edit our application.js file to add these three lines:

//= require jquery3
//= require popper
//= require bootstrap-sprockets

We should now be able to Bootstrap elements into our web page. In each of our views, edit the index.html.erb and add this to the top of your file.

<nav class=\"navbar navbar-toggleable-md navbar-light bg-faded\">\n  <button class=\"navbar-toggler navbar-toggler-right\" type=\"button\" data-toggle=\"collapse\" data-target=\"#navbarNav\" aria-controls=\"navbarNav\" aria-expanded=\"false\" aria-label=\"Toggle navigation\">\n    <span class=\"navbar-toggler-icon\"></span>\n  </button>\n  <a class=\"navbar-brand\" href=\"#\">Banner Web App</a>\n  <div class=\"collapse navbar-collapse\" id=\"navbarNav\">\n    <ul class=\"navbar-nav\">\n      <li class=\"nav-item active\">\n        <a class=\"nav-link\" href=\"#\">Home <span class=\"sr-only\">(current)</span></a>\n      </li>\n      <li class=\"nav-item\">\n        <a class=\"nav-link\" href=\"/courses\">Courses</a>\n      </li>\n      <li class=\"nav-item\">\n        <a class=\"nav-link\" href=\"/professors\">Professors</a>\n      </li>\n      <li class=\"nav-item\">\n        <a class=\"nav-link\" href=\"/sections\">Sections</a>\n      </li>\n    </ul>\n  </div>\n</nav>

Now if we start up our server and navigate to one of our index pages, we should see the hamburger icon at the top of the page! If we click on the icon we will have a drop down menu allowing us to navigate between our different index pages.

For the next portion will are going to create some new models to add into our application. Remember we are creating a Student and a Enrollment class. To accomplish this we will go back to using our handy rails generate command. In a terminal, navigate to the root of your project and issue these commands:

$ rails generate scaffold Student studentname:string
$ rails generate scaffold Enrollment student:references section:references

You should now see the new models listed under ./app/models.

Next, we are going to add in the ability to see which professors a student is enrolled with, as well as what students a professor has enrolled in their sections. You’ll need to add a couple of things into our controllers to allow us to accomplish this. In professors_controller.rb change the show method:

In the students_controller.rb change the show method as well.

Now we will need to go back into our views directory and edit the show.html.erb file for both the professor and student. For the professor add a portion into the file like this:

For the student, open up show.html.erb in the ./views/students directory and edit the file to add in this portion:

Now go ahead and restart your server, add a student, then enroll them into a section. While in the s index page if you click on view you should see something similar to the following image:

If we then navigate to the professors index and look at the professor that we\nsigned up for, we should see them listed under their view like this:

Conclusion

By making some small edits we were able to make our little web app a bit more robust. There is still a lot of improvement, but we are in a much better position than we were before. We have improved the ability to navigate around our site, improved the users experience by displaying what students are enrolled in your section (as a professor), as well as what sections your are enrolled in (as a student). There may be some more changes to come to this app in the future, but for now it is complete. If we wanted to go further we could make a nice homepage, add some graphics, and make some role based restrictions to pages. Next time…

Tue, 04 Jun 2019 02:56:27 +0000

Ruby Site pt. 2

[linkstandalone] Ruby Site pt.2

Ruby Site pt. 2

For this post we will be extending our web application from the earlier post. If you haven’t checked it out, read up on it here. Also, the code is available on GitHub(lost) for you to follow along with. There are several things which we will be implementing. First, we will be adding some validation to our input forms to make sure we are not getting bad data. Secondly, we are going to add authorization into our app. Lastly, we will be adding a search field into our application.

Introduction

The first task we will tackle will be to add authentication to our application. We want our users to register and be able to login with their email and a password. We could accomplish this by adding simple authentication in our app, but that will only allow for one user at a time. We need to be able to handle multiple users of different types. For instance in the future we will want to delineate the privileges between students and professors; but we are getting ahead of ourselves. For now will will just worry about handling multiple users. To accomplish this, we will use a library called Devise.

The next task we will be working on is to add some validation into our forms. We can make sure that certain fields are present when accepting input. For instance, when adding a professor into our application it would be a good idea to make the name field mandatory to be filled out. We do not want users with empty names appearing in our database. Later on we may go deeper and add more complex validations into our application but for now lets just make it a bit more robust.

The only other thing we will be doing is to add the ability to search for professors, courses, or sections. If this were a full fledged website there may be hundreds of entries on each of these pages and having a search field will greatly help out our user. We can specify what exactly we are searching for, as there may be multiple columns in our database to pick from. As an example, when searching on the professor page it would be the most logical to specify the professor name as the column to look through, but maybe in your case it might be better to specify courses; if you want to see all of the professors teaching a particular course.

Let’s Code

Starting off with our authentication task - Devise; this works through the use of cookies to keep track of different user sessions. It is a well tested gem and is better than writing our own (as well as keeping to the Ruby D.R.Y principle). Let us proceed by navigating to our root project directory in a terminal window. We will add the gem to our Gemfile (this is what keeps track of all the gems used in our project) by issuing this command:

$ echo \"gem 'devise'\" >> Gemfile

Now that it has been added, we can run bundler to actually install the Devise gem.

$ bundle install

After the installation finishes there are still a couple of things we need to do to get Devise configured. Run:

$ rails generate devise:install
$ rails generate devise user
$ rails db:migrate
$ rails generate devise:views users

This should get you off the ground. Go ahead and start up you application to take a look at one of your pages. You should still be able to view and access them normally. Why is this? Well, we need to modify each of our controllers to make sure they cannot be accessed by an unauthenticated user. Modify each of your controllers to their appropriate value:

Note: the controller files is located in (your project root)/app/controllers

By added the before_action statement on line 2, we now have restricted the viewing of these pages to only registered users. Go ahead and start up your application and attempt to view a page. You should see the new and improved webpage show up. Register yourself as a user and enjoy your new authentication setup!

Next we will work on adding some validation. To do so we will need to make some additions to our models. In your editor, navigate to ./app/models and make these additions to each one of your models, minus the new user.rb model (this was created when we installed Devise). Notice that in our Sections model, we have added two separate validates; this is because we need both sectionnum and course_id present. Go ahead and start up you app and give it a test. Here I attempted to add a professor without a name.

Next up, we will add our search functionality onto each of our respective index pages. Let’s start by editing our controllers again. In your editor change each of your controllers to look like the following (note that we are in the index method). Now we will need to make some changes to each s index.html.erb page. These can be found in ./app/views. Take note that these edits are in the section of our HTML. Let’s start up our application and give our new search fields a shot. We should be able to type into our box above our results and enter a query to narrow them down! We can push our changes up to our GitHub repo as a last step.

Conclusion

By making some small edits we were able to make our little web app a bit more robust. There is still a lot of improvement, but we are in a much better position than we were before. We have improved the security of our app, improved the users experience by adding search fields, and made it harder for users to enter bad input into our application. In the future we will be adding more and more functionality to improve this application.

[Originally posted 2017]

Fri, 31 May 2019 02:30:30 +0000

Intro to Ruby on Rails

[linkstandalone] Intro to Ruby on Rails

Ruby on Rails Intro

For this post we will be creating a small Ruby on Rails web application which can act as a school website. It will hold Professors, courses, and sections of those courses. It will need to have a database which will hold a persistent copy of the information which we will enter. For this we will use the standard SQLite3 database which comes with Rails. Also note; If you want to look at the code for this project, you can do so on ~GitHub here~ (code gone, tough cookie kid).

Introduction

Before we can get started, we will need to first install both Ruby, and Rails. This process will vary depending which type of operating system you are using. Since I am using Ubuntu (a Linux distribution), so I will show this process. Most Linux distributions should come with a copy of Ruby preinstalled. You can check if your system has Ruby by opening up a terminal and issuing the command:

$ ruby -v

If it is installed, you should see the command return a version number;something like this:

$ ruby 2.3.3p222 (2016-11-21) [x86_64-linux-gnu]

Next we will also do the same for Rails. Type in the following command:

$ rails -v

If it is installed, it will return something like this:

$ Rails 5.1.4

Otherwise, we can install Rails by issuing the command:

$ gem install rails

Gem is our package manager for Ruby. Anytime we need to use external libraries, we can use gem to go fetch them to include in our project. If this command fails for some reason there are plenty of resources available online to help you install Rails. For this sake of this post I wont go into the details of troubleshooting a Rails installation. Another thing to note is that using an IDE (Integrated Development Environment) when working with Ruby can be very helpful. I have used RubyMine and would recommend it; as it helps figure out a lot of the initial setup of your environment for you. If that’s not your style there is always Emacs.

Method

Now that we have our environment set up we can begin to work. But before we jump straight into writing our Ruby code, the next thing we will need to do is figure out the relationship between our different pieces of data. For the sake of simplicity we will say that the only information which we will require right now is that a Professor has a name and many sections. A section has a number and a course. And a course has a name and can have many sections. The models might look something like this laid out.

professor model: * professorName:string * section:reference

section model: * sectionNum:integer * course:references

course model: * courseName: string

Since we now know how the pieces are related to each other we can begin the code writing process. Go ahead and navigate to where you want to create your new project and issue these two commands in your terminal.

$ rails new myapp
$ cd myapp

This creates a barebones empty Rails project for us to begin working on. And we have changed into the new project directory which was created for us. Next we will we generate our models which our application uses. Go ahead and issue these three commands while in the myapp directory.

$ rails generate scaffold Professor professorname:string section:references
$ rails generate scaffold Section sectionnum:integer course:reference
$ rails generate scaffold Course coursename:string

By issuing these three commands Rails automatically created our models with their data types and the corresponding views and controllers for these classes. The next step we need to take is to perform a database migration. This instantiates our sqlite database and migrates these changes into our project. While still in the myapp directory issue this command.

$ rails db:migrate

We can now start up the server and take a look at our application as it sits so far. To do so, we simply type this command to start up our Puma web server.

$ rails server

Navigate your web browser to the address http://localhost:3000/ and you should see a default Rails welcome page. Before we make any further changes to our project, now is a good time to start tracking our changes in git. Git should have been pulled in when you created a new project so you can simply execute this command to add you files to track.

$ git add .

Now we will need to make some changes to the code to make it a bit easier to view and edit our information. Most of these changes are in the app directory. ~Here are the differences for the file changes which I have made~ (changes lost). Consider it an excercise to figure it out yourself ;)

Results

Take a look at each page. While your Puma server is still running, navigate to the page http://localhost:3000/courses and you should see a page allowing you to add Courses. Here we can create a new Course and assign it a string as a name. If we go to http://localhost:3000/sections We can create a section of a particular course and assign it a section number. Lastly, If we go to http://localhost:3000/professors we can now assign a professor to a particular section. All our basic functionality is there!

Conclusion

By following the Rails way of doing things we were able to get a small web application off the ground without (much) configuration. We can call this approach convention over configuration. There is obviously a lot more which we could add to this example, but this was just to show how to get up and running. In the future we will be adding more functionality to the site and other things like testing and error handling. I book I would recommend you check out for further reading is Agile Web Development with Rails 6 by Sam Ruby.

[Originally Posted 09/2017]

Sun, 26 May 2019 00:05:15 +0000

Git Tips

[linkstandalone] Untitled article

What exactly is Git

Long story short it’s simply a VCS. In plain english, Git is a valuable command line tool for working together in groups and keeping track of changes in a codebase. It is essentially a version control system. A program designed to help keep track of different versions of your codebase.

I went ahead and put together a list of some reference links for those who are just getting started with Git. There are plenty of people who are still new to Git, or are trying to move from a different VCS (e.g. TFS, SVN, Mercurial, etc.)

What Git does

Installing Git

Step-by-Step Walkthrough

Reference Cheat Sheet

GUI Git Clients


[Originally posted 08/2017] Git Tips Wed, 22 May 2019 01:16:32 +0000

Writing a Reddit/Twitter Bot pt. 1

[linkstandalone]

Intro

I have been curious about reddit and twitter bots for a while. If you aren’t sure what I mean, here’s a link to the xkcd transcriber bot just as an example.

People write all types of interesting bots, from Wikipedia fetchers, to user statistics, to comedic relief bots.I finally decided to dive in and give it a shot on my own. A bit of history; I began looking into this back in 2016 as a fun project. While I’m looking to change some things, This post will act as a sort of a retrospective on what I have learned and the different choices I made while building this. If you want to take a look at my code, take a look at the GitHub page here (old link was gone). To do what? I wanted to write a bot which acts as both an image mirror/archiver/aggregator.\nI wanted new posts to be sent out to twitter. Essentially I wanted to use multiple APIs and tie them together into something new.

Tools

I’m usually a C++ programmer, but I had worked with Ruby before and noticed that there were some gems on GitHub that allowed one to interact with both the Reddit, Twitter, and Imgur APIs. After attempting to put together a couple small prototypes and swearing at my computer. I gave up on using Ruby to write my bot. Why? At the time the libraries I was attempting to use were old, buggy, and ill maintained. There were officially supported libraries for all these services written in Python. While I had not ever written any code in Python, this seemed like it would be easier than dealing with terrible libraries. For anyone getting started, I would highly recommend using Python for your bot simply for the availability of high quality libraries. The ones I started off with were: PRAW, TwitterAPI, and Imgur

Update: It now appears that the Imgur python library is deprecated. I will need to find another way to access it.

Registering

For these services you need to register an application in order to get full access to their API. This is free. Some services will allow anonymous API access but typically the number of requests you are allowed to make is restricted. This part can be a little confusing so I will show you how to get registered. I will say right off the bat that it would be a good idea to set up a new email (or email alias) for all the services you will be signing up for.

Reddit

Go ahead and create a new account for your bot. Navigate to preferences -> apps. Choose that you want to create a new app. For the app type, I would recommend you choose script. You will be provided with an app key, and an app secret. You will need both of those for your app to talk to Reddit. Here is a link to the documentation

Twitter

You will need to provide a phone number to sign up for a Twitter account. I would recommend using a Google voice account. Go ahead and create your new twitter account and navigate in your browser to https://apps.twitter.com/. From there you can give your app a name, desciption and all that fun stuff. You will end up with a consumer key and secret, as well as an access token key and secret (4 different big long string). Take note of these as you will need them to go in your app. Read more from their documentation here

Imgur

Once again, create a new account for your bot (yawn, I think you’re getting the idea). Follow the API docs here to get up and running.

Conclusion

While there is a lot of setup to do before you get started writing your bot. You only have to do it once. While I recommended python, I’m sure there are plenty of other languages out there which would serve you well depending on the particular task you need to accomplish. Most APIs these days transport data in the JSON format. Back in ye olden days we had XML responses. I’d recommend reading up on it a little bit. It’s a pretty easy to read format. In the next section I will go over some of the specific code I used in my bot.

Sat, 11 May 2019 04:37:34 +0000

OCaml at Jane Street Capital

[linkstandalone] A great talk by Yaron Minsky, of Jane Street Capital. He disusses why they picked the obscure OCaml as their language of choice and what separates the language from others. While the talk is around an hour long, if you only watch one part, I recommend\nstarting at around 38 minutes in. Here he is talking about what makes the type system in OCaml so powerful, and compares it to its Java counterpart.

https://ocaml.org/ Sat, 11 May 2019 04:06:01 +0000