Class: Cumulus::VPC::Manager
- Inherits:
-
Common::Manager
- Object
- Common::Manager
- Cumulus::VPC::Manager
- Includes:
- VpcChange
- Defined in:
- lib/vpc/manager/Manager.rb
Constant Summary
Constants included from VpcChange
VpcChange::ADDRESSES, VpcChange::CIDR, VpcChange::DHCP, VpcChange::ENDPOINTS, VpcChange::NETWORK_ACLS, VpcChange::ROUTE_TABLES, VpcChange::SUBNETS, VpcChange::TAGS, VpcChange::TENANCY
Constants included from Common::DiffChange
Common::DiffChange::ADD, Common::DiffChange::MODIFIED, Common::DiffChange::UNMANAGED
Instance Method Summary collapse
- #added_diff(local) ⇒ Object
- #aws_resources ⇒ Object
- #diff_resource(local, aws) ⇒ Object
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
- #local_resources ⇒ Object
-
#migrate ⇒ Object
Public: Migrates the existing AWS config to Cumulus.
-
#migrate_network_acls(dir) ⇒ Object
Public: Migrate Network ACLs.
-
#migrate_policies(dir) ⇒ Object
Public: Migrates the endpoint policies.
-
#migrate_route_tables(dir) ⇒ Object
Public: Migrates route tables.
-
#migrate_subnets(dir, rt_map, acl_map) ⇒ Object
Public: Migrates subnets.
-
#migrate_vpcs(dir, rt_map, subnet_map, acl_map) ⇒ Object
Public: Migrates vpcs.
-
#rename(asset_type, old_name, new_name) ⇒ Object
Public: Renames an asset and all references to it and syncs the name tag change to AWS.
- #resource_name ⇒ Object
- #rt_ids(rt_names) ⇒ Object
- #unmanaged_diff(aws) ⇒ Object
- #update(local, diffs) ⇒ Object
Methods included from Common::DiffChange
Methods inherited from Common::Manager
#diff, #diff_one, #filter_local, #list, #sync, #sync_one
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
25 26 27 28 29 |
# File 'lib/vpc/manager/Manager.rb', line 25 def initialize super() @create_asset = false @client = Aws::EC2::Client.new(Configuration.instance.client) end |
Instance Method Details
#added_diff(local) ⇒ Object
47 48 49 |
# File 'lib/vpc/manager/Manager.rb', line 47 def added_diff(local) VpcDiff.added(local) end |
#aws_resources ⇒ Object
39 40 41 |
# File 'lib/vpc/manager/Manager.rb', line 39 def aws_resources @aws_resources ||= EC2::named_vpcs end |
#diff_resource(local, aws) ⇒ Object
51 52 53 |
# File 'lib/vpc/manager/Manager.rb', line 51 def diff_resource(local, aws) local.diff(aws) end |
#local_resources ⇒ Object
35 36 37 |
# File 'lib/vpc/manager/Manager.rb', line 35 def local_resources @local_resources ||= Hash[Loader.vpcs.map { |local| [local.name, local] }] end |
#migrate ⇒ Object
Public: Migrates the existing AWS config to Cumulus
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/vpc/manager/Manager.rb', line 243 def migrate # Create the directories vpc_dir = "#{@migration_root}/vpc" policies_dir = "#{vpc_dir}/policies" route_tables_dir = "#{vpc_dir}/route-tables" network_acls_dir = "#{vpc_dir}/network-acls" subnets_dir = "#{vpc_dir}/subnets" vpcs_dir = "#{vpc_dir}/vpcs" if !Dir.exists?(@migration_root) Dir.mkdir(@migration_root) end if !Dir.exists?(vpc_dir) Dir.mkdir(vpc_dir) end if !Dir.exists?(policies_dir) Dir.mkdir(policies_dir) end if !Dir.exists?(route_tables_dir) Dir.mkdir(route_tables_dir) end if !Dir.exists?(network_acls_dir) Dir.mkdir(network_acls_dir) end if !Dir.exists?(subnets_dir) Dir.mkdir(subnets_dir) end if !Dir.exists?(vpcs_dir) Dir.mkdir(vpcs_dir) end # Migrate the different assets migrate_policies(policies_dir) route_table_names = migrate_route_tables(route_tables_dir) network_acl_names = migrate_network_acls(network_acls_dir) subnet_names = migrate_subnets(subnets_dir, route_table_names, network_acl_names) migrate_vpcs(vpcs_dir, route_table_names, subnet_names, network_acl_names) end |
#migrate_network_acls(dir) ⇒ Object
Public: Migrate Network ACLs. Uses name if available or id to name file.
dir - the directory to store network acl configurations in
Returns a Hash of network acl id to file name
321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/vpc/manager/Manager.rb', line 321 def migrate_network_acls(dir) puts Colors.blue("Migrating network acls to #{dir}") Hash[EC2::named_network_acls.map do |name, network_acl| puts "Migrating network acl #{name}" cumulus_acl = NetworkAclConfig.new(name).populate!(network_acl) json = JSON.pretty_generate(cumulus_acl.to_hash) File.open("#{dir}/#{name}.json", "w") { |f| f.write(json) } [network_acl.network_acl_id, name] end] end |
#migrate_policies(dir) ⇒ Object
Public: Migrates the endpoint policies. Uses Version to name the file
dir - the directory to store policies in
285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/vpc/manager/Manager.rb', line 285 def migrate_policies(dir) puts Colors.blue("Migrating policies to #{dir}") # Get the policies for each endpoint policies = EC2::endpoints.map { |endpoint| endpoint.parsed_policy } policies.each do |policy| name = policy["Version"] puts "Migrating policy #{name}" json = JSON.pretty_generate(policy) File.open("#{dir}/#{name}.json", "w") { |f| f.write(json) } end end |
#migrate_route_tables(dir) ⇒ Object
Public: Migrates route tables. Uses name if available or id to name file.
dir - the directory to store route table configurations in
Returns a Hash of route table id to file name
302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/vpc/manager/Manager.rb', line 302 def migrate_route_tables(dir) puts Colors.blue("Migrating route tables to #{dir}") Hash[EC2::named_route_tables.map do |name, route_table| puts "Migrating route table #{name}" cumulus_rt = RouteTableConfig.new(name).populate!(route_table) json = JSON.pretty_generate(cumulus_rt.to_hash) File.open("#{dir}/#{name}.json", "w") { |f| f.write(json) } [route_table.route_table_id, name] end] end |
#migrate_subnets(dir, rt_map, acl_map) ⇒ Object
Public: Migrates subnets. Uses name if available or id to name file.
dir - the directory to store subnet configurations in rt_map - a map of route table ids to names to substitute for in the subnet config acl_map - a map of network acl ids to names to substitute for in the subnet config
Returns a hash of subnet id to file name
342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/vpc/manager/Manager.rb', line 342 def migrate_subnets(dir, rt_map, acl_map) puts Colors.blue("Migrating subnets to #{dir}") Hash[EC2::named_subnets.map do |name, subnet| puts "Migrating subnet #{name}" cumulus_subnet = SubnetConfig.new(name).populate!(subnet, rt_map, acl_map) json = JSON.pretty_generate(cumulus_subnet.to_hash) File.open("#{dir}/#{name}.json", "w") { |f| f.write(json) } [subnet.subnet_id, name] end] end |
#migrate_vpcs(dir, rt_map, subnet_map, acl_map) ⇒ Object
Public: Migrates vpcs. Uses name if available or id to name file.
dir - the directory to store vpc configurations in rt_map - a map of route table ids to names to substitute for in the vpc config subnet_map - a map of subnet ids to names to substitute for in the vpc config acl_map - a map of network acl ids to names to substitute fo rin the vpc config
362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/vpc/manager/Manager.rb', line 362 def migrate_vpcs(dir, rt_map, subnet_map, acl_map) puts Colors.blue("Migrating vpcs to #{dir}") EC2::named_vpcs.each do |name, vpc| puts "Migrating vpc #{name}" cumulus_vpc = (VpcConfig.new(name)).populate!(vpc, rt_map, subnet_map, acl_map) json = JSON.pretty_generate(cumulus_vpc.to_hash) File.open("#{dir}/#{name}.json", "w") { |f| f.write(json) } end end |
#rename(asset_type, old_name, new_name) ⇒ Object
Public: Renames an asset and all references to it and syncs the name tag change to AWS.
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 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/vpc/manager/Manager.rb', line 97 def rename(asset_type, old_name, new_name) # Strip .json from the old_name and new_name old_name = old_name.end_with?(".json") ? old_name[0...-5] : old_name new_name = new_name.end_with?(".json") ? new_name[0...-5] : new_name vpcs_dir = Configuration.instance.vpc.vpcs_directory subnets_dir = Configuration.instance.vpc.subnets_directory route_tables_dir = Configuration.instance.vpc.route_tables_directory policies_dir = Configuration.instance.vpc.policies_directory network_acls_dir = Configuration.instance.vpc.network_acls_directory case asset_type when "network-acl" puts Colors.blue("Renaming Network ACL #{old_name} to #{new_name}") # Update the Name tag and resave the file acl_local = Loader.network_acl(old_name) acl_local.["Name"] = new_name json = JSON.pretty_generate(acl_local.to_hash) File.open("#{network_acls_dir}/#{new_name}.json", "w") { |f| f.write(json) } # Update the tags in AWS acl_aws = EC2::named_network_acls[old_name] (acl_aws.network_acl_id, { "Name" => new_name}) # Update the vpc references to it Loader.vpcs.each do |vpc| vpc.network_acls.collect! { |acl_name| if acl_name == old_name then new_name else acl_name end } json = JSON.pretty_generate(vpc.to_hash) File.open("#{vpcs_dir}/#{vpc.name}.json", "w") { |f| f.write(json) } end # Update the subnet references to it Loader.subnets.each do |subnet| if subnet.network_acl == old_name subnet.network_acl = new_name json = JSON.pretty_generate(subnet.to_hash) File.open("#{subnets_dir}/#{subnet.name}.json", "w") { |f| f.write(json) } end end # Delete the old file File.delete("#{network_acls_dir}/#{old_name}.json") when "policy" puts Colors.blue("Renaming policy #{old_name} to #{new_name}") # Rename the file File.rename("#{policies_dir}/#{old_name}.json", "#{policies_dir}/#{new_name}.json") # Update the references to it in vpc endpoint Loader.vpcs.each do |vpc| endpoints_updated = false vpc.endpoints.each do |endpoint| if endpoint.policy == old_name endpoint.policy = new_name endpoints_updated = true end end if endpoints_updated json = JSON.pretty_generate(vpc.to_hash) File.open("#{vpcs_dir}/#{vpc.name}.json", "w") { |f| f.write(json) } end end when "route-table" puts Colors.blue("Renaming route table #{old_name} to #{new_name}") # Update the Name tag and resave the file rt_local = Loader.route_table(old_name) rt_local.["Name"] = new_name json = JSON.pretty_generate(rt_local.to_hash) File.open("#{route_tables_dir}/#{new_name}.json", "w") { |f| f.write(json) } # Update the tags in AWS rt_aws = EC2::named_route_tables[old_name] (rt_aws.route_table_id, { "Name" => new_name}) # Update the vpc references to it Loader.vpcs.each do |vpc| vpc.route_tables.collect! { |rt_name| if rt_name == old_name then new_name else rt_name end } json = JSON.pretty_generate(vpc.to_hash) File.open("#{vpcs_dir}/#{vpc.name}.json", "w") { |f| f.write(json) } end # Update the subnet references to it Loader.subnets.each do |subnet| if subnet.route_table == old_name subnet.route_table = new_name json = JSON.pretty_generate(subnet.to_hash) File.open("#{subnets_dir}/#{subnet.name}.json", "w") { |f| f.write(json) } end end # Delete the old file File.delete("#{route_tables_dir}/#{old_name}.json") when "subnet" puts Colors.blue("Renaming subnet #{old_name} to #{new_name}") # Update the Name tag and resave the file subnet_local = Loader.subnet(old_name) subnet_local.["Name"] = new_name json = JSON.pretty_generate(subnet_local.to_hash) File.open("#{subnets_dir}/#{new_name}.json", "w") { |f| f.write(json) } # Update the tags in AWS subnet_aws = EC2::named_subnets[old_name] (subnet_aws.subnet_id, { "Name" => new_name}) # Update the vpc references to it Loader.vpcs.each do |vpc| vpc.subnets.collect! { |subnet_name| if subnet_name == old_name then new_name else subnet_name end } json = JSON.pretty_generate(vpc.to_hash) File.open("#{vpcs_dir}/#{vpc.name}.json", "w") { |f| f.write(json) } end # Delete the old file File.delete("#{subnets_dir}/#{old_name}.json") when "vpc" puts Colors.blue("Renaming vpc #{old_name} to #{new_name}") # Update the Name tag and resave the file vpc_local = Loader.vpc(old_name) vpc_local.["Name"] = new_name json = JSON.pretty_generate(vpc_local.to_hash) File.open("#{vpcs_dir}/#{new_name}.json", "w") { |f| f.write(json) } # Update the tags in AWS vpc_aws = EC2::named_vpcs[old_name] (vpc_aws.vpc_id, { "Name" => new_name}) # Delete the old file File.delete("#{vpcs_dir}/#{old_name}.json") end end |
#resource_name ⇒ Object
31 32 33 |
# File 'lib/vpc/manager/Manager.rb', line 31 def resource_name "Virtual Private Cloud" end |
#rt_ids(rt_names) ⇒ Object
572 573 574 575 576 577 578 579 580 581 582 |
# File 'lib/vpc/manager/Manager.rb', line 572 def rt_ids(rt_names) rt_names.map do |rt_name| aws_rt = EC2::named_route_tables[rt_name] if aws_rt.nil? puts Colors.red("Error updating endpoints. No route table found for #{rt_name}.") exit 1 else aws_rt.route_table_id end end end |
#unmanaged_diff(aws) ⇒ Object
43 44 45 |
# File 'lib/vpc/manager/Manager.rb', line 43 def unmanaged_diff(aws) VpcDiff.unmanaged(aws) end |
#update(local, diffs) ⇒ Object
55 56 57 58 59 60 61 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 |
# File 'lib/vpc/manager/Manager.rb', line 55 def update(local, diffs) aws_vpc = EC2::named_vpcs[local.name] diffs.each do |diff| case diff.type when CIDR puts Colors.blue("CIDR Block cannot be updated. You must create a new VPC.") when TENANCY puts Colors.blue("Tenancy cannot be updated. You must create a new VPC.") when DHCP puts Colors.blue("Updating DHCP Options...") (aws_vpc, local.dhcp) when ROUTE_TABLES puts Colors.blue("Updating Route Tables...") update_route_tables(aws_vpc, diff.changes) when ENDPOINTS puts Colors.blue("Updating Endpoints...") update_endpoints(aws_vpc, diff.changes) when ADDRESSES puts Colors.blue("Updating Address Associations...") update_address_associations(aws_vpc, diff.changes) when NETWORK_ACLS puts Colors.blue("Updating Network ACLs") update_network_acls(aws_vpc, diff.changes) when SUBNETS puts Colors.blue("Updating Subnets...") update_subnets(aws_vpc, diff.changes) when TAGS puts Colors.blue("Updating Tags...") if !diff..empty? (aws_vpc.vpc_id, diff.) end if !diff..empty? (aws_vpc.vpc_id, diff.) end end end end |