Class: BoxGrinder::TarballPlugin

Inherits:
BasePlugin
  • Object
show all
Includes:
Archive::Tar, FileUtils
Defined in:
lib/dw-boxgrinder-tarball-platform-plugin/tarball_plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appliance_configObject

Returns the value of attribute appliance_config.



29
30
31
# File 'lib/dw-boxgrinder-tarball-platform-plugin/tarball_plugin.rb', line 29

def appliance_config
  @appliance_config
end

#dirObject

Returns the value of attribute dir.



29
30
31
# File 'lib/dw-boxgrinder-tarball-platform-plugin/tarball_plugin.rb', line 29

def dir
  @dir
end

#logObject

Returns the value of attribute log.



29
30
31
# File 'lib/dw-boxgrinder-tarball-platform-plugin/tarball_plugin.rb', line 29

def log
  @log
end

#plugin_infoObject

Returns the value of attribute plugin_info.



29
30
31
# File 'lib/dw-boxgrinder-tarball-platform-plugin/tarball_plugin.rb', line 29

def plugin_info
  @plugin_info
end

Instance Method Details

#after_initObject



31
32
33
34
35
36
37
38
# File 'lib/dw-boxgrinder-tarball-platform-plugin/tarball_plugin.rb', line 31

def after_init
  @tar_name = "#{@appliance_config.name}.tgz"
  register_deliverable(
    :tarball => @tar_name
  )
  register_supported_os('centos', ["4", "5", "6"])
  register_supported_os('fedora', ["15", "16"])
end

#create_script(script_name, options = {}, &line_processor) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dw-boxgrinder-tarball-platform-plugin/tarball_plugin.rb', line 110

def create_script(script_name, options = {}, &line_processor)
  script = (options[:prefix] || "")
  for line in options[:lines]
    processed_line = line_processor.call line
    script << processed_line
  end
  script << (options[:suffix] || "")
  File.open(tmp_path(script_name), 'w') {|script_file|
    script_file.puts(script)
  }
  # record the names of all generated scripts, so we can add them to the tarball
  @created_scripts ||= []
  @created_scripts << script_name
end

#executeObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dw-boxgrinder-tarball-platform-plugin/tarball_plugin.rb', line 40

def execute
  
  # Create a temporary directory to work from
  @working_dir = "#{@dir.tmp}"
  mkdir_p(@working_dir)
  
  # collect the RPMs and create an install_rpms.sh script to add to the payload
  install_rpms_script_name = "install_rpms.sh"
  create_script(install_rpms_script_name, :prefix => "yum install -y ", :lines => @appliance_config.packages) {|line|
    "#{line} "
  }
  
  # collect all post->base commands and create an install script to add to the payload
  install_post_script_name = "install_post.sh"
  create_script(install_post_script_name, :lines => @appliance_config.post['base']) {|line|
    "#{line}\n"
  }
  
  # create a root install script to install rpms and perform post
  install_all_script_name = "install.sh"
  create_script(install_all_script_name, :lines => [install_rpms_script_name, install_post_script_name]) {|line|
    "sh #{line}\n"
  }
  
  # Copy all files into the working directory to add to the payload
  for tgt_dir in @appliance_config.files.keys
    sources = @appliance_config.files[tgt_dir]
    for source in sources
      # convert '../xxx/yyy' to 'xxx/yyy'
      tgt_path_suffix = source.scan(/([^\.]+)[\\\/][^\\\/]+/)
      full_tgt_path = "#{tmp_path(tgt_dir)}/#{tgt_path_suffix}"
      mkdir_p(full_tgt_path)
      cp_r("#{source}", full_tgt_path)
    end
  end
  
  # create the tar in the build tmp directory, and it will be moved into the 
  # deliverables directory automatically
  root_tar_path = tmp_path('')
  @tar_name = "#{@appliance_config.name}.tgz"
  Dir.chdir(root_tar_path) do
    begin
      sgz = Zlib::GzipWriter.new(File.open(@tar_name, 'wb'))
      tar = Archive::Tar::Minitar::Output.new(sgz)
      uniq_entries = Set.new
      for tgt_dir in @appliance_config.files.keys
        # remove leading slashes for relative paths
        tgt_rel = "#{tgt_dir.scan(/[\\\/]*(.*)/)}"
        Find.find(tgt_rel) do |entry|
          if !uniq_entries.include?(entry)
            Minitar.pack_file(entry, tar) 
            uniq_entries << entry
          end
        end
      end
      # add all generated scripts
      for script_name in @created_scripts
        Minitar.pack_file(script_name, tar) 
      end
    ensure
      tar.close
    end
  end
  
end

#tmp_path(dir_name) ⇒ Object



106
107
108
# File 'lib/dw-boxgrinder-tarball-platform-plugin/tarball_plugin.rb', line 106

def tmp_path(dir_name)
  "#{@working_dir}/#{dir_name}"
end