Class: CfnVpn::Actions::Modify

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/cfnvpn/actions/modify.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



47
48
49
# File 'lib/cfnvpn/actions/modify.rb', line 47

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#create_bucket_if_bucket_not_setObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cfnvpn/actions/modify.rb', line 104

def create_bucket_if_bucket_not_set
  if !@options['bucket'] && !@config.has_key?(:bucket)
    if yes? "no s3 bucket supplied in the command or found in the config, select (Y) to generate a new one ot select (N) and re run teh command with the --bucket flag to import the existing bucket." 
      CfnVpn::Log.logger.info "creating s3 bucket"
      bucket = CfnVpn::S3Bucket.new(@options['region'], @name)
      bucket_name = bucket.generate_bucket_name
      bucket.create_bucket(bucket_name)
      @config[:bucket] = bucket_name
    else
      CfnVpn::Log.logger.info "rerun cfn-vpn modify #{name} command with the --bucket [BUCKET] flag"
      exit 1
    end
  elsif @options['bucket']
    @config[:bucket] = @options['bucket']
  end
end

#create_build_directoryObject



55
56
57
58
59
# File 'lib/cfnvpn/actions/modify.rb', line 55

def create_build_directory
  @build_dir = "#{CfnVpn.cfnvpn_path}/#{@name}"
  CfnVpn::Log.logger.debug "creating directory #{@build_dir}"
  FileUtils.mkdir_p(@build_dir)
end

#deploy_vpnObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cfnvpn/actions/modify.rb', line 121

def deploy_vpn
  compiler = CfnVpn::Compiler.new(@name, @config)
  template_body = compiler.compile
  CfnVpn::Log.logger.info "Creating cloudformation changeset for stack #{@name}-cfnvpn in #{@options['region']}"
  change_set, change_set_type = @deployer.create_change_set(template_body: template_body)
  @deployer.wait_for_changeset(change_set.id)
  changeset_response = @deployer.get_change_set(change_set.id)

  changes = {"Add" => [], "Modify" => [], "Remove" => []}
  change_colours = {"Add" => "green", "Modify" => 'yellow', "Remove" => 'red'}

  changeset_response.changes.each do |change|
    action = change.resource_change.action
    changes[action].push([
      change.resource_change.logical_resource_id,
      change.resource_change.resource_type,
      change.resource_change.replacement ? change.resource_change.replacement : 'N/A',
      change.resource_change.details.collect {|detail| detail.target.name }.join(' , ')
    ])
  end

  changes.each do |type, rows|
    next if !rows.any?
    puts "\n"
    table = Terminal::Table.new(
      :title => type,
      :headings => ['Logical Resource Id', 'Resource Type', 'Replacement', 'Changes'],
      :rows => rows)
    puts table.to_s.send(change_colours[type])
  end

  CfnVpn::Log.logger.info "Cloudformation changeset changes:"
  puts "\n"
  continue = yes? "Continue?", :green
  if !continue
    CfnVpn::Log.logger.info("Cancelled cfn-vpn modifiy #{@name}")
    exit 1
  end

  @deployer.execute_change_set(change_set.id)
  @deployer.wait_for_execute(change_set_type)
  CfnVpn::Log.logger.info "Changeset #{change_set_type} complete"
end

#finishObject



165
166
167
168
# File 'lib/cfnvpn/actions/modify.rb', line 165

def finish
  vpn = CfnVpn::ClientVpn.new(@name,@options['region'])
  CfnVpn::Log.logger.info "Client VPN #{vpn.endpoint_id} modified."
end

#initialize_configObject



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
# File 'lib/cfnvpn/actions/modify.rb', line 69

def initialize_config
  @config = CfnVpn::Config.get_config(@options[:region], @name)

  CfnVpn::Log.logger.debug "Current config:\n#{@config}"

  if @options[:param_yaml]
    CfnVpn::Log.logger.debug "Loading config from YAML file #{@options[:param_yaml]}"
    @config = CfnVpn::Config.get_config_from_yaml_file(@options[:param_yaml])
  else
    CfnVpn::Log.logger.debug "Loading config from options"
    @options.each do |key, value|
      next if [:verbose].include? key
      @config[key.to_sym] = value
    end

    if @options['add_subnet_ids']
      @config[:subnet_ids].concat @options['add_subnet_ids']
    end

    if @options['del_subnet_ids']
      @config[:subnet_ids].reject!{ |subnet| @options['del_subnet_ids'].include? subnet }
    end

    if @options['no_dns_servers']
      @config[:dns_servers] = []
    end
  end

  if (@config[:saml_arn] || @config[:directory_id]) && @options[:default_groups]
    @config[:default_groups] = @options[:default_groups]
  end

  CfnVpn::Log.logger.debug "Modified config:\n#{@config}"
end

#set_loglevelObject



51
52
53
# File 'lib/cfnvpn/actions/modify.rb', line 51

def set_loglevel
  CfnVpn::Log.logger.level = Logger::DEBUG if @options['verbose']
end

#stack_existObject



61
62
63
64
65
66
67
# File 'lib/cfnvpn/actions/modify.rb', line 61

def stack_exist
  @deployer = CfnVpn::Deployer.new(@options['region'],@name)
  if !@deployer.does_cf_stack_exist()
    CfnVpn::Log.logger.error "#{@name}-cfnvpn stack doesn't exists in this account in region #{@options['region']}\n Try running `cfn-vpn init #{@name}` to setup the stack"
    exit 1
  end
end