Skip to content

Commit

Permalink
Merge pull request #188 from alces-flight/feature/volume-devices
Browse files Browse the repository at this point in the history
Add representation of volume devices
  • Loading branch information
jamesremuscat authored Feb 29, 2024
2 parents 887565b + d80ada6 commit 4c088e3
Show file tree
Hide file tree
Showing 27 changed files with 480 additions and 47 deletions.
15 changes: 15 additions & 0 deletions app/models/device/volume_details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Device::VolumeDetails < Device::Details

validate :device_uses_volume_template

private

def device_uses_volume_template
reload_device
return unless device.present?
unless device.template.tag == 'volume'
self.errors.add(:device, 'must use the `volume` template if it has a Device::VolumeDetails')
end
end

end
34 changes: 32 additions & 2 deletions app/presenters/device/compute_details_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,38 @@ class Device::ComputeDetailsPresenter < Device::DetailsPresenter

delegate :public_ips, :private_ips, :login_user, :ssh_key, :volume_details, to: :o

def is_compute_device?
true
def additional_details
[].tap do |d|
if has_login_details?
d << [
'Access details:',
{
'Login user:': login_user || 'Unknown',
'Public IPs:': public_ips,
'Private IPs:': private_ips,
'SSH key:': ssh_key || 'Unknown'
}
]
end

if has_volume_details?
d << [
'Volume details:', volume_details
]
end

end
end


private

def has_login_details?
public_ips || private_ips || ssh_key || login_user
end

def has_volume_details?
!volume_details.empty?
end

end
4 changes: 2 additions & 2 deletions app/presenters/device/details_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Device::DetailsPresenter < Presenter

def is_compute_device?
false
def additional_details
[]
end

end
13 changes: 13 additions & 0 deletions app/presenters/device/network_details_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
class Device::NetworkDetailsPresenter < Device::DetailsPresenter

def additional_details
{
'Admin state up:': o.admin_state_up,
'DNS domain': o.dns_domain,
'L2 adjacency:': o.l2_adjacency,
'MTU:': o.mtu,
'Port security enabled:': o.port_security_enabled,
'Shared:': o.shared,
'QoS policy:': o.qos_policy
}
end

end
14 changes: 14 additions & 0 deletions app/presenters/device/volume_details_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Device::VolumeDetailsPresenter < Device::DetailsPresenter

def additional_details
{
'Availability zone:': o.availability_zone,
'Bootable:': o.bootable,
'Encrypted:': o.encrypted,
'Read-only:': o.read_only,
'Size (GB):': o.size,
'Volume type:': o.volume_type
}
end

end
18 changes: 1 addition & 17 deletions app/presenters/device_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DevicePresenter < Presenter
delegate :vcpus, :ram, :disk,
to: :template

delegate :is_compute_device?, to: :details
delegate :additional_details, to: :details

# location returns the location of the device. For devices in simple
# chassis, the chassis's location is returned. Devices in complex chassis,
Expand Down Expand Up @@ -56,22 +56,6 @@ def details
h.presenter_for(o.details)
end

def has_login_details?
is_compute_device? && (details.public_ips || details.private_ips || details.ssh_key || details.login_user)
end

def login_user
o.details.login_user.presence || h.content_tag(:em, 'Unknown')
end

def ssh_key
o.details.ssh_key.presence || h.content_tag(:em, 'Unknown')
end

def has_volume_details?
is_compute_device? && !o.details.volume_details.empty?
end

def has_metadata?
!metadata.empty?
end
Expand Down
6 changes: 3 additions & 3 deletions app/services/template_services/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def build_template(params)
rack_repeat_ratio: nil,

tag: params[:tag],
images: params[:images]
# Users can't upload images here, only use existing ones (though I
# suppose they could use fully-qualified URLs too)
images: params[:images] || {}
)

if template.images.empty?
# Users can't upload images here, only use existing ones (though I
# suppose they could use fully-qualified URLs too)
set_images_from_height(template)
end

Expand Down
4 changes: 4 additions & 0 deletions app/views/api/v1/devices/details/volume_details.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object @details

attributes :availability_zone, :bootable, :encrypted, :read_only, :size,
:volume_type
17 changes: 2 additions & 15 deletions app/views/devices/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,8 @@
dl.item "Status:", device.status
dl.item "Cost this billing period:", device.currency_cost

if device.has_login_details?
dl.sublist "Access details:" do |sl|
sl.item "Login user:", device.login_user
sl.item "Public IPs:", device.public_ips
sl.item "Private IPs:", device.private_ips
sl.item "SSH Key:", device.ssh_key
end
end

if device.has_volume_details?
dl.sublist "Volume details:" do |sl|
device.volume_details.each do |key, val|
sl.item key, val
end
end
device.additional_details.each do |label, details_list|
dl.recurse_items label, details_list
end

dl.sublist "Template:" do |sl|
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20240216141238_seed_network_device_template.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
class SeedNetworkDeviceTemplate < ActiveRecord::Migration[7.1]

class Template < ApplicationRecord
enum rackable: { rackable: 1, zerouable: 2, nonrackable: 3 }
end

