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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/chef/knife/instance_create.rb', line 131

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]
  end
end

Instance Method Details

#amiObject



177
178
179
# File 'lib/chef/knife/instance_create.rb', line 177

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

#availability_zoneObject



173
174
175
# File 'lib/chef/knife/instance_create.rb', line 173

def availability_zone
  config[:availability_zone]
end

#create_server_defObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/chef/knife/instance_create.rb', line 147

def create_server_def
  server_def = {
      :image_id => image,
      :groups => security_group,
      :flavor_id => config[:flavor],
      :key_name => config[:aws_ssh_key_id],
      :availability_zone => availability_zone,
      :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



196
197
198
199
# File 'lib/chef/knife/instance_create.rb', line 196

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



165
166
167
# File 'lib/chef/knife/instance_create.rb', line 165

def image
  config[:image]
end

#runObject



67
68
69
70
71
72
73
74
75
76
77
78
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
# File 'lib/chef/knife/instance_create.rb', line 67

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

#security_groupObject



169
170
171
# File 'lib/chef/knife/instance_create.rb', line 169

def security_group
  config[:security_groups]
end

#validate!Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/chef/knife/instance_create.rb', line 181

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