lono 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,7 +22,7 @@ This sets up a starter lono project with example templates.
22
22
  $ lono generate
23
23
  </pre>
24
24
 
25
- This generates the templates that have been defined in the config folder of the lono project.
25
+ This generates the templates that have been defined in the config folder of the lono project to the output folder.
26
26
 
27
27
  The starter lono template project config files looks like [this](lib/starter_project/config/lono.rb) and [this](lib/starter_project/config/lono/api.rb). Here's a snippet from one of the config files with the template call:
28
28
 
@@ -58,10 +58,12 @@ There are helper methods that are available in templates.
58
58
 
59
59
  * partial - can be use to embed other files in a template. The partial should be placed in the templates/partial folder of the project. So:
60
60
  * partial('launch_config.json.erb') -> templates/partial/launch_config.json.erb
61
+ * partial('launch_config.json.erb', :foo => "bar", :hello => "world") - variables can be passed to the partial helper method and will be available to the partial as instance variables. So, in this case @foo and @hello will be available in the launch_config.json.erb partial.
61
62
 
62
63
  * user_data - can be used to include a user data script which is written in bash script form. The user data script should be placed in the templates/user_data folder of the project. So:
63
64
  * user_data('bootstrap.sh.erb') -> templates/user_data/bootstrap.sh.erb
64
65
  * user_data('db.sh.erb') -> templates/user_data/db.sh.erb
66
+ * user_data('script1.sh.erb', 'script2.sh.erb') - multiple files can be used to build the user_data script, script1 and script2 will be combined
65
67
 
66
68
  Here's how you would call it in the template.
67
69
 
@@ -70,12 +72,102 @@ Here's how you would call it in the template.
70
72
  "Fn::Base64": {
71
73
  "Fn::Join": [
72
74
  "",
73
- <%= user_data('bootstrap.sh.erb') %>
75
+ <%= user_data('db.sh.erb') %>
74
76
  ]
75
77
  }
