Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Enable local compute support #1

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source 'https://rubygems.org'

gem 'elecksee', :git => 'git://github.com/chrisroberts/elecksee', :branch => 'develop'

gemspec
2 changes: 2 additions & 0 deletions lib/miasma/contrib/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module ApiCommon
def self.included(klass)
klass.class_eval do
attribute :object_store_root, String
attribute :container_root, String, :default => '/var/lib/lxc'
end
end

Expand All @@ -23,5 +24,6 @@ def self.included(klass)
end
end

Models::Compute.autoload :Local, 'miasma/contrib/local/compute'
Models::Storage.autoload :Local, 'miasma/contrib/local/storage'
end
193 changes: 193 additions & 0 deletions lib/miasma/contrib/local/compute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
require 'miasma'
require 'miasma/contrib/local'
require 'elecksee'
require 'tempfile'

module Miasma
module Models
class Compute
class Local < Compute

include Contrib::LocalApiCore::ApiCommon

# Provide full list of server instances
#
# @return [Array<Miasma::Models::Compute::Server>]
def server_all
c_roots = Dir.glob(
File.join(container_root, '*')
).find_all do |g_path|
File.directory?(g_path)
end
c_roots.map do |c_path|
lxc = Lxc.new(
File.basename(c_path),
:base_path => File.dirname(c_path)
)
Server.new(
self,
server_info(lxc)
).valid_state
end
end

# Save/create the server
#
# @param server [Miasma::Models::Compute::Server]
# @return [Miasma::Models::Compute::Server]
def server_save(server)
if(server.persisted?)
raise "What do we do?"
else
if(server.metadata && server.metadata[:ephemeral])
elxc = Lxc::Ephemeral.new(
:original => server.image_id,
:daemon => true,
:new_name => server.name
)
elxc.create!
elxc.start!(:detach)
lxc = elxc.lxc
else
lxc = Lxc::Clone.new(
:original => server.image_id,
:new_name => server.name
).clone!
end
lxc.start
m_data = Smash.new(
:name => server.name,
:ephemeral => server.metadata[:ephemeral],
:image_id => server.image_id,
:flavor_id => server.flavor_id
)
m_data_path = lxc.path.join('miasma.json').to_s
if(File.writable?(File.dirname(m_data_path)))
File.open(m_data_path, 'w') do |file|
file.write MultiJson.dump(m_data)
end
else
t_file = Tempfile.new('miasma-local-compute')
t_file.write MultiJson.dump(m_data)
t_file.close
lxc.run_command(
"mv #{t_file.path} #{m_data_path}",
:sudo => true
)
end
server.load_data(
server_info(lxc)
).valid_state
end
end

# Reload the server data
#
# @param server [Miasma::Models::Compute::Server]
# @return [Miasma::Models::Compute::Server]
def server_reload(server)
if(server.persisted?)
lxc = Lxc.new(
File.basename(server.id),
:base_path => File.dirname(server.id)
)
server.load_data(
server_info(lxc)
).valid_state
server
else
server
end
end

# Destroy the server
#
# @param server [Miasma::Models::Compute::Server]
# @return [TrueClass, FalseClass]
def server_destroy(server)
if(server.persisted?)
lxc = Lxc.new(
File.basename(server.id),
:base_path => File.dirname(server.id)
)
if(lxc.exists?)
if(lxc.running?)
lxc.stop
end
if(lxc.exists?) # ephemeral self-clean
lxc.destroy
end
end
true
else
false
end
end

protected

# Generate model attributes from container
#
# @param lxc [Lxc]
# @return [Smash]
def server_info(lxc)
if(lxc.exists?)
meta_path = lxc.path.join('miasma.json').to_s
if(File.exists?(meta_path))
sys_info = MultiJson.load(File.read(meta_path)).to_smash
else
info_path = lxc.rootfs.join('etc/os-release').to_s
if(File.exists?(info_path))
sys_info = Smash[
File.read(info_path).split("\n").map do |line|
line.split('=', 2).map do |item|
item.gsub(/(^"|"$)/, '')
end
end
]
sys_info[:image_id] = [
sys_info.fetch('ID', 'unknown-system'),
sys_info.fetch('VERSION_ID', 'unknown-version')
].join('_').tr('.', '')
sys_info[:name] = lxc.name
else
sys_info = Smash.new
end
end

Smash.new(
:id => lxc.path.to_s,
:name => sys_info[:name],
:state => lxc.state,
:image_id => sys_info.fetch(:image_id, 'unknown'),
:flavor_id => sys_info.fetch(:flavor_id, 'unknown'),
:addresses_public => lxc.running? ? [
Server::Address.new(
:version => 4,
:address => lxc.container_ip
)
] : [],
:addresses_private => [],
:status => lxc.state.to_s,
:metadata => Smash.new(
:ephemeral => sys_info[:ephemeral]
)
)
else
Smash.new(
:id => lxc.path.to_s,
:name => lxc.name,
:image_id => 'unknown',
:flavor_id => 'unknown',
:state => :terminated,
:addresses_public => [],
:addresses_private => [],
:status => 'Destroyed'
)
end
end

end
end
end
end
1 change: 1 addition & 0 deletions miasma-local.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Gem::Specification.new do |s|
s.description = 'Smoggy local API'
s.license = 'Apache 2.0'
s.require_path = 'lib'
s.add_runtime_dependency 'elecksee'
s.add_development_dependency 'miasma', '>= 0.2.12'
s.add_development_dependency 'pry'
s.add_development_dependency 'minitest'
Expand Down