Skip to content

Commit fa1eb2c

Browse files
author
Jeff Mendoza
committed
Initial commit.
0 parents  commit fa1eb2c

File tree

11 files changed

+568
-0
lines changed

11 files changed

+568
-0
lines changed

README.md

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
Description
2+
===========
3+
4+
This cookbook provides resources and providers to create an manage
5+
Windows Azure components. Currently supported resources are:
6+
7+
* Storage Accounts ('azure_storage_account')
8+
* Blob Storage Containers ('azure_storage_container')
9+
* SQL Azure Servers ('azure_sql_db_server')
10+
11+
**Note** This cookbook uses the `azure` RubyGem to interact with the
12+
Azure API. This gem requires `nokogiri` which requires compiling
13+
native extensions, which means build tools are required.
14+
15+
Requirements
16+
============
17+
18+
Requires Chef 0.7.10 or higher for Lightweight Resource and Provider
19+
support. Chef 0.8+ is recommended. While this cookbook can be used in
20+
`chef-solo` mode, to gain the most flexibility, we recommend using
21+
`chef-client` with a Chef Server.
22+
23+
A Windows Azure account is required. The Management Certificate and
24+
Subscriptoin ID are used to authenticate with Azure.
25+
26+
Azure Credentials
27+
===============
28+
29+
In order to manage Azure components, authentication credentials need
30+
to be available to the node. There are a number of ways to handle
31+
this, such as node attributes or roles. We recommend storing these in
32+
a databag (Chef 0.8+), and loading them in the recipe where the
33+
resources are needed.
34+
35+
DataBag recommendation:
36+
37+
% knife data bag show azure main
38+
{
39+
"id": "main",
40+
"management_certificate": "YOUR PEM FILE CONTENTS",
41+
"subscription_id": "YOUR SUBSCRIPTION ID"
42+
}
43+
44+
This can be loaded in a recipe with:
45+
46+
azure = data_bag_item("azure", "main")
47+
48+
And to access the values:
49+
50+
azure['management_certificate']
51+
azure['subscription_id']
52+
53+
We'll look at specific usage below.
54+
55+
Recipes
56+
=======
57+
58+
default.rb
59+
----------
60+
61+
The default recipe installs the `azure` RubyGem, which this cookbook
62+
requires in order to work with the Azure API. Make sure that the azure
63+
recipe is in the node or role `run_list` before any resources from
64+
this cookbook are used.
65+
66+
"run_list": [
67+
"recipe[azure]"
68+
]
69+
70+
The `gem_package` is created as a Ruby Object and thus installed
71+
during the Compile Phase of the Chef run.
72+
73+
Resources and Providers
74+
=======================
75+
76+
This cookbook provides three resources and corresponding providers.
77+
78+
## storage_account.rb
79+
80+
81+
Manage Azure Storage Accounts with this resource.
82+
83+
Actions:
84+
85+
* `create` - create a new storage account
86+
* `delete` - delete the specified storage account
87+
88+
Attribute Parameters:
89+
90+
* `management_certificate` - PEM file contents of Azure management
91+
certificate, required.
92+
* `subscription_id` - ID of Azure subscription, required.
93+
* `management_endpoint` - Endpoint for Azure API, defaults to
94+
`management.core.windows.net`.
95+
* `location` - Azure location to create storate account. Either
96+
location or affinity group are required.
97+
* `affinity_group_name` - Affinity group to create account in. Either
98+
location or affinity group are required.
99+
* `geo_replication_enabled` - True or false, defaults to true.
100+
101+
## storage_container.rb
102+
103+
Manage Azure Blob Containers with this resource
104+
105+
Actions:
106+
107+
* `create` - create a new container
108+
* `delete` - delete the specified container
109+
110+
Attribute Parameters:
111+
112+
* `storage_account` - Account to create container in, required.
113+
* `access_key` - Access key for storage account, required.
114+
115+
## sql_db_server.rb
116+
117+
Actions:
118+
119+
* `create` - create a new server. Use the Azure location as the `name`
120+
of the storage account. The server name is autogenerated.
121+
122+
Attribute Parameters:
123+
124+
* `management_certificate` - PEM file contents of Azure management
125+
certificate, required.
126+
* `subscription_id` - ID of Azure subscription, required.
127+
* `management_endpoint` - Endpoint for Azure API, defaults to
128+
`management.database.windows.net`.
129+
* `login` - Desired admin login for db server, required.
130+
* `password` - Desired admin password for db server, required.
131+
* `server_name` - This attribute is set by the provider, and can be
132+
used by consuming recipies.
133+
134+
Usage
135+
=====
136+
137+
The following examples assume that the recommended data bag item has
138+
been created and that the following has been included at the top of
139+
the recipe where they are used.
140+
141+
include_recipe "azure"
142+
azure = data_bag_item("azure", "main")
143+
144+
## azure_storage_accouint
145+
146+
This will create an account named `new-account` in the `West US`
147+
location.
148+
149+
azure_storage_account 'new-account' do
150+
management_certificate azure['management_certificate']
151+
subscription_id azure['subscription_id']
152+
location 'West US'
153+
action :create
154+
end
155+
156+
This will create an account named `new-account` in the existing
157+
`my-ag` affinity group.
158+
159+
azure_storage_account 'new-account' do
160+
management_certificate azure['management_certificate']
161+
subscription_id azure['subscription_id']
162+
affinity_group_name 'my-ag'
163+
action :create
164+
end
165+
166+
## azure_storage_container
167+
168+
This will create a container named `my-node` within the storage
169+
account `my-account`.
170+
171+
azure_storage_container 'my-node' do
172+
storage_account 'my-account'
173+
access_key azure['access_key']
174+
action :create
175+
end
176+
177+
## azure_sql_db_server
178+
179+
This will create a db server in the location `West US` with the login
180+
`admin` and password `password`.
181+
182+
azure_sql_db_server 'West US' do
183+
management_certificate azure['management_certificate']
184+
subscription_id azure['subscription_id']
185+
login 'admin'
186+
password 'password'
187+
action :create
188+
end
189+
190+
Here is an example of how you might retrieve the generated server
191+
name.
192+
193+
file '/etc/db_server_info' do
194+
content lazy {
195+
db2 = resources("azure_sql_db_server[West US]")
196+
"Url: https://#{db2.server_name}.database.windows.net"
197+
}
198+
mode 0600
199+
action :create
200+
end
201+
202+
203+
License and Author
204+
==================
205+
206+
* Author:: Jeff Mendoza (<[email protected]>)
207+
208+
Copyright (c) Microsoft Open Technologies, Inc.
209+
210+
Licensed under the Apache License, Version 2.0 (the "License");
211+
you may not use this file except in compliance with the License.
212+
You may obtain a copy of the License at
213+
214+
http://www.apache.org/licenses/LICENSE-2.0
215+
216+
Unless required by applicable law or agreed to in writing, software
217+
distributed under the License is distributed on an "AS IS" BASIS,
218+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
219+
See the License for the specific language governing permissions and
220+
limitations under the License.

