image_optimize 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ # Author: cary@rightscale.com
2
+ # Copyright 2014 RightScale, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ module ImageOptimize
18
+ module Command
19
+
20
+ # Returns Process::Status and output
21
+ def execute(command, quiet=false)
22
+ debug "COMMAND: #{command}"
23
+ output = ""
24
+ IO.popen("#{command} 2>&1") do |data|
25
+ while line = data.gets
26
+ puts line if !quiet
27
+ output << line
28
+ end
29
+ end
30
+ process_status = $?
31
+ debug "OUTPUT: #{output}"
32
+ return process_status, output
33
+ end
34
+
35
+ def debug(message)
36
+ @log.debug message if @log.respond_to?(:debug)
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ # Author: cary@rightscale.com
2
+ # Copyright 2014 RightScale, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ module ImageOptimize
18
+ module Common
19
+ def not_implemented
20
+ raise "not implemented"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,189 @@
1
+ #!/bin/bash -ex
2
+ #
3
+ # Author: cary@rightscale.com
4
+ # Copyright 2014 RightScale, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ #
19
+ # $API_USER_EMAIL - RightScale Dashboard User email
20
+ # $API_USER_PASSWORD - Rightscale Dashboard Password
21
+ #
22
+ # $AWS_ACCESS_KEY - EC2 Account Access Key
23
+ # $AWS_SECRET_KEY - EC2 Account Secret
24
+ # $AWS_ACCOUNT_NUMBER - EC2 Account ID
25
+ #
26
+ # $CACHED_IMAGE_PREFIX - Prefix to generated images name to help search for cached images
27
+ # $CACHED_IMAGE_DESCRIPTION - Description to add to cached images
28
+ #
29
+ # $KERNEL_ID_OVERRIDE - Kernel to use instead of what the VM is running. i.e. "aki-fc8f11cc"
30
+ # For a current list of IDs see http://goo.gl/dOS0mB
31
+ #
32
+ # $AWS_S3_BUNDLE_DIRECTORY - The local directory where the image bundle will be stored before uploading to S3.
33
+ # NOTE: this must have enough free space to hold the image bundle.
34
+ # Default: "/mnt/ephemeral/bundle"
35
+ #
36
+ # $AWS_S3_BUNDLE_NO_FILTER - If set, will disable the default filtering used by the ec2-bundle-vol command.
37
+ # WARNING: setting this option could leave ssh keys or other secrets on your image.
38
+ #
39
+
40
+ # source the profile
41
+ source /etc/profile
42
+
43
+ # update CA certs
44
+ # fixes intermediate "SSL certificate problem" errors from curl
45
+ # See https://forums.aws.amazon.com/thread.jspa?messageID=341463&#341463
46
+ #
47
+ update-ca-certificates
48
+
49
+
50
+ # install right_api_client gem (if not installed) into RightLink Sandbox
51
+ #
52
+ sandbox_bin=/opt/rightscale/sandbox/bin
53
+ gem_bin=$sandbox_bin/gem
54
+ if ! $gem_bin list right_api_client --installed ; then
55
+ echo "Installing right_api_client gem..."
56
+ $gem_bin install right_api_client --no-rdoc --no-ri
57
+ fi
58
+
59
+ if ! $gem_bin list trollop --installed ; then
60
+ echo "Installing trollop gem..."
61
+ $gem_bin install trollop --no-rdoc --no-ri
62
+ fi
63
+
64
+ # install EC2 tools
65
+ #
66
+ tools_dir=/tmp/ec2tools
67
+ if [ ! -d $tools_dir ]; then
68
+ mkdir -p $tools_dir
69
+
70
+ # install EC2 API tools
71
+ #
72
+ if [ ! -d $tools_dir/ec2-api-tools-1.6.13.0 ]; then
73
+ unzip $ATTACH_DIR/ec2-api-tools-1.6.13.0.zip -d $tools_dir
74
+ fi
75
+
76
+ # install EC2 AMI tools
77
+ #
78
+ if [ ! -d $tools_dir/ec2-ami-tools-1.5.0.0 ]; then
79
+ unzip $ATTACH_DIR/ec2-ami-tools-1.5.0.0.zip -d $tools_dir
80
+ fi
81
+
82
+ # install ec2 tools package dependencies
83
+ apt-get -y install grub kpartx gdisk
84
+ fi
85
+
86
+ # setup ec2 tools env
87
+ #
88
+ export EC2_HOME=$tools_dir/ec2-api-tools-1.6.13.0
89
+ export PATH=$tools_dir/ec2-api-tools-1.6.13.0/bin:${PATH}
90
+ export EC2_AMITOOL_HOME=$tools_dir/ec2-ami-tools-1.5.0.0
91
+ export PATH=$tools_dir/ec2-ami-tools-1.5.0.0/bin:${PATH}
92
+
93
+ # unpack attached tarred gem
94
+ #
95
+ if [ -d "$ATTACH_DIR" ]; then
96
+ echo "Unpacking attached tarred gem..."
97
+ cd $ATTACH_DIR
98
+ tar xvf image_optimize*.tar
99
+ chmod +x bin/image_optimize
100
+ fi
101
+
102
+ # setup S3 bundle creds (unless EBS)
103
+ #
104
+ opts="--aws-image-type $IMAGE_TYPE"
105
+ if [ "$IMAGE_TYPE" != "EBS" ]; then
106
+ echo "Setting up S3 bundle creds..."
107
+ export certs_dir=/tmp/certs
108
+
109
+ # set default bucket name for cached images (must be url safe)
110
+ # (TODO: pass this as command-line options)
111
+ if [ -n "$AWS_S3_IMAGE_BUCKET" ]; then
112
+ opts=$opts" --aws-s3-image-bucket $AWS_S3_IMAGE_BUCKET"
113
+ fi
114
+
115
+ # set required S3 environment variables
116
+ key_path=$certs_dir/x509.key
117
+ cert_path=$certs_dir/x509.cert
118
+
119
+ opts="$opts --aws-s3-key-path $key_path --aws-s3-cert-path $cert_path"
120
+
121
+ # write x.509 creds to disk
122
+ mkdir -p $certs_dir
123
+ set +x # don't echo these commands!
124
+ cat <<EOF>$key_path
125
+ $AWS_X509_KEY
126
+ EOF
127
+ cat <<EOF>$cert_path
128
+ $AWS_X509_CERT
129
+ EOF
130
+
131
+ # The local directory where the image bundle will be stored before uploading to S3.
132
+ # NOTE: this must have enough free space to hold the image bundle.
133
+ # Default: "/mnt/ephemeral/bundle"
134
+ if [ -n "$AWS_S3_BUNDLE_DIRECTORY" ]; then
135
+ opts=$opts" --aws-s3-bundle-directory $AWS_S3_BUNDLE_DIRECTORY"
136
+ fi
137
+
138
+ # If set, will disable the default filtering used by the ec2-bundle-vol command.
139
+ # WARNING: setting this option could leave ssh keys or other secrets on your image.
140
+ if [ -n "$AWS_S3_BUNDLE_NO_FILTER" ]; then
141
+ opts=$opts" --aws-s3-bundle-no-filter"
142
+ fi
143
+
144
+ fi
145
+
146
+ # enable debugging (if specified)
147
+ # WARNING: will write ec2 creds to log if set!!
148
+ if [ -n "$IMAGE_OPTIMIZE_DEBUG" ]; then
149
+ opts=$opts" --verbose"
150
+ fi
151
+
152
+ # If set, don't clean up some state files. This will allow the script to be
153
+ # rerun on the same instance multiple times which is useful for debugging.
154
+ # NOTE: Boot issues may occur if state is not properly cleaned up.
155
+ if [ -n "$DISABLE_CLEANUP" ]; then
156
+ opts=$opts" --no-cleanup"
157
+ fi
158
+
159
+ # Prefix to generated images name to help search for cached images
160
+ if [ -n "$CACHED_IMAGE_PREFIX" ]; then
161
+ opts=$opts" --prefix $CACHED_IMAGE_PREFIX"
162
+ fi
163
+
164
+ # Description to add to cached images
165
+ if [ -n "$CACHED_IMAGE_DESCRIPTION" ]; then
166
+ opts=$opts" --description $CACHED_IMAGE_DESCRIPTION"
167
+ fi
168
+
169
+ # Kernel to use instead of what the VM is running. i.e. "aki-fc8f11cc"
170
+ # For a current list of IDs see http://goo.gl/dOS0mB
171
+ if [ -n "$KERNEL_ID_OVERRIDE" ]; then
172
+ opts=$opts" --aws-kernel-id $KERNEL_ID_OVERRIDE"
173
+ fi
174
+
175
+ # run optimizer
176
+ #
177
+ echo "Running image_optimize utility..."
178
+ cmd="$sandbox_bin/ruby bin/image_optimize $opts"
179
+ echo "COMMAND: $cmd"
180
+ eval $cmd
181
+
182
+ # clean up creds after S3 bundling
183
+ #
184
+ if [ "$IMAGE_TYPE" != "EBS" ]; then
185
+ echo "clean up creds for S3 bundling"
186
+ rm -rf $certs_dir
187
+ fi
188
+
189
+ exit 0
data/setup.sh ADDED
@@ -0,0 +1,40 @@
1
+ #!/bin/bash -e x
2
+
3
+ apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5
4
+
5
+ # This only works for Ubuntu rightnow
6
+ # Anyone want to write a Chef recipe?
7
+ apt-get update
8
+
9
+ # install ec2 tools package dependencies
10
+ apt-get -y install grub kpartx gdisk
11
+
12
+ # update CA certs
13
+ # fixes intermediate "SSL certificate problem" errors from curl
14
+ # See https://forums.aws.amazon.com/thread.jspa?messageID=341463&#341463
15
+ #
16
+ update-ca-certificates
17
+
18
+ # setup ruby env
19
+ apt-get -y install ruby1.9.1
20
+ apt-get -y install ruby1.9.1-dev
21
+ update-alternatives --set ruby /usr/bin/ruby1.9.1
22
+ update-alternatives --set gem /usr/bin/gem1.9.1
23
+ gem install bundler --no-rdoc --no-ri
24
+ bundle
25
+
26
+ export TOOLS_DIR=/tmp/ec2tools
27
+ mkdir -p $TOOLS_DIR
28
+
29
+ # install EC2 tools v1.5 or greater
30
+ wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools-1.6.13.0.zip
31
+ unzip ec2-api-tools-1.6.13.0.zip -d $TOOLS_DIR
32
+ wget http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools-1.5.0.0.zip
33
+ unzip ec2-ami-tools-1.5.0.0.zip -d $TOOLS_DIR
34
+
35
+ # setup ec2 tools env
36
+ #
37
+ export EC2_HOME=$TOOLS_DIR/ec2-api-tools-1.6.13.0
38
+ export PATH=$TOOLS_DIR/ec2-api-tools-1.6.13.0/bin:${PATH}
39
+ export EC2_AMITOOL_HOME=$TOOLS_DIR/ec2-ami-tools-1.5.0.0
40
+ export PATH=$TOOLS_DIR/ec2-ami-tools-1.5.0.0/bin:${PATH}
@@ -0,0 +1,39 @@
1
+ # Author: cary@rightscale.com
2
+ # Copyright 2014 RightScale, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'api_client_15'))
18
+
19
+ describe "API 1.5 Client Creator" do
20
+
21
+ before(:each) do
22
+ RightScale::ApiClient15.any_instance.stub(:parse_user_data)
23
+ apiStub = double("RightApi::Client", :log => true)
24
+ RightApi::Client.should_receive(:new).and_return(apiStub)
25
+ end
26
+
27
+ it "creates an instance facing api connection" do
28
+ helper = RightScale::ApiClient15.new
29
+ helper.get_client(:instance)
30
+ end
31
+
32
+ it "creates an user api connection" do
33
+ ENV['API_USER_EMAIL'] = "somebody"
34
+ ENV['API_USER_PASSWORD'] = "somepassword"
35
+ helper = RightScale::ApiClient15.new
36
+ helper.get_client(:user)
37
+ end
38
+
39
+ end
@@ -0,0 +1,43 @@
1
+ # Author: cary@rightscale.com
2
+ # Copyright 2014 RightScale, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'mixins', 'command'))
18
+
19
+ describe ImageOptimize::Command do
20
+
21
+ class TestObj
22
+ include ImageOptimize::Command
23
+ def initialize
24
+ @log = Logger.new(STDOUT)
25
+ end
26
+ end
27
+
28
+ before(:each) do
29
+ Logger.any_instance.stub(:debug)
30
+ end
31
+
32
+ it "runs a child process" do
33
+ return_val, output = TestObj.new.execute("ls -l", true)
34
+ return_val.exitstatus.should == 0
35
+ end
36
+
37
+ it "logs debug message" do
38
+ Logger.any_instance.stub(:debug)
39
+ obj = TestObj.new
40
+ obj.debug("hey now")
41
+ end
42
+
43
+ end
@@ -0,0 +1,87 @@
1
+ # Author: cary@rightscale.com
2
+ # Copyright 2014 RightScale, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'image_bundle'))
18
+
19
+ describe ImageOptimize::ImageBundleEc2Base do
20
+
21
+ before(:each) do
22
+ # dont log stuff
23
+ Logger.any_instance.stub(:info)
24
+ Logger.any_instance.stub(:debug)
25
+
26
+ ImageOptimize::ImageBundleEc2Base.any_instance.stub(:load_meta_data)
27
+ @image_bundler = ImageOptimize::ImageBundleEc2Base.new(
28
+ "instance_client",
29
+ "api_client",
30
+ "aws_access_key",
31
+ "aws_secret_key"
32
+ )
33
+ end
34
+
35
+ # skip this test on a cloud instance -- since there is a metadata file
36
+ it "fails if metadata not found" do
37
+ unless File.exists?('/var/spool/cloud/meta-data-cache.rb')
38
+ lambda{ImageOptimize::ImageBundleEc2Base.new("instance_client", "api_client")}.should raise_error
39
+ end
40
+ end
41
+
42
+ it "registers image" do
43
+ @image_bundler.should_receive(:register_command)
44
+ status = double("ProcessStatus", :exitstatus => 0)
45
+ @image_bundler.should_receive(:execute).and_return([status, "Using AWS acces key \n IMAGE ami-64b5c054 "])
46
+ @image_bundler.should_receive(:wait_for_image)
47
+ @image_bundler.register_image("name", "description")
48
+ end
49
+
50
+ it "parsed ami" do
51
+ ami = "ami-64b5c054"
52
+ fake_output = "Using AWS acces key \n IMAGE #{ami} "
53
+ @image_bundler.send(:parse_ami, fake_output).should == ami
54
+ end
55
+
56
+ it "raise excaption on failure" do
57
+ @image_bundler.should_receive(:register_command)
58
+ status = double("ProcessStatus", :exitstatus => 1)
59
+ lambda{@image_bundler.register_image("name", "description")}.should raise_error
60
+ end
61
+
62
+ it "returns true if using a debug log level" do
63
+ @image_bundler.send(:debug_mode?).should == true
64
+ end
65
+
66
+ it "returns false if not using a debug log level" do
67
+ log = Logger.new(STDOUT)
68
+ log.level = Logger::INFO
69
+ @image_bundler.log(log)
70
+ @image_bundler.send(:debug_mode?).should == false
71
+ end
72
+
73
+ # def register_image(name=nil, description=nil)
74
+ # cmd = register_command(name, description)
75
+ # @log.info "Running register image command..."
76
+ # unless @dry_run
77
+ # status, cmd_output = execute(cmd)
78
+ # raise "FATAL: unable to register new image" unless status.exitstatus == 0
79
+ # image_uuid = cmd_output.sub("IMAGE","").strip
80
+ # @log.info "New image: #{image_uuid}"
81
+ #
82
+ # # wait for image to be available
83
+ # wait_for_image(image_uuid)
84
+ # end
85
+ # end
86
+
87
+ end