Skip to content

Commit

Permalink
Move cloud init into the API
Browse files Browse the repository at this point in the history
Co-authored-by: Peter Wall <[email protected]>
  • Loading branch information
joakimk and p-wall committed Dec 2, 2024
1 parent a92a6f5 commit eb567fa
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
43 changes: 43 additions & 0 deletions app/controllers/api/cloud_inits_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Api::CloudInitsController < ApiController
# This bootstraps github actions runners.
def show
data =
{
users: [
{
name: "username",
plain_text_passwd: "password",
lock_passwd: false,
chpasswd: { expire: false },
sudo: "ALL=(ALL) NOPASSWD:ALL",
shell: "/bin/bash"
}
],
disable_root: true,
ssh_pwauth: false,
ssh_deletekeys: true,
packages: [ "curl" ],
package_update: true,
package_upgrade: true,
write_files: [
{
path: "/etc/motd",
content: "Hello there."
}
],
runcmd: [
"systemctl stop sshd",
"systemctl disable sshd",
"curl https://maintenance.auctionet.dev/it-ran",
"reboot"
]
}

yaml = "#cloud-config\n" +
data
.deep_stringify_keys
.to_yaml.sub("---", "")

render plain: yaml, content_type: "text/cloud-config"
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace :api do
resource :build_status, only: :create
resource :github_actions_webhook, only: :create
resource :cloud_init, only: :show

delete "projects/:name" => "projects#destroy"
resource :build, only: [] do
Expand Down
28 changes: 0 additions & 28 deletions public/cloud-init.yaml

This file was deleted.

21 changes: 21 additions & 0 deletions spec/requests/api/cloud_inits_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "spec_helper"

RSpec.describe "GET /api/cloud_init", type: :request do
it "gets a cloud init config if you have the right api token" do
allow(App).to receive(:api_token).and_return("secret")

get "/api/cloud_init?token=secret"

expect(response).to be_successful
expect(response.body).to include("#cloud-config")
end

it "fails when the api token is wrong" do
allow(App).to receive(:api_token).and_return("secret")

get "/api/cloud_init?token=wrong"

expect(response).not_to be_successful
expect(response.body).not_to include("#cloud-config")
end
end

0 comments on commit eb567fa

Please sign in to comment.