cloud-mu 3.1.1 → 3.1.2beta2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1924,24 +1924,46 @@ MU.log "association I don't understand in #{@cloud_id}", MU::WARN, details: rtb_
1924
1924
 
1925
1925
  # Remove all network interfaces associated with the currently loaded deployment.
1926
1926
  # @param noop [Boolean]: If true, will only print what would be done
1927
- # @param tagfilters [Array<Hash>]: EC2 tags to filter against when search for resources to purge
1927
+ # @param filters [Array<Hash>]: EC2 tags to filter against when search for resources to purge
1928
1928
  # @param region [String]: The cloud provider region
1929
1929
  # @return [void]
1930
- def self.purge_interfaces(noop = false, tagfilters = [{name: "tag:MU-ID", values: [MU.deploy_id]}], region: MU.curRegion, credentials: nil)
1930
+ def self.purge_interfaces(noop = false, filters = [{name: "tag:MU-ID", values: [MU.deploy_id]}], region: MU.curRegion, credentials: nil)
1931
1931
  resp = MU::Cloud::AWS.ec2(credentials: credentials, region: region).describe_network_interfaces(
1932
- filters: tagfilters
1932
+ filters: filters
1933
1933
  )
1934
1934
  ifaces = resp.data.network_interfaces
1935
1935
 
1936
1936
  return if ifaces.nil? or ifaces.size == 0
1937
1937
 
1938
1938
  ifaces.each { |iface|
1939
+ if iface.vpc_id
1940
+ default_sg_resp = MU::Cloud::AWS.ec2(region: region, credentials: credentials).describe_security_groups(
1941
+ filters: [
1942
+ { name: "group-name", values: ["default"] },
1943
+ { name: "vpc-id", values: [iface.vpc_id] }
1944
+ ]
1945
+ ).security_groups
1946
+ if default_sg_resp and default_sg_resp.size == 1
1947
+ default_sg = default_sg_resp.first.group_id
1948
+ if iface.groups.size != 1 or
1949
+ iface.groups.first.group_id != default_sg
1950
+ MU.log "Removing extra security groups from ENI #{iface.network_interface_id}"
1951
+ MU::Cloud::AWS.ec2(credentials: credentials, region: region).modify_network_interface_attribute(
1952
+ network_interface_id: iface.network_interface_id,
1953
+ groups: [default_sg]
1954
+ )
1955
+ end
1956
+ end
1957
+ end
1939
1958
  begin
1940
1959
  if iface.attachment and iface.attachment.status == "attached"
1941
1960
  MU.log "Detaching Network Interface #{iface.network_interface_id} from #{iface.attachment.instance_owner_id}"
1942
1961
  tried_lbs = false
1943
1962
  begin
1944
1963
  MU::Cloud::AWS.ec2(credentials: credentials, region: region).detach_network_interface(attachment_id: iface.attachment.attachment_id) if !noop
1964
+ rescue Aws::EC2::Errors::OperationNotPermitted => e
1965
+ MU.log "Can't detach #{iface.network_interface_id}: #{e.message}", MU::WARN, details: iface.attachment
1966
+ next
1945
1967
  rescue Aws::EC2::Errors::InvalidAttachmentIDNotFound => e
1946
1968
  # suits me just fine
1947
1969
  rescue Aws::EC2::Errors::AuthFailure => e
@@ -1997,6 +2019,9 @@ MU.log "association I don't understand in #{@cloud_id}", MU::WARN, details: rtb_
1997
2019
  if retries < 19
1998
2020
  loglevel = (retries > 0 and (retries % 3) == 0) ? MU::NOTICE : MU::DEBUG
1999
2021
  MU.log "#{e.message} (retry #{retries.to_s}/20)", loglevel
2022
+ if loglevel == MU::NOTICE
2023
+ MU::Cloud::AWS::VPC.purge_interfaces(noop, [{name: "subnet-id", values: [subnet.subnet_id]}], region: region, credentials: credentials)
2024
+ end
2000
2025
  sleep 30
2001
2026
  retries = retries + 1
2002
2027
  retry