76
78
  ```
77
79
 
78
- Within a user_data script you can user helper methods that correspond to Cloud Formation [Instrinic Functions](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/concept-intrinsic-functions.html). Currently, base64, find_in_map, get_att, get_azs, join, and ref are supported. Examples of their usage are found in the starter [project template](https://github.com/tongueroo/lono/blob/master/lib/starter_project/templates/user_data/db.sh.erb)
80
+ Within the user_data script you can use helper methods that correspond to Cloud Formation [Instrinic Functions](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/concept-intrinsic-functions.html). Currently, base64, find_in_map, get_att, get_azs, join, and ref are supported. Here's a short example of a user_data script using a helper method:
81
+
82
+ If you have a templates/user_data/db.sh.erb that looks like this:
83
+
84
+ ```bash
85
+ #!/bin/bash -lexv
86
+
87
+ HOSTNAME_PREFIX=<%= find_in_map("EnvironmentMapping", "HostnamePrefix", ref("Environment")) %>
88
+
89
+ echo <%= ref("AWS::StackName") %> > /tmp/stack_name
90
+ # Helper function
91
+ function error_exit
92
+ {
93
+ /usr/local/bin/cfn-signal -e 1 -r "$1" '<%= ref("WaitHandle") %>'
94
+ exit 1
95
+ }
96
+ # Wait for the EBS volume to show up
97
+ while [ ! -e /dev/xvdf ]; do echo Waiting for EBS volume to attach; sleep 1; done
98
+ /bin/mkdir /media/redis
99
+ /sbin/mkfs -t ext4 /dev/xvdf
100
+ echo "/dev/xvdf /media/redis auto defaults 0 0" >> /etc/fstab
101
+ /bin/mount /media/redis
102
+ /usr/bin/redis-cli shutdown
103
+ sleep 10
104
+ mv /var/lib/redis/* /media/redis/
105
+ rm -r /var/lib/redis
106
+ ln -s /media/redis /var/lib/redis
107
+ chown -R redis:redis /var/lib/redis
108
+ chown -R redis:redis /media/redis
109
+ /usr/bin/redis-server
110
+ # If all is well so signal success
111
+ /usr/local/bin/cfn-signal -e $? -r "Ready to rock" '<%= ref("WaitHandle") %>'
112
+ ```
113
+
114
+ The user_data helper will transform the bash script into a json array of elements for Cloud Formation:
115
+
116
+ ```json
117
+ [
118
+ "#!/bin/bash -lexv\n",
119
+ "\n",
120
+ "HOSTNAME_PREFIX=",
121
+ {
122
+ "Fn::FindInMap": [
123
+ "EnvironmentMapping",
124
+ "HostnamePrefix",
125
+ {
126
+ "Ref": "Environment"
127
+ }
128
+ ]
129
+ },
130
+ "\n",
131
+ "\n",
132
+ "echo ",
133
+ {
134
+ "Ref": "AWS::StackName"
135
+ },
136
+ " > /tmp/stack_name\n",
137
+ "# Helper function\n",
138
+ "function error_exit\n",
139
+ "{\n",
140
+ " /usr/local/bin/cfn-signal -e 1 -r \"$1\" '",
141
+ {
142
+ "Ref": "WaitHandle"
143
+ },
144
+ "'\n",
145
+ "exit 1\n",
146
+ "}\n",
147
+ "# Wait for the EBS volume to show up\n",
148
+ "while [ ! -e /dev/xvdf ]; do echo Waiting for EBS volume to attach; sleep 1; done\n",
149
+ "/bin/mkdir /media/redis\n",
150
+ "/sbin/mkfs -t ext4 /dev/xvdf\n",
151
+ "echo \"/dev/xvdf /media/redis auto defaults 0 0\" >> /etc/fstab\n",
152
+ "/bin/mount /media/redis\n",
153
+ "/usr/bin/redis-cli shutdown\n",
154
+ "sleep 10\n",
155
+ "mv /var/lib/redis/* /media/redis/\n",
156
+ "rm -r /var/lib/redis\n",
157
+ "ln -s /media/redis /var/lib/redis\n",
158
+ "chown -R redis:redis /var/lib/redis\n",
159
+ "chown -R redis:redis /media/redis\n",
160
+ "/usr/bin/redis-server\n",
161
+ "# If all is well so signal success\n",
162
+ "/usr/local/bin/cfn-signal -e $? -r \"Ready to rock\" '",
163
+ {
164
+ "Ref": "WaitHandle"
165
+ },
166
+ "'\n"
167
+ ]
168
+ ```
169
+
170
+ More examples of user_data and instrinic function helper method usage are found in the starter [project template](https://github.com/tongueroo/lono/blob/master/lib/starter_project/templates/user_data/db.sh.erb)
79
171
 
80
172
  ## Converting UserData scripts
81
173
 
data/lib/lono/template.rb CHANGED
@@ -28,19 +28,22 @@ module Lono
28
28
  end
29
29
  end
30
30
 
31
- def partial(path)
31
+ def partial(path,vars={})
32
32
  path = "#{@options[:project_root]}/templates/partial/#{path}"
33
33
  template = IO.read(path)
34
+ variables(vars)
34
35
  ERB.new(template).result(binding)
35
36
  end
36
37
 
37
- def user_data(path)
38
- path = "#{@options[:project_root]}/templates/user_data/#{path}"
39
- template = IO.read(path)
40
- result = ERB.new(template).result(binding)
38
+ def user_data(*paths)
41
39
  output = []
42
- result.split("\n").each do |line|
43
- output += transform(line)
40
+ paths.each do |path|
41
+ path = "#{@options[:project_root]}/templates/user_data/#{path}"
42
+ template = IO.read(path)
43
+ result = ERB.new(template).result(binding)
44
+ result.split("\n").each do |line|
45
+ output += transform(line)
46
+ end
44
47
  end
45
48
  output.to_json
46
49
  end
data/lib/lono/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lono
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -178,7 +178,7 @@
178
178
  },
179
179
  "Type": "AWS::CloudWatch::Alarm"
180
180
  },
181
- <%= partial("host_record.json.erb") %>
181
+ <%= partial("host_record.json.erb", :domain => "mydomain.com") %>
182
182
  "LaunchConfig": {
183
183
  "Properties": {
184
184
  "BlockDeviceMappings": [
@@ -1,6 +1,6 @@
1
1
  "HostRecord": {
2
2
  "Properties": {
3
- "Comment": "DNS name for my stack.",
3
+ "Comment": "DNS name for <%= @domain %>",
4
4
  "HostedZoneName": ".mydomain.net.",
5
5
  "Name": {
6
6
  "Fn::Join": [
@@ -9,7 +9,7 @@
9
9
  {
10
10
  "Ref": "AWS::StackName"
11
11
  },
12
- ".mydomain.net"
12
+ ".<%= @domain %>"
13
13
  ]
14
14
  ]
15
15
  },
@@ -33,7 +33,7 @@
33
33
  "Fn::Base64": {
34
34
  "Fn::Join": [
35
35
  "",
36
- <%= user_data('db.sh.erb') %>
36
+ <%= user_data('db.sh.erb', 'db2.sh.erb') %>
37
37
  ]
38
38
  }
39
39
  }
@@ -0,0 +1 @@
1
+ DB2="test"
@@ -76,6 +76,12 @@ describe Lono do
76
76
  json['Mappings']['AWSRegionArch2AMI']['us-east-1']['64'].should == 'ami-123'
77
77
  end
78
78
 
79
+ it "should make trailing options pass to the partial helper available as instance variables" do
80
+ raw = IO.read("#{@project}/output/prod-api-app.json")
81
+ json = JSON.load(raw)
82
+ json['Resources']['HostRecord']['Properties']['Comment'].should == 'DNS name for mydomain.com'
83
+ end
84
+
79
85
  it "should generate db template" do
80
86
  raw = IO.read("#{@project}/output/prod-api-redis.json")
81
87
  json = JSON.load(raw)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lono
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-06 00:00:00.000000000 Z
12
+ date: 2013-07-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -222,6 +222,7 @@ files:
222
222
  - lib/starter_project/templates/partial/server.json.erb
223
223
  - lib/starter_project/templates/user_data/app.sh.erb
224
224
  - lib/starter_project/templates/user_data/db.sh.erb
225
+ - lib/starter_project/templates/user_data/db2.sh.erb
225
226
  - lono.gemspec
226
227
  - spec/fixtures/cfn.json
227
228
  - spec/lib/lono_spec.rb