| 1 | # | ||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||
| 3 | # | ||||||||||
| 4 | # vim: set ts=2 sw=2 tw=0: | ||||||||||
| 5 | # vim: set expandtab: | ||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | Rex::Apache::Inject - Configuration Injection module for Rex::Apache::Deploy | ||||||||||
| 10 | |||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||
| 12 | |||||||||||
| 13 | This is a (R)?ex module to inject configuration parameters into packages (*.tar.gz, *.tar.bz2, *.zip or *.war). | ||||||||||
| 14 | |||||||||||
| 15 | You can find examples and howtos on L<http://rexify.org/> | ||||||||||
| 16 | |||||||||||
| 17 | =head1 GETTING HELP | ||||||||||
| 18 | |||||||||||
| 19 | =over 4 | ||||||||||
| 20 | |||||||||||
| 21 | =item * Web Site: L<http://rexify.org/> | ||||||||||
| 22 | |||||||||||
| 23 | =item * IRC: irc.freenode.net #rex | ||||||||||
| 24 | |||||||||||
| 25 | =back | ||||||||||
| 26 | |||||||||||
| 27 | =head1 DEPENDENCIES | ||||||||||
| 28 | |||||||||||
| 29 | =over 4 | ||||||||||
| 30 | |||||||||||
| 31 | =item * | ||||||||||
| 32 | |||||||||||
| 33 | L<Rex> | ||||||||||
| 34 | |||||||||||
| 35 | =back | ||||||||||
| 36 | |||||||||||
| 37 | =head1 SYNOPSIS | ||||||||||
| 38 | |||||||||||
| 39 | use Rex::Apache::Inject Properties; | ||||||||||
| 40 | |||||||||||
| 41 | template_file "inject.conf"; | ||||||||||
| 42 | template_search_for "*.properties"; | ||||||||||
| 43 | |||||||||||
| 44 | desc "Inject LIVE Configuration"; | ||||||||||
| 45 | task "inject", sub { | ||||||||||
| 46 | inject "mypkg-1.0.1.tar.gz"; | ||||||||||
| 47 | }; | ||||||||||
| 48 | |||||||||||
| 49 | desc "Inject LIVE Configuration"; | ||||||||||
| 50 | task "inject", sub { | ||||||||||
| 51 | inject "mypkg-1.0.1.tar.gz", | ||||||||||
| 52 | pre_pack_hook => sub { | ||||||||||
| 53 | say "Pre Pack Hook"; | ||||||||||
| 54 | }, | ||||||||||
| 55 | post_pack_hook => { | ||||||||||
| 56 | say "Post Pack Hook"; | ||||||||||
| 57 | }; | ||||||||||
| 58 | }; | ||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | =head1 INJECT METHODS | ||||||||||
| 62 | |||||||||||
| 63 | =over 4 | ||||||||||
| 64 | |||||||||||
| 65 | =item Properties | ||||||||||
| 66 | |||||||||||
| 67 | This method is for Java-Like Property files. | ||||||||||
| 68 | |||||||||||
| 69 | use Rex::Apache::Inject Properties; | ||||||||||
| 70 | |||||||||||
| 71 | template_file "inject.conf"; | ||||||||||
| 72 | template_search_for "*.properties"; | ||||||||||
| 73 | |||||||||||
| 74 | task "inject", sub { | ||||||||||
| 75 | inject "myapp.war"; | ||||||||||
| 76 | }; | ||||||||||
| 77 | |||||||||||
| 78 | This will search all files named I<*.properties> inside of myapp.war and replace the parameters with these defined in I<template_file>. | ||||||||||
| 79 | |||||||||||
| 80 | Format of the I<template_file> is the same as the property files. | ||||||||||
| 81 | |||||||||||
| 82 | my.property.one = Value1 | ||||||||||
| 83 | my.property.two = Value no two | ||||||||||
| 84 | |||||||||||
| 85 | =item Template | ||||||||||
| 86 | |||||||||||
| 87 | This is a special method. It will search for template files within the archive and will generate new ones with the parameters defined in I<template_file>. | ||||||||||
| 88 | |||||||||||
| 89 | use Rex::Apache::Inject Template; | ||||||||||
| 90 | |||||||||||
| 91 | template_file "inject.conf"; | ||||||||||
| 92 | template_search_for "*.template.*"; | ||||||||||
| 93 | |||||||||||
| 94 | generate_real_name sub { | ||||||||||
| 95 | my ($template_file_name) = @_; | ||||||||||
| 96 | $template_file_name =~ s/\.template//; | ||||||||||
| 97 | return $template_file_name; | ||||||||||
| 98 | }; | ||||||||||
| 99 | |||||||||||
| 100 | task "inject", sub { | ||||||||||
| 101 | inject "myapp.tar.gz"; | ||||||||||
| 102 | }; | ||||||||||
| 103 | |||||||||||
| 104 | This will search for files named I<*.template.*> inside of myapp.tar.gz. And will generate new files on the basis of I<generate_real_name>. | ||||||||||
| 105 | |||||||||||
| 106 | Example: | ||||||||||
| 107 | |||||||||||
| 108 | # Template Configuration file (inside myapp.tar.gz): config.template.php | ||||||||||
| 109 | <?php | ||||||||||
| 110 | $db['host'] = "@db.host@"; | ||||||||||
| 111 | $db['port'] = @db.port@; | ||||||||||
| 112 | $db['user'] = "@db.user@"; | ||||||||||
| 113 | $db['pass'] = "@db.pass@"; | ||||||||||
| 114 | ?> | ||||||||||
| 115 | |||||||||||
| 116 | # Template file (specified by ,,template_file'') | ||||||||||
| 117 | db.host = "db01" | ||||||||||
| 118 | db.port = 3306 | ||||||||||
| 119 | db.user = "myuser" | ||||||||||
| 120 | db.pass = "mypass" | ||||||||||
| 121 | |||||||||||
| 122 | |||||||||||
| 123 | The I<inject> action will generate a file called I<config.php> with the following content: | ||||||||||
| 124 | |||||||||||
| 125 | <?php | ||||||||||
| 126 | $db['host'] = "db01"; | ||||||||||
| 127 | $db['port'] = 3306; | ||||||||||
| 128 | $db['user'] = "myuser"; | ||||||||||
| 129 | $db['pass'] = "mypass"; | ||||||||||
| 130 | ?> | ||||||||||
| 131 | |||||||||||
| 132 | =item YAML | ||||||||||
| 133 | |||||||||||
| 134 | This method will parse YAML files inside the archive. For this method you need to have the I<YAML> Module installed. | ||||||||||
| 135 | |||||||||||
| 136 | template_file "live_config.yml"; | ||||||||||
| 137 | template_search_for "application.yml"; | ||||||||||
| 138 | |||||||||||
| 139 | task "inject", sub { | ||||||||||
| 140 | inject "myapp.tar.gz", | ||||||||||
| 141 | pre_pack_hook => sub { | ||||||||||
| 142 | run "BUNDLE_PATH=vendor/bundle bundle install"; | ||||||||||
| 143 | }; | ||||||||||
| 144 | }; | ||||||||||
| 145 | |||||||||||
| 146 | This will search the file I<application.yml> inside of myapp.tar.gz and replace the configuration values inside of it with these defined in I<live_config.yml>. Also it do a pre pack hook that will run I<bundle install>. | ||||||||||
| 147 | |||||||||||
| 148 | =back | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | package Rex::Apache::Inject; | ||||||||||
| 153 | |||||||||||
| 154 | 1 | our $VERSION = "0.12.0"; | |||||||||
| 155 | |||||||||||
| 156 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 157 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 158 | |||||||||||
| 159 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 160 | |||||||||||
| 161 | 1 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=3 SCAR=16.5
| |||||||||||
| 162 | |||||||||||
| 163 | 1 | my ($call_class) = caller; | |||||||||
| 164 | |||||||||||
| 165 | 1 | ▶ | return unless $_[1]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | |||||||||||
| 167 | 0 | ▶ | die("Invalid input format") unless ( $_[1] =~ m/^[a-z0-9_]+$/i ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | |||||||||||
| 169 | 0 | eval "use $_[0]::$_[1] '$call_class';"; | |||||||||
| 170 | |||||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | 1 | 1; | |||||||||