From 2a8208163cad201a95a9ea56bca9aacfee0825e0 Mon Sep 17 00:00:00 2001 From: tintoy Date: Wed, 23 Nov 2016 09:34:14 +1100 Subject: [PATCH] Merge changes from feature/dns branch and conditionally disable DNS stuff if terraform/dns.tf is not present. --- .editorconfig | 5 ++ README.md | 2 +- setup.py | 206 ++++++++++++++++++++++++++-------------------- terraform/main.tf | 4 +- 4 files changed, 124 insertions(+), 93 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..6e3c965 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*] +indent_style = space +indent_size = 4 diff --git a/README.md b/README.md index 1eb6e65..dff9760 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ It might work on Windows, but I haven't tested it. If you have problems, try usi * Your CloudControl credentials configured in the `MCP_USER` and `MCP_PASSWORD` environment variables * A tag key called "roles" defined in CloudControl * A DNS service hosted in AWS Route 53 -*Note* - if you don't want DNS, then just delete `terraform/dns.tf`. +*Note* - if you don't want DNS, just delete `terraform/dns.tf` and you're good to go. 1. Edit [terraform/main.tf](terraform/main.tf) to configure target data center, etc 2. Run `./setup.py` (if you leave the "Client IP" question blank, it will attempt to auto-detect it) diff --git a/setup.py b/setup.py index f7b134a..2e360b7 100755 --- a/setup.py +++ b/setup.py @@ -1,122 +1,150 @@ #!/usr/bin/env python2 +from collections import OrderedDict import json +import os +from os import path import socket import urllib2 -local_vars_path = './terraform/local-vars.tf' +local_vars = {} +local_vars_path = path.join(path.dirname(__file__), 'terraform/local-vars.tf') + +# Quick-and-dirty questions and answers +variable_descriptions = OrderedDict() +variable_descriptions['client_ip'] = 'Client IP address' +variable_descriptions['ssh_public_key_file'] = 'SSH public key file' +variable_descriptions['ssh_bootstrap_password'] = 'SSH bootstrap password' +variable_descriptions['dns_domain_name'] = 'Top-level domain name' +variable_descriptions['dns_subdomain_name'] = 'Sub-domain name' +variable_descriptions['dns_hosted_zone_id'] = 'AWS hosted DNS zone Id' +variable_descriptions['aws_access_key'] = 'AWS access key' +variable_descriptions['aws_secret_key'] = 'AWS secret key' + +# For padding questions so text lines up +variable_description_max = max( + len(description) for description in variable_descriptions.values() +) + def load_config(): - variables = {} + local_vars['ssh_public_key_file'] = path.join( + os.getenv('HOME'), ".ssh/id_rsa" + ) - try: - with open(local_vars_path, 'r') as local_vars_file: - # Read local variables - local_vars_config = json.load(local_vars_file)['variable'] + try: + with open(local_vars_path, 'r') as local_vars_file: + # Read local variables + local_vars_config = json.load(local_vars_file)['variable'] - for variable_name in local_vars_config.keys(): - variable_value = local_vars_config[variable_name]['default'] + for variable_name in local_vars_config.keys(): + variable_value = local_vars_config[variable_name]['default'] - variables[variable_name] = variable_value - except IOError: - pass # No existing configuration. + local_vars[variable_name] = variable_value + except IOError: + print('(no existing configuration)') + pass # No existing configuration. - return variables -def save_config(variables): - local_vars_data = { - 'variable': {} - } - for name, value in variables.items(): - local_vars_data['variable'][name] = { - 'default': value +def save_config(): + local_vars_data = { + 'variable': {} } + for name, value in local_vars.items(): + local_vars_data['variable'][name] = { + 'default': value + } - with open(local_vars_path, 'w') as local_vars_file: - json.dump(local_vars_data, local_vars_file, indent=2) + with open(local_vars_path, 'w') as local_vars_file: + json.dump(local_vars_data, local_vars_file, indent=2) -def show_config(variables): - if 'client_ip' in variables: - print('Client IP = "{}"'.format( - variables['client_ip']) - ) - if 'ssh_public_key_file' in variables: - print('SSH public key file = "{}"'.format( - variables['ssh_public_key_file']) - ) - if 'ssh_bootstrap_password' in variables: - print('SSH bootstrap password = "{}"'.format( - variables['ssh_bootstrap_password']) - ) - if 'dns_domain_name' in variables: - print('Top-level domain name = "{}"'.format( - variables['dns_domain_name']) - ) - if 'dns_subdomain_name' in variables: - print('Sub-domain name = "{}"'.format( - variables['dns_subdomain_name']) - ) - if 'dns_hosted_zone_id' in variables: - print('AWS DNS hosted zone Id = "{}"'.format( - variables['dns_hosted_zone_id']) - ) - if 'aws_access_key' in variables: - print('AWS access key = "{}"'.format( - variables['aws_access_key']) + +def show_config(): + for variable_name in variable_descriptions.keys(): + if variable_name not in local_vars: + continue + + print('{} = "{}"'.format( + variable_descriptions[variable_name].ljust( + variable_description_max, ' ' + ), + local_vars[variable_name] + )) + + +def ask_variable(key): + value = raw_input('{} (currently "{}")] = '.format( + variable_descriptions[key], + local_vars.get(key, '') + )) + + if value != "": + local_vars[key] = value.strip() + + +def clear_variable(key): + local_vars.pop(key, None) + + +def detect_client_ip(): + request = urllib2.Request( + 'http://{}/json'.format( + socket.gethostbyname('ifconfig.co') # We need the IPv4 address + ), + headers={'Host': 'ifconfig.co'} ) - if 'aws_secret_key' in variables: - print('AWS secret key = "{}"'.format( - variables['aws_secret_key']) + + response = json.loads( + urllib2.urlopen(request).read() ) -def ask_variable(variables, key, prompt): - value = raw_input('{} (currently "{}"): '.format( - prompt, variables.get(key, '') - )) - if value != "": - local_vars[key] = value - -def detect_client_ip(variables): - request = urllib2.Request( - 'http://{}/json'.format( - socket.gethostbyname('ifconfig.co') # We need the IPv4 address - ), - headers = {'Host': 'ifconfig.co'} - ) - - response = json.loads( - urllib2.urlopen(request).read() - ) - - variables['client_ip'] = response['ip'] - -local_vars = load_config() + local_vars['client_ip'] = response['ip'] + + +def have_dns_config(): + try: + os.stat( + path.join(path.dirname(__file__), 'terraform/dns.tf') + ) + except FileNotFoundError: + return False + else: + return True + +load_config() if len(local_vars) > 0: - print('Existing configuration:\n') - show_config(local_vars) + print('Existing configuration:\n') + show_config() print('') print('=' * 80) print('') -ask_variable(local_vars, 'client_ip', 'Client IP address') +ask_variable('client_ip') if 'client_ip' not in local_vars: - print('Detecting client IP...') - detect_client_ip(local_vars) -ask_variable(local_vars, 'ssh_public_key_file', 'SSH public key file') -ask_variable(local_vars, 'ssh_bootstrap_password', 'SSH bootstrap password file') -ask_variable(local_vars, 'dns_domain_name', 'Top-level domain name') -ask_variable(local_vars, 'dns_subdomain_name', 'Sub-domain name') -ask_variable(local_vars, 'dns_hosted_zone_id', 'AWS DNS hosted zone Id') -ask_variable(local_vars, 'aws_access_key', 'AWS access key') -ask_variable(local_vars, 'aws_secret_key', 'AWS secret key') - -save_config(local_vars) + print('Detecting client IP...') + detect_client_ip() +ask_variable('ssh_public_key_file') +ask_variable('ssh_bootstrap_password') +if have_dns_config(): + ask_variable('dns_domain_name') + ask_variable('dns_subdomain_name') + ask_variable('dns_hosted_zone_id') + ask_variable('aws_access_key') + ask_variable('aws_secret_key') +else: + clear_variable('dns_domain_name') + clear_variable('dns_subdomain_name') + clear_variable('dns_hosted_zone_id') + clear_variable('aws_access_key') + clear_variable('aws_secret_key') + +save_config() print('') print('=' * 80) print('') print('Current configuration:\n') -show_config(local_vars) +show_config() diff --git a/terraform/main.tf b/terraform/main.tf index 00d58b4..85dcde0 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,7 +1,5 @@ provider "ddcloud" { - region = "AU" - - auto_create_tag_keys = true + region = "AU" } #################