|
| 1 | +--- |
| 2 | +title: Ruby on Rails Baseline Testing on Google Axion C4A Arm Virtual Machine |
| 3 | +weight: 5 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## Baseline Setup for Ruby on Rails with PostgreSQL |
| 10 | +This section covers the installation and configuration of **PostgreSQL** and a **Rails application** on a SUSE Arm-based GCP VM. It includes setting up PostgreSQL, creating a Rails app, configuring the database, and starting the Rails server. |
| 11 | + |
| 12 | +### Install and Configure PostgreSQL |
| 13 | +PostgreSQL is used with Ruby on Rails as a robust, production-ready relational database that reliably stores and manages application data. |
| 14 | + |
| 15 | +```console |
| 16 | +sudo zypper install postgresql-devel postgresql-server |
| 17 | +sudo systemctl start postgresql |
| 18 | +sudo systemctl enable postgresql |
| 19 | +``` |
| 20 | +- `postgresql-devel` is required to compile the pg gem for Rails. |
| 21 | + |
| 22 | +Verify that the PostgreSQL service is active and running: |
| 23 | + |
| 24 | +```console |
| 25 | +systemctl status postgresql |
| 26 | +``` |
| 27 | + |
| 28 | +This command creates a new PostgreSQL role (user) named `gcpuser` with **superuser privileges**. |
| 29 | + |
| 30 | +```console |
| 31 | +sudo -u postgres createuser --superuser gcpuser |
| 32 | +``` |
| 33 | +- `sudo -u postgres` → Runs the command as the `postgres` user (default PostgreSQL superuser). |
| 34 | +- `createuser --superuser gcpuser` → Creates a PostgreSQL role named `gcpuser` with full admin privileges. |
| 35 | + - Can create databases |
| 36 | + - Can create other roles/users |
| 37 | + - Can grant privileges |
| 38 | + |
| 39 | +This role will be used by Rails to connect to the PostgreSQL database. |
| 40 | + |
| 41 | +### Create a Rails App with PostgreSQL |
| 42 | +Creates a new Rails application configured to use PostgreSQL as its database. |
| 43 | + |
| 44 | +```console |
| 45 | +rails new db_test_rubyapp -d postgresql |
| 46 | +cd db_test_rubyapp |
| 47 | +bundle install |
| 48 | +``` |
| 49 | +- Creates a new Rails application called `db_test_app`. |
| 50 | +- `d postgresql` → Tells Rails to use PostgreSQL as the database instead of the default SQLite. |
| 51 | +- `bundle install` ensures all required gems are installed. |
| 52 | + |
| 53 | +{{% notice Note %}} |
| 54 | +Check `config/database.yml` to ensure the `username` and `password` match your PostgreSQL role `(gcpuser)`. |
| 55 | +{{% /notice %}} |
| 56 | + |
| 57 | +### Verify and Update Database Configuration |
| 58 | +Open the Rails database configuration file: |
| 59 | + |
| 60 | +```console |
| 61 | +nano config/database.yml |
| 62 | +``` |
| 63 | +Find the `default`: and `development`: sections. |
| 64 | +Ensure the username matches the PostgreSQL user you created (gcpuser): |
| 65 | + |
| 66 | +You should see output similar to: |
| 67 | +```output |
| 68 | +default: &default |
| 69 | + adapter: postgresql |
| 70 | + encoding: unicode |
| 71 | + username: gcpuser |
| 72 | + password: |
| 73 | + host: localhost |
| 74 | + pool: 5 |
| 75 | +
|
| 76 | +development: |
| 77 | + <<: *default |
| 78 | +``` |
| 79 | +### Create and Initialize the Database |
| 80 | +Initializes and creates the development and test databases for your Rails app using PostgreSQL. |
| 81 | + |
| 82 | +```console |
| 83 | +rails db:create |
| 84 | +``` |
| 85 | +You should see output similar to: |
| 86 | +```output |
| 87 | +Created database 'db_test_rubyapp_development' |
| 88 | +Created database 'db_test_rubyapp_test' |
| 89 | +``` |
| 90 | +This means Rails successfully connected to PostgreSQL and created both dev and test databases. |
| 91 | + |
| 92 | +### Generate a Scaffold for Testing |
| 93 | +A database and Scaffold are required to create the actual PostgreSQL database for your Rails app and quickly generate the model, controller, views, and migrations for your data. |
| 94 | +Let’s create a small test model and table — for example, a simple Task tracker: |
| 95 | + |
| 96 | +```console |
| 97 | +rails generate scaffold task title:string due_date:date |
| 98 | +``` |
| 99 | +This command automatically generates: |
| 100 | +- Database migration for the tasks table |
| 101 | +- A model (task.rb) |
| 102 | +- A controller and views for CRUD operations |
| 103 | +- **Scaffold** → Automatically generates boilerplate code for CRUD operations, saving time and ensuring your app has working forms and routes. |
| 104 | + |
| 105 | +Then apply the migration: |
| 106 | + |
| 107 | +```console |
| 108 | +rails db:migrate |
| 109 | +``` |
| 110 | + |
| 111 | +You should see output similar to: |
| 112 | +```output |
| 113 | +== 20251006101717 CreateTasks: migrating ====================================== |
| 114 | +-- create_table(:tasks) |
| 115 | + -> 0.0127s |
| 116 | +== 20251006101717 CreateTasks: migrated (0.0128s) ============================= |
| 117 | +``` |
| 118 | + |
| 119 | +Database schema successfully updated. |
| 120 | + |
| 121 | +### Verify Table and Database Connectivity |
| 122 | +The previous command `rails generate scaffold task title:string due_date:date` created a `tasks` table in your PostgreSQL database. |
| 123 | + |
| 124 | +Now, verify that the table exists and has the correct structure following the steps below: |
| 125 | + |
| 126 | +```console |
| 127 | +sudo -u postgres psql |
| 128 | +\c db_test_rubyapp_development |
| 129 | +\d tasks |
| 130 | +``` |
| 131 | +- `sudo -u postgres psql` → Launches the PostgreSQL shell as the superuser `postgres`. |
| 132 | +- `\c db_test_rubyapp_development` → Connects to the Rails app’s development database. |
| 133 | +- `\d tasks` → Displays the schema (columns and types) of the `tasks` table. |
| 134 | + |
| 135 | +You should see output similar to: |
| 136 | +```output |
| 137 | +psql (15.10) |
| 138 | +Type "help" for help. |
| 139 | +
|
| 140 | +postgres=# \c db_test_rubyapp_development |
| 141 | +You are now connected to database "db_test_rubyapp_development" as user "postgres". |
| 142 | +db_test_rubyapp_development=# \d tasks |
| 143 | + Table "public.tasks" |
| 144 | + Column | Type | Collation | Nullable | Default |
| 145 | +------------+--------------------------------+-----------+----------+----------------------------------- |
| 146 | + id | bigint | | not null | nextval('tasks_id_seq'::regclass) |
| 147 | + title | character varying | | | |
| 148 | + due_date | date | | | |
| 149 | + created_at | timestamp(6) without time zone | | not null | |
| 150 | + updated_at | timestamp(6) without time zone | | not null | |
| 151 | +Indexes: |
| 152 | + "tasks_pkey" PRIMARY KEY, btree (id) |
| 153 | +``` |
| 154 | + |
| 155 | +### Run Rails Server |
| 156 | +Before proceeding to run the Rails server, you need to allow port 3000 from your GCP console. Below are the steps to do that: |
| 157 | + |
| 158 | +a. On the GCP console, navigate to **Firewall** -> **Create Firewall Rule** |
| 159 | + |
| 160 | +  |
| 161 | + |
| 162 | +b. Fill in the details as below: |
| 163 | + |
| 164 | +Give a **name** for your desired port (e.g., `allow-3000-ingress`). |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +Set **Direction of Traffic** to **"Ingress"**. |
| 170 | + |
| 171 | +Set **Target** to **"All Instances in the Network"**. You can also choose **"Specific Tags"**. |
| 172 | + |
| 173 | +Set the **Source IPv4 range** to **"0.0.0.0/0"**, for global access. |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | +In the **"Protocols and Ports"**, click on **"TCP"**, and mention the port number **"3000"**. |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +Click on **"Create"**. The Firewall rule will be created successfully and can be viewed in the Firewall Policies Page: |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | +Once done, go back to the VM, and execute the below commands to allow port 3000: |
| 187 | + |
| 188 | +```console |
| 189 | +sudo firewall-cmd --permanent --add-port=3000/tcp |
| 190 | +sudo firewall-cmd --reload |
| 191 | +``` |
| 192 | +Now that port 3000 is allowed in your VM’s ingress firewall rules, you can start the Rails server using the following command: |
| 193 | + |
| 194 | +```console |
| 195 | +rails server -b 0.0.0.0 |
| 196 | +``` |
| 197 | +- `rails server -b 0.0.0.0` → Starts the Rails server and binds it to all network interfaces, not just `localhost`. |
| 198 | +- Binding to `0.0.0.0` allows other machines (or your local browser) to access the Rails app running on the VM using its external IP. |
| 199 | + |
| 200 | + |
| 201 | +### Access the Application: |
| 202 | +Open a web browser on your local machine (Chrome, Firefox, Edge, etc.) and enter the following URL in the address bar: |
| 203 | + |
| 204 | +```console |
| 205 | +http://[YOUR_VM_EXTERNAL_IP]:3000 |
| 206 | +``` |
| 207 | +- Replace `<YOUR_VM_PUBLIC_IP>` with the public IP of your GCP VM. |
| 208 | + |
| 209 | +You will see a Rails welcome page in your browser if everything is set up correctly. It looks like this: |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +This verifies the basic functionality of the Ruby/Rails installation before proceeding to the benchmarking. |
0 commit comments