Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow enabling ip forward when creating instance #46

Merged
merged 5 commits into from
Feb 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ doc/
.swo
Gemfile.lock
.rvmrc
.ruby-version
19 changes: 17 additions & 2 deletions lib/chef/knife/google_disk_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class GoogleDiskCreate < Knife
:long => "--gce-disk-size SIZE",
:description => "Size of the persistent disk between 1 and 10000 GB, specified in GB; default is '1' GB",
:default => "1"

option :disk_type,
:long => "--gce-disk-type TYPE",
:description => "Disk type to use to create the disk. Possible values are pd-standard, pd-ssd and local-ssd",
:default => "pd-standard"

def run
$stdout.sync = true
Expand All @@ -50,6 +55,15 @@ def run
ui.error("Zone '#{config[:zone]}' not found")
exit 1
end

begin
disk_type = client.disk_types.get(:name => config[:disk_type],
:zone => selflink2name(zone.self_link))
rescue Google::Compute::ResourceNotFound
ui.error("DiskType '#{config[:disk_type]}' not found")
exit 1
end


begin
disk_size = config[:disk_size].to_i
Expand All @@ -58,10 +72,11 @@ def run
ui.error("Size of the persistent disk must be between 1 and 10000 GB.")
exit 1
end

zone_operation = client.disks.create(:name => @name_args.first,
:sizeGb => config[:disk_size],
:zone => zone.name)
:zone => zone.name,
:type => disk_type.self_link)
end
end
end
Expand Down
44 changes: 41 additions & 3 deletions lib/chef/knife/google_server_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ class GoogleServerCreate < Knife
:description => "Compute Engine can migrate your VM instance to other hardware without downtime prior to periodic infrastructure maintenance, otherwise the server is terminated; enabled by default.",
:boolean => true,
:default => true

option :can_ip_forward,
:long => "--[no-]gce-can-ip-forward",
:description => "Forwarding allows the instance to help route packets.",
:boolean => true,
:default => false

option :network,
:short => "-n NETWORK",
Expand All @@ -101,6 +107,12 @@ class GoogleServerCreate < Knife
:proc => Proc.new { |metadata| metadata.split(',') },
:default => []

option :metadata_from_file,
:long => "--gce-metadata-from-file Key=File[,Key=File...]",
:description => "Additional metadata loaded from a YAML file",
:proc => Proc.new { |metadata| metadata.split(',') },
:default => []

option :service_account_scopes,
:long => "--gce-service-account-scopes SCOPE1,SCOPE2,SCOPE3",
:proc => Proc.new { |service_account_scopes| service_account_scopes.split(',') },
Expand Down Expand Up @@ -376,6 +388,12 @@ def run
auto_migrate = 'TERMINATE'
end

if config[:can_ip_forward] then
can_ip_forward = true
else
can_ip_forward = false
end

(checked_custom, checked_all) = false
begin
image_project = config[:image_project_id]
Expand Down Expand Up @@ -474,7 +492,25 @@ def run
exit 1
end

metadata = config[:metadata].collect{|pair| Hash[*pair.split('=')] }
metadata_items = []

config[:metadata].collect do |pair|
mkey, mvalue = pair.split('=')
metadata_items << {'key' => mkey, 'value' => mvalue}
end

# Metadata from file

config[:metadata_from_file].each do |p|
mkey, filename = p.split('=')
begin
file_content = File.read(filename)
rescue
puts "Not possible to read metadata file #{filename}"
end
metadata_items << {'key' => mkey, 'value' => file_content}
end

network_interface = {'network'=>network}

if config[:public_ip] == 'EPHEMERAL'
Expand All @@ -496,19 +532,21 @@ def run
:zone => selflink2name(zone),
:machineType => machine_type,
:disks => disks,
:canIpForward => can_ip_forward,
:networkInterfaces => [network_interface],
:scheduling => {
'automaticRestart' => auto_restart,
'onHostMaintenance' => auto_migrate
},
:metadata => { 'items' => metadata },
:metadata => { 'items' => metadata_items },
:tags => { 'items' => config[:tags] }
)
else
zone_operation = client.instances.create(:name => @name_args.first,
:zone=> selflink2name(zone),
:machineType => machine_type,
:disks => disks,
:canIpForward => can_ip_forward,
:networkInterfaces => [network_interface],
:serviceAccounts => [{
'kind' => 'compute#serviceAccount',
Expand All @@ -519,7 +557,7 @@ def run
'automaticRestart' => auto_restart,
'onHostMaintenance' => auto_migrate
},
:metadata => { 'items'=>metadata },
:metadata => { 'items'=>metadata_items },
:tags => { 'items' => config[:tags] }
)
end
Expand Down
1 change: 1 addition & 0 deletions lib/google/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Compute
require 'google/compute/client'
require 'google/compute/exception'
require 'google/compute/disk'
require 'google/compute/disk_type'
require 'google/compute/firewall'
require 'google/compute/image'
require 'google/compute/server'
Expand Down
4 changes: 4 additions & 0 deletions lib/google/compute/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ def disks
CreatableResourceCollection.new(:resource_class => Google::Compute::Disk, :dispatcher=>@dispatcher)
end

def disk_types
CreatableResourceCollection.new(:resource_class => Google::Compute::DiskType, :dispatcher=>@dispatcher)
end

def firewalls
CreatableResourceCollection.new(:resource_class => Google::Compute::Firewall, :dispatcher => @dispatcher)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/google/compute/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Disk < Resource

attr_reader :zone, :size_gb, :status, :options
attr_reader :source_snapshot, :source_snapshot_id
attr_reader :type

def from_hash(disk_data)
super(disk_data)
Expand All @@ -32,6 +33,7 @@ def from_hash(disk_data)
@options = disk_data["options"]
@source_snapshot = disk_data["sourceSnapshot"]
@source_snapshot_id = disk_data["sourceSnapshotId"]
@type = disk_data["type"]
end
end
end
Expand Down
29 changes: 29 additions & 0 deletions lib/google/compute/disk_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2013 Google Inc. All Rights Reserved.
#
# 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.

module Google
module Compute
class DiskType < Resource

attr_reader :deprecated, :valid_disk_size, :default_disk_size_gb

def from_hash(data)
super(data)
@valid_disk_size = data["validDiskSize"]
@deprecated = data["deprecated"]
@default_disk_size_gb = data["defaultDiskSizeGb"].to_i
end
end
end
end