GhostiFi — The server.create() function

Overview

As promised in my initial blog post, I am continuing to publish the entire development process of GhostiFi, from concept, to sketch, to pseudocode, to actual code.

In this post (part 6), I will be sharing how I turned the server.create() pseudocode into actual Python:

8 “mini” functions

Rather than writing all the steps of server.create() into one function, I created 8 smaller, reusable chunks of code, and then ran them all within the server.create() function.

_create_vps() — Creates a Vultr VPS
_get_vps_status() 
— Gets Vultr VPS status for up to 500 seconds, waiting for it to finish installing before moving on
_create_cf_dns() 
— Creates an A record at CloudFlare DNS
_install_openvpn() 
— Installs OpenVPN from GhostiFi’s openvpn-install.sh public Github repo
_get_ovpn_file() 
— SFTPs into the VPS and copies the OVPN file output from the OpenVPN script to the GhostiFi webserver to be served from the user dashboard later
_create_vps_snapshot() 
— Creates a VPS snapshot on Vultr, will use this later for rebuilds
_get_root_password() 
— Gets the VPS root password and stores it for later, to be given to the user in the GhostiFi dashboard
_send_email(targets, msg_txt, subject) 
— Sends a notification email when the VPS VPN has finished setting up

Making the OpenVPN install script

One of the more challenging parts of this function was figuring out how to automate the OpenVPN installation. I was able to find a popular OpenVPN installation Github repo here: https://github.com/angristan/openvpn-install/

So I used that as a starting point.

The contributors had already optimized the security settings, so I left everything as they had recommended. I only modified the script to remove the need for user input, and change the default OpenVPN server port from 1194 to 443, in order to potentially help GhostiFi users get around some of the more restrictive firewalls out there.

When finished, I uploaded it to a GhostiFi public Github repo: https://github.com/GhostiFi/openvpn-install.sh/blob/master/openvpn-install.sh. New GhostiFi servers will pull the latest version from there when they are built.

Screenshots

Output of server.py/server.create() function building a new server
OVPN file copied from VPS back to GhostiFi webserver
Info stored back to the database after server is created
VPS VPN setup complete notification email

Server.py

At this point, after a user purchases a GhostiFi subscription, the VPS will be built, OpenVPN installed, and info stored back to the database.

There is no user dashboard/frontend yet, and no way to rebuild or destroy servers. Lot of work to be done still, but this is a big milestone!

Closing thoughts

I hope this was helpful for anyone who is an aspiring programmer or entrepreneur to see my thought process in how I went about turning a sketched design of a user interface for my product idea into a step-by-step process that any programmer might be able to understand and turn into code.

If you have any feedback on how I could improve this design please let me know in the comments section!

I am also looking for feedback on the concept itself, as well as beta testers. Please sign up for the newsletter at https://ghostifi.net if you are interested.

If you want to know when I release another post about building GhostiFi, you can follow me on Twitter: @_rchase_ or Medium: Reilly Chase