attributes/default.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Author Jeff Mendoza ([email protected])
2+
#-------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Open Technologies, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
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+
default['azure']['azure_gem_version'] = "0.6.0"

libraries/azure.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Author Jeff Mendoza ([email protected])
2+
#-------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Open Technologies, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
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 Azure
18+
module Cookbook
19+
20+
def setup_management_service
21+
begin
22+
require 'azure'
23+
rescue LoadError
24+
Chef::Log.error("Missing gem 'azure'. Use the default azure recipe to install it first.")
25+
end
26+
mc = Tempfile.new(['mc', '.pem'])
27+
mc.chmod(0600)
28+
mc.write(new_resource.management_certificate)
29+
mc.close
30+
Azure.configure do |config|
31+
config.management_certificate = mc.path
32+
config.subscription_id = new_resource.subscription_id
33+
config.management_endpoint = new_resource.management_endpoint
34+
end
35+
mc
36+
end
37+
38+
def setup_storage_service
39+
begin
40+
require 'azure'
41+
rescue LoadError
42+
Chef::Log.error("Missing gem 'azure'. Use the default azure recipe to install it first.")
43+
end
44+
Azure.configure do |config|
45+
config.storage_account_name = new_resource.storage_account
46+
config.storage_access_key = new_resource.access_key
47+
end
48+
end
49+
50+
end
51+
end

metadata.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Author Jeff Mendoza ([email protected])
2+
#-------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Open Technologies, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
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+
name "azure"
18+
maintainer "Microsoft Open Technologies, Inc."
19+
maintainer_email "[email protected]"
20+
license "Apache 2.0"
21+
description "LWRPs for managing Azure resources"
22+
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
23+
version "0.1.0"
24+
recipe "azure", "Installs the azure gem during compile time"

providers/sql_db_server.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Author Jeff Mendoza ([email protected])
2+
#-------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Open Technologies, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
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+
include Azure::Cookbook
18+
19+
action :create do
20+
mc = setup_management_service
21+
22+
sms = Azure::SqlDatabaseManagementService.new
23+
24+
locs = []
25+
sms.list_servers.each { |srv| locs.push(srv.location) }
26+
27+
if locs.include?(new_resource.location)
28+
Chef::Log.debug("DB in #{new_resource.location} already exists.")
29+
sms.list_servers.each do |srv|
30+
if srv.location == new_resource.location
31+
@new_resource.server_name(srv.name)
32+
end
33+
end
34+
else
35+
Chef::Log.debug("Creating DB in #{new_resource.location}.")
36+
server = sms.create_server(new_resource.login, new_resource.password, new_resource.location)
37+
@new_resource.server_name(server.name)
38+
Chef::Log.debug("Created DB #{server.name}.")
39+
sms.set_sql_server_firewall_rule(server.name, 'chef-node')
40+
end
41+
mc.unlink
42+
end

0 commit comments

Comments
 (0)