Makes the image registration/upload an LWRP

Adds a slightly modified version of the RCBOPS glance_image
LWRP. This cleans up a bunch of stuff in the glance::api
recipe and makes it much easier to read and diagnose.
This commit is contained in:
Jay Pipes 2013-04-12 11:44:36 -04:00
parent 3c8472dcd9
commit f4f9fb5412
5 changed files with 177 additions and 82 deletions

View File

@ -40,27 +40,6 @@ Swift
"image_upload": true
}
Cloud Files
-----------
"glance": {
"api": {
"default_store": "swift",
"swift_store_user": "<Cloud Files Tenant ID>:<Rackspace Cloud Files Username>",
"swift_store_key": "<Rackspace Cloud Password>",
"swift_store_auth_version": "2",
"swift_store_auth_address": "https://identity.api.rackspacecloud.com/v2.0"
},
"images": [
"cirros"
],
"image_upload": true
}
To obtain your Cloud Files Tenant ID use the following:
curl -s -X POST https://identity.api.rackspacecloud.com/v2.0/tokens -d '{"auth": {"passwordCredentials": {"username": "<Rackspace Cloud User Name>", "password": "<Rackspace Cloud Password"}}}' -H "Content-type: application/json" | python -mjson.tool | grep "tenantId.*Mosso" | head -1
Requirements
============
@ -69,8 +48,8 @@ Chef 0.10.0 or higher required (for Chef environment use)
Platform
--------
* Ubuntu-12.04
* Fedora-17
* Ubuntu-12.04+
* Fedora-17+
Cookbooks
---------
@ -80,27 +59,40 @@ The following cookbooks are dependencies:
* database
* keystone
* mysql
* openssl
* openstack-common
=======
Providers
=========
`image` (`:action` `:upload`
- `:image_url`: Location of the image to be loaded into Glance.
- `:image_name`: A name for the image.
- `:image_type`: `qcow2` or `ami`. Defaults to `qcow2`.
- `:keystone_user`: Username of the Keystone admin user.
- `:keystone_pass`: Password for the Keystone admin user.
- `:keystone_tenant`: Name of the Keystone admin user's tenant.
- `:keystone_uri`: URI of the Identity API endpoint.
Recipes
=======
default
-------
-Includes recipes `api`, `registry`
api
------
-Installs the glance-api server
registry
--------
-Includes recipe `mysql:client`
-Installs the glance-registry server
keystone-registration
---------------------
- Registers the API endpoint and glance service Keystone user
db
--
- Creates the Glance registry database
Attributes
==========

View File

@ -3,7 +3,7 @@ maintainer "Opscode, Inc."
license "Apache 2.0"
description "Installs and configures the Glance Image Registry and Delivery Service"
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "2012.2.2"
version "2012.2.3"
recipe "glance::api", "Installs packages required for a glance api server"
recipe "glance::registry", "Installs packages required for a glance registry server"
recipe "glance::db", "Creates the Glance registry database"

35
providers/image.rb Normal file
View File

@ -0,0 +1,35 @@
#
# Cookbook Name:: glance
# Resource:: image
#
# Copyright 2012, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
actions :upload
# In earlier versions of Chef the LWRP DSL doesn't support specifying
# a default action, so you need to drop into Ruby.
def initialize(*args)
super
@action = :upload
end
attribute :image_url, :kind_of => String
attribute :image_type, :kind_of => String, :default => "unknown", :equal_to => ["unknown", "ami", "qcow"]
attribute :image_name, :kind_of => String, :default => "default"
attribute :keystone_user, :kind_of => String
attribute :keystone_pass, :kind_of => String
attribute :keystone_tenant, :kind_of => String
attribute :keystone_uri, :kind_of => String

View File

@ -4,6 +4,7 @@
#
# Copyright 2012, Rackspace US, Inc.
# Copyright 2012, Opscode, Inc.
# Copyright 2013, AT&T
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -19,7 +20,6 @@
#
require "uri"
require "chef/mixin/shell_out"
class ::Chef::Recipe
include ::Openstack
@ -224,58 +224,16 @@ template "/etc/glance/glance-scrubber-paste.ini" do
mode 00644
end
# TODO(jaypipes) Turn the below into an LWRP
if node["glance"]["image_upload"]
insecure = node["openstack"]["auth"]["validate_certs"] ? "" : " --insecure"
glance_cmd = "glance#{insecure} -I #{service_user} -K #{service_pass} -T #{service_tenant_name} -N #{auth_uri}"
current_images = Chef::Mixin::ShellOut.new("#{glance_cmd} image-list | grep active | awk '{print $4}'").run_command
image_list = current_images.stdout.split( /\n/ )
Chef::Log.info("Current images in glance are #{image_list.join(', ')}")
node["glance"]["images"].each do |img|
Chef::Log.info("Checking to see if #{img.to_s}-image should be uploaded.")
if !image_list.include? "#{img.to_s}-image"
Chef::Log.info("Adding #{img.to_s}-image to glance")
bash "default image setup for #{img.to_s}" do
cwd "/tmp"
user "root"
case File.extname(node["glance"]["image"][img.to_sym])
when ".gz", ".tgz"
code <<-EOH
set -e
set -x
mkdir -p images/#{img.to_s}
cd images/#{img.to_s}
curl -L #{node["glance"]["image"][img.to_sym]} | tar -zx
image_name=$(basename #{node["glance"]["image"][img]} .tar.gz)
image_name=${image_name%-multinic}
kernel_file=$(ls *vmlinuz-virtual | head -n1)
if [ ${#kernel_file} -eq 0 ]; then
kernel_file=$(ls *vmlinuz | head -n1)
fi
ramdisk=$(ls *-initrd | head -n1)
if [ ${#ramdisk} -eq 0 ]; then
ramdisk=$(ls *-loader | head -n1)
fi
kernel=$(ls *.img | head -n1)
kid=$(#{glance_cmd} image-create --name="${image_name}-kernel" --is-public=true --disk-format=aki --container-format=aki < ${kernel_file} | cut -d: -f2 | sed 's/ //')
rid=$(#{glance_cmd} image-create --name="${image_name}-initrd" --is-public=true --disk-format=ari --container-format=ari < ${ramdisk} | cut -d: -f2 | sed 's/ //')
glance image-create --name="#{img.to_s}-image" --is-public=true --disk-format=ami --container-format=ami --property kernel_id=$kid --property ramdisk_id=$rid < ${kernel}0
EOH
when ".img", ".qcow2"
code <<-EOH
#{glance_cmd} image-create --name="#{img.to_s}-image" --is-public=true --container-format=bare --disk-format=qcow2 --location="#{node["glance"]["image"][img]}"
EOH
end
end
glance_image "Image setup for #{img.to_s}" do
image_url node["glance"]["image"][img.to_sym]
image_name img
keystone_user service_user
keystone_pass service_pass
keystone_tenant service_tenant_name
keystone_uri auth_uri
action :upload
end
end
end

110
resources/image.rb Normal file
View File

@ -0,0 +1,110 @@
#
# Cookbook Name:: glance
# Provider:: image
#
# Copyright 2012, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
action :upload do
@user = new_resource.keystone_user
@pass = new_resource.keystone_pass
@tenant = new_resource.keystone_tenant
@ks_uri = new_resource.keystone_uri
@glance_cmd = "glance --insecure -I #{@user} -K #{@pass} -T #{@tenant} -N #{@ks_uri}"
name = new_resource.image_name
url = new_resource.image_url
type = new_resource.image_type
if type == "unknown"
type = _determine_type(url)
end
_upload_image(type, name, url)
new_resource.updated_by_last_action(true)
end
private
def _determine_type(url)
# Lets do our best to determine the type from the file extension
case ::File.extname(url)
when ".gz", ".tgz"
return "ami"
when ".qcow2", ".img"
return "qcow"
end
end
private
def _upload_image(type, name, url)
case type
when 'ami'
_upload_ami(name, url)
when 'qcow'
_upload_qcow(name, url)
end
end
private
def _upload_qcow(name, url)
c_fmt = "--container-format bare"
d_fmt = "--disk-format qcow2"
bash "Uploading QCOW2 image #{name}" do
cwd "/tmp"
user "root"
code <<-EOH
#{@glance_cmd} image-create --name "#{name}" --is-public true #{c_fmt} #{d_fmt} --location "#{url}"
EOH
not_if "#{@glance_cmd} image-list | grep #{name.to_s}"
end
end
private
def _upload_ami(name, url)
aki_fmt = "--container-format aki --disk-format aki"
ari_fmt = "--container-format ari --disk-format ari"
ami_fmt = "--container-format ami --disk-format ami"
bash "Uploading AMI image #{name}" do
cwd "/tmp"
user "root"
code <<-EOH
set -x
mkdir -p images/#{name}
cd images/#{name}
curl -L #{url} | tar -zx
image_name=$(basename #{url} .tar.gz)
image_name=${image_name%-multinic}
kernel_file=$(ls *vmlinuz-virtual | head -n1)
if [ ${#kernel_file} -eq 0 ]; then
kernel_file=$(ls *vmlinuz | head -n1)
fi
ramdisk=$(ls *-initrd | head -n1)
if [ ${#ramdisk} -eq 0 ]; then
ramdisk=$(ls *-loader | head -n1)
fi
kernel=$(ls *.img | head -n1)
kid=$(#{@glance_cmd} image-create --name "${image_name}-kernel" --is-public true #{aki_fmt} < ${kernel_file} | cut -d: -f2 | sed 's/ //')
rid=$(#{@glance_cmd} image-create --name "${image_name}-initrd" --is-public true #{ari_fmt} < ${ramdisk} | cut -d: -f2 | sed 's/ //')
#{@glance_cmd} image-create --name "#{name}" --is-public true #{ami_fmt} --property "kernel_id=$kid" --property "ramdisk_id=$rid" < ${kernel}
EOH
not_if "#{@glance_cmd} image-list | grep #{name.to_s}"
end
end