module Deployment module Methods def set(name, value) Helper.validates_presence_of name Helper.validates_presence_of value $recipe_config[name] = value end end def self.deliver(&block) worker = Worker.new worker.instance_eval(&block) return worker end class Worker def initialize # initialize logging if not $recipe_config[:log_level].nil? case $recipe_config[:log_level] when "DEBUG" $log.level('stdout_logger', Log4r::DEBUG) when "INFO" $log.level('stdout_logger', Log4r::INFO) when "WARN" $log.level('stdout_logger', Log4r::WARN) when "ERROR" $log.level('stdout_logger', Log4r::ERROR) when "FATAL" $log.level('stdout_logger', Log4r::FATAL) end end if $recipe_config[:log_trace] $log.enable_trace end # read global deployment configuration from etc/config.yaml if ENV['USE_DEPENGINE_AS_GEM'] deployment_configuration_file = \ ( File.join(File.dirname($exec_file_path), '../../etc/config.yaml') ) else deployment_configuration_file = \ ( File.join(File.dirname($exec_file_path), '../etc/config.yaml') ) end if File.file? deployment_configuration_file $recipe_config.merge! Helper.yaml_parse(deployment_configuration_file) else puts "Can not read deployment configuration etc/config.yaml" end # generate hash from environemnt variables and merge $recipe_config.merge! Helper.get_environemnt_variables($recipe_config['env_exceptions']) # generate version hash from environment variable @version = Helper.parse_version($recipe_config[:version]) # validate needed environment variables Helper.validates_not_empty $recipe_config[:env], \ "env is not set" Helper.validates_not_empty $recipe_config[:version], \ "version is not set" Helper.validates_not_empty $recipe_config[:deploy_home], \ "deploy_home is not set" # read configuration from cdb @cdb = get_cdb_parameters(@version['cdb']) # clean workspace if needed if $recipe_config[:cleanup_workspace_before] $log.writer.info "Remove all files from Workspace" Helper.cleanup_workspace!($recipe_config[:deploy_home]) end # init workspace with default directorys Helper.init_workspace($recipe_config[:deploy_home]) # the logfile needs a correct WORKSPACE and the initial dirs # so we have to set it here if $recipe_config[:log_file] level = Log4r::INFO $log.writer.info "Logfile level set to #{$recipe_config[:log_file_level]}" case $recipe_config[:log_file_level] when "DEBUG" level = Log4r::DEBUG when "INFO" level = Log4r::INFO when "WARN" level = Log4r::WARN when "ERROR" level = Log4r::ERROR when "FATAL" level = Log4r::FATAL end $log.add_outputter(Log4r::FileOutputter.new('log_file', \ :filename => File.join($recipe_config[:deploy_home], 'log', \ $recipe_config[:log_file])), \ level) end end # access configdata like this... # @hudson['USER'] # $recipe_config[:application_name] def copy(source, target, options={}) Processor.copy(File.join($recipe_config[:deploy_home], source), \ File.join($recipe_config[:deploy_home], target), \ options) end def mkdir(directory) Processor.mkdir(File.join($recipe_config[:deploy_home], directory)) end def chmod(file, mode, options={}) Processor.chmod(File.join($recipe_config[:deploy_home], file), \ mode, options) end def remove(file) Processor.remove(File.join($recipe_config[:deploy_home], file)) end def mklink(source, target) Processor.mklink(source, File.join($recipe_config[:deploy_home], target)) end def check_filelist(list, target) Processor.check_filelist(File.join($recipe_config[:deploy_home], list), \ File.join($recipe_config[:deploy_home], target)) end def unzip_file(zip_file, target_dir, filename=nil) Helper.validates_presence_of @cdb['unzip_bin'] Processor.unzip_file(File.join($recipe_config[:deploy_home], zip_file), \ File.join($recipe_config[:deploy_home], target_dir), \ filename, @cdb['unzip_bin']) end def tgz_file(tgz_file, source_dir, filename=nil) Helper.validates_presence_of @cdb['tar_bin'] Processor.tgz_file(File.join($recipe_config[:deploy_home], tgz_file), \ File.join($recipe_config[:deploy_home], source_dir), \ filename, @cdb['tar_bin']) end def untgz_file(tgz_file, target_dir, filename=nil) Helper.validates_presence_of @cdb['tar_bin'] Processor.untgz_file(File.join($recipe_config[:deploy_home], tgz_file), \ File.join($recipe_config[:deploy_home], target_dir), \ filename, @cdb['tar_bin']) end def parse_template(source, target, content, options={}) template = Processor::Template.new template.cdb = @cdb template.basepath = options[:basepath] || $recipe_config[:deploy_home] options[:excludes] = [] if options[:excludes].nil? options[:excludes] << '.svn' template.parse_template(source, content, target, options) end def parse_erb_template(source, target, content, options={}) template = Processor::Template.new template.cdb = @cdb template.basepath = options[:basepath] || $recipe_config[:deploy_home] options[:excludes] = [] if options[:excludes].nil? options[:excludes] << '.svn' template.parse_erb_template(source, content, target, options) end def get_all_config_parameters # merge cdb data $recipe_config.merge @cdb end def get_cdb_parameters(version) Helper.validates_presence_of version, "version parameter is missing" Helper.validates_presence_of $recipe_config['cdb_host'], \ "cdb_host parameter is missing" Helper.validates_presence_of $recipe_config['cdb_context'], \ "cdb_context parameter is missing" cdb = Provider::CDB.new cdb.protocol = 'http' cdb.host = $recipe_config['cdb_host'] cdb.context = $recipe_config['cdb_context'] cdb.env = $recipe_config[:env] cdb.get_parameters(File.join( $recipe_config[:module_name], \ $recipe_config[:application_name] ), version) end def set_cdb_parameter(key, value) Helper.validates_presence_of $recipe_config['cdb_host'], \ "cdb_host parameter is missing" Helper.validates_presence_of $recipe_config['cdb_context'], \ "cdb_context parameter is missing" cdb = Provider::CDB.new cdb.protocol = 'http' cdb.host = $recipe_config['cdb_host'] cdb.context = $recipe_config['cdb_context'] cdb.env = $recipe_config[:env] cdb.set_parameter(File.join( $recipe_config[:module_name], \ $recipe_config[:application_name] ), key, value) end def set_cdb_parameter_4_app(module_name, app_name, key, value) Helper.validates_presence_of $recipe_config['cdb_host'], \ "cdb_host parameter is missing" Helper.validates_presence_of $recipe_config['cdb_context'], \ "cdb_context parameter is missing" cdb = Provider::CDB.new cdb.protocol = 'http' cdb.host = $recipe_config['cdb_host'] cdb.context = $recipe_config['cdb_context'] cdb.env = $recipe_config['ENVIRONMENT'] cdb.set_parameter(File.join( module_name, app_name ), key, value) end def get_dir_from_samba(source, target) Helper.validates_presence_of @cdb['samba_repository'], "Samba repository not set" samba = Provider::Repository.new samba.repository = @cdb['samba_repository'] samba.samba_mountpoints = @cdb['samba_mountpoints'] samba.method = "samba" samba.worker = self samba.get_from_repository(source, File.join($recipe_config[:deploy_home], target)) end def get_file_from_maven(source, target) Helper.validates_presence_of @cdb['maven_repository'], "Maven repository not set" maven = Provider::Repository.new maven.repository = @cdb['maven_repository'] maven.user = @cdb['maven_user'] maven.password = @cdb['maven_password'] maven.method = "maven" maven.get_from_repository(source, File.join($recipe_config[:deploy_home], target)) end def get_file_via_scp(source, target, options={}) Helper.validates_presence_of @cdb['scp_source_host'], \ "scp source host not set" Helper.validates_presence_of @cdb['scp_source_user'], \ "scp source user not set" scp = Provider::Repository.new scp.host = @cdb['scp_source_host'] scp.user = @cdb['scp_source_user'] if options[:ssh_key_file] scp.sshkey = File.join(File.dirname($exec_file_path), \ options[:ssh_key_file]) else scp.sshkey = File.join(File.dirname($exec_file_path), \ @cdb['ssh_key_file']) end scp.method = "scp" scp.get_from_repository(source, File.join($recipe_config[:deploy_home], target)) end def get_file_via_svn(source, target, options={}) Helper.validates_presence_of @cdb['svn_binary'], \ "svn binary not set" Helper.validates_presence_of @cdb['svn_source_base_url'], \ "svn base url not set" Helper.validates_presence_of @cdb['svn_source_user'], \ "svn source user not set" Helper.validates_presence_of @cdb['svn_source_command'], \ "svn command not set" svn = Provider::Repository.new svn.svnbinary = @cdb['svn_binary'] svn.user = @cdb['svn_source_user'] svn.password = @cdb['svn_source_password'] if @cdb['svn_source_password'] svn.method = "svn" if options[:svn_command] svn.svncmd = options[:svn_command] else svn.svncmd = @cdb['svn_source_command'] + " -r#{options[:revision]}" end if options[:svn_repository] svn.repository = options[:svn_repository] else svn.repository = @cdb['svn_source_base_url'] end svn.get_from_repository(source, \ File.join($recipe_config[:deploy_home], target)) end def send_dweb_command(command, options={}) Helper.validates_presence_of @cdb['staging_host'], "Staging Host not set" Helper.validates_presence_of @cdb['ssh_key_file_command'], "SSH keyfile not set" Helper.validates_presence_of @cdb['siteid'], "Siteid not set" publisher = Publisher::Dweb.new publisher.staging_host = @cdb['staging_host'] publisher.ssh_key_file = @cdb['ssh_key_file_command'] publisher.siteid = @cdb['siteid'] publisher.send_command(command, options) end def dweb_upload(source, target, options={}) Helper.validates_presence_of @cdb['staging_host'], "Staging Host not set" Helper.validates_presence_of @cdb['ssh_key_file'], "SSH keyfile not set" Helper.validates_presence_of @cdb['siteid'], "Siteid not set" Helper.validates_presence_of source Helper.validates_presence_of target publisher = Publisher::Dweb.new publisher.staging_host = @cdb['staging_host'] publisher.ssh_key_file = @cdb['ssh_key_file'] publisher.siteid = @cdb['siteid'] vhost = options[:siteid] || @cdb['siteid'] options[:excludes] = options[:excludes] || ['.svn'] publisher.upload(File.join($recipe_config[:deploy_home], source), \ File.join('/vhosts/', vhost, target), options) end def dweb_link(source, symlink, options={}) publisher = Publisher::Dweb.new publisher.staging_host = @cdb['staging_host'] publisher.ssh_key_file = @cdb['ssh_key_file'] publisher.siteid = @cdb['siteid'] publisher.remote_symlink(source, symlink, options) end def remote_execute(command, options={}) Helper.validates_presence_of command, "No command to execute!" Helper.validates_presence_of @cdb['ssh_key_file'], "SSH keyfile not set" publisher = Publisher::Ssh.new publisher.remote_host = options[:remote_host] || @cdb['remote_host'] publisher.remote_user = options[:remote_user] || @cdb['remote_user'] publisher.ssh_key_file = options[:ssh_key_file] || @cdb['ssh_key_file'] publisher.remote_execute(command) end def rsync(source, target, options={}) if options[:local].nil? or options[:local] == false Helper.validates_presence_of @cdb['ssh_key_file'], "SSH-keyfile not set" end Helper.validates_presence_of source Helper.validates_presence_of target publisher = Publisher::Rsync.new publisher.remote_host = options[:remote_host] || @cdb['remote_host'] publisher.remote_user = options[:remote_user] || @cdb['remote_user'] publisher.ssh_key_file = options[:ssh_key_file] || @cdb['ssh_key_file'] # publisher.ssh_key_file = File.join(File.dirname($exec_file_path), \ # publisher.ssh_key_file) if options[:local].nil? or options[:local] == false Helper.validates_presence_of publisher.remote_host, "Remote host not set" Helper.validates_presence_of publisher.remote_user, "Remote user not set" end if source[0].chr == "/" source_path = source else source_path = File.join($recipe_config[:deploy_home], source) end publisher.sync(source_path, target, options) end def samba_upload(source, target, options={}) Helper.validates_presence_of source Helper.validates_presence_of target publisher = Publisher::Samba.new publisher.samba_share = options[:samba_share] || @cdb['samba_share'] publisher.worker = self publisher.samba_mountpoints = options[:samba_mountpoints] || @cdb['samba_mountpoints'] Helper.validates_presence_of publisher.samba_share, "Sambe share not set" Helper.validates_presence_of publisher.samba_mountpoints, "Sambe mountpoints not set" publisher.copy(File.join($recipe_config[:deploy_home], source), \ target, options) end def sftp_upload(source, target, options={}) Helper.validates_presence_of @cdb['ssh_key_file'], "SSH-keyfile not set" Helper.validates_presence_of source Helper.validates_presence_of target publisher = Publisher::Sftp.new publisher.remote_host = options[:remote_host] || @cdb['remote_host'] publisher.remote_user = options[:remote_user] || @cdb['remote_user'] publisher.ssh_key_file = options[:ssh_key_file] || @cdb['ssh_key_file'] publisher.ssh_key_file = File.join(File.dirname($exec_file_path), \ publisher.ssh_key_file) Helper.validates_presence_of publisher.remote_host, "Remote host not set" Helper.validates_presence_of publisher.remote_user, "Remote user not set" publisher.copy(File.join($recipe_config[:deploy_home], source), \ target, options) end def sftp_chmod(path, permissions, options={}) Helper.validates_presence_of @cdb['ssh_key_file'], "SSH-keyfile not set" Helper.validates_presence_of path Helper.validates_presence_of permissions publisher = Publisher::Sftp.new publisher.remote_host = options[:remote_host] || @cdb['remote_host'] publisher.remote_user = options[:remote_user] || @cdb['remote_user'] publisher.ssh_key_file = options[:ssh_key_file] || @cdb['ssh_key_file'] publisher.ssh_key_file = File.join(File.dirname($exec_file_path), \ publisher.ssh_key_file) Helper.validates_presence_of publisher.remote_host, "Remote host not set" Helper.validates_presence_of publisher.remote_user, "Remote user not set" publisher.chmod(path, permissions, options) end def sendmail(options) Helper.validates_presence_of options[:from], "Mail-FROM not set" Helper.validates_presence_of options[:to], "Mail-TO not set" Helper.validates_presence_of options[:subject], "No mail subject set" Helper.validates_presence_of options[:body], "No mail body set" Helper.validates_presence_of @cdb['smtp_host'], "SMTP host not set" helper = Helper::Mail.new helper.smtp_host = @cdb['smtp_host'] helper.sendmail(options) end def patch_properties (source, target, options={}) patch_set = 'patch_properties' if not options[:patch_set].nil? patch_set = options[:patch_set] end Helper.validates_presence_of @cdb[patch_set], \ "Can not find a valid patch_set" assigner = '=' if not options[:assigner].nil? assigner = options[:assigner] end properties = Processor::Properties.new properties.properties_hash = @cdb[patch_set] ### add db_endpoint properties.properties_hash['db_endpoint_1'] = @cdb['db_endpoint_1'] properties.properties_hash['db_endpoint_2'] = @cdb['db_endpoint_2'] properties.assigner = assigner properties.patch(File.join($recipe_config[:deploy_home], source), \ File.join($recipe_config[:deploy_home], target, \ File.basename(source))) end def patch_strings (source, target, options={}) patch_set = 'patch_properties' if not options[:patch_set].nil? patch_set = options[:patch_set] end Helper.validates_presence_of @cdb[patch_set], "Properties not set" assigner = '=' if not options[:assigner].nil? assigner = options[:assigner] end properties = Processor::Properties.new properties.assigner = assigner properties.properties_hash = @cdb[patch_set] ### add db_endpoint properties.properties_hash['db_endpoint_1'] = @cdb['db_endpoint_1'] properties.properties_hash['db_endpoint_2'] = @cdb['db_endpoint_2'] properties.substitute(File.join($recipe_config[:deploy_home], source), \ File.join($recipe_config[:deploy_home], target, \ File.basename(source))) end def patch_strings_r (source, target, options={}) patch_set = 'patch_properties' if not options[:patch_set].nil? patch_set = options[:patch_set] end Helper.validates_presence_of @cdb[patch_set], "Properties not set" assigner = '=' if not options[:assigner].nil? assigner = options[:assigner] end source_path = File.join($recipe_config[:deploy_home], source) target_path = File.join($recipe_config[:deploy_home], target) properties = Processor::Properties.new properties.assigner = assigner properties.properties_hash = @cdb[patch_set] ### add db_endpoint properties.properties_hash['db_endpoint_1'] = @cdb['db_endpoint_1'] properties.properties_hash['db_endpoint_2'] = @cdb['db_endpoint_2'] properties.substitute_r(source_path, target_path) end def add_properties (source, target, options={}) patch_set = 'patch_properties' if not options[:patch_set].nil? patch_set = options[:patch_set] end Helper.validates_presence_of @cdb[patch_set], "Properties not set" assigner = '=' if not options[:assigner].nil? assigner = options[:assigner] end properties = Processor::Properties.new properties.assigner = assigner properties.properties_hash = @cdb[patch_set] properties.add(File.join($recipe_config[:deploy_home], source), \ File.join($recipe_config[:deploy_home], target, \ File.basename(source))) end def sed_strings() Sed.copy($recipe_config[:source],$recipe_config[:target], $recipe_config[:pattern],$recipe_config[:replacement]) end def iis_build() Helper.validates_presence_of @cdb['iis_build_url'], \ "iis_build_url parameter is missing" Helper.validates_presence_of @cdb['iis_build_protocol'], \ "iis_build_protocol parameter is missing" Helper.validates_presence_of $recipe_config['TAG'], \ "TAG parameter is missing" Helper.validates_presence_of $recipe_config['ENVIRONMENT'], \ "ENV parameter is missing" Helper.validates_presence_of @version['app'], \ "VERSION parameter is missing" Helper.validates_presence_of $recipe_config['VALUE'], \ "VALUE parameter is missing" iis = Publisher::Iis_build.new iis.iis_build_protocol = @cdb['iis_build_protocol'] iis.iis_build_url = @cdb['iis_build_url'] if @cdb['http_read_timeout'] iis.http_read_timeout = @cdb['http_read_timeout'] else iis.http_read_timeout = 180 end iis.build($recipe_config['TAG'], $recipe_config['ENVIRONMENT'], @version['app'], $recipe_config['VALUE'], $recipe_config[:application_name]) end def iis_deploy(options={}) Helper.validates_presence_of options[:iis_deploy_url] || @cdb['iis_deploy_url'], \ "iis_deploy_url parameter is missing" Helper.validates_presence_of @cdb['iis_application_name'], \ "iis_application_name parameter is missing" Helper.validates_presence_of @cdb['iis_deploy_protocol'], \ "iis_deploy_protocol parameter is missing" Helper.validates_presence_of $recipe_config['ENVIRONMENT'], \ "ENV parameter is missing" Helper.validates_presence_of @version['app'], \ "VERSION parameter is missing" iis = Publisher::Iis_deploy.new iis.iis_deploy_protocol = @cdb['iis_deploy_protocol'] iis.iis_deploy_url = options[:iis_deploy_url] || @cdb['iis_deploy_url'] if @cdb['http_read_timeout'] iis.http_read_timeout = @cdb['http_read_timeout'] else iis.http_read_timeout = 180 end iis.deploy(@version['app'], @cdb['iis_application_name'], $recipe_config['ENVIRONMENT'].downcase) end def iis_appcmd() Helper.validates_presence_of @cdb['iis_appcmd_url'], \ "iis_appcmd_url parameter is missing" Helper.validates_presence_of $recipe_config['VALUE'], \ "VALUE parameter is missing" iis = Publisher::Iis_appcmd.new iis.iis_appcmd_protocol = @cdb['iis_appcmd_protocol'] iis.iis_appcmd_url = @cdb['iis_appcmd_url'] if @cdb['http_read_timeout'] iis.http_read_timeout = @cdb['http_read_timeout'] else iis.http_read_timeout = 180 end iis.appcmd($recipe_config['VALUE']) end def assert_url_response_of(options={}) asserter = Asserter::Url.new asserter.check_protocol = options[:check_protocol] || @cdb['check_protocol'] || "http" asserter.check_host = options[:check_host] || @cdb['check_host'] asserter.check_port = options[:check_port] || @cdb['check_port'] || '80' asserter.check_uri = options[:check_uri] || @cdb['check_uri'] || '/' asserter.check_response_string = options[:chek_response_string] || @cdb['check_response_string'] || 'html' asserter.assert_url_response_of(options) end def report_to_cdb(options={}) reporter = Reporter::Cdb.new reporter.version = @version.to_json reporter.worker = self reporter.set_version(options) end def report_by_mail(options={}) reporter = Reporter::Mail.new reporter.deploy_email_from = options[:deploy_email_from] || @cdb['deploy_email_from'] reporter.deploy_email_to = options[:deploy_email_to] || @cdb['deploy_email_to'] reporter.application_name = options[:application_name] || $recipe_config[:application_name] || @cdb['application_name'] reporter.module_name = options[:module_name] || $recipe_config[:module_name] || @cdb['module_name'] reporter.environment = $recipe_config['ENVIRONMENT'] reporter.version = @version.to_json reporter.worker = self reporter.send(options) end def tomcat_deploy(options={}) publisher = Publisher::Tomcat.new publisher.servers = options[:servers] || @cdb['servers'] publisher.runners = options[:runners] || @cdb['runners'] || publisher.servers publisher.initd_script = options[:initd_script] || @cdb['initd_script'] publisher.application_name = options[:application_name] || $recipe_config[:application_name] || @cdb['application_name'] publisher.catalina_home = options[:catalina_home] || @cdb['catalina_home'] publisher.tomcat_context = options[:tomcat_context] || @cdb['tomcat_context'] publisher.logfilename = options[:logfilename] || @cdb['logfilename'] publisher.check_host = options[:check_host] || @cdb['check_host'] publisher.worker = self publisher.deploy(options) end def samba_mount (remote_path, local_path) smb = Helper::Smb.new smb.remote_path = remote_path smb.local_path = local_path smb.samba_mount() end def samba_umount (local_path) smb = Helper::Smb.new smb.local_path = local_path smb.samba_umount() end def git_fetch(options={}) publisher = Publisher::Git.new publisher.repository_url = options[:git_repository_url] || @cdb['git_repository_url'] publisher.repository_local_dir = options[:git_repository_local] || @cdb['git_repository_local'] publisher.fetch() end def git_checkout(branch_name, options={}) publisher = Publisher::Git.new publisher.repository_url = options[:git_repository_url] || @cdb['git_repository_url'] publisher.repository_local_dir = options[:git_repository_local] || @cdb['git_repository_local'] publisher.checkout(branch_name, options) end def git_tag(tag_name, options={}) publisher = Publisher::Git.new publisher.repository_url = options[:git_repository_url] || @cdb['git_repository_url'] publisher.repository_local_dir = options[:git_repository_local] || @cdb['git_repository_local'] publisher.tag(tag_name, options) end end end