lib/chef/knife/google_base.rb in knife-google-0.0.1 vs lib/chef/knife/google_base.rb in knife-google-1.0.0

- old
+ new

@@ -1,100 +1,71 @@ +# Copyright 2013 Google Inc. All Rights Reserved. # -# Author:: Chirag Jog (<chiragj@websym.com>) -# Copyright:: Copyright (c) 2012 Opscode, Inc. -# License:: Apache License, Version 2.0 -# # 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. -# -require 'stringio' -require 'yajl' -require 'mixlib/shellout' +require 'chef/knife' +require 'google/compute' -$CLI_PREFIX='gcutil' class Chef class Knife module GoogleBase - @parser = Yajl::Parser.new - @gcompute = nil - @cygwin_path = nil - def is_platform_windows? - return RUBY_PLATFORM.scan('w32').size > 0 - end + # hack for mixlib-cli workaround + # https://github.com/opscode/knife-ec2/blob/master/lib/chef/knife/ec2_base.rb + def self.included(includer) + includer.class_eval do + deps do + require 'google/compute' + require 'chef/json_compat' + end - def is_cygwin_installed? - ENV['CYGWINPATH'] != nil + option :compute_credential_file, + :short => "-f CREDENTIAL_FILE", + :long => "--compute-credential-file CREDENTIAL_FILE", + :description => "Google Compute credential file (google setup can create this)" + end end - def gcompute - return if @gcompute - if is_platform_windows? - if is_cygwin_installed? - #Remove extra quotes - @cygwin_path = ENV['CYGWINPATH'].chomp('\'').reverse.chomp('\'').reverse - #FIXME Generalize the python binary - @gcompute="#{@cygwin_path}\\bin\\python2.6.exe #{@cygwin_path}\\bin\\#{$CLI_PREFIX}" - else - puts "Cannot Find Cygwin Installation !!! Please set CYGWINPATH to point to the Cygwin installation" - exit 1 - end - else - Chef::Log.debug("Linux Environment") - @gcompute = $CLI_PREFIX + def client + @client ||= begin + Google::Compute::Client.from_json(config[:compute_credential_file]) end end - def parser - if @parser.nil? - @parser = Yajl::Parser.new + def selflink2name(selflink) + selflink.to_s == '' ? selflink.to_s : selflink.split('/').last + end + + def msg_pair(label, value, color=:cyan) + if value && !value.to_s.empty? + ui.info("#{ui.color(label, color)}: #{value}") end end - - def to_json(data) - data_s = StringIO::new(data.strip) - parser.parse(data_s) {|obj| return obj} + + def disks(instance) + instance.disks.collect{|d|d.device_name}.compact end - def exec_shell_cmd(cmd) - if is_platform_windows? and is_cygwin_installed? - #Change the HOME PATH From Windows to Cygwin - cygwin_home = "#{@cygwin_path}\\home\\#{ENV['USER']}" - - #Auth token should exist in either ENV['HOME'] or cygwin_home - #XXX Find a way to remove the hard-coded file name - if not File.file?("#{ENV['HOME']}\\.#{$CLI_PREFIX}_auth") - ENV['HOME'] = cygwin_home - end - end - shell_cmd = Mixlib::ShellOut.new(cmd) - shell_cmd.run_command + def private_ips(instance) + instance.network_interfaces.collect{|ni|ni.network_ip}.compact end - def validate_project(project_id) - cmd = "#{gcompute} getproject --project_id=#{project_id}" - Chef::Log.debug 'Executing ' + cmd - getprj = exec_shell_cmd(cmd) - if getprj.status.exitstatus > 0 - if not getprj.stdout.scan("Enter verification code").empty? - ui.error("Failed to authenticate the account") - ui.error("If not authenticated, please Authenticate gcompute to access the Google Compute Cloud") - ui.error("Authenticate by executing gcutil auth --project_id=<project_id>") - exit 1 - end - ui.error("#{getprj.stderr}") - exit 1 - end + def public_ips(instance) + instance.network_interfaces.collect{|ni| + ni.access_configs.map{|ac| + ac.nat_ip + } + }.flatten.compact end end end end