require 'rubygems/package' require 'zlib' require 'pathname' require 'picsolve_docker_builder/frame' require 'picsolve_docker_builder/builder/builder' module PicsolveDockerBuilder # Build a node js project class NodeJs < Frame def output_dir config['nodejs']['output_dir'] end def default_config c = super c['nodejs'] = { 'build_commands' => [ 'npm install', 'grunt jenkins' ] } c end def prepare_volumes execute [ 'chown', '-c', build_user, build_user_home ] end def docker_build_files f = [] f << Builder::File.new( 'dist.tar.gz', source: File.join(base_dir, 'dist.tar.gz'), destination: '/var/www/html' ) begin add_files = config['docker']['docker_build']['add_files'] rescue NoMethodError add_files = [] end add_files.each do |file| f << Builder::File.new( file, source: File.join(base_dir, file) ) end f end def docker_builder_hooks begin hooks = config['docker']['dockerfile_hooks']['docker_build'] # symbolize the keys hooks.keys.each do |key| begin key_sym = key.to_sym rescue key_sym = key end hooks[key_sym || key] = hooks.delete(key) end rescue NoMethodError hooks = {} end hooks[:before_adds] = [ '# remove existing /var/www/html', 'RUN rm -rf /var/www/html' ] hooks[:after_adds] = [ '# ensure rights', 'RUN chown -R root:www-data /var/www/html' ] hooks end def docker_builder Builder::Builder.new( base_image: runtime_image_name, maintainer: 'Picsolve Onlineops ', files: docker_build_files, hooks: docker_builder_hooks ) end def docker_build @docker_build = docker_builder.build end def asset_build_command runs = config['nodejs']['build_commands'].map do |tasks| "#{tasks}" end runs = [ 'export LANG=en_US.UTF-8', 'export LANGUAGE=en_US.UTF-8', 'export LC_ALL=en_US.UTF-8', "export PATH=$PATH:#{File.join(build_dir, '/node_modules/.bin')}" ] + runs runs.join ' && ' end def asset_build log.info "start asset building with image #{image_name}" start prepare_volumes build_cmd = [ 'gosu', build_user, 'bash', '-c', asset_build_command ] execute_attach build_cmd log.info "finished asset building with image #{image_name}" stop end end end