Skip to content

Commit

Permalink
Load CloudInit API data from the database
Browse files Browse the repository at this point in the history
  • Loading branch information
joakimk committed Dec 4, 2024
1 parent 27152af commit 2c76962
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 68 deletions.
52 changes: 2 additions & 50 deletions app/controllers/api/cloud_inits_controller.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,6 @@
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", "jq" ],
package_update: true,
package_upgrade: true,
write_files: [
{
path: "/etc/environment",
content: "RUNNER_CFG_PAT=#{App.github_actions_runner_cfg_pat}",
append: true,
},
],
runcmd: [
"systemctl stop sshd",
"systemctl disable sshd",
"curl https://maintenance.auctionet.dev/running; true",

# The script relies on running sudo but we've disabled root which means it will bring up a prompt to set a password. Running su will run it as the default user that can sudo and also reload the /etc/environment variables.
# -f replaces any existing runner with the same name so that you can reinstall a runner and have it replace the old one.
"su username -c 'cd; curl -s https://raw.githubusercontent.com/actions/runner/main/scripts/create-latest-svc.sh | bash -s -- -f -s #{App.github_actions_runner_scope}'",

# git clone https://github.com/vbem/multi-runners
# MR_GITHUB_PAT=... in .env
# ./mr.bash add --org #{App.github_actions_runner_scope} --count 3

"curl https://maintenance.auctionet.dev/it-ran; true",
"reboot",
],
}

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

render plain: yaml, content_type: "text/cloud-config"
data = CloudInit.find_by!(name: params[:name]).data
render plain: data, content_type: "text/cloud-config"

This comment has been minimized.

Copy link
@p-wall

p-wall Dec 4, 2024

Member

i’m thinking this should do envsubs things, so that tokens and whatnot are still envs

This comment has been minimized.

Copy link
@joakimk

joakimk Dec 5, 2024

Author Member

Okay, will do that

end
end
16 changes: 0 additions & 16 deletions config/initializers/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@ def self.api_token
end
end

def self.github_actions_runner_cfg_pat
if Rails.env.test? || Rails.env.development?
"test-runner-cfg-pat"
else
ENV.fetch("CLOUD_INIT_GITHUB_ACTIONS_RUNNER_CFG_PAT")
end
end

def self.github_actions_runner_scope
if Rails.env.test? || Rails.env.development?
"test-runner-scope"
else
ENV.fetch("CLOUD_INIT_GITHUB_ACTIONS_RUNNER_SCOPE")
end
end

def self.revisions_to_keep
(ENV["REVISIONS_TO_KEEP"] || 500).to_i
end
Expand Down
16 changes: 14 additions & 2 deletions spec/requests/api/cloud_inits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@

RSpec.describe "GET /api/cloud_init", type: :request do
it "gets a cloud-init config if you have the right api token" do
cloud_init = CloudInit.create!(name: "foo", data: "#cloud-config...")
allow(App).to receive(:api_token).and_return("secret")

get "/api/cloud_init?token=secret"
get "/api/cloud_init?token=secret&name=foo"

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

it "fails when the api token is wrong" do
cloud_init = CloudInit.create!(name: "foo", data: "#cloud-config...")
allow(App).to receive(:api_token).and_return("secret")

get "/api/cloud_init?token=wrong"
get "/api/cloud_init?token=wrong&name=foo"

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

it "fails when the name is unknown" do
cloud_init = CloudInit.create!(name: "foo", data: "#cloud-config...")
allow(App).to receive(:api_token).and_return("secret")

get "/api/cloud_init?token=wrong&name=bar"

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

0 comments on commit 2c76962

Please sign in to comment.