Skip to content

Commit

Permalink
feat(ruby): run post-installation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Oct 15, 2024
1 parent 411eb9e commit 802e8d2
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
28 changes: 28 additions & 0 deletions service/lib/agama/http.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

# Copyright (c) [2024] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

module Agama
# Namespace for HTTP-related code
module HTTP
end
end

require "agama/http/clients"
30 changes: 30 additions & 0 deletions service/lib/agama/http/clients.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# Copyright (c) [2024] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

module Agama
module HTTP
# Namespace for HTTP clients
module Clients
end
end
end

require "agama/http/clients/scripts"
58 changes: 58 additions & 0 deletions service/lib/agama/http/clients/scripts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

# Copyright (c) [2024] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "uri"
require "net/http"

module Agama
module HTTP
module Clients
# HTTP client to interact with the scripts API.
class Scripts
def initialize
@base_url = "http://localhost/api"
end

# Runs the scripts
def run
Net::HTTP.post(uri("/api/scripts/run"), "", headers)
end

private

def uri(path)
URI.join(@base_url, path)
end

def headers
@headers = {
Accept: "application/json",
Authorization: "Bearer #{auth_token}"
}
end

def auth_token
File.read("/run/agama/token")
end
end
end
end
end
13 changes: 13 additions & 0 deletions service/lib/agama/storage/finisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def possible_steps
IguanaStep.new(logger),
SnapshotsStep.new(logger),
CopyLogsStep.new(logger),
PostScripts.new(logger),
UnmountStep.new(logger)
]
end
Expand Down Expand Up @@ -217,6 +218,18 @@ def run
end
end

class PostScripts < Step
def label
"Running user-defined scripts"
end

def run
require "agama/http"
client = Agama::HTTP::Clients::Scripts.new
client.run
end
end

# Step to unmount the target file-systems
class UnmountStep < Step
def label
Expand Down
43 changes: 43 additions & 0 deletions service/test/agama/http/clients/scripts_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# Copyright (c) [2024] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../../test_helper"
require "agama/http/clients/scripts"

describe Agama::HTTP::Clients::Scripts do
subject(:scripts) { described_class.new }

before do
allow(File).to receive(:read).with("/run/agama/token")
.and_return("123456")
end

describe "#run" do
it "calls the end-point to run the scripts" do
url = URI("http://localhost/api/scripts/run")
expect(Net::HTTP).to receive(:post).with(url, "", {
Accept: "application/json",
Authorization: "Bearer 123456"
})
scripts.run
end
end
end

0 comments on commit 802e8d2

Please sign in to comment.