Sha256: bfe9f48fbff5d53ef26cd119478efa98d9af409fe82adcfe6d5bb4308de3095d

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

class TestLab
  class Container

    module IO

      # Export the container
      #
      # @return [Boolean] True if successful.
      def export(compression=9)
        @ui.logger.debug { "Container Export: #{self.id} " }

        (self.lxc.state == :not_created) and return false

        self.down

        sc_file = File.join("/", "tmp", "#{self.id}.sc")
        local_file = File.join(Dir.pwd, File.basename(sc_file))

        please_wait(:ui => @ui, :message => format_object_action(self, 'Compress', :cyan)) do
          self.node.ssh.bootstrap(<<-EOF)
set -x
set -e
find #{self.lxc.container_root} -print0 -depth | cpio -o0 | pbzip2 -#{compression} -vfcz > #{sc_file}
chown ${SUDO_USER}:${SUDO_USER} #{sc_file}
EOF
        end

        please_wait(:ui => @ui, :message => format_object_action(self, 'Export', :cyan)) do
          File.exists?(local_file) and FileUtils.rm_f(local_file)
          self.node.ssh.download(sc_file, local_file)
        end

        puts("Your shipping container is now exported and available at '#{local_file}'!")

        true
      end

      # Import the container
      #
      # @return [Boolean] True if successful.
      def import(local_file)
        @ui.logger.debug { "Container Import: #{self.id} " }

        self.down
        self.destroy

        sc_file = File.join("/", "tmp", "#{self.id}.sc")

        please_wait(:ui => @ui, :message => format_object_action(self, 'Import', :cyan)) do
          self.node.ssh.exec(%(sudo rm -fv #{sc_file}), :silence => true, :ignore_exit_status => true)
          self.node.ssh.upload(local_file, sc_file)
        end

        please_wait(:ui => @ui, :message => format_object_action(self, 'Expand', :cyan)) do
          self.node.ssh.bootstrap(<<-EOF)
set -x
set -e
pbzip2 -vdc #{sc_file} | cpio -uid && rm -fv #{sc_file}
EOF
        end

        puts("Your shipping container is now imported and available for use!")

        true
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
testlab-0.6.7 lib/testlab/container/io.rb