Skip to content

Commit

Permalink
[frontend] Add an optional vpc subnet field to cloud upload page
Browse files Browse the repository at this point in the history
Provides an optional subnet field to that allows to provide a distinct
subnet to be used for the upload to AWS.
If no vpc subnet id was provided, the upload script generates a
temporary one.
  • Loading branch information
bgeuken committed Feb 20, 2018
1 parent dfc99dd commit f538cce
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 9 deletions.
12 changes: 9 additions & 3 deletions dist/clouduploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@
FileUtils.ln_s(image_path, File.join(Dir.pwd, filename))

def get_ec2_credentials(data)
# Credentials are stored in ~/.aws/credentials
out, err, status = Open3.capture3(
command = [
'aws',
'sts',
'assume-role',
"--role-arn=#{data['arn']}",
"--external-id=#{data['external_id']}",
'--role-session-name=obs',
"--duration-seconds=#{THIRTY_MINUTES}"
)
]

if data['vpc_subnet_id']
command << "--vpc-subnet-id=#{data['vpc_subnet_id']}"
end

# Credentials are stored in ~/.aws/credentials
out, err, status = Open3.capture3(command)

if status.success?
STDOUT.write("Successfully authenticated.\n")
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/controllers/webui/cloud/upload_jobs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index
end

def new
xml_object = OpenStruct.new(params.slice(:project, :package, :repository, :arch, :filename))
xml_object = OpenStruct.new(params.slice(:project, :package, :repository, :arch, :filename, :vpc_subnet_id))
@upload_job = ::Cloud::Backend::UploadJob.new(xml_object: xml_object)
@ec2_regions = ::Cloud::Ec2::Configuration::REGIONS
end
Expand Down Expand Up @@ -59,7 +59,7 @@ def validate_configuration_presence

def permitted_params
params.require(:cloud_backend_upload_job).permit(
:project, :package, :repository, :arch, :filename, :region, :ami_name, :target
:project, :package, :repository, :arch, :filename, :region, :ami_name, :target, :vpc_subnet_id
)
end

Expand Down
1 change: 1 addition & 0 deletions src/api/app/models/cloud/backend/upload_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class UploadJob
:repository,
:arch,
:filename,
:vpc_subnet_id,
:size,
:backend_response
alias_method :id, :name
Expand Down
5 changes: 3 additions & 2 deletions src/api/app/models/cloud/upload_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ class UploadJob
include ActiveModel::Model
extend Forwardable

attr_accessor :user_upload_job, :backend_upload_job, :target_params, :filename, :arch, :target, :user
attr_accessor :user_upload_job, :backend_upload_job, :target_params, :filename, :arch, :target, :user, :vpc_subnet_id
validate :validate_dependencies
validates :user, presence: true
validates :filename, presence: true, format: {
with: /\A.+(.raw.xz|.vhdfixed.xz)\z/, message: "'%{value}' is not a valid cloud image (needs to be a raw.xz or vhdfixed.xz file)"
}
validates :arch, inclusion: { in: ['x86_64'], message: "'%{value}' is not a valid cloud architecture" }
validates :target, inclusion: { in: ['ec2'] }
validates :vpc_subnet_id, format: { with: /\Avpc-[-\w]+\z/, message: 'not a valid format', allow_blank: true }

def_delegator :backend_upload_job, :id

def self.create(params)
upload_job = new(params.slice(:filename, :arch, :user, :target))
upload_job = new(params.slice(:filename, :arch, :user, :target, :vpc_subnet_id))
return upload_job if upload_job.invalid?

upload_job.target_params = upload_job.target_validator_class.build(params)
Expand Down
3 changes: 3 additions & 0 deletions src/api/app/views/webui/cloud/upload_jobs/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
%p
= upload_job_form.label 'cloud_backend_upload_job[region]', "Region:"
= select_tag('cloud_backend_upload_job[region]', options_for_select(@ec2_regions))
%p
= upload_job_form.label 'cloud_backend_upload_job[vpc_subnet_id]', 'VPC Subnet ID (optional):'
= upload_job_form.text_field(:vpc_subnet_id, size: 35, name: 'cloud_backend_upload_job[vpc_subnet_id]')
%p
= upload_job_form.hidden_field :project
= upload_job_form.hidden_field :package
Expand Down
4 changes: 2 additions & 2 deletions src/api/lib/backend/api/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module Cloud
# Triggers a cloud upload job
# @return [String]
def self.upload(params)
data = params.slice(:region, :ami_name)
data = params.slice(:region, :ami_name, :vpc_subnet_id)
user = params[:user]
params = params.except(:region, :ami_name).merge(user: user.login, target: params[:target])
data = user.ec2_configuration.attributes.except('id', 'created_at', 'updated_at').merge(data).to_json
data = user.ec2_configuration.upload_parameters.merge(data).to_json
http_post('/cloudupload', params: params, data: data)
end

Expand Down
2 changes: 2 additions & 0 deletions src/api/spec/models/cloud/upload_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
it { is_expected.not_to allow_value('foo.rpm').for(:filename) }
it { is_expected.not_to allow_value('foo.vhdfixed').for(:filename) }
it { is_expected.not_to allow_value('foo.raw').for(:filename) }
it { is_expected.to allow_value('vpc-23sdfg54').for(:vpc_subnet_id) }
it { is_expected.not_to allow_value('vpc-2$sdfg54').for(:vpc_subnet_id) }
end

describe '.create' do
Expand Down

0 comments on commit f538cce

Please sign in to comment.