Class: Chef::Knife::InstanceCreate

Inherits:
ZestKnife show all
Defined in:
lib/chef/knife/instance_create.rb

Constant Summary

Constants inherited from ZestKnife

ZestKnife::OPTS, ZestKnife::VALIDATORS

Instance Attribute Summary collapse

Attributes inherited from ZestKnife

#base_domain, #internal_domain

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ZestKnife

AWS_REGIONS, aws_for_region, #check_services, #domain, #domain_prefix, #environment_prefix, #errors, #errors?, #find_ec2, #find_item, #find_r53, #fqdn, #generate_hostname, in_all_aws_regions, #msg_pair, #random_hostname, #random_three_digit_number, #setup_config, #validate_color, #validate_domain, #validate_env, #validate_force_deploy, #validate_hostname, #validate_prod, #validate_region, validates, with_opts, with_validated_opts, #zone, #zone_from_name

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



17
18
19
# File 'lib/chef/knife/instance_create.rb', line 17

def hostname
  @hostname
end

Class Method Details

.new_with_defaults(environment, region, color, base_domain, opts) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/chef/knife/instance_create.rb', line 143

def self.new_with_defaults environment, region, color, base_domain, opts
  new.tap do |ic|
    ic.config[:environment]               = environment
    ic.config[:cluster_tag]               = color
    ic.config[:region]                    = region
    ic.config[:base_domain]               = base_domain
    ic.config[:aws_ssh_key_id]            = opts[:aws_ssh_key_id]
    ic.config[:aws_access_key_id]         = opts[:aws_access_key_id]
    ic.config[:aws_secret_access_key]     = opts[:aws_secret_access_key]
    ic.config[:availability_zone]         = opts[:availability_zone]
    ic.config[:encrypted_data_bag_secret] = opts[:encrypted_data_bag_secret]
    ic.config[:wait_for_it]               = opts[:wait_for_it]
    ic.config[:image]                     = opts[:image]
    ic.config[:subnet_id]                 = opts[:subnet_id]
  end
end

Instance Method Details

#amiObject



188
189
190
# File 'lib/chef/knife/instance_create.rb', line 188

def ami
  @ami ||= ZestKnife.aws_for_region(@region).compute.images.get(image)
end

#availability_zoneObject



184
185
186
# File 'lib/chef/knife/instance_create.rb', line 184

def availability_zone
  config[:availability_zone]
end

#create_server_defObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/chef/knife/instance_create.rb', line 160

def create_server_def
  server_def = {
      :image_id           => image,
      :groups             => config[:security_groups],
      :security_group_ids => config[:security_group_ids],
      :flavor_id          => config[:flavor],
      :key_name           => config[:aws_ssh_key_id],
      :availability_zone  => availability_zone,
      :subnet_id          => config[:subnet_id],
      :tags => {
          'Name'        => hostname,
          'environment' => @environment
      },
      :user_data                 => config[:without_user_data] ? "" : get_user_data,
      :iam_instance_profile_name => config[:iam_role]
  }

  server_def
end

#get_user_dataObject



207
208
209
210
# File 'lib/chef/knife/instance_create.rb', line 207

def get_user_data
  generator = Zest::BootstrapGenerator.new(Chef::Config[:validation_key], Chef::Config[:validation_client_name], Chef::Config[:chef_server_url], @environment, config[:run_list], hostname, @color, @base_domain, config[:encrypted_data_bag_secret])
  generator.generate
end

#imageObject



180
181
182
# File 'lib/chef/knife/instance_create.rb', line 180

def image
  config[:image]
end

#runObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/knife/instance_create.rb', line 79

def run
  $stdout.sync = true
  setup_config

  @environment = config[:environment]
  @base_domain = config[:base_domain]
  @hostname    = config[:hostname] || generate_hostname(@environment)
  @color       = config[:cluster_tag]
  @region      = config[:region]

  validate!

  get_user_data

  if config[:show_server_options]
    details = create_server_def
    ui.info(
        ui.color("Creating server with options\n", :bold) +
            ui.color(JSON.pretty_generate(details.reject { |k, v| k == :user_data }), :blue) +
            ui.color("\nWith user script\n", :bold) +
            ui.color(details[:user_data], :cyan)
    )
    exit 0
  end

  server = ZestKnife.aws_for_region(@region).compute.servers.create(create_server_def)

  msg_pair("Zest Hostname", fqdn(hostname))
  msg_pair("Environment", @environment)
  msg_pair("Run List", config[:run_list].join(', '))
  msg_pair("Instance ID", server.id)
  msg_pair("Flavor", server.flavor_id)
  msg_pair("Image", server.image_id)
  msg_pair("Region", @region)
  msg_pair("Availability Zone", server.availability_zone)
  msg_pair("Security Groups", server.groups.join(", "))
  msg_pair("SSH Key", server.key_name)

  return unless config[:wait_for_it]

  print "\n#{ui.color("Waiting for server", :magenta)}"

  # wait for it to be ready to do stuff
  server.wait_for { print "."; ready? }

  puts "\n"
  msg_pair("Public DNS Name", server.dns_name)
  msg_pair("Public IP Address", server.public_ip_address)
  msg_pair("Private DNS Name", server.private_dns_name)
  msg_pair("Private IP Address", server.private_ip_address)
  msg_pair("Instance ID", server.id)
  msg_pair("Flavor", server.flavor_id)
  msg_pair("Image", server.image_id)
  msg_pair("Region", @region)
  msg_pair("Availability Zone", server.availability_zone)
  msg_pair("Security Groups", server.groups.join(", "))
  msg_pair("SSH Key", server.key_name)
  msg_pair("Root Device Type", server.root_device_type)

  zone.records.create(:name => fqdn(hostname), :type => 'A', :value => server.private_ip_address, :ttl => 300)

  server
end

#validate!Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/chef/knife/instance_create.rb', line 192

def validate!
  unless File.exists?(config[:encrypted_data_bag_secret])
    errors << "Could not find encrypted data bag secret. Tried #{config[:encrypted_data_bag_secret]}"
  end

  if ami.nil?
    errors << "You have not provided a valid image. Tried to find '#{image}'."
  end

  validate_hostname hostname

  super([:aws_access_key_id, :aws_secret_access_key,
         :flavor, :aws_ssh_key_id, :run_list])
end