# -*- encoding: utf-8 -*-
#
# Author:: Fletcher Nichol (<fnichol@nichol.ca>)
# Copyright:: Copyright (c) 2012 Fletcher Nichol
# 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 "chef/knife/server_bootstrap_base"

class Chef
  class Knife
    # Provisions an EC2 instance and sets up an Open Source Chef Server.
    class ServerBootstrapEc2 < Knife

      banner "knife server bootstrap ec2 (options)"

      include Knife::ServerBootstrapBase

      deps do
        require "knife/server/ssh"
        require "knife/server/credentials"
        require "knife/server/ec2_security_group"

        begin
          require "chef/knife/ec2_server_create"
          require "fog"
          Chef::Knife::Ec2ServerCreate.load_deps

          current_options = options
          self.options = Chef::Knife::Ec2ServerCreate.options.dup
          options.merge!(current_options)
        rescue LoadError => ex
          ui.error [
            "Knife plugin knife-ec2 could not be loaded.",
            "Please add the knife-ec2 gem to your Gemfile or",
            "install the gem manually with `gem install knife-ec2'.",
            "(#{ex.message})"
          ].join(" ")
          exit 1
        end
      end

      option :security_groups,
        :short => "-G X,Y,Z",
        :long => "--groups X,Y,Z",
        :description => "The security groups for this server",
        :default => ["infrastructure"],
        :proc => proc { |groups| groups.split(",") }

      def run
        super
        config_security_group
        ec2_bootstrap.run
        fetch_validation_key
        create_root_client
        install_client_key
      end

      def ec2_bootstrap
        setup_environment
        bootstrap = Chef::Knife::Ec2ServerCreate.new
        Chef::Knife::Ec2ServerCreate.options.keys.each do |attr|
          val = config_val(attr)
          next if val.nil?

          bootstrap.config[attr] = val
        end
        bootstrap.config[:tags] = bootstrap_tags
        bootstrap.config[:distro] = bootstrap_distro
        bootstrap
      end

      def ec2_connection
        @ec2_connection ||= Fog::Compute.new(
          :provider => "AWS",
          :aws_access_key_id => config_val(:aws_access_key_id),
          :aws_secret_access_key => config_val(:aws_secret_access_key),
          :region => config_val(:region)
        )
      end

      def server_dns_name
        server = ec2_connection.servers.find do |s|
          s.state == "running" &&
            s.tags["Name"] == config_val(:chef_node_name) &&
            s.tags["Role"] == "chef_server"
        end

        server && server.dns_name
      end

      private

      def validate!
        super

        if config[:chef_node_name].nil?
          ui.error "You did not provide a valid --node-name value."
          exit 1
        end
        if config_val(:platform) == "auto"
          ui.error "Auto platform mode cannot be used with knife-ec2 plugin"
          exit 1
        end
      end

      def setup_environment
        ENV["WEBUI_PASSWORD"] = config_val(:webui_password)
        ENV["AMQP_PASSWORD"] = config_val(:amqp_password)
        ENV["NO_TEST"] = "1" if config[:no_test]
      end

      def config_security_group(name = nil)
        ids = config[:security_group_ids]

        if ids.nil? || ids.empty?
          name = config_val(:security_groups).first if name.nil?
          ::Knife::Server::Ec2SecurityGroup.new(ec2_connection, ui).
            configure_chef_server_group(name, :description => "#{name} group")
        else
          config[:security_groups] = nil
        end
      end

      def bootstrap_tags
        Hash[Array(config_val(:tags)).map { |t| t.split("=") }].
          merge("Role" => "chef_server").map { |k, v| "#{k}=#{v}" }
      end

      def ssh_connection
        opts = {
          :host => server_dns_name,
          :user => config_val(:ssh_user),
          :port => config_val(:ssh_port),
          :keys => [config_val(:identity_file)].compact
        }
        if config_val(:host_key_verify) == false
          opts[:user_known_hosts_file] = "/dev/null"
          opts[:paranoid] = false
        end

        ::Knife::Server::SSH.new(opts)
      end
    end
  end
end
