knife-google 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,96 @@
1
+ = Knife Google
2
+
3
+ = DESCRIPTION:
4
+
5
+ This is the official Opscode Knife plugin for Google. This plugin gives knife the ability to create, bootstrap, and manage servers on the Google Cloud.
6
+
7
+ = INSTALLATION (LINUX/OSX):
8
+ Be sure you are running the latest version Chef. Versions earlier than 0.10.0 don't support plugins:
9
+
10
+ gem install chef
11
+
12
+ This plugin depends on the gcutil CLI utility
13
+ pip install https://dl.google.com/dl/compute/gcutil.tar.gz
14
+
15
+ This plugin is distributed as a Ruby Gem. To build and install it, run:
16
+
17
+ gem build knife-google
18
+ gem install knife-google
19
+
20
+ Alternatively, the rake utility installs the gcutil CLI utility as part of installation
21
+
22
+ rake install
23
+
24
+ = INSTALLATION (WINDOWS):
25
+ Be sure you are running the latest version Chef. Versions earlier than 0.10.0 don't support plugins:
26
+
27
+ Follow these instructions to install Chef-Workstation on Windows: http://wiki.opscode.com/display/chef/Workstation+Setup+for+Windows
28
+
29
+ The gcutil tool can be installed on Windows, via Cygwin.
30
+
31
+ Install Cygwin with Python Environment: https://developers.google.com/compute/docs/gcutil_setup
32
+
33
+ In Windows, set CYGWINPATH environment variable to point the Cygwin Installation and add %CYGWINPATH%\\bin to the PATH environment variable
34
+
35
+ set CYGWINPATH=<CYGWIN INSTALLATION PATH>
36
+ set PATH=%CYGWINPATH%\\bin;%PATH%
37
+
38
+ Use the rake utility to install gcutil CLI utility and the knife plugin
39
+
40
+ rake install
41
+
42
+ = CONFIGURATION:
43
+ Run gcutil auth to request a token. This command prints a URL where you can acquire an OAuth 2.0 refresh token for Google Compute.
44
+
45
+ gcutil auth --project_id=<your_project_id>
46
+ Go to the following link in your browser:
47
+ <link>
48
+
49
+ Enter verification code:
50
+
51
+
52
+ # provision a new 2 Core 1GB Ubuntu 10.04 webserver
53
+ knife google server create -N <server-name> -i <SSH Identity Key> -k <SSH Public Key> --tcp 22,80,8080
54
+ --udp 10000 --project_id <project-name> --flavor standard-2-cpu-ephemeral-disk
55
+
56
+ Additionally the following options may be set in your `knife.rb`:
57
+
58
+ * flavor
59
+ * image
60
+ * distro
61
+ * template_file
62
+
63
+ = SUBCOMMANDS:
64
+
65
+ This plugin provides the following Knife subcommands. Specific command options can be found by invoking the subcommand with a <tt>--help</tt> flag
66
+
67
+ == knife google server create
68
+
69
+ Provisions a new server in the Google Cloud and then perform a Chef bootstrap (using the SSH protocol). The goal of the bootstrap is to get Chef installed
70
+ on the target system so it can run Chef Client with a Chef Server. The main assumption is a baseline OS installation exists (provided by the provisioning). It is primarily intended for Chef Client systems that talk to a Chef server. By default the server is bootstrapped using the {ubuntu10.04-gems}[https://github.com/opscode/chef/blob/master/chef/lib/chef/knife/bootstrap/ubuntu10.04-gems.erb] template. This can be overridden using the <tt>-d</tt> or <tt>--template-file</tt> command options.
71
+
72
+ == knife google server delete
73
+
74
+ Deletes an existing server in the currently configured Google Cloud account by the server/instance id. You can find the instance id by entering 'knife google server list'. Please note - this does not delete the associated node and client objects from the Chef server.
75
+
76
+ == knife google server list
77
+
78
+ Outputs a list of all servers in the currently configured Google Cloud account. Please note - this shows all instances associated with the account, some of which may not be currently managed by the Chef server.
79
+
80
+ = LICENSE:
81
+
82
+ Author:: Chirag Jog (<chiragj@websym.com>)
83
+ Copyright:: Copyright (c) 2012 Opscode, Inc.
84
+ License:: Apache License, Version 2.0
85
+
86
+ Licensed under the Apache License, Version 2.0 (the "License");
87
+ you may not use this file except in compliance with the License.
88
+ You may obtain a copy of the License at
89
+
90
+ http://www.apache.org/licenses/LICENSE-2.0
91
+
92
+ Unless required by applicable law or agreed to in writing, software
93
+ distributed under the License is distributed on an "AS IS" BASIS,
94
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
95
+ See the License for the specific language governing permissions and
96
+ limitations under the License.
@@ -0,0 +1,100 @@
1
+ #
2
+ # Author:: Chirag Jog (<chiragj@websym.com>)
3
+ # Copyright:: Copyright (c) 2012 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
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
+ require 'stringio'
20
+ require 'yajl'
21
+ require 'mixlib/shellout'
22
+
23
+ $CLI_PREFIX='gcutil'
24
+ class Chef
25
+ class Knife
26
+ module GoogleBase
27
+ @parser = Yajl::Parser.new
28
+ @gcompute = nil
29
+ @cygwin_path = nil
30
+
31
+ def is_platform_windows?
32
+ return RUBY_PLATFORM.scan('w32').size > 0
33
+ end
34
+
35
+ def is_cygwin_installed?
36
+ ENV['CYGWINPATH'] != nil
37
+ end
38
+
39
+ def gcompute
40
+ return if @gcompute
41
+ if is_platform_windows?
42
+ if is_cygwin_installed?
43
+ #Remove extra quotes
44
+ @cygwin_path = ENV['CYGWINPATH'].chomp('\'').reverse.chomp('\'').reverse
45
+ #FIXME Generalize the python binary
46
+ @gcompute="#{@cygwin_path}\\bin\\python2.6.exe #{@cygwin_path}\\bin\\#{$CLI_PREFIX}"
47
+ else
48
+ puts "Cannot Find Cygwin Installation !!! Please set CYGWINPATH to point to the Cygwin installation"
49
+ exit 1
50
+ end
51
+ else
52
+ Chef::Log.debug("Linux Environment")
53
+ @gcompute = $CLI_PREFIX
54
+ end
55
+ end
56
+
57
+ def parser
58
+ if @parser.nil?
59
+ @parser = Yajl::Parser.new
60
+ end
61
+ end
62
+
63
+ def to_json(data)
64
+ data_s = StringIO::new(data.strip)
65
+ parser.parse(data_s) {|obj| return obj}
66
+ end
67
+
68
+ def exec_shell_cmd(cmd)
69
+ if is_platform_windows? and is_cygwin_installed?
70
+ #Change the HOME PATH From Windows to Cygwin
71
+ cygwin_home = "#{@cygwin_path}\\home\\#{ENV['USER']}"
72
+
73
+ #Auth token should exist in either ENV['HOME'] or cygwin_home
74
+ #XXX Find a way to remove the hard-coded file name
75
+ if not File.file?("#{ENV['HOME']}\\.#{$CLI_PREFIX}_auth")
76
+ ENV['HOME'] = cygwin_home
77
+ end
78
+ end
79
+ shell_cmd = Mixlib::ShellOut.new(cmd)
80
+ shell_cmd.run_command
81
+ end
82
+
83
+ def validate_project(project_id)
84
+ cmd = "#{gcompute} getproject --project_id=#{project_id}"
85
+ Chef::Log.debug 'Executing ' + cmd
86
+ getprj = exec_shell_cmd(cmd)
87
+ if getprj.status.exitstatus > 0
88
+ if not getprj.stdout.scan("Enter verification code").empty?
89
+ ui.error("Failed to authenticate the account")
90
+ ui.error("If not authenticated, please Authenticate gcompute to access the Google Compute Cloud")
91
+ ui.error("Authenticate by executing gcutil auth --project_id=<project_id>")
92
+ exit 1
93
+ end
94
+ ui.error("#{getprj.stderr}")
95
+ exit 1
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,261 @@
1
+ # Author:: Chirag Jog (<chiragj@websym.com>)
2
+ # Copyright:: Copyright (c) 2012 Opscode, Inc.
3
+ # License:: Apache License, Version 2.0
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
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'highline'
18
+ require 'net/ssh/multi'
19
+ require 'net/scp'
20
+ require 'tempfile'
21
+
22
+ require 'chef/knife'
23
+ require 'chef/knife/google_base'
24
+
25
+ class Chef
26
+ class Knife
27
+ class GoogleServerCreate < Knife
28
+
29
+ deps do
30
+ require 'readline'
31
+ require 'chef/json_compat'
32
+ require 'chef/knife/bootstrap'
33
+ Chef::Knife::Bootstrap.load_deps
34
+ end
35
+
36
+ include Knife::GoogleBase
37
+
38
+ banner "knife google server create NAME [RUN LIST...] (options)"
39
+
40
+ option :run_list,
41
+ :short => "-r RUN_LIST",
42
+ :long => "--run-list RUN_LIST",
43
+ :description => "Comma separated list of roles/recipes to apply",
44
+ :proc => lambda { |o| o.split(/[\s,]+/) },
45
+ :default => []
46
+
47
+ option :availability_zone,
48
+ :short => "-Z ZONE",
49
+ :long => "--availability-zone ZONE",
50
+ :description => "The Availability Zone",
51
+ :default => "us-east-a",
52
+ :proc => Proc.new { |key| Chef::Config[:knife][:availability_zone] = key }
53
+
54
+ option :distro,
55
+ :short => "-d DISTRO",
56
+ :long => "--distro DISTRO",
57
+ :description => "Bootstrap a distro using a template; default is 'ubuntu10.04-gems'",
58
+ :proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
59
+ :default => "ubuntu10.04-gems"
60
+
61
+ option :template_file,
62
+ :long => "--template-file TEMPLATE",
63
+ :description => "Full path to location of template to use",
64
+ :proc => Proc.new { |t| Chef::Config[:knife][:template_file] = t },
65
+ :default => false
66
+
67
+ option :chef_node_name,
68
+ :short => "-N NAME",
69
+ :long => "--node-name NAME",
70
+ :description => "The Chef node name for your new node",
71
+ :proc => Proc.new { |t| Chef::Config[:knife][:chef_node_name] = t }
72
+
73
+ option :ssh_user,
74
+ :short => "-x USERNAME",
75
+ :long => "--ssh-user USERNAME",
76
+ :description => "The ssh username; default is 'ubuntu'",
77
+ :default => "ubuntu"
78
+
79
+ option :server_name,
80
+ :short => "-N NAME",
81
+ :long => "--server-name NAME",
82
+ :description => "The server name",
83
+ :proc => Proc.new { |server_name| Chef::Config[:knife][:server_name] = server_name }
84
+
85
+ option :flavor,
86
+ :short => "-f FLAVOR",
87
+ :long => "--flavor FLAVOR",
88
+ :description => "The flavor of server (standard-1-cpu,standard-2-cpu-ephemeral-disk, etc)",
89
+ :proc => Proc.new { |f| Chef::Config[:knife][:flavor] = f },
90
+ :default => "standard-1-cpu"
91
+
92
+ option :image,
93
+ :short => "-I IMAGE",
94
+ :long => "--google-image IMAGE",
95
+ :description => "Your google virtual app template/image name",
96
+ :proc => Proc.new { |template| Chef::Config[:knife][:image] = template },
97
+ :default => "gcompute8-standard"
98
+
99
+ option :private_key_file,
100
+ :short => "-i PRIVATE_KEY_FILE",
101
+ :long => "--private-key-file PRIVATE_KEY_FILE",
102
+ :description => "The SSH private key file used for authentication",
103
+ :proc => Proc.new { |identity| Chef::Config[:knife][:private_key_file] = identity }
104
+
105
+ option :public_key_file,
106
+ :short => "-k PUBLIC_KEY_FILE",
107
+ :long => "--public-key-file PUBLIC_KEY_FILE",
108
+ :description => "The SSH public key file used for authentication",
109
+ :proc => Proc.new { |identity| Chef::Config[:knife][:public_key_file] = identity }
110
+
111
+ option :network,
112
+ :short => "-n NETWORKNAME",
113
+ :long => "--network NETWORKNAME",
114
+ :description => "The Network in which to create the Virtual machine",
115
+ :proc => Proc.new { |network| Chef::Config[:knife][:network] = network},
116
+ :default => "default"
117
+
118
+ option :external_ip_address,
119
+ :short => "-e IPADDRESS",
120
+ :long => "--external-ip-address IPADDRESS",
121
+ :description => "A Static IP provided by Google",
122
+ :proc => Proc.new { |ipaddr| Chef::Config[:knife][:external_ip_address] = ipaddr},
123
+ :default => "ephemeral"
124
+
125
+ option :internal_ip_address,
126
+ :short => "-P IPADDRESS",
127
+ :long => "--internal-ip-address IPADDRESS",
128
+ :description => "A Static IP provided by Google",
129
+ :proc => Proc.new { |ipaddr| Chef::Config[:knife][:internal_ip_address] = ipaddr}
130
+
131
+ option :project,
132
+ :short => "-p PROJECT",
133
+ :long => "--project_id PROJECT",
134
+ :description => "Google Compute Project",
135
+ :proc => Proc.new { |project| Chef::Config[:knife][:google_project] = project}
136
+
137
+ def h
138
+ @highline ||= HighLine.new
139
+ end
140
+
141
+ def locate_config_value(key)
142
+ key = key.to_sym
143
+ config[key] || Chef::Config[:knife][key]
144
+ end
145
+
146
+ def tcp_test_ssh(hostname, port)
147
+ tcp_socket = TCPSocket.new(hostname, port)
148
+ readable = IO.select([tcp_socket], nil, nil, 5)
149
+ if readable
150
+ Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
151
+ yield
152
+ true
153
+ else
154
+ false
155
+ end
156
+ rescue Errno::ETIMEDOUT
157
+ false
158
+ rescue Errno::EPERM
159
+ false
160
+ rescue Errno::ECONNREFUSED
161
+ sleep 2
162
+ false
163
+ rescue Errno::EHOSTUNREACH
164
+ sleep 2
165
+ false
166
+ ensure
167
+ tcp_socket && tcp_socket.close
168
+ end
169
+
170
+ def run
171
+ unless Chef::Config[:knife][:server_name]
172
+ ui.error("Server Name is a compulsory parameter")
173
+ exit 1
174
+ end
175
+
176
+ unless Chef::Config[:knife][:public_key_file]
177
+ ui.error("SSH public key file is a compulsory parameter")
178
+ exit 1
179
+ end
180
+
181
+ unless Chef::Config[:knife][:google_project]
182
+ ui.error("Project ID is a compulsory parameter")
183
+ exit 1
184
+ end
185
+
186
+ $stdout.sync = true
187
+
188
+ project_id = Chef::Config[:knife][:google_project]
189
+ validate_project(project_id)
190
+
191
+ server_name = Chef::Config[:knife][:server_name]
192
+ image = Chef::Config[:knife][:image]
193
+ key_file = locate_config_value(:public_key_file)
194
+ network = locate_config_value(:network)
195
+ flavor = locate_config_value(:flavor)
196
+ zone = locate_config_value(:availability_zone)
197
+ user = locate_config_value(:ssh_user)
198
+ external_ip_address = locate_config_value(:external_ip_address)
199
+ internal_ip_address = locate_config_value(:internal_ip_address) || nil
200
+ puts "\n#{ui.color("Waiting for the server to be Instantiated", :magenta)}"
201
+ cmd_add_instance = "#{@gcompute} addinstance #{server_name} --machine_type #{flavor} " +
202
+ "--zone #{zone} --project_id #{project_id} --tags #{server_name} " +
203
+ "--authorized_ssh_keys #{user}:#{key_file} --network #{network} " +
204
+ "--external_ip_address #{external_ip_address} --print_json"
205
+ cmd_add_instance << " --internal_ip_address #{internal_ip_address}" if internal_ip_address
206
+
207
+ Chef::Log.debug 'Executing ' + cmd_add_instance
208
+ create_server = exec_shell_cmd(cmd_add_instance)
209
+
210
+ if create_server.stderr.downcase.scan("error").size > 0
211
+ ui.error("\nFailed to create server: #{create_server.stderr}")
212
+ exit 1
213
+ end
214
+ if create_server.stdout.downcase.scan("error").size > 0
215
+ output = to_json(create_server.stdout)
216
+ ui.error("\nFailed to create server: #{output["error"]}")
217
+ exit 1
218
+ end
219
+
220
+ #Fetch server information
221
+ cmd_get_instance = "#{@gcompute} getinstance #{server_name} --project_id #{project_id} --print_json "
222
+ Chef::Log.debug 'Executing ' + cmd_get_instance
223
+ get_instance = exec_shell_cmd(cmd_get_instance)
224
+
225
+ if not get_instance.stderr.downcase.scan("error").empty?
226
+ ui.error("Failed to fetch server details.")
227
+ exit 1
228
+ end
229
+
230
+ server = to_json(get_instance.stdout)
231
+ private_ip = []
232
+ public_ip = []
233
+ server["networkInterfaces"].each do |interface|
234
+ private_ip << interface["networkIP"]
235
+ interface["accessConfigs"].select { |cfg| public_ip << cfg["natIP"] }
236
+ end
237
+
238
+ puts "#{ui.color("Public IP Address", :cyan)}: #{public_ip[0]}"
239
+ puts "#{ui.color("Private IP Address", :cyan)}: #{private_ip[0]}"
240
+ puts "\n#{ui.color("Waiting for sshd.", :magenta)}"
241
+ puts(".") until tcp_test_ssh(public_ip[0], "22") { sleep @initial_sleep_delay ||= 10; puts("done") }
242
+ puts "\nBootstrapping #{h.color(server_name, :bold)}..."
243
+ bootstrap_for_node(server_name, public_ip[0]).run
244
+ end
245
+
246
+ def bootstrap_for_node(server_name, public_ip)
247
+ bootstrap = Chef::Knife::Bootstrap.new
248
+ bootstrap.name_args = [public_ip]
249
+ bootstrap.config[:run_list] = locate_config_value(:run_list)
250
+ bootstrap.config[:ssh_user] = locate_config_value(:ssh_user) || "root"
251
+ bootstrap.config[:identity_file] = locate_config_value(:private_key_file)
252
+ bootstrap.config[:chef_node_name] = locate_config_value(:chef_node_name) || server_name
253
+ bootstrap.config[:distro] = locate_config_value(:distro)
254
+ bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
255
+ bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
256
+ bootstrap.config[:template_file] = locate_config_value(:template_file)
257
+ bootstrap
258
+ end
259
+ end
260
+ end
261
+ end
@@ -0,0 +1,61 @@
1
+ #
2
+ # Author:: Chirag Jog (<chiragj@websym.com>)
3
+ # Copyright:: Copyright (c) 2012 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
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
+ require 'chef/knife'
20
+ require 'chef/json_compat'
21
+
22
+ require 'chef/knife/google_base'
23
+
24
+ class Chef
25
+ class Knife
26
+ class GoogleServerDelete < Knife
27
+
28
+ include Knife::GoogleBase
29
+ banner "knife google server delete SERVER (options)"
30
+
31
+ option :project_id,
32
+ :short => "-p PROJECTNAME",
33
+ :long => "--project_id PROJECTNAME",
34
+ :description => "Your Google Compute Project Name",
35
+ :proc => Proc.new { |project| Chef::Config[:knife][:google_project] = project }
36
+
37
+ def run
38
+ unless Chef::Config[:knife][:google_project]
39
+ ui.error("Project ID is a compulsory parameter")
40
+ exit 1
41
+ end
42
+
43
+ $stdout.sync = true
44
+ project_id = Chef::Config[:knife][:google_project]
45
+ validate_project(project_id)
46
+
47
+ @name_args.each do |server|
48
+ confirm("Do you really want to delete the server - #{server} ?")
49
+ del_instance = exec_shell_cmd("#{@gcompute} deleteinstance #{server} --print_json --project_id=#{project_id} -f")
50
+ Chef::Log.debug 'Executing ' + del_instance.command
51
+
52
+ if not del_instance.stderr.downcase.scan("error").empty?
53
+ ui.error("Failed to delete server. Error: #{error}")
54
+ exit 1
55
+ end
56
+ ui.warn("Deleted server #{server}")
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,96 @@
1
+ # Author:: Chirag Jog (<chiragj@websym.com>)
2
+ # Copyright:: Copyright (c) 2012 Opscode, Inc.
3
+ # License:: Apache License, Version 2.0
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
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'stringio'
18
+ require 'yajl'
19
+ require 'highline'
20
+ require 'chef/knife'
21
+ require 'chef/json_compat'
22
+ require 'tempfile'
23
+
24
+ require 'chef/knife/google_base'
25
+
26
+ class Chef
27
+ class Knife
28
+ class GoogleServerList < Knife
29
+
30
+ include Knife::GoogleBase
31
+
32
+ banner "knife google server list (options)"
33
+
34
+ option :project_id,
35
+ :short => "-p PROJECTNAME",
36
+ :long => "--project_id PROJECTNAME",
37
+ :description => "Your Google Compute Project Name",
38
+ :proc => Proc.new { |project| Chef::Config[:knife][:google_project] = project }
39
+
40
+ def h
41
+ @highline ||= HighLine.new
42
+ end
43
+
44
+ def run
45
+ unless Chef::Config[:knife][:google_project]
46
+ ui.error("Project ID is a compulsory parameter")
47
+ exit 1
48
+ end
49
+ $stdout.sync = true
50
+
51
+ project_id = Chef::Config[:knife][:google_project]
52
+ validate_project(project_id)
53
+ list_instances = exec_shell_cmd("#{@gcompute} listinstances --print_json --project_id=#{project_id}")
54
+ Chef::Log.debug 'Executing ' + list_instances.command
55
+ list_instances.run_command
56
+
57
+ if not list_instances.stderr.downcase.scan("error").empty?
58
+ ui.error("Failed to list instances. Error: #{error}")
59
+ exit 1
60
+ end
61
+ instances_json = to_json(list_instances.stdout)
62
+
63
+ server_list = [
64
+ h.color('ID', :bold),
65
+ h.color('Name', :bold),
66
+ h.color('PublicIP', :bold),
67
+ h.color('PrivateIP', :bold),
68
+ h.color('OperatingSystem', :bold)
69
+
70
+ ]
71
+
72
+ if not instances_json.has_key?("items")
73
+ exit 0
74
+ end
75
+
76
+ instances_json["items"].each do |item|
77
+ server_list << item["id"]
78
+ server_list << item["name"].split("/").last
79
+ private_ip = []
80
+ public_ip = []
81
+ item["networkInterfaces"].each do |interface|
82
+ private_ip << interface["networkIP"]
83
+ interface["accessConfigs"].select { |cfg| public_ip << cfg["natIP"] }
84
+ end
85
+
86
+ server_list << public_ip.join(",")
87
+ server_list << private_ip.join(",")
88
+ server_list << item["image"].split("/").last
89
+ end
90
+
91
+ puts h.list(server_list, :columns_across, 5)
92
+
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,3 @@
1
+ module KnifeGoogle
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-google
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Chirag Jog
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-29 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: chef
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 55
29
+ segments:
30
+ - 0
31
+ - 10
32
+ - 0
33
+ version: 0.10.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: net-ssh
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 9
45
+ segments:
46
+ - 2
47
+ - 0
48
+ - 3
49
+ version: 2.0.3
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: net-ssh-multi
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 21
61
+ segments:
62
+ - 1
63
+ - 0
64
+ - 1
65
+ version: 1.0.1
66
+ type: :runtime
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: net-scp
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 31
77
+ segments:
78
+ - 1
79
+ - 0
80
+ - 4
81
+ version: 1.0.4
82
+ type: :runtime
83
+ version_requirements: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ name: highline
86
+ prerelease: false
87
+ requirement: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ type: :runtime
97
+ version_requirements: *id005
98
+ - !ruby/object:Gem::Dependency
99
+ name: yajl-ruby
100
+ prerelease: false
101
+ requirement: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ type: :runtime
111
+ version_requirements: *id006
112
+ description: Google Compute Cloud Support for Chef's Knife Command
113
+ email: chiragj@websym.com
114
+ executables: []
115
+
116
+ extensions: []
117
+
118
+ extra_rdoc_files:
119
+ - README.rdoc
120
+ - LICENSE
121
+ files:
122
+ - LICENSE
123
+ - README.rdoc
124
+ - lib/chef/knife/google_base.rb
125
+ - lib/chef/knife/google_server_create.rb
126
+ - lib/chef/knife/google_server_list.rb
127
+ - lib/chef/knife/google_server_delete.rb
128
+ - lib/knife-google/version.rb
129
+ homepage: http://wiki.opscode.com/display/chef
130
+ licenses: []
131
+
132
+ post_install_message:
133
+ rdoc_options: []
134
+
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ hash: 3
152
+ segments:
153
+ - 0
154
+ version: "0"
155
+ requirements: []
156
+
157
+ rubyforge_project:
158
+ rubygems_version: 1.8.10
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: Google Compute Cloud Support for Chef's Knife Command
162
+ test_files: []
163
+