def change
Template.reset_column_information
reversible do |dir|

dir.up do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class MigrateDefaultRackTemplateToTag < ActiveRecord::Migration[7.1]
class Template < ApplicationRecord; end

def change
Template.reset_column_information
reversible do |dir|
dir.up do
# Intentionally not using Template.default_rack_template here!
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20240220115416_create_device_volume_details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateDeviceVolumeDetails < ActiveRecord::Migration[7.1]
def change
create_table :device_volume_details, id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string :availability_zone
t.boolean :bootable
t.boolean :encrypted
t.boolean :read_only
t.integer :size
t.string :volume_type

t.timestamps
end
end
end
38 changes: 38 additions & 0 deletions db/migrate/20240220150125_seed_volume_device_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class SeedVolumeDeviceTemplate < ActiveRecord::Migration[7.1]

class Template < ApplicationRecord
enum rackable: { rackable: 1, zerouable: 2, nonrackable: 3 }
end

def change
Template.reset_column_information
reversible do |dir|

dir.up do
t = Template.new(
name: 'volume',
template_type: 'Device',
tag: 'volume',
version: 1,
height: 2,
depth: 2,
rows: 1,
columns: 1,
rackable: 'rackable',
simple: true,
description: 'Volume',
images: {
'front' => 'disk_front_2u.png',
'rear' => 'generic_rear_2u.png',
}
)
t.save!
end

dir.down do
Template.find_by_tag('volume')&.destroy!
end

end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions docs/api/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ facing are `f` or `b` for front and back respectively.
./create-device.sh <NAME> <RACK_ID> <FACING> <START_U> <TEMPLATE_ID>
```

To create network and volume devices use the following scripts. They are
similar to the `create-device.sh` script except the template id is determined
automatically.

```
./create-network.sh <NAME> <RACK_ID> <FACING> <START_U>
./create-volume.sh <NAME> <RACK_ID> <FACING> <START_U>
```

Move a device.

```
Expand Down
16 changes: 12 additions & 4 deletions docs/api/examples/create-and-populate-rack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ echo "Created empty rack ${RACK_NAME}" >&2
"${SCRIPT_DIR}/show-rack.sh" "${RACK_ID}"
echo


OUTPUT=$("${SCRIPT_DIR}/create-volume.sh" vol-1 "${RACK_ID}" f 1)
if [ $? -ne 0 ] ; then
# Errors will have been sent to stderr.
exit
fi
echo "Created volume device"

# Create a badly named and located device in that empty rack.
LARGEST_TEMPLATE=$( "${SCRIPT_DIR}/list-templates.sh" | jq "sort_by(.height) | reverse | .[0]" )
LARGEST_TEMPLATE=$( "${SCRIPT_DIR}/list-templates.sh" | jq "map(select(.tag == null)) | sort_by(.height) | reverse | .[0]" )
TEMPLATE_ID=$(echo "${LARGEST_TEMPLATE}" | jq -r .id)
TEMPLATE_HEIGHT=$(echo "${LARGEST_TEMPLATE}" | jq -r .height)
START_U=$(( ${RACK_HEIGHT} - ${TEMPLATE_HEIGHT} + 1 ))
Expand All @@ -52,7 +60,7 @@ echo "Renamed device" >&2
echo

# Correct the location of the device.
OUTPUT=$("${SCRIPT_DIR}/move-device.sh" "${DEVICE_ID}" "${RACK_ID}" f 1)
OUTPUT=$("${SCRIPT_DIR}/move-device.sh" "${DEVICE_ID}" "${RACK_ID}" f 3)
if [ $? -ne 0 ] ; then
# Errors will have been sent to stderr.
exit
Expand All @@ -69,8 +77,8 @@ fi
echo "Created network device"

# Leave some space at the top to allow dragging devices around the IRV.
START_U=$(( ${TEMPLATE_HEIGHT} + 1 ))
END_U=$(( $RACK_HEIGHT - 4 ))
START_U=$(( ${TEMPLATE_HEIGHT} + 3 ))
END_U=$(( $RACK_HEIGHT - 6 ))
"${SCRIPT_DIR}/populate-rack.sh" ${RACK_ID} ${START_U} ${END_U} comp102

"${SCRIPT_DIR}/show-rack.sh" "${RACK_ID}"
Expand Down
2 changes: 1 addition & 1 deletion docs/api/examples/create-network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RACK_ID=${2}
FACING=${3}
START_U=${4}

NETWORK_TEMPLATE_ID=$( "${SCRIPT_DIR}/list-templates.sh" | jq -r "sort_by(.height) | (.[] | select(.tag | . and contains(\"network\"))) | .id" )
NETWORK_TEMPLATE_ID=$( "${SCRIPT_DIR}/list-templates.sh" | jq -r '.[] | select(.tag == "network") | .id' )

if [ -z "${NETWORK_TEMPLATE_ID}" ]; then
echo "Couldn't find a template with tag='network'"
Expand Down
Loading

0 comments on commit 4c088e3

Please sign in to comment.