Class: Mist::LXCTemplate
- Inherits:
-
Object
- Object
- Mist::LXCTemplate
- Defined in:
- lib/mist/lxc_template.rb
Instance Method Summary collapse
- #clone(name) ⇒ Object
- #create(config) ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(distro, release) ⇒ LXCTemplate
constructor
A new instance of LXCTemplate.
Constructor Details
#initialize(distro, release) ⇒ LXCTemplate
Returns a new instance of LXCTemplate.
19 20 21 22 23 24 25 |
# File 'lib/mist/lxc_template.rb', line 19 def initialize(distro, release) @distro = distro @release = release @template_name = "template-#{distro}-#{release}" @template = LXC::Container.new(@template_name) end |
Instance Method Details
#clone(name) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/mist/lxc_template.rb', line 48 def clone(name) container = @template.clone(name, flags: LXC::LXC_CLONE_SNAPSHOT, bdev_type: 'overlayfs') container.wait(:stopped, 10) return container rescue StandardError => ex Mist.logger.error "Failed to clone template #{@template_name}: #{ex}" raise end |
#create(config) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mist/lxc_template.rb', line 31 def create(config) Mist.logger.info "Creating #{@template_name}..." # Create our own user with a known key & strong random password username = config.username pubkey = config.ssh_public_key password = SecureRandom.urlsafe_base64(32) @template.create(@distro, nil, {}, 0, ['--release', @release, '--user', username, '--auth-key', pubkey, '--password', password]) rescue StandardError => ex Mist.logger.error "Failed to create template #{@template_name}: #{ex}" raise end |
#exists? ⇒ Boolean
27 28 29 |
# File 'lib/mist/lxc_template.rb', line 27 def exists? @template.defined? end |