Module: Inception::CliHelpers::Infrastructure

Included in:
Inception::Cli
Defined in:
lib/inception/cli_helpers/infrastructure.rb

Instance Method Summary collapse

Instance Method Details

#choose_fog_providerObject

Displays a prompt for known IaaS that are configured within .fog config file if found.

If no ~/.fog file found or user chooses “Alternate credentials” then no changes are made to settings.

For example:

  1. AWS (default)

  2. AWS (bosh)

  3. Alternate credentials

Choose infrastructure: 1

If .fog config only contains one provider, do not prompt.

fog config file looks like: :default:

:aws_access_key_id:     PERSONAL_ACCESS_KEY
:aws_secret_access_key: PERSONAL_SECRET

:bosh:

:aws_access_key_id:     SPECIAL_IAM_ACCESS_KEY
:aws_secret_access_key: SPECIAL_IAM_SECRET_KEY

Convert this into: { “AWS (default)” => => …, “AWS (bosh)” => … }

Then display options to user to choose.

Currently detects following fog providers:

  • AWS

  • OpenStack

If “Alternate credentials” is selected, then user is prompted for fog credentials:

  • provider?

  • access keys?

  • API URI or region?

Sets (unless ‘Alternate credentials’ is chosen)

  • settings.provider.name

  • settings.provider.credentials

For AWS, the latter has keys:

{:aws_access_key_id, :aws_secret_access_key}

For OpenStack, the latter has keys:

{:openstack_username, :openstack_api_key, :openstack_tenant
   :openstack_auth_url, :openstack_region }


62
63
64
65
66
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
# File 'lib/inception/cli_helpers/infrastructure.rb', line 62

def choose_fog_provider
  fog_providers = {}
  # Prepare menu options:
  # each provider/profile name gets a menu choice option
  fog_config.inject({}) do |iaas_options, fog_profile|
    profile_name, profile = fog_profile
    if profile[:aws_access_key_id]
      # TODO does fog have inbuilt detection algorithm?
      fog_providers["AWS (#{profile_name})"] = {
        "name" => "aws",
        "provider" => "AWS",
        "aws_access_key_id" => profile[:aws_access_key_id],
        "aws_secret_access_key" => profile[:aws_secret_access_key]
      }
    end
    if profile[:openstack_username]
      # TODO does fog have inbuilt detection algorithm?
      fog_providers["OpenStack (#{profile_name})"] = {
        "name" => "openstack",
        "provider" => "OpenStack",
        "openstack_username" => profile[:openstack_username],
        "openstack_api_key" => profile[:openstack_api_key],
        "openstack_tenant" => profile[:openstack_tenant],
        "openstack_auth_url" => profile[:openstack_auth_url],
        "openstack_region" => profile[:openstack_region]
      }
    end
  end
  # Display menu
  # Include "Alternate credentials" as the last option
  if fog_providers.keys.size > 0
    hl.choose do |menu|
      menu.prompt = "Choose infrastructure:  "
      fog_providers.each do |label, credentials|
        menu.choice(label) do
          settings.set("provider.name", credentials.delete("name"))
          settings.set("provider.credentials", credentials)
          save_settings!
        end
      end
      menu.choice("Alternate credentials")
    end
  end
end

#choose_providerObject

Prompts user to pick from the supported regions



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/inception/cli_helpers/infrastructure.rb', line 108

def choose_provider
  hl.choose do |menu|
    menu.prompt = "Choose infrastructure:  "
    menu.choice("AWS") do
      settings.provider["name"] = "aws"
    end
    menu.choice("OpenStack") do
      settings.provider["name"] = "openstack"
    end
  end
end

#configure_providerObject

Prompts user to choose an Iaas provider Sets settings.provider.name



5
6
7
8
9
10
11
12
# File 'lib/inception/cli_helpers/infrastructure.rb', line 5

def configure_provider
  unless valid_infrastructure?
    choose_fog_provider unless settings.exists?("provider.name")
    choose_provider unless settings.exists?("provider.name")
    setup_provider_credentials
  end
  confirm_infrastructure
end

#confirm_infrastructureObject



137
138
139
# File 'lib/inception/cli_helpers/infrastructure.rb', line 137

def confirm_infrastructure
  confirm "Using #{settings.provider.name}/#{settings.provider.region}"
end

#fog_configObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/inception/cli_helpers/infrastructure.rb', line 141

def fog_config
  @fog_config ||= begin
    if File.exists?(File.expand_path(fog_config_path))
      say "Found infrastructure API credentials at #{fog_config_path} (override with $FOG)"
      YAML.load_file(File.expand_path(fog_config_path))
    else
      say "No existing #{fog_config_path} fog configuration file", :yellow
      {}
    end
  end
end

#fog_config_pathObject



153
154
155
# File 'lib/inception/cli_helpers/infrastructure.rb', line 153

def fog_config_path
  ENV['FOG'] || "~/.fog"
end

#setup_provider_credentialsObject



120
121
122
123
124
125
126
127
128
# File 'lib/inception/cli_helpers/infrastructure.rb', line 120

def setup_provider_credentials
  say "Using provider #{settings.provider.name}:"
  say ""
  settings.set_default("provider", {}) # to ensure settings.provider exists
  provider_cli = Bosh::Providers.provider_cli(settings.provider.name, settings.provider)
  provider_cli.perform
  settings["provider"] = provider_cli.export_attributes
  settings.create_accessors!
end

#valid_infrastructure?Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
# File 'lib/inception/cli_helpers/infrastructure.rb', line 130

def valid_infrastructure?
  settings.exists?("provider.name") &&
    settings.exists?("provider.region") &&
    settings.exists?("provider.credentials") &&
    provider_client
end