Skip to content

Commit

Permalink
Microbox names and namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
danhunsaker committed Apr 11, 2023
1 parent 3d3e23a commit 1e26019
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 75 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016 Nanobox
Copyright (c) 2015-2019 Nanobox, 2019-present The Microbox Team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
An implementation of the [Nanobox Custom Provider](https://docs.nanobox.io/providers/create/) spec for Amazon Web Services.
An implementation of the [Microbox Custom Provider](https://docs.microbox.cloud/providers/create/) spec for Amazon Web Services.

## Development

Expand Down
8 changes: 4 additions & 4 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if ENV['FORCE_HTTPS']
redirect request.url.sub('http', 'https') unless request.secure?
end

content_type 'application/json'
request.body.rewind
request_body = request.body.read
Expand All @@ -30,8 +30,8 @@

get '/' do
content_type 'text/html'
'AWS API Adapter for Nanobox. ' \
'source: https://github.com/nanobox-io/nanobox-adapter-aws'
'AWS API Adapter for Microbox. ' \
'source: https://github.com/mu-box/microbox-adapter-aws'
end

get '/meta' do
Expand Down Expand Up @@ -109,7 +109,7 @@ def client
key_id = request.env['HTTP_AUTH_ACCESS_KEY_ID']
access_key = request.env['HTTP_AUTH_SECRET_ACCESS_KEY']
region = request.env['HTTP_REGION_ID'] || ::EC2::DEFAULT_REGION

::EC2::Client.new(key_id, access_key, region)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ec2/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def generate
# provide this information via a nice json api: /instances.json
#
# The challenge here is that the data from this service is structured
# completely opposite to how the nanobox catalog needs to be structured.
# completely opposite to how the microbox catalog needs to be structured.
# So essentially we fetch the data, iterate over the data and construct
# the data set according to how we need it to be.
#
Expand Down
4 changes: 2 additions & 2 deletions lib/ec2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def server(id)
# Order a server to run on AWS
#
# attrs:
# name: Nanobox-generated name
# name: Microbox-generated name
# region: the region wherein to launch the server
# size: the size of server to provision
# ssh_key: id of the SSH key
Expand All @@ -74,7 +74,7 @@ def server_order(attrs)
# todo: make this round-robin between availability zones
az = compute.availability_zones.first

# find or create a nanobox security group
# find or create a microbox security group
security = ::EC2::Security.new(manager)
group = security.group

Expand Down
88 changes: 44 additions & 44 deletions lib/ec2/compute.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require 'right_aws_api'

class ::EC2::Compute

attr_reader :manager

def initialize(manager)
@manager = manager
end

def permission?
begin
manager.DescribeInstances('DryRun' => true)
Expand All @@ -19,22 +19,22 @@ def permission?
end
end
end

def instances
list = []
# filter the collection to just nanobox instances
filter = [{'Name' => 'tag:Nanobox', 'Value' => 'true'}]

# filter the collection to just microbox instances
filter = [{'Name' => 'tag:Microbox', 'Value' => 'true'}]

# query the api
res = manager.DescribeInstances('Filter' => filter)

# extract the instance collection
instances = res["DescribeInstancesResponse"]["reservationSet"]

# short-circuit if the collection is empty
return [] if instances.nil?

# instances might not be a collection, but a single item
collection = begin
if instances['item'].is_a? Array
Expand All @@ -43,41 +43,41 @@ def instances
[instances['item']]
end
end

# grab the instances and process them
collection.each do |instance|
list << process_instance(instance["instancesSet"]["item"])
end

list
end

def instance(id)
# query the api
res = manager.DescribeInstances('InstanceId' => id)

# extract the instance collection
instances = res["DescribeInstancesResponse"]["reservationSet"]

# short-circuit if the collection is empty
return nil if instances.nil?

# grab the fist instance and process it
process_instance(instances["item"]["instancesSet"]["item"])
end

# Run an on-demand compute instance
#
#
# attrs:
# name: nanobox-specific name of instance
# name: microbox-specific name of instance
# size: instance type/size (t2.micro etc)
# disk: size of disk for ebs volume
# ami: amazon machine image to use to launch instance
# availability_zone: availability zone to run instance on
# key: id of ssh key
# security_group: id of security group
def run_instance(attrs)

# launch the instance
res = manager.RunInstances(
'ImageId' => attrs[:ami],
Expand All @@ -96,66 +96,66 @@ def run_instance(attrs)
'VolumeSize' => attrs[:disk],
'DeleteOnTermination' => true}} ]
)

# extract the instance
instance = process_instance(res["RunInstancesResponse"]["instancesSet"]["item"])

# name the instance
set_instance_name(instance[:id], attrs[:name])

# now update the name and return it
instance.tap do |i|
i[:name] = attrs[:name]
end
end

def reboot_instance(id)
res = manager.RebootInstances( "InstanceId" => id )
end

def terminate_instance(id)
# todo: catch error?
res = manager.TerminateInstances( "InstanceId" => id )
res["TerminateInstancesResponse"]["instancesSet"]["item"]["currentState"]["name"]
end

def set_instance_name(id, name)
# set tags
res = manager.CreateTags(
'ResourceId' => id,
'Tag' => [
{
'Key' => 'Nanobox',
'Key' => 'Microbox',
'Value' => 'true'
},
{
'Key' => 'Name',
'Value' => name
},
{
'Key' => 'Nanobox-Name',
'Key' => 'Microbox-Name',
'Value' => name
}
]
)
end

def availability_zones
# Ultimately, we only want the availability_zones that are available to
# this account AND have a default subnet. Querying the subnets for
# this account AND have a default subnet. Querying the subnets for
# default-for-az finds this list pretty quickly
# filter the collection to just nanobox instances

# filter the collection to just microbox instances
filter = [{'Name' => 'default-for-az', 'Value' => 'true'}]

# query the api
res = manager.DescribeSubnets('Filter' => filter)

subnets = res['DescribeSubnetsResponse']['subnetSet']

# if we don't have any default subnets, let's return an empty set
return [] if subnets.nil?

# subnets might not be a collection, but a single item
collection = begin
if subnets['item'].is_a? Array
Expand All @@ -164,28 +164,28 @@ def availability_zones
[subnets['item']]
end
end

availability_zones = []

collection.each do |subnet|
availability_zones << subnet['availabilityZone']
end

availability_zones.uniq.sort
end

private

def process_instance(data)
{
id: data['instanceId'],
name: (process_tag(data['tagSet']['item'], 'Nanobox-Name') rescue 'unknown'),
name: (process_tag(data['tagSet']['item'], 'Microbox-Name') rescue 'unknown'),
status: translate_status(data['instanceState']['name']),
external_ip: data['ipAddress'],
internal_ip: data['privateIpAddress']
}
end

def process_tag(tags, key)
tags.each do |tag|
if tag['key'] == key
Expand All @@ -194,7 +194,7 @@ def process_tag(tags, key)
end
''
end

def translate_status(status)
case status
when 'running'
Expand All @@ -203,5 +203,5 @@ def translate_status(status)
status
end
end

end
2 changes: 1 addition & 1 deletion lib/ec2/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def to_json
{ key: :secret_access_key, label: 'Secret Access Key' }
],
instructions: instructions,
bootstrap_script: 'https://s3.amazonaws.com/tools.nanobox.io/bootstrap/ubuntu.sh',
bootstrap_script: 'https://s3.amazonaws.com/tools.microbox.cloud/bootstrap/ubuntu.sh',
ssh_key_method: 'reference'
}.to_json
end
Expand Down
Loading

0 comments on commit 1e26019

Please sign in to comment.